Skip to content

Commit

Permalink
current progress
Browse files Browse the repository at this point in the history
trying to apply
  • Loading branch information
Olumayowa-Olowomeye committed Nov 18, 2024
1 parent 7d277d1 commit 5a24728
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 136 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"lg_internal.h": "c"
}
},
"C_Cpp.errorSquiggles": "enabled"
}
3 changes: 2 additions & 1 deletion experimental/algorithm/LAGraph_Louvain.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ int LAGraph_Louvain(
GRB_TRY(LAGr_Modularity2(&Q,gamma,A,S,msg));
printf("Iterations: %d\n", iter);
printf("Q:%.15g\n",Q);
LG_FREE_ALL;
// LG_FREE_ALL;
LAGraph_Random_Finalize(msg);
return 0;
}
183 changes: 116 additions & 67 deletions experimental/algorithm/LAGraph_Louvain2.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define LG_FREE_LOUV2 \
#undef LG_FREE_ALL
#define LG_FREE_ALL \
{ \
GrB_free (&A) ; \
GrB_free (&k) ; \
Expand All @@ -37,12 +37,56 @@
GrB_free (&p) ; \
GrB_free (&srxt) ; \
GrB_free (&t_q) ; \
GrB_free (&Theta) ; \
}
#define LG_FREE_ALL \
{ \
LG_FREE_LOUV2; \
#define DEBUG 1

typedef struct tuple_fp64{
int64_t k;
double v;
}tuple_fp64;
#define FP64_K "typedef struct tuple_fp64 { int64_t k ; double v ; } tuple_fp64 ;"
void make_fp64(tuple_fp64 *z,
const double *x, GrB_Index ix, GrB_Index jx,
const void *y, GrB_Index iy, GrB_Index jy,
const void *theta)
{
z->k = (int64_t)jx;
z->v = (*x);
}
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))
{
z->k = x->k;
z->v = x->v;
}else{
z->k = y->k;
z->v = y->v;
}
#define DEBUG 0
}
#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" \
" { \n" \
" z->k = x->k; \n" \
" z->v = x->v; \n" \
" }else{ \n" \
" z->k = y->k; \n" \
" z->v = y->v; \n" \
" } \n" \
"}"
#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 void *theta) \n" \
"{ \n" \
" z->k = (int64_t)jx; \n" \
" z->v = (*x); \n" \
"}"



int LAGraph_Louvain2(
//output
GrB_Matrix S,
Expand All @@ -54,6 +98,8 @@ int LAGraph_Louvain2(
char MATRIX_TYPE[LAGRAPH_MSG_LEN];
if (DEBUG)
GrB_set (GrB_GLOBAL, true, GxB_BURBLE);


//assignment of monoids, bops, and semis
GrB_Monoid plusmon = GrB_PLUS_MONOID_FP64;
GrB_Monoid maxmon = GrB_MAX_MONOID_FP64;
Expand All @@ -79,11 +125,36 @@ int LAGraph_Louvain2(
bool * vals = NULL;
GrB_Index *p_cs=NULL;
double * p_vals;


GrB_Matrix A = G->A;
// GxB_print(A,5);
GrB_Index n,b;



//index bin op definitions
//------------------------------------------------------------------------------------------------------------------
GrB_Scalar Theta;
GrB_Scalar_new(&Theta, GrB_BOOL);
GRB_TRY(GrB_Scalar_setElement_BOOL(Theta, 0));
GzB_IndexBinaryOp Iop;
GrB_BinaryOp Bop;
GrB_Type Tuple;
GRB_TRY(GxB_Type_new(&Tuple, sizeof(tuple_fp64), "tuple_fp64", FP64_K));
GRB_TRY(GzB_IndexBinaryOp_new (&Iop, make_fp64, Tuple, GrB_FP64, GrB_BOOL, GrB_BOOL,"make_fp64", MAKE_FP64));
GRB_TRY(GzB_BinaryOp_new_IndexOp(&Bop, Iop, Theta));
tuple_fp64 id;
memset(&id, 0, sizeof(tuple_fp64));
id.k = INT64_MAX;
id.v = (double)(-INFINITY);
GrB_Monoid Mon;
GRB_TRY(GxB_BinaryOp_new(&Bop, max_fp64, Tuple, Tuple, Tuple, "max_fp64", MAX_FP64));
GrB_Semiring Semiring;
GRB_TRY(GrB_Monoid_new_UDT(&Mon, Bop, &id));
GRB_TRY(GrB_Semiring_new(&Semiring, Mon, Bop));
//------------------------------------------------------------------------------------------------------------------

GRB_TRY(LAGraph_Random_Init(msg));

GRB_TRY(GrB_Matrix_nrows(&n,A));
GRB_TRY(GrB_Matrix_ncols(&b,A));

Expand Down Expand Up @@ -114,8 +185,6 @@ int LAGraph_Louvain2(
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(&t, GrB_BOOL, n));
GRB_TRY(GrB_Vector_new(&p,GrB_FP64,n));
GRB_TRY(GrB_Vector_new(&z,GrB_FP64,n));

GrB_Matrix dS;
Expand All @@ -130,8 +199,9 @@ int LAGraph_Louvain2(
GrB_Index *dSp, *dSj, dSp_size, dSj_size, dSx_size ;
bool dS_jumbled, dS_iso;
GRB_TRY(GrB_Matrix_diag(&dS,temp,0));


GrB_Vector y_rand;
GRB_TRY(GrB_Vector_new(&y_rand, GrB_UINT64,n));
GRB_TRY (GrB_assign (y_rand, NULL, NULL, 0, GrB_ALL, n, NULL)) ;

bool changed = true;
int max_iter = 20;
Expand Down Expand Up @@ -164,9 +234,9 @@ int LAGraph_Louvain2(
Sp_size, Sj_size, Sx_size, NULL, S_jumbled, NULL));

////////////////////////////////////////////////////////////
//-------------q1<t_q> = 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,
Expand All @@ -193,69 +263,48 @@ int LAGraph_Louvain2(
//q1<t_q> = 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 -----------//
uint64_t seed = 213;
GRB_TRY(LAGraph_Random_Seed(y_rand,seed,msg));
GxB_print(y_rand,5);
GrB_Vector max_q1;
GrB_Vector_new(&max_q1,Tuple,n);
GxB_print(q1,5);
GRB_TRY(GrB_mxv(max_q1,t_q,NULL,Semiring,(GrB_Matrix)q1,y_rand,NULL));
GxB_print(max_q1,5);

//t = (q1 == [max_i q_1(i)])
double max_q1;
GRB_TRY(GrB_Vector_reduce_FP64(&max_q1,NULL,maxmon,q1,NULL));
GRB_TRY(GrB_Vector_select_FP64(t,NULL,NULL,GrB_VALUEEQ_FP64,q1,max_q1,NULL));
// GxB_print(t,5);
///////////////////////////////////////////////////////////


GRB_TRY(GrB_Vector_nvals(&nvals_t,t));
while(nvals_t>1){
// printf("%ld\n",nvals_t);
GRB_TRY(LAGraph_Malloc ((void **) &p_cs, nvals_t, sizeof (GrB_Index), msg)); //free p_cs and P-vals
GRB_TRY(LAGraph_Malloc ((void **) &p_vals, nvals_t, sizeof (double), msg)) ;
GRB_TRY(GrB_Vector_extractTuples_FP64(p_cs,p_vals,&nvals_t,t));

// p = random() x t
for(int j = 0;j<nvals_t;j++){
double y = rd();
GRB_TRY(GrB_Vector_setElement_FP64(p,y*p_vals[j],p_cs[j]));
}
// GxB_print(p,5);
//t = (p== [max_i p_1(i)])
double max_p;
GRB_TRY(GrB_Vector_reduce_FP64(&max_p,NULL,maxmon,p,NULL));
// printf("max_p:%f\n",max_p);
GRB_TRY(GrB_Vector_select_FP64(t,NULL,NULL,GrB_VALUEEQ_FP64,p,max_p,NULL));
// GxB_print(t,5);
GRB_TRY(GrB_Vector_nvals(&nvals_t,t));
// LAGraph_Free((void**)p_cs,msg);
free(p_cs);
free(p_vals);
}
// GxB_print(t,5);

//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;
}
// //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;
}
iter++;
}
// GxB_print(S,5);
GxB_print(S,5);
double Q;
double gamma = 1;
GRB_TRY(LAGr_Modularity2(&Q,gamma,A,S,msg));
Expand Down
64 changes: 0 additions & 64 deletions experimental/algorithm/temp_ibop.c

This file was deleted.

1 change: 1 addition & 0 deletions experimental/test/test_louvain.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const matrix_info files[] = {
//Store matrix by row
void test_Louvain(void){
LAGraph_Init(msg);
//Lagraph+RAndom_init
printf("\n");
for(int k = 0;;k++){
if (strlen(files[k].matrix_file) == 0)
Expand Down
4 changes: 2 additions & 2 deletions experimental/test/test_louvain2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion experimental/test/test_modularity.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void test_modularity (void)
GrB_Matrix B=NULL;
double gamma = files[k].gamma;
OK(LAGr_Modularity2(Q,gamma,A,S,msg));
bool ok_mod;
bool ok_mod =false;
// GxB_print()
printf("Q:%.15g\n",*Q);
// printf("mod:%.15g\n",files[k].mod);
Expand Down

0 comments on commit 5a24728

Please sign in to comment.