#include "LAGraph_demo.h"
#include "LAGraphX.h"
#include "LG_alg_internal.h"
#undef LG_FREE_ALL
#define LG_FREE_ALL \
{ \
LAGraph_Delete (&G, NULL) ; \
GrB_free (&components) ; \
GrB_free (&components2) ; \
}
#define NTHREAD_LIST 1
#define THREAD_LIST 0
GrB_Index countCC (GrB_Vector f, GrB_Index n)
{
GrB_Index nCC = 0;
GrB_Index *w_val = NULL ;
LAGraph_Malloc ((void **) &w_val, n, sizeof (GrB_Index), NULL) ;
if (w_val == NULL) { printf ("out of memory\n") ; abort ( ) ; }
GrB_Index *i_val = NULL ;
#if LAGRAPH_SUITESPARSE
#else
LAGraph_Malloc ((void **) &i_val, n, sizeof (GrB_Index), NULL) ;
if (i_val == NULL) { printf ("out of memory\n") ; abort ( ) ; }
#endif
GrB_Vector_extractTuples (i_val, w_val, &n, f) ;
for (GrB_Index i = 0; i < n; i++)
{
if (w_val[i] == i)
{
nCC++ ;
}
}
LAGraph_Free ((void **) &i_val, NULL) ;
LAGraph_Free ((void **) &w_val, NULL) ;
return nCC;
}
int main (int argc, char **argv)
{
char msg [LAGRAPH_MSG_LEN] ;
LAGraph_Graph G = NULL ;
GrB_Vector components = NULL, components2 = NULL ;
bool burble = false ;
demo_init (burble) ;
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") ;
char *matrix_name = (argc > 1) ? argv [1] : "stdin" ;
printf ("\n%s:\n", matrix_name) ;
LAGRAPH_TRY (readproblem (&G,
NULL, true, 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)) ;
fflush (stdout) ; fflush (stderr) ;
LAGRAPH_TRY (LAGr_ConnectedComponents (&components, G, msg)) ;
GrB_Index nCC = countCC (components, n) ;
printf ("nCC: %llu\n", (long long unsigned int) nCC) ;
#if 0#endif
#define NTRIALS 16
printf ("# of trials: %d\n\n", NTRIALS) ;
fflush (stdout) ; fflush (stderr) ;
for (int trial = 1 ; trial <= nt ; trial++)
{
int nthreads = Nthreads [trial] ;
if (nthreads > nthreads_max) continue ;
LAGRAPH_TRY (LAGraph_SetNumThreads (1, nthreads, NULL)) ;
double ttt = 0 ;
int ntrials = NTRIALS ;
for (int k = 0 ; k < ntrials ; k++)
{
GrB_free (&components2) ;
double ttrial = LAGraph_WallClockTime ( ) ;
LAGRAPH_TRY (LAGr_ConnectedComponents (&components2, G, msg)) ;
ttrial = LAGraph_WallClockTime ( ) - ttrial ;
ttt += ttrial ;
#if LAGRAPH_SUITESPARSE
#if GxB_IMPLEMENTATION >= GxB_VERSION (10,0,0)
printf ("SV7") ;
#else
printf ("SV6") ;
#endif
#endif
printf (": nthreads: %2d trial: %2d time: %10.4f sec\n",
nthreads, k, ttrial) ;
GrB_Index nCC2 = countCC (components2, n) ;
if (nCC != nCC2) printf ("failure! %g %g diff %g\n",
(double) nCC, (double) nCC2, (double) (nCC-nCC2)) ;
fflush (stdout) ; fflush (stderr) ;
}
ttt = ttt / ntrials ;
printf ( "Avg: CC threads %3d: %10.3f sec, graph: %s\n",
nthreads, ttt, matrix_name) ;
fprintf (stderr, "Avg: CC threads %3d: %10.3f sec, graph: %s\n",
nthreads, ttt, matrix_name) ;
fflush (stdout) ; fflush (stderr) ;
}
#if 0#endif
#if 0#endif
#if 0#endif
#if 0#endif
#if 0#endif
LG_FREE_ALL ;
LAGRAPH_TRY (LAGraph_Finalize (msg)) ;
return (GrB_SUCCESS) ;
}