Skip to content

Commit 9566dc4

Browse files
authored
Merge pull request #3 from peicasey/demo
Demo
2 parents df36359 + 605ab39 commit 9566dc4

File tree

3 files changed

+246
-47
lines changed

3 files changed

+246
-47
lines changed

experimental/algorithm/LAGr_EdgeBetweennessCentrality.c

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// LAGr_EdgeBetweennessCentrality: Exact algorithm for computing
2020
// betweeness centrality.
2121

22-
// This is an Advanced algorithm (G->AT is required).
22+
// This is an Advanced algorithm (no self edges allowed)
2323

2424

2525
//------------------------------------------------------------------------------
@@ -82,19 +82,19 @@
8282
// (1+x)/y function for double: z = (1 + x) / y
8383
//------------------------------------------------------------------------------
8484

85-
void add_one_divide_function (void *z, const void *x, const void *y)
85+
void add_one_divide_function (double *z, const double *x, const double *y)
8686
{
87-
double a = (*((double *) x)) ;
88-
double b = (*((double *) y)) ;
89-
(*((double *) z)) = (1 + a) / b ;
87+
double a = (*(x)) ;
88+
double b = (*(y)) ;
89+
(*(z)) = (1 + a) / b ;
9090
}
9191

9292
#define ADD_ONE_DIVIDE_FUNCTION_DEFN \
93-
"void add_one_divide_function (void *z, const void *x, const void *y) \n" \
93+
"void add_one_divide_function (double *z, const double *x, const double *y)\n" \
9494
"{ \n" \
95-
" double a = (*((double *) x)) ; \n" \
96-
" double b = (*((double *) y)) ; \n" \
97-
" (*((double *) z)) = (1 + a) / b ; \n" \
95+
" double a = (*(x)) ; \n" \
96+
" double b = (*(y)) ; \n" \
97+
" (*(z)) = (1 + a) / b ; \n" \
9898
"}"
9999

100100
//------------------------------------------------------------------------------
@@ -154,6 +154,7 @@ int LAGr_EdgeBetweennessCentrality
154154
LG_TRY (LAGraph_CheckGraph (G, msg)) ;
155155

156156
GrB_Matrix A = G->A ;
157+
#if 0
157158
GrB_Matrix AT ;
158159
if (G->kind == LAGraph_ADJACENCY_UNDIRECTED ||
159160
G->is_symmetric_structure == LAGraph_TRUE)
@@ -167,12 +168,14 @@ int LAGr_EdgeBetweennessCentrality
167168
AT = G->AT ;
168169
LG_ASSERT_MSG (AT != NULL, LAGRAPH_NOT_CACHED, "G->AT is required") ;
169170
}
171+
#endif
170172

171173
// =========================================================================
172174
// === initialization =====================================================
173175
// =========================================================================
174176

175-
GRB_TRY (GxB_BinaryOp_new (&Add_One_Divide, add_one_divide_function,
177+
GRB_TRY (GxB_BinaryOp_new (&Add_One_Divide,
178+
(GxB_binary_function) add_one_divide_function,
176179
GrB_FP64, GrB_FP64, GrB_FP64,
177180
"add_one_divide_function", ADD_ONE_DIVIDE_FUNCTION_DEFN)) ;
178181

@@ -214,10 +217,12 @@ int LAGr_EdgeBetweennessCentrality
214217

215218
GRB_TRY (GrB_Matrix_clear (Update)) ;
216219

217-
// Extract row root from A into frontier vector: frontier = AT(root,:)
218-
GRB_TRY (GrB_Col_extract (frontier, NULL, NULL, AT, GrB_ALL, n, root,
219-
NULL)) ;
220+
// Extract row root from A into frontier vector: frontier = A(root,:)
221+
GRB_TRY (GrB_Col_extract (frontier, NULL, NULL, A, GrB_ALL, n, root,
222+
GrB_DESC_T0)) ;
223+
220224
GRB_TRY (GrB_Vector_nvals (&frontier_size, frontier)) ;
225+
GRB_TRY (GrB_assign (frontier, frontier, NULL, 1.0, GrB_ALL, n, GrB_DESC_S)) ;
221226

222227
while (frontier_size != 0)
223228
{
@@ -242,7 +247,8 @@ int LAGr_EdgeBetweennessCentrality
242247
//----------------------------------------------------------------------
243248

244249
GRB_TRY (LG_SET_FORMAT_HINT (frontier, LG_SPARSE)) ;
245-
GRB_TRY (GrB_vxm (frontier, paths, NULL, GxB_PLUS_FIRST_FP64, frontier,
250+
GRB_TRY (GrB_vxm (frontier, paths, NULL, /* LAGraph_plus_first_fp64 */
251+
GxB_PLUS_FIRST_FP64, frontier,
246252
A, GrB_DESC_RSC )) ;
247253

248254
//----------------------------------------------------------------------
@@ -269,8 +275,12 @@ int LAGr_EdgeBetweennessCentrality
269275

270276
// Backtrack through the BFS and compute centrality updates for each vertex
271277
// GrB_Index fd1_size;
278+
279+
// printf ("\n----------------------------- backtrack:\n") ;
280+
272281
while (depth >= 1)
273282
{
283+
// printf ("\n----------------------------- backtrack depth : %" PRId64 "\n", depth) ;
274284
GrB_Vector f_d = Search [depth] ;
275285
GrB_Vector f_d1 = Search [depth - 1] ;
276286

@@ -291,20 +301,20 @@ int LAGr_EdgeBetweennessCentrality
291301
// combine
292302

293303
// intermediate matrix for Fd1 * A
294-
// GRB_TRY (GrB_eWiseMult(Fd1A, NULL, NULL, GrB_TIMES_FP64, I_matrix, AT, NULL)) ;
295-
GRB_TRY(GrB_mxm(Fd1A, NULL, NULL, GrB_PLUS_TIMES_SEMIRING_FP64,
304+
GRB_TRY(GrB_mxm(Fd1A, NULL, NULL, LAGraph_plus_first_fp64,
296305
I_matrix, A, NULL)) ;
297306

298-
// GRB_TRY (GrB_eWiseMult(U, NULL, NULL, GrB_TIMES_FP64, Fd1A, J_matrix, NULL)) ;
299307
GRB_TRY(GrB_mxm(Update, NULL, NULL, GrB_PLUS_TIMES_SEMIRING_FP64,
300308
Fd1A, J_matrix, NULL)) ;
301309

302-
303-
// 22 centrality = centrality + Update
304-
// GRB_TRY (GrB_assign(centrality, centrality, GrB_PLUS_FP64, Update, GrB_ALL, n, GrB_ALL, n,
305-
// GrB_DESC_S)) ;
310+
#if 1
311+
// 22 centrality{A} += Update, using assign
312+
GRB_TRY (GrB_assign(*centrality, A, GrB_PLUS_FP64, Update, GrB_ALL, n, GrB_ALL, n,
313+
GrB_DESC_S)) ;
314+
#else
315+
// 22 centrality = centrality + Update using eWiseAdd
306316
GRB_TRY (GrB_eWiseAdd (*centrality, NULL, NULL, GrB_PLUS_FP64, *centrality, Update, NULL)) ;
307-
317+
#endif
308318

309319
// 23 v = Update +.
310320

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
#define LG_FREE_ALL \
3+
printf ("done here: %d\n", __LINE__) ; \
4+
printf ("msg: [%s]\n", msg) ; \
5+
GrB_free (&centrality) ; \
6+
GrB_free (&A) ; \
7+
LAGraph_Delete (&G, msg) ; \
8+
9+
#include "LAGraphX.h"
10+
#include "LG_internal.h"
11+
#include <stdio.h>
12+
13+
double difference(GrB_Matrix bc, GrB_Matrix reference_bc)
14+
{
15+
GrB_Matrix diff = NULL ;
16+
17+
uint64_t n ;
18+
GrB_Matrix_nrows (&n, bc) ;
19+
20+
// Compute diff = max(abs(reference_bc - bc))
21+
GrB_Matrix_new(&diff, GrB_FP64, n, n) ;
22+
GrB_eWiseAdd(diff, NULL, NULL, GrB_MINUS_FP64, reference_bc, bc, NULL) ;
23+
GrB_apply(diff, NULL, NULL, GrB_ABS_FP64, diff, NULL) ;
24+
25+
double err = 1 ;
26+
GrB_reduce(&err, NULL, GrB_MAX_MONOID_FP64, diff, NULL) ;
27+
28+
GrB_free(&diff) ;
29+
30+
return err ;
31+
} ;
32+
33+
int main (int argc, char **argv)
34+
{
35+
36+
//--------------------------------------------------------------------------
37+
// startup LAGraph and GraphBLAS
38+
//--------------------------------------------------------------------------
39+
40+
char msg [LAGRAPH_MSG_LEN] ; // for error messages from LAGraph
41+
LAGraph_Graph G = NULL ;
42+
GrB_Matrix centrality = NULL, A = NULL ;
43+
GrB_Info info ;
44+
45+
// start GraphBLAS and LAGraph
46+
LAGRAPH_TRY (LAGraph_Init (msg)) ;
47+
48+
//--------------------------------------------------------------------------
49+
// read in the graph via a Matrix Market file from stdin
50+
//--------------------------------------------------------------------------
51+
52+
if (argc < 2)
53+
{
54+
printf("Usage: %s <matrix-market-file>\n", argv[0]);
55+
return (GrB_INVALID_VALUE) ;
56+
}
57+
58+
FILE *f = fopen(argv[1], "r");
59+
if (f == NULL)
60+
{
61+
printf("Error: unable to open file %s\n", argv[1]);
62+
return (GrB_INVALID_VALUE) ;
63+
}
64+
65+
double t = LAGraph_WallClockTime ( ) ;
66+
LAGRAPH_TRY (LAGraph_MMRead (&A, f, msg)) ;
67+
fclose(f);
68+
uint64_t n ;
69+
GRB_TRY (GrB_Matrix_nrows (&n, A)) ;
70+
71+
LAGRAPH_TRY (LAGraph_New (&G, &A, LAGraph_ADJACENCY_DIRECTED, msg)) ;
72+
LAGRAPH_TRY (LAGraph_DeleteSelfEdges (G, msg)) ;
73+
LAGRAPH_TRY (LAGraph_Cached_AT (G, msg)) ;
74+
t = LAGraph_WallClockTime ( ) - t ;
75+
printf ("Time to read the graph: %g sec\n", t) ;
76+
77+
printf ("\n==========================The input graph matrix G:\n") ;
78+
LAGRAPH_TRY (LAGraph_Graph_Print (G, LAGraph_SHORT, stdout, msg)) ;
79+
80+
//--------------------------------------------------------------------------
81+
// compute edge betweenness centrality
82+
//--------------------------------------------------------------------------
83+
84+
// LG_SET_BURBLE (true) ;
85+
86+
t = LAGraph_WallClockTime ( ) ;
87+
LAGRAPH_TRY (LAGr_EdgeBetweennessCentrality (&centrality, G, msg)) ;
88+
t = LAGraph_WallClockTime ( ) - t ;
89+
printf ("Time for LAGr_EdgeBetweennessCentrality: %g sec\n", t) ;
90+
91+
// LG_SET_BURBLE (false) ;
92+
93+
//--------------------------------------------------------------------------
94+
// check the results using LG_check_edgeBetweennessCentrality
95+
//--------------------------------------------------------------------------
96+
97+
GrB_Matrix reference_centrality = NULL;
98+
t = LAGraph_WallClockTime ( ) ;
99+
LAGRAPH_TRY (LG_check_edgeBetweennessCentrality(&reference_centrality, G, msg)) ;
100+
t = LAGraph_WallClockTime ( ) - t ;
101+
printf ("Time for LG_check_edgeBetweennessCentrality: %g sec\n", t) ;
102+
103+
104+
double err = difference(centrality, reference_centrality) ;
105+
printf ("Error between computed and reference centrality: %e\n", err) ;
106+
if (err < 1e-4)
107+
{
108+
printf ("Test passed.\n") ;
109+
}
110+
else
111+
{
112+
printf ("Test failure!\n") ;
113+
}
114+
115+
//--------------------------------------------------------------------------
116+
// free everything and finish
117+
//--------------------------------------------------------------------------
118+
119+
GrB_free (&centrality) ;
120+
GrB_free (&reference_centrality) ;
121+
LAGraph_Delete (&G, msg) ;
122+
LAGRAPH_TRY (LAGraph_Finalize (msg)) ;
123+
return (GrB_SUCCESS) ;
124+
}

0 commit comments

Comments
 (0)