Skip to content

Commit 09123b6

Browse files
committed
simplify partitionquality code and increase efficiency
1 parent acbc83c commit 09123b6

1 file changed

Lines changed: 26 additions & 29 deletions

File tree

experimental/algorithm/LAGr_PartitionQuality.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@
3030

3131
// https://arxiv.org/abs/0906.0612 pp. 15
3232

33+
#include "GraphBLAS.h"
3334
#define LG_FREE_WORK \
3435
{ \
35-
GrB_free(&trace); \
3636
GrB_free(&k); \
37+
GrB_free(&x); \
3738
GrB_free(&C); \
38-
GrB_free(&CA); \
39-
GrB_free(&_A); \
4039
GrB_free(&ONE_INT64); \
4140
}
4241

@@ -60,11 +59,9 @@ int LAGr_PartitionQuality(
6059
char *msg)
6160
{
6261
#if LAGRAPH_SUITESPARSE
63-
GrB_Vector trace = NULL;
6462
GrB_Vector k = NULL;
63+
GrB_Vector x = NULL;
6564
GrB_Matrix C = NULL;
66-
GrB_Matrix CA = NULL;
67-
GrB_Matrix _A = NULL;
6865

6966
GrB_Scalar ONE_INT64 = NULL;
7067

@@ -100,47 +97,47 @@ int LAGr_PartitionQuality(
10097
fclose(f);
10198
#endif
10299

103-
GRB_TRY(GrB_Matrix_nrows(&n, A));
104-
GRB_TRY(GrB_Matrix_nvals(&nedges, A));
100+
GRB_TRY (GrB_Matrix_nrows (&n, A));
101+
GRB_TRY (GrB_Matrix_nvals (&nedges, A));
105102

106-
GRB_TRY(GrB_Matrix_new(&C, GrB_INT64, n, n));
107-
GRB_TRY(GrB_Matrix_new(&CA, GrB_INT64, n, n));
108-
GRB_TRY(GrB_Vector_new(&trace, GrB_INT64, n));
109-
GRB_TRY(GrB_Vector_new(&k, GrB_INT64, n));
110-
GRB_TRY(GrB_Scalar_new(&ONE_INT64, GrB_INT64));
103+
GRB_TRY (GrB_Matrix_new (&C, GrB_INT64, n, n));
104+
GRB_TRY (GrB_Vector_new (&k, GrB_INT64, n));
105+
GRB_TRY (GrB_Vector_new (&x, GrB_BOOL, n));
106+
GRB_TRY (GrB_Scalar_new (&ONE_INT64, GrB_INT64));
111107

112-
GRB_TRY(GrB_Scalar_setElement_BOOL(ONE_INT64, (uint64_t)1));
108+
GRB_TRY (GrB_assign(x, NULL, NULL, (bool) true, GrB_ALL, 0, NULL)) ;
109+
GRB_TRY (GrB_Scalar_setElement_BOOL(ONE_INT64, (int64_t)1));
113110

114111
// convert the cluster vector to a boolean matrix C where
115112
// C(i, j) = 1 if and only if vertex j is in cluster i
116113
GrB_Index *cI, *cX;
117-
LAGRAPH_TRY(LAGraph_Malloc((void **)&cI, n, sizeof(GrB_Index), msg));
118-
LAGRAPH_TRY(LAGraph_Malloc((void **)&cX, n, sizeof(GrB_Index), msg));
119-
GRB_TRY(GrB_Vector_extractTuples_INT64(cI, (int64_t *) cX, &n, c));
120-
GRB_TRY(GxB_Matrix_build_Scalar(C, cX, cI, ONE_INT64, n));
121-
GrB_Matrix_wait(C, GrB_MATERIALIZE);
114+
LAGRAPH_TRY (LAGraph_Malloc ((void **)&cI, n, sizeof(GrB_Index), msg));
115+
LAGRAPH_TRY (LAGraph_Malloc ((void **)&cX, n, sizeof(GrB_Index), msg));
116+
GRB_TRY (GrB_Vector_extractTuples_INT64 (cI, (int64_t *) cX, &n, c));
117+
GRB_TRY (GxB_Matrix_build_Scalar (C, cX, cI, ONE_INT64, n));
122118
LAGraph_Free((void **)&cI, NULL);
123119
LAGraph_Free((void **)&cX, NULL);
124120

125121
bool is_undirected = (G->is_symmetric_structure == LAGraph_TRUE);
126122

127123
// k = sum(C) .^ 2
128-
GRB_TRY(GrB_reduce(k, NULL, NULL, GrB_PLUS_MONOID_INT64, C, NULL));
129-
GRB_TRY(GrB_Vector_apply_BinaryOp2nd_INT64(k, NULL, NULL, GxB_POW_INT64, k,
130-
2, NULL));
124+
GRB_TRY (GrB_mxv (k, NULL, NULL, GxB_PLUS_PAIR_INT64, C, x, NULL));
125+
GRB_TRY (GrB_Vector_apply_BinaryOp2nd_INT64(
126+
k, NULL, NULL, GxB_POW_INT64, k, 2, NULL));
131127
// sum_k2 = total number of possible intra-cluster edges
132-
GRB_TRY(GrB_reduce(&sum_k2, NULL, GrB_PLUS_MONOID_INT64, k, NULL));
128+
GRB_TRY (GrB_reduce (&sum_k2, NULL, GrB_PLUS_MONOID_INT64, k, NULL));
133129

134130
// Calculate actual number of intra-cluster edges, if A is weighted
135131
// then we ignore the weights and just count the number of edges as
136132
// performance and coverage are inherently counting problems
137-
GRB_TRY(GrB_mxm(CA, NULL, NULL, LAGraph_plus_one_int64, C, A, NULL));
138-
GRB_TRY(GrB_mxm(CA, NULL, NULL, GrB_PLUS_TIMES_SEMIRING_INT64, CA, C,
139-
GrB_DESC_T1));
140-
GRB_TRY(GxB_Vector_diag(trace, CA, 0, NULL));
141133

142-
GRB_TRY(
143-
GrB_reduce(&n_intraEdges, NULL, GrB_PLUS_MONOID_INT64, trace, NULL));
134+
// Count the number of edges per node in the cluster originating at a node
135+
// in the cluster
136+
GRB_TRY (GrB_mxm (C, C, NULL, GxB_PLUS_PAIR_INT64, C, A, GrB_DESC_S));
137+
138+
// sum the intra-cluster edges per node for all nodes
139+
GRB_TRY (
140+
GrB_reduce (&n_intraEdges, NULL, GrB_PLUS_MONOID_INT64, C, NULL)) ;
144141

145142
if (G->nself_edges > 0)
146143
{

0 commit comments

Comments
 (0)