From 081504820afe5ac51bc7d29012d93a0ccd3a29d1 Mon Sep 17 00:00:00 2001 From: Olumayowa Olowomeye Date: Mon, 2 Dec 2024 21:34:33 -0600 Subject: [PATCH] changes to louvain2 --- .vscode/settings.json | 3 +- experimental/algorithm/LAGraph_Louvain.c | 2 +- experimental/algorithm/LAGraph_Louvain2.c | 129 +++++++++------------- experimental/test/test_louvain2.c | 8 +- include/LAGraphX.h | 2 +- 5 files changed, 62 insertions(+), 82 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6729f50a31..118a212caa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "files.associations": { - "lg_internal.h": "c" + "lg_internal.h": "c", + "lagraphx.h": "c" }, "C_Cpp.errorSquiggles": "enabled" } \ No newline at end of file diff --git a/experimental/algorithm/LAGraph_Louvain.c b/experimental/algorithm/LAGraph_Louvain.c index 88048c3c12..433afdc487 100644 --- a/experimental/algorithm/LAGraph_Louvain.c +++ b/experimental/algorithm/LAGraph_Louvain.c @@ -124,7 +124,7 @@ int LAGraph_Louvain( bool changed = true; int max_iter = 20; int iter =0; - while(changed && iter <= max_iter){ + while(changed && iter < max_iter){ changed = false; double k_i; for(int i=0;ik = (int64_t)jx; - z->v = (*x); + z->v = (*x) + (*y)*10e-3; } void max_fp64(tuple_fp64 *z, const tuple_fp64 *x, const tuple_fp64 *y){ - if (x->v > y->v || (x->v == y->v && x->k < y->k)) - { + if (x->v > y->v ){ z->k = x->k; z->v = x->v; }else{ @@ -66,7 +66,7 @@ void max_fp64(tuple_fp64 *z, const tuple_fp64 *x, const tuple_fp64 *y){ } #define MAX_FP64 \ "void max_fp64(tuple_fp64 *z, const tuple_fp64 *x, const tuple_fp64 *y){ \n" \ -" if (x->v > y->v || (x->v == y->v && x->k < y->k)) \n" \ +" if (x->v > y->v) \n" \ " { \n" \ " z->k = x->k; \n" \ " z->v = x->v; \n" \ @@ -78,11 +78,11 @@ void max_fp64(tuple_fp64 *z, const tuple_fp64 *x, const tuple_fp64 *y){ #define MAKE_FP64 \ "void make_fp64(tuple_fp64 *z, \n" \ " const double *x, GrB_Index ix, GrB_Index jx, \n" \ -" const void *y, GrB_Index iy, GrB_Index jy, \n" \ +" const int *y, GrB_Index iy, GrB_Index jy, \n" \ " const void *theta) \n" \ "{ \n" \ " z->k = (int64_t)jx; \n" \ -" z->v = (*x); \n" \ +" z->v = (*x) + (*y)*10e-3; \n" \ "}" @@ -115,35 +115,33 @@ int LAGraph_Louvain2( double *Sx = NULL ; //try S as double not bool GrB_Index *Sp = NULL , *Sj = NULL, Sp_size = 0, Sj_size = 0, Sx_size = 0 ; bool S_jumbled = false, S_iso = false; - GrB_Vector t_q = NULL, sr = NULL, q = NULL, q1=NULL, t=NULL, p=NULL,v=NULL; - GrB_Vector srxt = NULL; + GrB_Vector t_q = NULL, q1=NULL, t=NULL,v=NULL; GrB_Vector k = NULL ; GrB_Vector x = NULL ; - GrB_Index nvals_srxt,nvals_t; GrB_Vector z = NULL ; GrB_Index *coor = NULL; - bool * vals = NULL; - GrB_Index *p_cs=NULL; - double * p_vals = NULL ; GrB_Index n,b; - + GrB_Matrix S = NULL; + GrB_Vector sr = NULL; // add these to GB_FREE_ALL: - GzB_IndexBinaryOp Iop = NULL ; - GrB_BinaryOp Bop = NULL, MonOp = NULL ; - GrB_Scalar Theta = NULL ; - GrB_Type Tuple = NULL ; - GrB_Monoid Mon = NULL ; GrB_Semiring Semiring = NULL ; + GrB_Vector srxq = NULL; + GrB_Index vals_srxq; GrB_Matrix dS = NULL ; GrB_Vector dSk = NULL, vtS = NULL ; GrB_Vector temp = NULL ; GrB_Vector y_rand = NULL ; GrB_Vector max_q1 = NULL ; - + GzB_IndexBinaryOp Iop = NULL ; + GrB_BinaryOp Bop = NULL, MonOp = NULL ; + GrB_Scalar Theta = NULL ; + GrB_Type Tuple = NULL ; + GrB_Monoid Mon = NULL ; double *dSx = NULL ; GrB_Index *dSp = NULL, *dSj = NULL, dSp_size, dSj_size, dSx_size ; bool dS_jumbled = false, dS_iso = false ; - + tuple_fp64 o; + double k_i = 0; // FIXME: add check to see if S_result is NULL (*S_result) = NULL ; @@ -189,28 +187,23 @@ int LAGraph_Louvain2( // GxB_print(i,5); GRB_TRY(GrB_Matrix_diag(&S,x,0)); // GxB_print(S,5); - + GRB_TRY(GrB_Vector_new(&v,GrB_FP64,n)); //var used in for loop - GrB_Index vertices_changed; - GRB_TRY(GrB_Vector_nvals(&vertices_changed,k)); - GRB_TRY(GrB_Vector_new(&v, GrB_FP64, n)); - GRB_TRY(GrB_Vector_new(&srxt, GrB_FP64, n)); GRB_TRY(GrB_Vector_new(&t_q, GrB_FP64, n)); - GRB_TRY(GrB_Vector_new(&sr, GrB_FP64, n)); - GRB_TRY(GrB_Vector_new(&q, GrB_FP64, n)); GRB_TRY(GrB_Vector_new(&q1, GrB_FP64, n)); GRB_TRY(GrB_Vector_new(&z,GrB_FP64,n)); - GRB_TRY(GrB_Vector_new(&dSk,GrB_FP64,n)); GRB_TRY(GrB_Vector_new(&vtS,GrB_FP64,n)); // temp used to set dS to 0 matrix GRB_TRY(GrB_Vector_new(&temp,GrB_FP64,n)); - GRB_TRY(GrB_assign(temp, NULL, NULL, 0, GrB_ALL,n, NULL)); + GRB_TRY(GrB_assign(temp, NULL, NULL, 0, GrB_ALL,n,NULL)); GRB_TRY(GrB_Vector_new(&max_q1,Tuple,1)); - - GRB_TRY(GrB_Matrix_diag(&dS,temp,0)); + GRB_TRY(GrB_Vector_new(&srxq,GrB_BOOL,n)); + GRB_TRY(GrB_Matrix_diag(&dS,temp,0)); GRB_TRY(GrB_Vector_new(&y_rand, GrB_UINT64,n)); + GRB_TRY(GrB_Vector_new(&sr, GrB_FP64,n)); + GRB_TRY(GrB_Vector_new(&srxq, GrB_FP64,n)); GRB_TRY (GrB_assign (y_rand, NULL, NULL, 0, GrB_ALL, n, NULL)) ; bool changed = true; @@ -220,8 +213,6 @@ int LAGraph_Louvain2( GRB_TRY(GrB_mxv(z,NULL,NULL,stdmxm,S,k,NULL)); while(changed && iter < max_iter){ changed = false; - double k_i; - for(int i=0;i = a(kTS)+vTS----------- -----------// double alpha = -k_i/m; - //z += dS^t*k //compute dS GRB_TRY (GxB_Matrix_unpack_CSR (dS, &dSp, &dSj, (void ** )&dSx, &dSp_size, &dSj_size, &dSx_size, NULL, NULL, NULL)) ; @@ -269,51 +257,42 @@ int LAGraph_Louvain2( // vtS GRB_TRY(GrB_vxm(vtS,NULL,NULL,stdmxm,v,S,GrB_DESC_T0)); // GxB_print(vtS,5); - - //Compute q1 - //q1 = alpha (ktS) + (vtS) - // GxB_print(t_q,5); GRB_TRY(GrB_Vector_eWiseAdd_BinaryOp(q1,t_q,NULL,plusf64,z,vtS,GrB_DESC_RT0)); - GxB_print(q1,5); + // GxB_print(q1,5); /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// //-------------Index Binary OP Rand_argminmax -----------// seed++ ; GRB_TRY(LAGraph_Random_Seed(y_rand,seed,msg)); - GxB_print(y_rand,5); - GxB_print(q1,5); - GRB_TRY(GrB_mxv(max_q1,NULL,NULL,Semiring,(GrB_Matrix)q1,y_rand,NULL)); - GxB_print(max_q1,5); + + // GxB_print(y_rand,5); + // GxB_print(q1,5); + GRB_TRY(GrB_mxv(max_q1,NULL,NULL,Semiring,(GrB_Matrix)q1,y_rand,GrB_DESC_T0)); /////////////////////////////////////////////////////////// - // //S(i:)=t - // GRB_TRY (LAGraph_Malloc ((void **) &coor, nvals_t, sizeof (GrB_Index), msg));//declare statically - // GRB_TRY (LAGraph_Malloc ((void **) &vals, nvals_t, sizeof (bool), msg)) ; - // GRB_TRY(GrB_Vector_extractTuples_BOOL(coor,vals,&nvals_t,t)); - // GRB_TRY (GxB_Matrix_unpack_CSR (S, &Sp, &Sj, (void ** )&Sx, - // &Sp_size, &Sj_size, &Sx_size, NULL, &S_jumbled, NULL)) ; - // Sj[i] = coor[0]; - // Sx[i] = true; - // GRB_TRY (GxB_Matrix_pack_CSR (S, &Sp, &Sj, (void**)&Sx, - // Sp_size, Sj_size, Sx_size, NULL, S_jumbled, NULL)); - // free(coor); - // free(vals); - // // GxB_print(S,5); - // GRB_TRY(GrB_Vector_eWiseMult_BinaryOp(srxt,NULL,NULL,timesf64,sr,t,NULL)); - // GRB_TRY(GrB_Vector_nvals(&nvals_srxt,srxt)); - // // GxB_print(srxt,5); - // if(nvals_srxt==0){ - // // GRB_TRY(GrB_Vector_nvals(&vertices_changed,k)); - // changed = true; - // } - // vertices_changed -=1; - // vc = vertices_changed; + GRB_TRY(GrB_Vector_extractElement_UDT((void*)&o,max_q1,0)); + // printf("k:%ld\n",(long)o.k); + GRB_TRY (GxB_Matrix_unpack_CSR (S, &Sp, &Sj, (void ** )&Sx, + &Sp_size, &Sj_size, &Sx_size, NULL, &S_jumbled, NULL)) ; + Sj[i] = o.k; + Sx[i] = true; + GRB_TRY (GxB_Matrix_pack_CSR (S, &Sp, &Sj, (void**)&Sx, + Sp_size, Sj_size, Sx_size, NULL, S_jumbled, NULL)); + // GxB_print(sr,5); + GRB_TRY(GrB_Vector_eWiseMult_BinaryOp(srxq,NULL,NULL,timesf64,sr,q1,NULL)); + GRB_TRY(GrB_Vector_nvals(&vals_srxq,srxq)); + // GxB_print(srxq,5); + // printf("%ld",vals_srxq); + // printf("%d",vals_srxq==0); + if(vals_srxq==0){ + changed = true; + } } iter++; } - GxB_print(S,5); + // GxB_print(S,5); double Q; double gamma = 1; GRB_TRY(LAGr_Modularity2(&Q,gamma,A,S,msg)); diff --git a/experimental/test/test_louvain2.c b/experimental/test/test_louvain2.c index 25706dbdf3..22241121cd 100644 --- a/experimental/test/test_louvain2.c +++ b/experimental/test/test_louvain2.c @@ -19,8 +19,8 @@ typedef struct const matrix_info files[] = { {"comm0.mtx", 0.357142857142857}, - // {"res1.mtx", 0.0}, - // {"karate2.mtx", .42}, + {"res1.mtx", 0.0}, + {"karate2.mtx", .42}, {"",-1} }; //Store matrix by row @@ -54,9 +54,9 @@ void test_Louvain(void){ { G->kind = LAGraph_ADJACENCY_UNDIRECTED ; } - GrB_Matrix S=NULL; + GrB_Matrix S; double tsimple = LAGraph_WallClockTime ( ) ; - OK(LAGraph_Louvain2(S,G,msg)); + OK(LAGraph_Louvain2(&S,G,msg)); tsimple = LAGraph_WallClockTime ( ) - tsimple ; printf(" time: %f\n",tsimple); diff --git a/include/LAGraphX.h b/include/LAGraphX.h index a9f909a221..04f7edb947 100644 --- a/include/LAGraphX.h +++ b/include/LAGraphX.h @@ -1257,7 +1257,7 @@ LAGRAPHX_PUBLIC int LAGraph_Louvain2 ( //ouptut - GrB_Matrix S, + GrB_Matrix *S_result, // input LAGraph_Graph G, char* msg