#include "LAGraph_demo.h"
#define NTHREAD_LIST 1
#define THREAD_LIST 0
#define LG_FREE_ALL \
{ \
LAGraph_Delete (&G, NULL) ; \
GrB_free (&c2) ; \
GrB_free (¢rality) ; \
GrB_free (&SourceNodes) ; \
}
#define BATCH_SIZE 4
int main (int argc, char **argv)
{
char msg [LAGRAPH_MSG_LEN] ;
LAGraph_Graph G = NULL ;
GrB_Vector centrality = NULL, c2 = NULL ;
GrB_Matrix SourceNodes = NULL ;
bool burble = false ;
demo_init (burble) ;
int batch_size = BATCH_SIZE ;
int nt = NTHREAD_LIST ;
int Nthreads [20] = { 0, THREAD_LIST } ;
int nthreads_max, nthreads_outer, nthreads_inner ;
LAGRAPH_TRY (LAGraph_GetNumThreads (&nthreads_outer, &nthreads_inner, msg)) ;
nthreads_max = nthreads_outer * nthreads_inner ;
if (Nthreads [1] == 0)
{
Nthreads [1] = nthreads_max ;
for (int t = 2 ; t <= nt ; t++)
{
Nthreads [t] = Nthreads [t-1] / 2 ;
if (Nthreads [t] == 0) nt = t-1 ;
}
}
printf ("threads to test: ") ;
for (int t = 1 ; t <= nt ; t++)
{
int nthreads = Nthreads [t] ;
if (nthreads > nthreads_max) continue ;
printf (" %d", nthreads) ;
}
printf ("\n") ;
double *tt = NULL ;
LAGRAPH_TRY (LAGraph_Malloc ((void **) &tt, nthreads_max+1,
sizeof (double), msg)) ;
char *matrix_name = (argc > 1) ? argv [1] : "stdin" ;
LAGRAPH_TRY (readproblem (&G, &SourceNodes,
false, false, true, NULL, false, argc, argv)) ;
GrB_Index n, nvals ;
GRB_TRY (GrB_Matrix_nrows (&n, G->A)) ;
GRB_TRY (GrB_Matrix_nvals (&nvals, G->A)) ;
GrB_Index nsource ;
GRB_TRY (GrB_Matrix_nrows (&nsource, SourceNodes)) ;
if (nsource % batch_size != 0)
{
printf ("SourceNode size must be multiple of batch_size (%d)\n",
batch_size) ;
exit (1) ;
}
fflush (stdout) ; fflush (stderr) ;
int ntrials = 0 ;
for (int nrepeat = 0 ; nrepeat <= 1 ; nrepeat++)
{
memset (tt, 0, (nthreads_max+1) * sizeof (double)) ;
ntrials = 0 ;
for (int64_t kstart = 0 ; kstart < nsource ; kstart += batch_size)
{
ntrials++ ;
printf ("\nTrial %d : sources: [", ntrials) ;
GrB_Index vertex_list [BATCH_SIZE] ;
for (int64_t k = 0 ; k < batch_size ; k++)
{
int64_t source = -1 ;
GRB_TRY (GrB_Matrix_extractElement (&source, SourceNodes,
k + kstart, 0)) ;
source-- ;
vertex_list [k] = source ;
printf (" %"PRId64, source) ;
}
printf (" ]\n") ;
LAGRAPH_TRY (LAGraph_SetNumThreads (nthreads_outer, nthreads_inner, msg)) ;
for (int t = 1 ; t <= nt ; t++)
{
if (Nthreads [t] > nthreads_max) continue ;
LAGRAPH_TRY (LAGraph_SetNumThreads (1, Nthreads [t], msg)) ;
GrB_free (¢rality) ;
double t2 = LAGraph_WallClockTime ( ) ;
LAGRAPH_TRY (LAGr_Betweenness (¢rality, G, vertex_list,
batch_size, msg)) ;
t2 = LAGraph_WallClockTime ( ) - t2 ;
printf ("BC time %2d: %12.4f (sec)\n", Nthreads [t], t2) ;
tt [t] += t2 ;
fflush (stdout) ; fflush (stderr) ;
if (nrepeat == 0) break ;
}
GrB_free (¢rality) ;
if (burble) break ;
}
if (nrepeat == 0)
{
printf ("warmup average: %g sec\n", tt [1] / ntrials) ;
}
}
printf ("\nntrials: %d\n", ntrials) ;
printf ("\n") ;
for (int t = 1 ; t <= nt ; t++)
{
if (Nthreads [t] > nthreads_max) continue ;
double t2 = tt [t] / ntrials ;
printf ( "Avg: BC threads %3d: %10.3f sec, graph: %s\n",
Nthreads [t], t2, matrix_name) ;
fprintf (stderr, "Avg: BC threads %3d: %10.3f sec, graph: %s\n",
Nthreads [t], t2, matrix_name) ;
}
fflush (stdout) ; fflush (stderr) ;
LAGraph_Free ((void **) &tt, msg) ;
LG_FREE_ALL ;
LAGRAPH_TRY (LAGraph_Finalize (msg)) ;
return (GrB_SUCCESS) ;
}