Skip to content

Commit 5413e56

Browse files
Merge pull request #361 from GomezGab/MSF
2 parents cfcacb1 + a66320c commit 5413e56

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed

experimental/algorithm/LAGraph_msf.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ static void tupleEq(bool *z, const pairW *x, const pairW *y)
221221
GrB_free (&pairMin_monoid); \
222222
GrB_free (&pairMin2nd); \
223223
GrB_free (&lg_pair); \
224+
GrB_free (&max_weight); \
224225
}
225226
int LAGraph_msf
226227
(
@@ -249,6 +250,7 @@ int LAGraph_msf
249250
GrB_Monoid pairMin_monoid = NULL;
250251
GrB_Semiring minComb = NULL, pairMin2nd = NULL;
251252
GrB_UnaryOp fst = NULL, snd = NULL;
253+
GrB_Scalar max_weight = NULL;
252254
int edge_h = GrB_DEFAULT;
253255
uint64_t edge_size = 0, edge_n = 0;
254256
GrB_IndexUnaryOp s1 = NULL, s2 = NULL;
@@ -349,19 +351,25 @@ int LAGraph_msf
349351
pairW inf = {.wInt = INT64_MAX, .idx = UINT64_MAX};
350352
if(tcode == GrB_FP64_CODE) inf.wFp = INFINITY;
351353

354+
GRB_TRY (GxB_Scalar_new(&max_weight, weight_type)) ;
355+
356+
352357
GRB_TRY (GxB_BinaryOp_new (
353358
&comb, (GxB_binary_function) combine,
354359
lg_pair, weight_type, GrB_UINT64, "combine", COMBINE
355360
)) ;
361+
356362
if(tcode == GrB_INT64_CODE)
357363
{
364+
GRB_TRY (GxB_Scalar_setElement_INT64(max_weight, INT64_MAX)) ;
358365
GRB_TRY (GxB_BinaryOp_new (
359366
&pairMin, (GxB_binary_function) tupleMinInt,
360367
lg_pair, lg_pair, lg_pair, "tupleMinInt", TUPLEMININT
361368
)) ;
362369
}
363370
else
364371
{
372+
GRB_TRY (GxB_Scalar_setElement_FP64(max_weight, INFINITY)) ;
365373
GRB_TRY (GxB_BinaryOp_new (
366374
&pairMin, (GxB_binary_function) tupleMinFp,
367375
lg_pair, lg_pair, lg_pair, "tupleMinFp", TUPLEMINFP
@@ -372,13 +380,16 @@ int LAGraph_msf
372380
&pairSec, (GxB_binary_function) tuple2nd,
373381
lg_pair, GrB_BOOL, lg_pair, "tuple2nd", TUPLE2ND
374382
)) ;
383+
375384
GRB_TRY (GxB_BinaryOp_new (
376385
&pairEq, (GxB_binary_function) tupleEq,
377386
GrB_BOOL, lg_pair, lg_pair, "tupleEq", TUPLEEQ
378387
)) ;
388+
379389
GRB_TRY (GrB_Monoid_new_UDT (&pairMin_monoid, pairMin, (void *) &inf)) ;
380390
GRB_TRY (GrB_Semiring_new (&minComb, pairMin_monoid, comb)) ;
381391
GRB_TRY (GrB_Semiring_new (&pairMin2nd, pairMin_monoid, pairSec)) ;
392+
382393
GRB_TRY (GxB_UnaryOp_new (
383394
&fst, (GxB_unary_function) get_fst, weight_type, lg_pair,
384395
"get_fst", GETFST)) ;
@@ -413,11 +424,12 @@ int LAGraph_msf
413424
// edge[u] = u's minimum edge (weight and index are encoded together)
414425
GRB_TRY (GrB_Vector_assign_UDT (
415426
edge, NULL, NULL, (void *) &inf, GrB_ALL, 0, NULL)) ;
416-
GRB_TRY (GrB_mxv (edge, 0, pairMin, minComb, S, f, NULL)) ;
427+
GRB_TRY (GrB_mxv (edge, NULL, pairMin, minComb, S, f, NULL)) ;
428+
417429
// cedge[u] = children's minimum edge | if u is a root
418-
// = (INT_MAX, u) | otherwise
419-
GRB_TRY (GrB_assign (t, NULL, NULL, (uint64_t) INT_MAX, GrB_ALL, 0, NULL)) ;
420-
GRB_TRY (GrB_eWiseMult (cedge, NULL, NULL, comb, t, I, NULL)) ;
430+
// = (max_weight, u) | otherwise
431+
GRB_TRY (GrB_Vector_apply_BinaryOp1st_Scalar (
432+
cedge, NULL, NULL, comb, max_weight, I, NULL)) ;
421433
LG_TRY (LAGraph_FastAssign_Semiring(
422434
cedge, NULL, pairMin, parent_v, edge, ramp, pairMin2nd, NULL, msg
423435
)) ;
@@ -443,7 +455,7 @@ int LAGraph_msf
443455

444456
// 3. each root picks a vertex from its children to generate the solution
445457
GRB_TRY (GrB_assign (index_v, NULL, NULL, n, GrB_ALL, 0, NULL)) ;
446-
GRB_TRY (GrB_assign (index_v, mask, 0, I, GrB_ALL, 0, NULL)) ;
458+
GRB_TRY (GrB_assign (index_v, mask, NULL, I, GrB_ALL, 0, NULL)) ;
447459
GRB_TRY (GrB_assign (t, NULL, NULL, n, GrB_ALL, 0, NULL)) ;
448460
LG_TRY (LAGraph_FastAssign_Semiring(
449461
t, NULL, GrB_MIN_UINT64, parent_v, index_v, ramp,

experimental/test/test_msf.c

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct
4141
uint64_t ans_n;
4242
const uint64_t *ans_i;
4343
const uint64_t *ans_j;
44+
double ans_w;
4445
}
4546
matrix_info ;
4647
const uint64_t A_mtx_i [] = {1, 2, 3, 4, 5, 6};
@@ -51,27 +52,24 @@ const uint64_t mtx8_i [] = {1, 2, 3, 4, 5, 6};
5152
const uint64_t mtx8_j [] = {4, 3, 0, 6, 2, 3};
5253
const matrix_info files [ ] =
5354
{
54-
#if LG_SUITESPARSE_GRAPHBLAS_V10
55-
{ 1, "A.mtx", 6, A_mtx_i, A_mtx_j},
56-
{ 1, "jagmesh7.mtx", 1137, NULL, NULL},
57-
{ 0, "west0067.mtx", 66, NULL, NULL}, // unsymmetric
58-
{ 1, "bcsstk13.mtx", 2002, NULL, NULL}, // overflows an INT32
59-
{ 0, "matrix_int8.mtx", 6, mtx8_i, mtx8_j},
60-
{ 0, "matrix_uint8.mtx", 6, mtx8u_i, mtx8u_j},
61-
{ 1, "karate.mtx", 33, NULL, NULL},
62-
{ 1, "ldbc-cdlp-undirected-example.mtx", 7, NULL, NULL},
63-
{ 1, "ldbc-undirected-example-bool.mtx", 8, NULL, NULL},
64-
{ 1, "ldbc-undirected-example-unweighted.mtx", 8, NULL, NULL},
65-
{ 1, "ldbc-undirected-example.mtx", 8, NULL, NULL},
66-
{ 1, "ldbc-wcc-example.mtx", 9, NULL, NULL},
67-
#endif
55+
{ 1, "A.mtx", 6, A_mtx_i, A_mtx_j, NAN},
56+
{ 1, "jagmesh7.mtx", 1137, NULL, NULL, NAN},
57+
{ 0, "west0067.mtx", 66, NULL, NULL, -63.9103636}, // unsymmetric
58+
{ 1, "bcsstk13.mtx", 2002, NULL, NULL, -27812381075940.4},
59+
{ 0, "matrix_int8.mtx", 6, mtx8_i, mtx8_j, -120.0},
60+
{ 0, "matrix_uint8.mtx", 6, mtx8u_i, mtx8u_j, 8.0},
61+
{ 1, "karate.mtx", 33, NULL, NULL, NAN},
62+
{ 1, "ldbc-cdlp-undirected-example.mtx", 7, NULL, NULL, NAN},
63+
{ 1, "ldbc-undirected-example-bool.mtx", 8, NULL, NULL, NAN},
64+
{ 1, "ldbc-undirected-example-unweighted.mtx", 8, NULL, NULL, NAN},
65+
{ 1, "ldbc-undirected-example.mtx", 8, NULL, NULL, NAN},
66+
{ 1, "ldbc-wcc-example.mtx", 9, NULL, NULL, NAN},
6867
{ 0, "" },
6968
} ;
7069

7170
//****************************************************************************
7271
void test_msf (void)
7372
{
74-
#if LAGRAPH_SUITESPARSE
7573
LAGraph_Init (msg) ;
7674
GrB_Scalar zeroB = NULL;
7775
GrB_Scalar_new(&zeroB, GrB_BOOL);
@@ -163,6 +161,10 @@ void test_msf (void)
163161
}
164162

165163
printf ("\nmsf:\n") ;
164+
double tot_weight, ans_w = files[k].ans_w;
165+
OK (GrB_Matrix_reduce_FP64(&tot_weight, NULL, GrB_PLUS_MONOID_FP64, C, NULL)) ;
166+
TEST_CHECK (isnan(files[k].ans_w) ||
167+
fabs(tot_weight - ans_w) <= 1E-10 * fabs(ans_w)) ;
166168
OK (LAGraph_Matrix_Print (C, pr, stdout, msg)) ;
167169
OK (LAGraph_Delete (&G, msg)) ;
168170
OK (LAGraph_Delete (&G_C, msg)) ;
@@ -175,7 +177,64 @@ void test_msf (void)
175177
}
176178
GrB_free(&zeroB);
177179
LAGraph_Finalize (msg) ;
178-
#endif
180+
}
181+
182+
//------------------------------------------------------------------------------
183+
// infinity test
184+
//------------------------------------------------------------------------------
185+
186+
void test_inf_msf (void)
187+
{
188+
LAGraph_Init (msg) ;
189+
GrB_Scalar zeroB = NULL;
190+
GrB_Scalar_new(&zeroB, GrB_BOOL);
191+
GrB_Scalar_setElement_BOOL(zeroB, false);
192+
193+
// load the matrix as A
194+
const char *aname = "bcsstk13.mtx" ;
195+
bool symmetric = 1 ;
196+
printf ("\n================================== %s:\n", aname) ;
197+
TEST_CASE (aname) ;
198+
snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
199+
FILE *f = fopen (filename, "r") ;
200+
TEST_CHECK (f != NULL) ;
201+
OK (LAGraph_MMRead (&A, f, msg)) ;
202+
fclose (f) ;
203+
204+
GrB_Index n = 0;
205+
OK (GrB_Matrix_nrows (&n, A)) ;
206+
OK (GrB_Matrix_new(&S, GrB_BOOL, n, n)) ;
207+
208+
// Make A be iso infinity and S iso true.
209+
OK (GrB_Matrix_assign_FP64(
210+
A, A, NULL, INFINITY, GrB_ALL, n, GrB_ALL, n, GrB_DESC_S)) ;
211+
OK (GrB_Matrix_assign_BOOL(
212+
S, A, NULL, (bool) true, GrB_ALL, n, GrB_ALL, n, GrB_DESC_S)) ;
213+
214+
// compute the min spanning forest
215+
S_C = C = NULL ;
216+
// GxB_Global_Option_set(GxB_BURBLE, true);
217+
int result = LAGraph_msf (&C, A, false, msg) ;
218+
// GxB_Global_Option_set(GxB_BURBLE, false);
219+
printf ("result: %d\n", result) ;
220+
OK(result);
221+
222+
// GxB_Global_Option_set(GxB_BURBLE, true);
223+
result = LAGraph_msf (&S_C, S, false, msg) ;
224+
// GxB_Global_Option_set(GxB_BURBLE, false);
225+
printf ("result: %d\n", result) ;
226+
OK(result);
227+
228+
bool ok = false ;
229+
// check structure is equal.
230+
OK (LAGraph_Matrix_IsEqualOp(&ok, C, S_C, GrB_ONEB_BOOL, msg));
231+
TEST_CHECK(ok);
232+
OK (GrB_free (&S)) ;
233+
OK (GrB_free (&S_C)) ;
234+
OK (GrB_free (&C)) ;
235+
OK (GrB_free (&A)) ;
236+
GrB_free(&zeroB);
237+
LAGraph_Finalize (msg) ;
179238
}
180239

181240
//------------------------------------------------------------------------------
@@ -184,9 +243,9 @@ void test_msf (void)
184243

185244
void test_errors (void)
186245
{
187-
#if LG_SUITESPARSE_GRAPHBLAS_V10
188246
LAGraph_Init (msg) ;
189247

248+
#if LG_SUITESPARSE_GRAPHBLAS_V10
190249
// C and A are NULL
191250
int result = LAGraph_msf (NULL, NULL, true, msg) ;
192251
TEST_CHECK (result == GrB_NULL_POINTER) ;
@@ -197,20 +256,29 @@ void test_errors (void)
197256
TEST_CHECK (result == GrB_DIMENSION_MISMATCH) ;
198257
OK (GrB_free (&A)) ;
199258

200-
// A must be square
259+
// A must real
201260
OK (GrB_Matrix_new (&A, GxB_FC32, 4, 4)) ;
202261
result = LAGraph_msf (&C, A, true, msg) ;
203262
TEST_CHECK (result == GrB_DOMAIN_MISMATCH) ;
263+
264+
#else
265+
// Not implemented
266+
OK (GrB_Matrix_new (&A, GrB_BOOL, 4, 4)) ;
267+
int result = LAGraph_msf (&C, A, true, msg) ;
268+
TEST_CHECK (result == GrB_NOT_IMPLEMENTED) ;
269+
#endif
204270

205271
OK (GrB_free (&A)) ;
206272
LAGraph_Finalize (msg) ;
207-
#endif
208273
}
209274

210275
//****************************************************************************
211276

212277
TEST_LIST = {
278+
#if LG_SUITESPARSE_GRAPHBLAS_V10
213279
{"msf", test_msf},
280+
{"inf_msf", test_inf_msf},
281+
#endif
214282
{"msf_errors", test_errors},
215283
{NULL, NULL}
216284
};

0 commit comments

Comments
 (0)