Skip to content

Commit af97c11

Browse files
maxflow revisions
1 parent 5aa0567 commit af97c11

File tree

1 file changed

+72
-62
lines changed

1 file changed

+72
-62
lines changed

experimental/algorithm/LAGr_MaxFlow.c

Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ static GrB_Info LG_augment_maxflow
9393
GrB_free(&Jvec); \
9494
GrB_free(&Prune); \
9595
GrB_free(&UpdateFlow); \
96-
GrB_free(&UpdateHeight); \
97-
GrB_free(&ExtractResidualFlow); \
96+
GrB_free(&Relabel); \
97+
GrB_free(&ResidualFlow); \
9898
GrB_free(&MxeIndexMult); \
9999
GrB_free(&MxeMult); \
100100
GrB_free(&MxeAdd); \
@@ -109,11 +109,11 @@ static GrB_Info LG_augment_maxflow
109109
GrB_free(&RxdMult); \
110110
GrB_free(&InitForwardFlow); \
111111
GrB_free(&InitBackwardFlow); \
112-
GrB_free(&CreateResidualForward); \
113-
GrB_free(&CreateResidualBackward); \
112+
GrB_free(&ResidualForward); \
113+
GrB_free(&ResidualBackward); \
114114
GrB_free(&zero); \
115115
GrB_free(&empty); \
116-
GrB_free(&Re); \
116+
GrB_free(&t); \
117117
GrB_free(&invariant); \
118118
GrB_free(&CheckInvariant); \
119119
GrB_free(&check); \
@@ -183,14 +183,14 @@ JIT_STR(typedef struct{
183183
// unary ops to create R from input adjacency matrix G->A and G->AT
184184
//------------------------------------------------------------------------------
185185

186-
// unary op for R = CreateResidualForward (A)
187-
JIT_STR(void MF_CreateResidualForward(MF_flowEdge *z, const double *y) {
186+
// unary op for R = ResidualForward (A)
187+
JIT_STR(void MF_ResidualForward(MF_flowEdge *z, const double *y) {
188188
z->capacity = (*y);
189189
z->flow = 0;
190190
}, CRF_STR)
191191

192-
// unary op for R<!struct(A)> = CreateResidualBackward (AT)
193-
JIT_STR(void MF_CreateResidualBackward(MF_flowEdge *z, const double *y) {
192+
// unary op for R<!struct(A)> = ResidualBackward (AT)
193+
JIT_STR(void MF_ResidualBackward(MF_flowEdge *z, const double *y) {
194194
z->capacity = 0;
195195
z->flow = 0;
196196
}, CRB_STR)
@@ -291,13 +291,13 @@ JIT_STR(void MF_RxdAdd32(MF_resultTuple32 * z,
291291
}, RXDADD_STR32)
292292

293293
//------------------------------------------------------------------------------
294-
// unary ops for delta_vec = ExtractResidualFlow (y)
294+
// unary ops for delta_vec = ResidualFlow (y)
295295
//------------------------------------------------------------------------------
296296

297-
JIT_STR(void MF_ExtractResidualFlow64(double *z, const MF_resultTuple64 *x)
297+
JIT_STR(void MF_ResidualFlow64(double *z, const MF_resultTuple64 *x)
298298
{ (*z) = x->residual; }, EXTRACTRESIDUALFLOW_STR64)
299299

300-
JIT_STR(void MF_ExtractResidualFlow32(double *z, const MF_resultTuple32 *x)
300+
JIT_STR(void MF_ResidualFlow32(double *z, const MF_resultTuple32 *x)
301301
{ (*z) = x->residual; }, EXTRACTRESIDUALFLOW_STR32)
302302

303303
//------------------------------------------------------------------------------
@@ -311,28 +311,28 @@ JIT_STR(void MF_UpdateFlow(MF_flowEdge *z,
311311
}, UPDATEFLOW_STR)
312312

313313
//------------------------------------------------------------------------------
314-
// binary op for d<struct(y)> = UpdateHeight (d, y) using eWiseMult
314+
// binary op for d<struct(y)> = Relabel (d, y) using eWiseMult
315315
//------------------------------------------------------------------------------
316316

317-
JIT_STR(void MF_UpdateHeight64(int64_t *z,
317+
JIT_STR(void MF_Relabel64(int64_t *z,
318318
const int64_t *x, const MF_resultTuple64 *y) {
319319
if((*x) < y->d+1){
320320
(*z) = y->d + 1;
321321
}
322322
else {
323323
(*z) = (*x);
324324
}
325-
}, UPDATEHEIGHT_STR64)
325+
}, RELABEL_STR64)
326326

327-
JIT_STR(void MF_UpdateHeight32(int32_t *z,
327+
JIT_STR(void MF_Relabel32(int32_t *z,
328328
const int32_t *x, const MF_resultTuple32 *y) {
329329
if((*x) < y->d+1){
330330
(*z) = y->d + 1;
331331
}
332332
else {
333333
(*z) = (*x);
334334
}
335-
}, UPDATEHEIGHT_STR32)
335+
}, RELABEL_STR32)
336336

337337
//------------------------------------------------------------------------------
338338
// unary op for Jvec = extractJ (yd), where Jvec(i) = yd(i)->j
@@ -351,7 +351,7 @@ JIT_STR(void MF_extractYJ64(int64_t *z, const MF_resultTuple64 *x) { (*z) = x->j
351351
JIT_STR(void MF_extractYJ32(int32_t *z, const MF_resultTuple32 *x) { (*z) = x->j; }, EXTRACTYJ_STR32)
352352

353353
//------------------------------------------------------------------------------
354-
// binary op for R(src,:) = InitForwardFlow (R (src,:), Re')
354+
// binary op for R(src,:) = InitForwardFlow (R (src,:), t')
355355
//------------------------------------------------------------------------------
356356

357357
JIT_STR(void MF_InitForwardFlow(MF_flowEdge * z,
@@ -361,7 +361,7 @@ JIT_STR(void MF_InitForwardFlow(MF_flowEdge * z,
361361
}, INITFLOWF_STR)
362362

363363
//------------------------------------------------------------------------------
364-
// binary op for R(:,src) = InitBackwardFlow (R (:,src), Re)
364+
// binary op for R(:,src) = InitBackwardFlow (R (:,src), t)
365365
//------------------------------------------------------------------------------
366366

367367
JIT_STR(void MF_InitBackwardFlow(MF_flowEdge * z,
@@ -470,7 +470,7 @@ JIT_STR(void MF_Prune32(bool * z, const MF_resultTuple32 * x,
470470
}, PRUNE_STR32)
471471

472472
//------------------------------------------------------------------------------
473-
// unary op for Re = MakeFlow (e), where Re(i) = (0, e(i))
473+
// unary op for t = MakeFlow (e), where t(i) = (0, e(i))
474474
//------------------------------------------------------------------------------
475475

476476
JIT_STR(void MF_MakeFlow(MF_flowEdge * flow_edge, const double * flow){
@@ -544,11 +544,11 @@ int LAGr_MaxFlow
544544
LAGraph_Graph G2 = NULL ;
545545

546546
// to create R
547-
GrB_UnaryOp CreateResidualForward = NULL, CreateResidualBackward = NULL ;
547+
GrB_UnaryOp ResidualForward = NULL, ResidualBackward = NULL ;
548548
GrB_Matrix R = NULL ;
549549

550550
// to initialize R with initial saturated flows
551-
GrB_Vector e = NULL, Re = NULL ;
551+
GrB_Vector e = NULL, t = NULL ;
552552
GrB_UnaryOp MakeFlow = NULL ;
553553
GrB_BinaryOp InitForwardFlow = NULL, InitBackwardFlow = NULL ;
554554

@@ -584,15 +584,15 @@ int LAGr_MaxFlow
584584
GxB_IndexBinaryOp MxeIndexMult = NULL ;
585585

586586
// to extract the residual flow
587-
GrB_UnaryOp ExtractResidualFlow = NULL ;
587+
GrB_UnaryOp ResidualFlow = NULL ;
588588
GrB_UnaryOp ExtractMatrixFlow = NULL ;
589589

590590
// Delta structures
591591
GrB_Vector delta_vec = NULL ;
592592
GrB_Matrix Delta = NULL ;
593593

594594
// update height
595-
GrB_BinaryOp UpdateHeight = NULL ;
595+
GrB_BinaryOp Relabel = NULL ;
596596

597597
// update R structure
598598
GrB_BinaryOp UpdateFlow = NULL ;
@@ -657,17 +657,17 @@ int LAGr_MaxFlow
657657
#endif
658658

659659
// create R from A
660-
GRB_TRY(GxB_UnaryOp_new(&CreateResidualForward,
661-
F_UNARY(MF_CreateResidualForward), FlowEdge , GrB_FP64,
662-
"MF_CreateResidualForward", CRF_STR));
663-
GRB_TRY(GxB_UnaryOp_new(&CreateResidualBackward,
664-
F_UNARY(MF_CreateResidualBackward), FlowEdge , GrB_FP64,
665-
"MF_CreateResidualBackward", CRB_STR));
660+
GRB_TRY(GxB_UnaryOp_new(&ResidualForward,
661+
F_UNARY(MF_ResidualForward), FlowEdge , GrB_FP64,
662+
"MF_ResidualForward", CRF_STR));
663+
GRB_TRY(GxB_UnaryOp_new(&ResidualBackward,
664+
F_UNARY(MF_ResidualBackward), FlowEdge , GrB_FP64,
665+
"MF_ResidualBackward", CRB_STR));
666666
GRB_TRY(GrB_Matrix_new(&R, FlowEdge, n, n));
667-
// R = CreateResidualForward (A)
668-
GRB_TRY(GrB_apply(R, NULL, NULL, CreateResidualForward, A, NULL));
669-
// R<!struct(A)> = CreateResidualBackward (AT)
670-
GRB_TRY(GrB_apply(R, A, NULL, CreateResidualBackward, AT, GrB_DESC_SC));
667+
// R = ResidualForward (A)
668+
GRB_TRY(GrB_apply(R, NULL, NULL, ResidualForward, A, NULL));
669+
// R<!struct(A)> = ResidualBackward (AT)
670+
GRB_TRY(GrB_apply(R, A, NULL, ResidualBackward, AT, GrB_DESC_SC));
671671

672672
// ops to initialize R with initial saturated flows from the source node
673673
GRB_TRY(GxB_BinaryOp_new(&InitForwardFlow,
@@ -678,10 +678,9 @@ int LAGr_MaxFlow
678678
"MF_InitBackwardFlow", INITFLOWB_STR));
679679
GRB_TRY(GxB_UnaryOp_new(&MakeFlow, F_UNARY(MF_MakeFlow), FlowEdge, GrB_FP64,
680680
"MF_MakeFlow", MAKEFLOW_STR));
681-
GRB_TRY(GrB_Vector_new(&Re, FlowEdge, n));
682681
GRB_TRY(GrB_Vector_new(&e, GrB_FP64, n));
683682

684-
//extract n_active from e masking sink and src then assign to e
683+
// construct [src,sink] mask
685684
GRB_TRY(GrB_Vector_new(&src_and_sink, GrB_BOOL, n));
686685
GRB_TRY (GrB_Vector_setElement (src_and_sink, true, sink)) ;
687686
GRB_TRY (GrB_Vector_setElement (src_and_sink, true, src)) ;
@@ -737,9 +736,9 @@ int LAGr_MaxFlow
737736
GRB_TRY(GrB_assign(d, NULL, NULL, 0, GrB_ALL, n, NULL));
738737
GRB_TRY(GrB_assign(d, NULL, NULL, n, &src, 1, NULL));
739738

740-
GRB_TRY(GxB_UnaryOp_new(&ExtractResidualFlow,
741-
F_UNARY(MF_ExtractResidualFlow64), GrB_FP64, ResultTuple,
742-
"MF_ExtractResidualFlow64", EXTRACTRESIDUALFLOW_STR64));
739+
GRB_TRY(GxB_UnaryOp_new(&ResidualFlow,
740+
F_UNARY(MF_ResidualFlow64), GrB_FP64, ResultTuple,
741+
"MF_ResidualFlow64", EXTRACTRESIDUALFLOW_STR64));
743742

744743
// create semiring and vectors for y<struct(e)> = R*d
745744
GRB_TRY(GrB_Scalar_new(&theta, GrB_INT64));
@@ -784,9 +783,9 @@ int LAGr_MaxFlow
784783
GRB_TRY(GrB_Monoid_new_UDT(&MxeAddMonoid, MxeAdd, &id));
785784

786785
// update height binary op
787-
GRB_TRY(GxB_BinaryOp_new(&UpdateHeight,
788-
F_BINARY(MF_UpdateHeight64), GrB_INT64, GrB_INT64, ResultTuple,
789-
"MF_UpdateHeight64", UPDATEHEIGHT_STR64));
786+
GRB_TRY(GxB_BinaryOp_new(&Relabel,
787+
F_BINARY(MF_Relabel64), GrB_INT64, GrB_INT64, ResultTuple,
788+
"MF_Relabel64", RELABEL_STR64));
790789

791790
}else{
792791

@@ -808,9 +807,9 @@ int LAGr_MaxFlow
808807
"MF_CheckInvariant32", CHECKINVARIANT_STR32));
809808
#endif
810809

811-
GRB_TRY(GxB_UnaryOp_new(&ExtractResidualFlow,
812-
F_UNARY(MF_ExtractResidualFlow32), GrB_FP64, ResultTuple,
813-
"MF_ExtractResidualFlow32", EXTRACTRESIDUALFLOW_STR32));
810+
GRB_TRY(GxB_UnaryOp_new(&ResidualFlow,
811+
F_UNARY(MF_ResidualFlow32), GrB_FP64, ResultTuple,
812+
"MF_ResidualFlow32", EXTRACTRESIDUALFLOW_STR32));
814813

815814
// create and init d vector
816815
GRB_TRY(GrB_Vector_new(&d, GrB_INT32, n));
@@ -861,9 +860,9 @@ int LAGr_MaxFlow
861860
GRB_TRY(GrB_Monoid_new_UDT(&MxeAddMonoid, MxeAdd, &id));
862861

863862
// update height binary op
864-
GRB_TRY(GxB_BinaryOp_new(&UpdateHeight,
865-
F_BINARY(MF_UpdateHeight32), GrB_INT32, GrB_INT32, ResultTuple,
866-
"MF_UpdateHeight32", UPDATEHEIGHT_STR32));
863+
GRB_TRY(GxB_BinaryOp_new(&Relabel,
864+
F_BINARY(MF_Relabel32), GrB_INT32, GrB_INT32, ResultTuple,
865+
"MF_Relabel32", RELABEL_STR32));
867866
}
868867

869868
GRB_TRY(GrB_Matrix_new(&Map, CompareTuple, n,n));
@@ -914,22 +913,33 @@ int LAGr_MaxFlow
914913
GRB_TRY(GrB_assign(d, lvl, NULL, n, GrB_ALL, n, GrB_DESC_SC));
915914

916915
if(iter == 0){
917-
// e<struct(lvl)> = A (src,:)
918-
GRB_TRY(GrB_extract(e, lvl, NULL, A, GrB_ALL, n, src, GrB_DESC_ST0));
919-
// Re = MakeFlow (e), where Re(i) = (0, e(i))
920-
GRB_TRY(GrB_apply(Re, NULL, NULL, MakeFlow, e, NULL));
921-
// R(src,:) = InitForwardFlow (R (src,:), Re')
922-
GRB_TRY(GrB_assign(R, NULL, InitForwardFlow, Re, src, GrB_ALL, n, NULL));
923-
// R(:,src) = InitBackwardFlow (R (:,src), Re)
924-
GRB_TRY(GrB_assign(R, NULL, InitBackwardFlow, Re, GrB_ALL, n, src, NULL));
916+
917+
// OLD:
918+
// // e<struct(lvl)> = A (src,:)
919+
// GRB_TRY(GrB_extract(e, lvl, NULL, A, GrB_ALL, n, src, GrB_DESC_ST0));
920+
921+
// FIXME: why not just do this? Then this code can be removed from here and
922+
// placed in the initializations, before the while loop:
923+
// e = A (src,:)
924+
GRB_TRY(GrB_extract(e, NULL, NULL, A, GrB_ALL, n, src, GrB_DESC_T0));
925+
926+
// t = MakeFlow (e), where t(i) = (0, e(i))
927+
GRB_TRY(GrB_Vector_new(&t, FlowEdge, n));
928+
GRB_TRY(GrB_apply(t, NULL, NULL, MakeFlow, e, NULL));
929+
// R(src,:) = InitForwardFlow (R (src,:), t')
930+
GRB_TRY(GrB_assign(R, NULL, InitForwardFlow, t, src, GrB_ALL, n, NULL));
931+
// R(:,src) = InitBackwardFlow (R (:,src), t)
932+
GRB_TRY(GrB_assign(R, NULL, InitBackwardFlow, t, GrB_ALL, n, src, NULL));
933+
GrB_free(&t) ;
925934
// augment the maxflow with the initial flows
926935
LG_TRY (LG_augment_maxflow (f, e, sink, src_and_sink, &n_active, msg)) ;
927936
}
928-
else{
937+
// FIXME: why not do this for iter 0?
938+
// else{
929939
// delete nodes in e that cannot be reached from the sink
930940
// e<!struct(lvl)> = empty scalar
931941
GrB_assign (e, lvl, NULL, empty, GrB_ALL, n, GrB_DESC_SC) ;
932-
}
942+
// }
933943

934944
GrB_free(&lvl);
935945
LG_TRY(LAGraph_Delete(&G2, msg));
@@ -974,8 +984,8 @@ int LAGr_MaxFlow
974984
GRB_TRY(GrB_select(y, NULL, NULL, Prune, y, -1, NULL));
975985

976986
// relabel, update heights
977-
// d<struct(y)> = UpdateHeight (d, y) using eWiseMult
978-
GRB_TRY(GrB_eWiseMult(d, y, NULL, UpdateHeight, d, y, GrB_DESC_S));
987+
// d<struct(y)> = Relabel (d, y) using eWiseMult
988+
GRB_TRY(GrB_eWiseMult(d, y, NULL, Relabel, d, y, GrB_DESC_S));
979989

980990
#ifdef DBG
981991
// assert invariant for all labels
@@ -990,8 +1000,8 @@ int LAGr_MaxFlow
9901000
//--------------------------------------------------------------------------
9911001

9921002
// extract residual flows from y
993-
// delta_vec = ExtractResidualFlow (y), obtaining just the residual flows
994-
GRB_TRY(GrB_apply(delta_vec, NULL, NULL, ExtractResidualFlow, y, NULL));
1003+
// delta_vec = ResidualFlow (y), obtaining just the residual flows
1004+
GRB_TRY(GrB_apply(delta_vec, NULL, NULL, ResidualFlow, y, NULL));
9951005

9961006
// delta_vec = min (delta_vec, e), where e is dense
9971007
GRB_TRY(GrB_eWiseMult(delta_vec, NULL, NULL, GrB_MIN_FP64, delta_vec, e, NULL));

0 commit comments

Comments
 (0)