#include "LAGraph_demo.h"
#define NTHREAD_LIST 1
#define THREAD_LIST 0
#define LG_FREE_ALL \
{ \
LAGraph_Delete (&G, msg) ; \
GrB_free (&A) ; \
GrB_free (&Abool) ; \
GrB_free (&parent) ; \
GrB_free (&level) ; \
GrB_free (&SourceNodes) ; \
}
int main (int argc, char **argv)
{
char msg [LAGRAPH_MSG_LEN] ;
LAGraph_Graph G = NULL ;
GrB_Matrix A = NULL ;
GrB_Matrix Abool = NULL ;
GrB_Vector level = NULL ;
GrB_Vector parent = NULL ;
GrB_Matrix SourceNodes = NULL ;
bool burble = false ;
demo_init (burble) ;
uint64_t seed = 1 ;
FILE *f ;
int nthreads ;
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 ;
printf ("nthreads_max: %d\n", nthreads_max) ;
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 *tpl = NULL, *tp = NULL, *tl = NULL ;
LAGRAPH_TRY (LAGraph_Malloc ((void **) &tpl, nthreads_max+1,
sizeof (double), msg)) ;
LAGRAPH_TRY (LAGraph_Malloc ((void **) &tp, nthreads_max+1,
sizeof (double), msg)) ;
LAGRAPH_TRY (LAGraph_Malloc ((void **) &tl, 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)) ;
LAGRAPH_TRY (LAGraph_Cached_OutDegree (G, msg)) ;
LAGRAPH_TRY (LAGraph_Cached_InDegree (G, msg)) ;
GrB_Index n ;
GRB_TRY (GrB_Matrix_nrows (&n, G->A)) ;
GrB_Index ntrials ;
GRB_TRY (GrB_Matrix_nrows (&ntrials, SourceNodes)) ;
fflush (stdout) ; fflush (stderr) ;
int64_t src ;
GRB_TRY (GrB_Matrix_extractElement (&src, SourceNodes, 0, 0)) ;
src-- ; double twarmup = LAGraph_WallClockTime ( ) ;
LAGRAPH_TRY (LAGr_BreadthFirstSearch (NULL, &parent, G, src, msg)) ;
GrB_free (&parent) ;
twarmup = LAGraph_WallClockTime ( ) - twarmup ;
printf ("warmup: parent only, pushpull: %g sec\n", twarmup) ;
fflush (stdout) ; fflush (stderr) ;
for (int tt = 1 ; tt <= nt ; tt++)
{
int nthreads = Nthreads [tt] ;
if (nthreads > nthreads_max) continue ;
LAGRAPH_TRY (LAGraph_SetNumThreads (1, nthreads, msg)) ;
tp [nthreads] = 0 ;
tl [nthreads] = 0 ;
tpl [nthreads] = 0 ;
printf ("\n------------------------------- threads: %2d\n", nthreads) ;
for (int trial = 0 ; trial < ntrials ; trial++)
{
int64_t src ;
GRB_TRY (GrB_Matrix_extractElement (&src, SourceNodes, trial, 0)) ;
src-- ;
{
GrB_free (&parent) ;
double ttrial = LAGraph_WallClockTime ( ) ;
LAGRAPH_TRY (LAGr_BreadthFirstSearch (NULL, &parent,
G, src, msg)) ;
ttrial = LAGraph_WallClockTime ( ) - ttrial ;
tp [nthreads] += ttrial ;
printf ("parent only pushpull trial: %2d threads: %2d "
"src: %12" PRId64 " %10.4f sec\n",
trial, nthreads, src, ttrial) ;
fflush (stdout) ; fflush (stderr) ;
int32_t maxlevel ;
GrB_Index nvisited ;
#if LG_CHECK_RESULT
if (trial == 0)
{
double tcheck = LAGraph_WallClockTime ( ) ;
LAGRAPH_TRY (LG_check_bfs (NULL, parent, G, src, msg)) ;
tcheck = LAGraph_WallClockTime ( ) - tcheck ;
printf (" n: %g check: %g sec\n", (double) n, tcheck) ;
}
#endif
GrB_free (&parent) ;
#if 0#endif
GrB_free (&parent) ;
GrB_free (&level) ;
}
}
{
tp [nthreads] = tp [nthreads] / ntrials ;
tl [nthreads] = tl [nthreads] / ntrials ;
tpl [nthreads] = tpl [nthreads] / ntrials ;
printf ( "Avg: BFS pushpull parent only, threads %3d: %10.3f sec, graph: %s\n",
nthreads, tp [nthreads], matrix_name) ;
fprintf (stderr, "Avg: BFS pushpull parent only, threads %3d: %10.3f sec, graph: %s\n",
nthreads, tp [nthreads], matrix_name) ;
fflush (stdout) ; fflush (stderr) ;
#if 0#endif
}
}
LAGRAPH_TRY (LAGraph_SetNumThreads (nthreads_outer, nthreads_inner, msg)) ;
printf ("\n") ;
LAGraph_Free ((void **) &tpl, msg) ;
LAGraph_Free ((void **) &tp, msg) ;
LAGraph_Free ((void **) &tl, msg) ;
LG_FREE_ALL ;
LAGRAPH_TRY (LAGraph_Finalize (msg)) ;
return (GrB_SUCCESS) ;
}