|
| 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(°); \ |
| 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 (°, 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 | +} |
0 commit comments