Skip to content

Commit 5376b9f

Browse files
elawh1Olumayowa-Olowomeye
authored andcommitted
jaccard implementation temp commit
1 parent 53d2c6c commit 5376b9f

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//------------------------------------------------------------------------------
2+
// LAGraph_Jaccard - parallel jaccard similarity
3+
// our second approach to compute jaccard similarity
4+
//------------------------------------------------------------------------------
5+
6+
#define LG_FREE_WORK \
7+
{ \
8+
GrB_free(&deg); \
9+
GrB_free(&Au); \
10+
GrB_free(&R); \
11+
GrB_free(&J); \
12+
GrB_free(&B); \
13+
}
14+
15+
#define LG_FREE_ALL \
16+
{ \
17+
LG_FREE_WORK ; \
18+
if (JC != NULL && *JC != NULL) \
19+
{ \
20+
GrB_free(JC); \
21+
} \
22+
}
23+
24+
#include "LG_internal.h"
25+
#include <LAGraph.h>
26+
#include "LAGraphX.h"
27+
#include <sys/time.h>
28+
#ifdef _OPENMP
29+
#include <omp.h>
30+
#endif
31+
32+
int LAGraph_Jaccard // a simple algorithm, just for illustration
33+
(
34+
// output
35+
GrB_Matrix *JC,
36+
// input: not modified
37+
LAGraph_Graph G,
38+
bool all_pairs,
39+
char *msg
40+
)
41+
{
42+
GrB_Matrix B = NULL, J = NULL, Au = NULL, R = NULL;
43+
GrB_Index n;
44+
GrB_Vector deg = NULL;
45+
//--------------------------------------------------------------------------
46+
// check inputs
47+
//--------------------------------------------------------------------------
48+
LG_CLEAR_MSG ;
49+
50+
int nthreads = omp_get_max_threads() ;
51+
printf("num of threads %d\n", nthreads);
52+
53+
LG_ASSERT (JC != NULL, GrB_NULL_POINTER) ;
54+
(*JC) = NULL ;
55+
LG_TRY (LAGraph_CheckGraph (G, msg)) ;
56+
57+
GrB_Semiring semiring = NULL;
58+
59+
GrB_Matrix A = G->A ;
60+
struct timeval stop, start, st_deg, en_deg, st_intersection, en_intersection, st_union, en_union, en_select;
61+
62+
gettimeofday(&start, NULL);
63+
64+
GRB_TRY( GrB_Matrix_nrows (&n, A) );
65+
//--------------------------------------------------------------------------
66+
// calculating out degree matrix deg
67+
//--------------------------------------------------------------------------
68+
gettimeofday(&st_deg, NULL);
69+
GrB_Type int_type = (n > INT32_MAX) ? GrB_INT64 : GrB_INT32 ;
70+
GRB_TRY (GrB_Vector_new (&deg, int_type, n)) ;
71+
GRB_TRY( GrB_reduce(deg, NULL, NULL, (int_type == GrB_INT64) ? GrB_PLUS_INT64 : GrB_PLUS_INT32, A, NULL));
72+
gettimeofday(&en_deg, NULL);
73+
74+
// GRB_TRY (LAGraph_Vector_Print (deg, LAGraph_COMPLETE_VERBOSE, stdout, msg)) ;
75+
//--------------------------------------------------------------------------
76+
// B is intersection matrix
77+
//--------------------------------------------------------------------------
78+
gettimeofday(&st_intersection, NULL);
79+
GRB_TRY(GrB_Matrix_new(&B, GrB_FP32, n, n));
80+
81+
//make a copy of A
82+
GRB_TRY(GrB_Matrix_new(&Au, GrB_UINT32, n, n));
83+
GRB_TRY( GrB_select(Au, NULL, NULL, GrB_VALUENE_BOOL, A, 0, NULL));
84+
GRB_TRY(GrB_mxm(B, all_pairs ? NULL : A, NULL, GxB_PLUS_TIMES_UINT32, Au, Au, NULL));
85+
86+
GRB_TRY( GrB_select(B, NULL, NULL, GrB_TRIU, B, (int64_t)1, NULL));
87+
GRB_TRY (GrB_Matrix_wait (B, GrB_COMPLETE)) ;
88+
gettimeofday(&en_select, NULL);
89+
90+
91+
//--------------------------------------------------------------------------
92+
// S is jaccard index
93+
//--------------------------------------------------------------------------
94+
// assign deg //
95+
GrB_Matrix_new(&R, GrB_FP32, n, n);
96+
for (GrB_Index j = 0; j < n; j++) {
97+
GRB_TRY( GrB_assign(R, NULL, NULL, deg, (GrB_Index*) GrB_ALL, n, j, NULL));
98+
}
99+
// assign deg into every row and ADD to current R: R(i, :) += v^T
100+
for (GrB_Index i = 0; i < n; i++) {
101+
GRB_TRY( GrB_assign(R, NULL, GrB_PLUS_INT32, deg, i, (GrB_Index*) GrB_ALL, n, GrB_DESC_T0));
102+
}
103+
104+
GRB_TRY( GrB_eWiseAdd(R, B, NULL, GrB_MINUS_FP32, R, B, NULL) );
105+
GRB_TRY( GrB_eWiseMult(B, NULL, NULL, GrB_DIV_FP32, B, R, NULL) );
106+
gettimeofday(&en_union, NULL);
107+
GRB_TRY (GrB_Matrix_wait (B, GrB_COMPLETE)) ;
108+
109+
gettimeofday(&stop, NULL);
110+
111+
long duration_usec = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;
112+
printf("execute time is %lu microseconds\n", duration_usec);
113+
printf("degree calculation time is %lu microseconds\n", ((en_deg.tv_sec - st_deg.tv_sec) * 1000000 + en_deg.tv_usec - st_deg.tv_usec));
114+
printf("intesection and select time is %lu microseconds\n",
115+
((en_select.tv_sec - st_intersection.tv_sec) * 1000000 + en_select.tv_usec - st_intersection.tv_usec));
116+
printf("union time is %lu microseconds\n", ((en_union.tv_sec - st_union.tv_sec) * 1000000 + en_union.tv_usec - st_union.tv_usec));
117+
118+
119+
// GRB_TRY (LAGraph_Matrix_Print (B, LAGraph_COMPLETE, stdout, msg)) ;
120+
(*JC) = B;
121+
B = NULL;
122+
LG_FREE_WORK;
123+
124+
return (GrB_SUCCESS) ;
125+
}

include/LAGraphX.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,17 @@ int LAGraph_MinCut(
15751575
GrB_Index t, //sink node index
15761576
char *msg
15771577
);
1578+
1579+
LAGRAPHX_PUBLIC
1580+
int LAGraph_Jaccard
1581+
(
1582+
// output
1583+
GrB_Matrix *coefficients,
1584+
// input
1585+
LAGraph_Graph G,
1586+
bool all_pairs,
1587+
char *msg
1588+
);
15781589
//------------------------------------------------------------------------------
15791590
// Louvain sub-algorithms
15801591
//------------------------------------------------------------------------------

0 commit comments

Comments
 (0)