Skip to content

Commit d201cd6

Browse files
committed
update to Re_adv
1 parent 9d5a888 commit d201cd6

4 files changed

Lines changed: 293 additions & 142 deletions

File tree

‎experimental/algorithm/LAGraph_CFL_AllPaths_advanced.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
} \
77
} \
88
LAGraph_Free((void**)&outputs_reachability, NULL); \
9+
LAGraph_Free((void**)&reachability_matrices, NULL); \
910
LAGraph_Free((void **)&T, NULL); \
1011
GrB_free(&false_scalar); \
1112
GrB_free(&identity_matrix); \
@@ -188,6 +189,7 @@ GrB_Info LAGraph_CFL_AllPaths_adv(
188189
GrB_Matrix* T = NULL;
189190
GrB_Scalar false_scalar = NULL;
190191
GrB_Matrix* outputs_reachability = NULL;
192+
GrB_Matrix* reachability_matrices = NULL;
191193
bool* t_empty_flags = NULL;
192194
LAGraph_rule_EWCNF* new_rules = NULL;
193195
size_t new_rules_count = 0;
@@ -238,8 +240,13 @@ GrB_Info LAGraph_CFL_AllPaths_adv(
238240
LAGraph_Calloc((void**)&outputs_reachability, symbols_amount, sizeof(GrB_Matrix), msg);
239241
LG_TRY(LAGraph_CFL_reachability_adv(outputs_reachability, adj_matrices, symbols_amount, rules, rules_count, msg, optimizations));
240242

243+
LG_TRY(LAGraph_Calloc((void**)&reachability_matrices, symbols_amount, sizeof(GrB_Matrix), msg));
241244
for (size_t i = 0; i < symbols_amount; i++) {
242-
GrB_set(outputs_reachability[i], GrB_ROWMAJOR, GrB_STORAGE_ORIENTATION_HINT);
245+
reachability_matrices[i] = outputs_reachability[i] != NULL ? outputs_reachability[i] : adj_matrices[i];
246+
if (outputs_reachability[i] != NULL) {
247+
GRB_TRY(GrB_set(outputs_reachability[i], GrB_ROWMAJOR,
248+
GrB_STORAGE_ORIENTATION_HINT));
249+
}
243250
}
244251

245252
for (size_t i = 0; i < rules_count; i++) {
@@ -341,7 +348,7 @@ GrB_Info LAGraph_CFL_AllPaths_adv(
341348

342349
for (size_t i = 0; i < symbols_amount; i++) {
343350
GrB_Index temp_nvals = 0;
344-
GrB_Matrix_nvals(&temp_nvals, outputs_reachability[i]);
351+
GRB_TRY(GrB_Matrix_nvals(&temp_nvals, reachability_matrices[i]));
345352
if (temp_nvals != 0) {
346353
t_empty_flags[i] = false;
347354
}
@@ -355,7 +362,8 @@ GrB_Info LAGraph_CFL_AllPaths_adv(
355362
GrB_BinaryOp acc_op = t_empty_flags[bin_rule.nonterm] ? GrB_NULL : AllPaths_add;
356363
GRB_TRY(GrB_mxm(
357364
T[bin_rule.nonterm], GrB_NULL, acc_op,
358-
AllPaths_semiring, outputs_reachability[bin_rule.prod_A], outputs_reachability[bin_rule.prod_B],
365+
AllPaths_semiring, reachability_matrices[bin_rule.prod_A],
366+
reachability_matrices[bin_rule.prod_B],
359367
GrB_NULL
360368
));
361369
}

‎experimental/algorithm/LAGraph_CFL_optimized_matrix_opt.c‎

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ typedef enum CFL_Matrix_block Matrix_block;
9494

9595
GrB_Info matrix_print_lazy(Matrix *A, int8_t optimizations);
9696

97+
// Frees the stale copy after a write into one format only
98+
static GrB_Info matrix_drop_other_format(Matrix *matrix) {
99+
if (!matrix->is_both) {
100+
return GrB_SUCCESS;
101+
}
102+
103+
if (matrix->base == matrix->base_row) {
104+
TRY(GrB_Matrix_free(&matrix->base_col));
105+
} else {
106+
TRY(GrB_Matrix_free(&matrix->base_row));
107+
}
108+
matrix->is_both = false;
109+
110+
return GrB_SUCCESS;
111+
}
112+
97113
GrB_Info matrix_to_format(Matrix *matrix, int32_t format, bool is_both) {
98114
TRY(CFL_matrix_update(matrix));
99115

@@ -360,7 +376,7 @@ GrB_Info block_matrix_hyper_rotate_i(Matrix *matrix, enum CFL_Matrix_block forma
360376
TRY(GxB_Matrix_build_Scalar(new, nrows, ncols, scalar_true, matrix->nvals));
361377
matrix->base_col = new;
362378
}
363-
matrix->format = GrB_ROWMAJOR;
379+
matrix->format = matrix->base_row != NULL ? GrB_ROWMAJOR : GrB_COLMAJOR;
364380
}
365381
TRY(CFL_matrix_update(matrix));
366382
// TRY(CFL_matrix_free(matrix_p));
@@ -507,6 +523,13 @@ GrB_Info CFL_matrix_to_base(Matrix **matrix_p, Matrix *input, int8_t optimizatio
507523

508524
Matrix *matrix;
509525
TRY(CFL_matrix_create_lazy(&matrix, input->nrows, input->ncols));
526+
GrB_Info info = LAGraph_Realloc((void **)&matrix->base_matrices,
527+
input->base_matrices_count,
528+
matrix->base_matrices_count, sizeof(Matrix *), NULL);
529+
if (info < GrB_SUCCESS) {
530+
TRY(CFL_matrix_free(&matrix));
531+
return info;
532+
}
510533
TRY(CFL_matrix_free(&matrix->base_matrices[0]));
511534
matrix->base_matrices_count = 0;
512535

@@ -534,9 +557,50 @@ GrB_Info CFL_matrix_to_base(Matrix **matrix_p, Matrix *input, int8_t optimizatio
534557
return GrB_SUCCESS;
535558
}
536559

560+
GrB_Info CFL_matrix_extract_base(GrB_Matrix *base, Matrix *matrix, int8_t optimizations) {
561+
if (matrix->is_lazy) {
562+
// GraphBLAS never adds sparse matrices in-place: each addition builds a new
563+
// matrix, so its cost depends on the size of both operands. Smaller base
564+
// matrices are summed first (from the smallest one), and the largest base
565+
// matrix is added only once at the end
566+
TRY(matrix_sort_lazy(matrix, false));
567+
568+
size_t last = matrix->base_matrices_count - 1;
569+
Matrix *largest = matrix->base_matrices[last];
570+
if (last > 0) {
571+
Matrix *acc = matrix->base_matrices[0];
572+
for (size_t i = 1; i < last; i++) {
573+
TRY(CFL_wise(acc, acc, matrix->base_matrices[i], false, optimizations));
574+
}
575+
576+
TRY(CFL_wise(largest, largest, acc, false, optimizations));
577+
}
578+
579+
TRY(CFL_matrix_extract_base(base, largest, optimizations));
580+
return GrB_SUCCESS;
581+
}
582+
583+
// Matrix contains both formats, so free the one that is not extracted
584+
if (matrix->is_both) {
585+
GrB_Matrix other =
586+
matrix->base == matrix->base_row ? matrix->base_col : matrix->base_row;
587+
TRY(GrB_Matrix_free(&other));
588+
}
589+
590+
*base = matrix->base;
591+
matrix->base = NULL;
592+
matrix->base_row = NULL;
593+
matrix->base_col = NULL;
594+
matrix->is_both = false;
595+
matrix->nvals = 0;
596+
597+
return GrB_SUCCESS;
598+
}
599+
537600
GrB_Info matrix_combine_lazy(Matrix *A, size_t threshold, int8_t optimizations) {
538601
Matrix **new_matrices;
539-
TRY(LAGraph_Calloc((void **)&new_matrices, 50, sizeof(Matrix *), NULL));
602+
TRY(LAGraph_Calloc((void **)&new_matrices, A->base_matrices_count,
603+
sizeof(Matrix *), NULL));
540604

541605
size_t new_size = 0;
542606

@@ -654,8 +718,7 @@ GrB_Info CFL_matrix_from_base_lazy(Matrix **matrix_p, GrB_Matrix base) {
654718
matrix->is_lazy = true;
655719
TRY(LAGraph_Calloc(
656720
(void **)&matrix->base_matrices, 40, sizeof(CFL_Matrix *),
657-
NULL)); // this is enough for this centry i guess, because 40th matrices must
658-
// have 10^40 nvals for being putten in base_matrices, this is 2^132
721+
NULL)); // Initial storage; addition and copying resize it as needed.
659722
matrix->base_matrices[0] = result;
660723
matrix->base_matrices_count = 1;
661724
matrix->base = NULL;
@@ -667,9 +730,12 @@ GrB_Info CFL_matrix_from_base_lazy(Matrix **matrix_p, GrB_Matrix base) {
667730
GrB_Info CFL_matrix_create(Matrix **matrix, GrB_Index nrows, GrB_Index ncols) {
668731
GrB_Matrix _result;
669732
TRY(GrB_Matrix_new(&_result, GrB_BOOL, nrows, ncols));
670-
TRY(CFL_matrix_from_base(matrix, _result));
733+
GrB_Info info = CFL_matrix_from_base(matrix, _result);
734+
if (info < GrB_SUCCESS) {
735+
GrB_Matrix_free(&_result);
736+
}
671737

672-
return GrB_SUCCESS;
738+
return info;
673739
}
674740

675741
GrB_Info CFL_matrix_create_lazy(Matrix **matrix, GrB_Index nrows, GrB_Index ncols) {
@@ -716,6 +782,7 @@ GrB_Info matrix_mxm(Matrix *output, Matrix *first, Matrix *second, bool accum,
716782
// CFL_matrix_update(output);
717783
TRY(GrB_mxm(output->base, GrB_NULL, accum ? GxB_ANY_BOOL : GrB_NULL,
718784
GxB_ANY_PAIR_BOOL, left->base, right->base, GrB_NULL));
785+
TRY(matrix_drop_other_format(output));
719786
// IS_ISO(output->base, "MXM output");
720787
TRY(CFL_matrix_update(output));
721788

@@ -848,8 +915,24 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
848915
}
849916

850917
if (first->block_type == CELL && second->block_type == CELL) {
851-
TRY(matrix_mxm_lazy(output, first, second, accum, swap, optimizations));
852-
return GrB_SUCCESS;
918+
if (output->block_type == CELL) {
919+
TRY(matrix_mxm_lazy(output, first, second, accum, swap, optimizations));
920+
return GrB_SUCCESS;
921+
}
922+
923+
// N_i -> A B: compute the square product and add it to every output block.
924+
Matrix *temp;
925+
TRY(CFL_matrix_create(&temp, swap ? second->nrows : first->nrows,
926+
swap ? first->ncols : second->ncols));
927+
GrB_Info info = matrix_mxm_lazy(temp, first, second, false, swap, optimizations);
928+
if (info >= GrB_SUCCESS && !accum) {
929+
info = matrix_clear_empty(output, optimizations);
930+
}
931+
if (info >= GrB_SUCCESS) {
932+
info = matrix_wise_block(output, output, temp, false, optimizations);
933+
}
934+
TRY(CFL_matrix_free(&temp));
935+
return info;
853936
}
854937

855938
if (first->block_type == CELL) {
@@ -860,6 +943,9 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
860943
TRY(CFL_matrix_create(&temp, swap ? second->nrows : first->nrows,
861944
swap ? first->ncols : second->ncols));
862945
TRY(matrix_mxm_lazy(temp, first, second, accum, swap, optimizations));
946+
if (!accum) {
947+
TRY(matrix_clear_empty(output, optimizations));
948+
}
863949
TRY(matrix_wise_block(output, output, temp, false, optimizations));
864950
TRY(CFL_matrix_free(&temp));
865951

@@ -874,6 +960,9 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
874960
TRY(CFL_matrix_create(&temp, swap ? second->nrows : first->nrows,
875961
swap ? first->ncols : first->ncols));
876962
TRY(matrix_mxm_lazy(temp, first, second, accum, swap, optimizations));
963+
if (!accum) {
964+
TRY(matrix_clear_empty(output, optimizations));
965+
}
877966
TRY(matrix_wise_block(output, output, temp, false, optimizations));
878967
TRY(CFL_matrix_free(&temp));
879968

@@ -893,6 +982,9 @@ GrB_Info matrix_mxm_block(Matrix *output, Matrix *first, Matrix *second, bool ac
893982
Matrix *temp;
894983
TRY(CFL_matrix_create(&temp, first->nrows, first->ncols));
895984
TRY(matrix_mxm_lazy(temp, first, diag, false, swap, optimizations));
985+
if (!accum) {
986+
TRY(matrix_clear_empty(output, optimizations));
987+
}
896988
TRY(matrix_wise_block(output, output, temp, false, optimizations));
897989

898990
TRY(CFL_matrix_free(&temp));
@@ -1042,9 +1134,18 @@ GrB_Info matrix_wise_lazy(Matrix *output, Matrix *first, Matrix *second, bool ac
10421134
TRY(CFL_matrix_create(&other, output->nrows, output->ncols));
10431135
TRY(matrix_dup_empty(other, second, optimizations));
10441136

1045-
size_t other_nvals = other->nvals >= 10 ? other->nvals : 10;
1137+
// Reserve before merging so allocation failure leaves the components intact.
1138+
GrB_Info info = LAGraph_Realloc((void **)&first->base_matrices,
1139+
first->base_matrices_count + 1,
1140+
first->base_matrices_count, sizeof(Matrix *), NULL);
1141+
if (info < GrB_SUCCESS) {
1142+
TRY(CFL_matrix_free(&other));
1143+
return info;
1144+
}
10461145

10471146
while (true) {
1147+
// A merge changes other's size and can make another component comparable.
1148+
size_t other_nvals = other->nvals >= 10 ? other->nvals : 10;
10481149
bool found = false;
10491150

10501151
for (size_t i = 0; i < first->base_matrices_count; i++) {
@@ -1147,6 +1248,7 @@ GrB_Info matrix_wise_block(Matrix *output, Matrix *first, Matrix *second, bool a
11471248
GrB_Info matrix_rsub(Matrix *output, Matrix *mask) {
11481249
TRY(GrB_eWiseAdd(output->base, mask->base, GrB_NULL, GxB_ANY_BOOL, output->base,
11491250
output->base, GrB_DESC_RSC));
1251+
TRY(matrix_drop_other_format(output));
11501252

11511253
TRY(CFL_matrix_update(output));
11521254

@@ -1335,4 +1437,4 @@ GrB_Info CFL_clear(Matrix *A, int8_t optimizations) {
13351437

13361438
TRY(CFL_matrix_update(A));
13371439
return GrB_SUCCESS;
1338-
}
1440+
}

‎experimental/algorithm/LAGraph_CFL_optimized_matrix_opt.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,19 @@ GrB_Info CFL_matrix_to_base(
103103
// input
104104
CFL_Matrix *matrix, int8_t optimizations);
105105

106+
// Moves the underlying base matrix out of a matrix (lazy or regular) without
107+
// copying it. A lazy matrix is first merged into its largest base matrix.
108+
// The source matrix doesn't own the extracted matrix anymore, but it still must be
109+
// freed with CFL_matrix_free
110+
//
111+
// Parameters:
112+
// base - [out] Extracted matrix. Caller must free
113+
// matrix - [in] Pointer to the source matrix (may be lazy).
114+
// optimizations - [in] Bitmask specifying enabled optimizations.
115+
GrB_Info CFL_matrix_extract_base(
116+
// output
117+
GrB_Matrix *base,
118+
// input
119+
CFL_Matrix *matrix, int8_t optimizations);
120+
106121
GrB_Info CFL_clear(CFL_Matrix *A, int8_t optimizations);

0 commit comments

Comments
 (0)