Skip to content

Commit 5d03cb8

Browse files
msf: fix test for tcode, add TODO, cleanup spacing
1 parent 99c2684 commit 5d03cb8

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

experimental/algorithm/LAGraph_msf.c

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
// TODO: is this ready for src?
2323

24+
// TODO: a "sanitize" input is fine for now in the experimental folder, but it
25+
// doesn't fit with the standard LAGraph API. It will need to be removed when
26+
// this method is moved to the src folder. The input will also become an
27+
// LAGraph_Graph, not a plain GrB_Matrix A.
28+
2429
#include "LG_internal.h"
2530
#include <LAGraph.h>
2631
#include <LAGraphX.h>
@@ -42,7 +47,7 @@ typedef struct
4247
" double wFp;\n" \
4348
" };\n" \
4449
" uint64_t idx;\n" \
45-
"} pairW;\n"
50+
"} pairW;\n"
4651

4752
typedef struct
4853
{
@@ -64,7 +69,7 @@ typedef struct
6469
" };\n" \
6570
" uint64_t idx;\n" \
6671
" } *w_partner;\n" \
67-
"} MSF_context;\n"
72+
"} MSF_context;\n"
6873

6974

7075
//****************************************************************************
@@ -135,8 +140,8 @@ static void get_snd (uint64_t *y, const pairW *x)
135140
static void tupleMinInt(pairW *z, const pairW *x, const pairW *y)
136141
{
137142
bool xSmaller = x->wInt < y->wInt || (x->wInt == y->wInt && x->idx < y->idx);
138-
z->wInt = (xSmaller)? x->wInt: y->wInt;
139-
z->idx = (xSmaller)? x->idx: y->idx;
143+
z->wInt = (xSmaller)? x->wInt: y->wInt;
144+
z->idx = (xSmaller)? x->idx: y->idx;
140145
}
141146
#define TUPLEMININT \
142147
"void tupleMinInt(pairW *z, const pairW *x, const pairW *y)\n"\
@@ -148,8 +153,8 @@ static void tupleMinInt(pairW *z, const pairW *x, const pairW *y)
148153
static void tupleMinFp(pairW *z, const pairW *x, const pairW *y)
149154
{
150155
bool xSmaller = x->wFp < y->wFp || (x->wFp == y->wFp && x->idx < y->idx);
151-
z->wFp = (xSmaller)? x->wFp: y->wFp;
152-
z->idx = (xSmaller)? x->idx: y->idx;
156+
z->wFp = (xSmaller)? x->wFp: y->wFp;
157+
z->idx = (xSmaller)? x->idx: y->idx;
153158
}
154159
#define TUPLEMINFP \
155160
"void tupleMinFp(pairW *z, const pairW *x, const pairW *y)\n"\
@@ -162,8 +167,8 @@ static void tupleMinFp(pairW *z, const pairW *x, const pairW *y)
162167
// Set z to the second -- sets bits regardless of weight type.
163168
static void tuple2nd(pairW *z, const void *x, const pairW *y)
164169
{
165-
z->wInt = y->wInt;
166-
z->idx = y->idx;
170+
z->wInt = y->wInt;
171+
z->idx = y->idx;
167172
}
168173
#define TUPLE2ND \
169174
"void tuple2nd(pairW *z, const void *x, const pairW *y)\n"\
@@ -200,7 +205,7 @@ static void tupleEq(bool *z, const pairW *x, const pairW *y)
200205
GrB_free (&cedge); \
201206
GrB_free (&tedge); \
202207
GrB_free (&mask); \
203-
GrB_free (&index_v); \
208+
GrB_free (&index_v); \
204209
GrB_free (&comb); \
205210
GrB_free (&minComb); \
206211
GrB_free (&fst); \
@@ -228,7 +233,7 @@ int LAGraph_msf
228233
#if LG_SUITESPARSE_GRAPHBLAS_V10
229234
LG_CLEAR_MSG ;
230235
MSF_context context = {
231-
.parent = NULL, .w_partner = NULL,
236+
.parent = NULL, .w_partner = NULL,
232237
// .type = GrB_UINT64_CODE
233238
};
234239
GrB_Info info;
@@ -238,21 +243,21 @@ int LAGraph_msf
238243
edge = NULL, cedge = NULL, mask = NULL, index_v = NULL, ramp = NULL;
239244

240245
GrB_Index *SI = NULL, *SJ = NULL;
241-
int64_t *SX = NULL;
246+
void *SX = NULL;
242247
GrB_Type contx_type = NULL, lg_pair = NULL, weight_type = NULL;
243248
GrB_BinaryOp comb = NULL, pairMin = NULL, pairSec = NULL, pairEq = NULL;
244249
GrB_Monoid pairMin_monoid = NULL;
245250
GrB_Semiring minComb = NULL, pairMin2nd = NULL;
246251
GrB_UnaryOp fst = NULL, snd = NULL;
247252
int edge_h = GrB_DEFAULT;
248-
uint64_t edge_size = 0, edge_n = 0;
253+
uint64_t edge_size = 0, edge_n = 0;
249254
GrB_IndexUnaryOp s1 = NULL, s2 = NULL;
250255

251256

252257
//--------------------------------------------------------------------------
253258
// Check inputs
254259
//--------------------------------------------------------------------------
255-
260+
256261
if (result == NULL || A == NULL) return (GrB_NULL_POINTER) ;
257262
GrB_Index ncols ;
258263
GRB_TRY (GrB_Matrix_nrows (&n, A)) ;
@@ -277,30 +282,30 @@ int LAGraph_msf
277282
case GrB_UINT64_CODE:
278283
tcode = GrB_INT64_CODE;
279284
GRB_TRY (GrB_Matrix_new (&S, GrB_INT64, n, n)) ;
280-
GRB_TRY (GrB_Matrix_eWiseAdd_BinaryOp
285+
GRB_TRY (GrB_Matrix_eWiseAdd_BinaryOp
281286
(S, NULL, NULL, GrB_MIN_INT64, A, A, GrB_DESC_T1)) ;
282287
break;
283288
case GrB_FP32_CODE:
284289
case GrB_FP64_CODE:
285290
tcode = GrB_FP64_CODE;
286291
GRB_TRY (GrB_Matrix_new (&S, GrB_FP64, n, n)) ;
287-
GRB_TRY (GrB_Matrix_eWiseAdd_BinaryOp
292+
GRB_TRY (GrB_Matrix_eWiseAdd_BinaryOp
288293
(S, NULL, NULL, GrB_MIN_FP64, A, A, GrB_DESC_T1)) ;
289294
break;
290295
default:
291296
LG_ASSERT(false, GrB_DOMAIN_MISMATCH) ;
292297
break;
293298
}
294-
weight_type = tcode == GrB_INT64_CODE? GrB_INT64: GrB_FP64;
299+
weight_type = (tcode == GrB_INT64_CODE) ? GrB_INT64 : GrB_FP64 ;
295300
}
296301
else
297302
{
298303
// Use the input as-is, and assume it is symmetric
299304
GrB_Matrix_get_INT32(A, (int *) &(tcode), GrB_EL_TYPE_CODE) ;
300305
LG_ASSERT(tcode < 12 && tcode > 0, GrB_DOMAIN_MISMATCH) ;
301-
tcode = (tcode == GrB_FP32_CODE || tcode == GrB_FP64_CODE)?
306+
tcode = (tcode == GrB_FP32_CODE || tcode == GrB_FP64_CODE)?
302307
GrB_FP64_CODE: GrB_INT64_CODE;
303-
weight_type = tcode == GrB_INT64_CODE? GrB_INT64: GrB_FP64;
308+
weight_type = (tcode == GrB_INT64_CODE) ? GrB_INT64 : GrB_FP64 ;
304309
GRB_TRY (GrB_Matrix_new (&S, weight_type, n, n)) ;
305310
GRB_TRY (GrB_Matrix_assign
306311
(S, NULL, NULL, A, GrB_ALL, n, GrB_ALL, n, NULL)) ;
@@ -319,10 +324,11 @@ int LAGraph_msf
319324

320325
LG_TRY (LAGraph_Malloc ((void **) &SI, 2*n, sizeof (GrB_Index), msg)) ;
321326
LG_TRY (LAGraph_Malloc ((void **) &SJ, 2*n, sizeof (GrB_Index), msg)) ;
322-
LG_TRY (LAGraph_Malloc ((void **) &SX, 2*n, sizeof (GrB_Index), msg)) ;
327+
size_t sx_size = (tcode == GrB_INT64_CODE) ? sizeof (int64_t) : sizeof (double) ;
328+
LG_TRY (LAGraph_Malloc (&SX, 2*n, sx_size, msg)) ;
323329

324330
// context arrays
325-
LG_TRY (LAGraph_Malloc
331+
LG_TRY (LAGraph_Malloc
326332
((void **) &context.parent, n, sizeof (uint64_t), msg)) ;
327333

328334
// prepare vectors
@@ -337,7 +343,7 @@ int LAGraph_msf
337343
ramp, NULL, NULL, (uint64_t) 0, GrB_ALL, n + 1, NULL)) ;
338344
GRB_TRY (GrB_Vector_apply_IndexOp_INT64 (
339345
ramp, NULL, NULL, GrB_ROWINDEX_INT64, ramp, (int64_t) 0, NULL)) ;
340-
GRB_TRY (GxB_Vector_load(parent_v, (void **) &context.parent,
346+
GRB_TRY (GxB_Vector_load(parent_v, (void **) &context.parent,
341347
GrB_UINT64, n, 3 * n * sizeof (uint64_t), GxB_IS_READONLY, NULL)) ;
342348
// semiring & monoid
343349
pairW inf = {.wInt = INT64_MAX, .idx = UINT64_MAX};
@@ -350,31 +356,31 @@ int LAGraph_msf
350356
if(tcode == GrB_INT64_CODE)
351357
{
352358
GRB_TRY (GxB_BinaryOp_new (
353-
&pairMin, (GxB_binary_function) tupleMinInt,
359+
&pairMin, (GxB_binary_function) tupleMinInt,
354360
lg_pair, lg_pair, lg_pair, "tupleMinInt", TUPLEMININT
355361
)) ;
356362
}
357363
else
358364
{
359365
GRB_TRY (GxB_BinaryOp_new (
360-
&pairMin, (GxB_binary_function) tupleMinFp,
366+
&pairMin, (GxB_binary_function) tupleMinFp,
361367
lg_pair, lg_pair, lg_pair, "tupleMinFp", TUPLEMINFP
362368
)) ;
363369
}
364-
370+
365371
GRB_TRY (GxB_BinaryOp_new (
366-
&pairSec, (GxB_binary_function) tuple2nd,
372+
&pairSec, (GxB_binary_function) tuple2nd,
367373
lg_pair, GrB_BOOL, lg_pair, "tuple2nd", TUPLE2ND
368374
)) ;
369375
GRB_TRY (GxB_BinaryOp_new (
370-
&pairEq, (GxB_binary_function) tupleEq,
376+
&pairEq, (GxB_binary_function) tupleEq,
371377
GrB_BOOL, lg_pair, lg_pair, "tupleEq", TUPLEEQ
372378
)) ;
373379
GRB_TRY (GrB_Monoid_new_UDT (&pairMin_monoid, pairMin, (void *) &inf)) ;
374380
GRB_TRY (GrB_Semiring_new (&minComb, pairMin_monoid, comb)) ;
375381
GRB_TRY (GrB_Semiring_new (&pairMin2nd, pairMin_monoid, pairSec)) ;
376382
GRB_TRY (GxB_UnaryOp_new (
377-
&fst, (GxB_unary_function) get_fst, weight_type, lg_pair,
383+
&fst, (GxB_unary_function) get_fst, weight_type, lg_pair,
378384
"get_fst", GETFST)) ;
379385
GRB_TRY (GxB_UnaryOp_new (
380386
&snd, (GxB_unary_function) get_snd, GrB_UINT64, lg_pair,
@@ -383,14 +389,14 @@ int LAGraph_msf
383389
// context type
384390
GRB_TRY (GxB_Type_new (
385391
&contx_type, sizeof (MSF_context), "MSF_context", MSF_CONT)) ;
386-
392+
387393
// ops for GrB_select
388394
GRB_TRY(GxB_IndexUnaryOp_new (
389-
&s1, (GxB_index_unary_function) selectEdge, GrB_BOOL, weight_type,
395+
&s1, (GxB_index_unary_function) selectEdge, GrB_BOOL, weight_type,
390396
contx_type, "selectEdge", SELECTEDGE
391397
)) ;
392398
GRB_TRY(GxB_IndexUnaryOp_new (
393-
&s2, (void *) removeEdge, GrB_BOOL, GrB_UINT64, contx_type,
399+
&s2, (void *) removeEdge, GrB_BOOL, GrB_UINT64, contx_type,
394400
"removeEdge", REMOVEEDGE
395401
)) ;
396402

@@ -440,7 +446,7 @@ int LAGraph_msf
440446
GRB_TRY (GrB_assign (index_v, mask, 0, I, GrB_ALL, 0, NULL)) ;
441447
GRB_TRY (GrB_assign (t, NULL, NULL, n, GrB_ALL, 0, NULL)) ;
442448
LG_TRY (LAGraph_FastAssign_Semiring(
443-
t, NULL, GrB_MIN_UINT64, parent_v, index_v, ramp,
449+
t, NULL, GrB_MIN_UINT64, parent_v, index_v, ramp,
444450
GrB_MIN_SECOND_SEMIRING_UINT64, NULL, msg
445451
)) ;
446452
GRB_TRY (GxB_Vector_extract_Vector (
@@ -464,11 +470,11 @@ int LAGraph_msf
464470
GRB_TRY (GrB_Vector_nvals (&num, edge)) ;
465471
GRB_TRY (GrB_apply (t, NULL, NULL, snd, edge, NULL)) ;
466472
GRB_TRY (GrB_Vector_extractTuples (NULL, SJ + ntuples, &num, t)) ;
467-
if(tcode == GrB_UINT64_CODE)
473+
if(tcode == GrB_INT64_CODE)
468474
{
469475
GRB_TRY (GrB_apply (t, NULL, NULL, fst, edge, NULL)) ;
470476
GRB_TRY (GrB_Vector_extractTuples_INT64 (
471-
SI + ntuples, SX + ntuples, &num, t)) ;
477+
SI + ntuples, ((int64_t *) SX) + ntuples, &num, t)) ;
472478
GRB_TRY (GrB_Vector_clear (t)) ;
473479
}
474480
else
@@ -477,12 +483,12 @@ int LAGraph_msf
477483
GRB_TRY (GrB_Vector_new(&t, weight_type, n)) ;
478484
GRB_TRY (GrB_apply (t, NULL, NULL, fst, edge, NULL)) ;
479485
GRB_TRY (GrB_Vector_extractTuples_FP64 (
480-
SI + ntuples, (double *) SX + ntuples, &num, t)) ;
486+
SI + ntuples, ((double *) SX) + ntuples, &num, t)) ;
481487
GRB_TRY (GrB_Vector_clear (t)) ;
482488
GRB_TRY (GrB_free(&t)) ;
483489
GRB_TRY (GrB_Vector_new(&t, GrB_UINT64, n)) ;
484490
}
485-
491+
486492
ntuples += num;
487493

488494
// path halving until every vertex points on a root
@@ -503,7 +509,7 @@ int LAGraph_msf
503509
}
504510

505511
GRB_TRY (GrB_Matrix_clear (T)) ;
506-
if(tcode == GrB_UINT64_CODE)
512+
if(tcode == GrB_INT64_CODE)
507513
{
508514
GRB_TRY (GrB_Matrix_build_INT64 (
509515
T, SI, SJ, (int64_t *)SX, ntuples, GxB_IGNORE_DUP)) ;
@@ -522,4 +528,4 @@ int LAGraph_msf
522528
#else
523529
return (GrB_NOT_IMPLEMENTED) ;
524530
#endif
525-
}
531+
}

0 commit comments

Comments
 (0)