Skip to content

Commit cfd45ae

Browse files
committed
Feat: lazy optimizations now works
1 parent 69c29cb commit cfd45ae

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

experimental/algorithm/LAGraph_CFL_reachability_advanced.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,16 @@ typedef struct Matrix {
156156
} Matrix;
157157

158158
void matrix_update(Matrix *matrix) {
159-
GrB_Matrix_nvals(&matrix->nvals, matrix->base);
159+
if (matrix->base_matrices_count == 0) {
160+
GrB_Matrix_nvals(&matrix->nvals, matrix->base);
161+
} else {
162+
size_t new_nnz = 0;
163+
for (size_t i = 0; i < matrix->base_matrices_count; i++) {
164+
new_nnz += matrix->base_matrices[i].nvals;
165+
}
166+
167+
matrix->nvals = new_nnz;
168+
}
160169
GrB_Matrix_nrows(&matrix->size, matrix->base);
161170
// GrB_get(matrix->base, &matrix->format, GrB_STORAGE_ORIENTATION_HINT);
162171
}
@@ -166,7 +175,7 @@ Matrix matrix_from_base(GrB_Matrix matrix) {
166175
result.base = matrix;
167176
result.base_row = matrix;
168177
result.base_col = NULL;
169-
result.base_matrices = malloc(sizeof(Matrix) * 10);
178+
result.base_matrices = malloc(sizeof(Matrix) * 40);
170179
result.base_matrices_count = 0;
171180
result.nvals = 0;
172181
result.size = 0;
@@ -346,6 +355,10 @@ GrB_Info matrix_mxm_lazy(Matrix *output, Matrix *first, Matrix *second, bool acc
346355
matrix_wise_empty(&acc_matrix, &acc_matrix, &acc_matrices[i], false);
347356
}
348357

358+
if (accum) {
359+
return matrix_wise_empty(output, output, &acc_matrix, false);
360+
}
361+
349362
return matrix_dup_empty(output, &acc_matrix);
350363
}
351364

@@ -520,7 +533,8 @@ void matrix_print_lazy(Matrix *A) {
520533
matrix_wise_empty(&temp, &temp, &A->base_matrices[i], false);
521534
}
522535

523-
GxB_print(temp.base, 1);
536+
A = &temp;
537+
GxB_print(A->base, 1);
524538
}
525539

526540
// LAGraph_CFL_reachability: Context-Free Language Reachability Matrix-Based Algorithm
@@ -842,6 +856,9 @@ GrB_Info LAGraph_CFL_reachability_adv(
842856
Matrix *C = &temp_matrices[bin_rule.nonterm];
843857

844858
mxm(C, A, B, false, false);
859+
// matrix_print_lazy(A);
860+
// matrix_print_lazy(B);
861+
// matrix_print_lazy(C);
845862
}
846863
TIMER_STOP("MXM 1", &mxm1);
847864

@@ -851,6 +868,8 @@ GrB_Info LAGraph_CFL_reachability_adv(
851868
Matrix *C = &matrices[i];
852869

853870
wise(C, C, A, false);
871+
// matrix_print_lazy(A);
872+
// matrix_print_lazy(C);
854873
}
855874
TIMER_STOP("WISE 1", &wise1);
856875

@@ -860,8 +879,11 @@ GrB_Info LAGraph_CFL_reachability_adv(
860879
Matrix *A = &matrices[bin_rule.prod_B];
861880
Matrix *B = &delta_matrices[bin_rule.prod_A];
862881
Matrix *C = &temp_matrices[bin_rule.nonterm];
863-
882+
// printf("ITER: %ld\n", i);
864883
mxm(C, A, B, true, true);
884+
// matrix_print_lazy(A);
885+
// matrix_print_lazy(B);
886+
// matrix_print_lazy(C);
865887
}
866888
TIMER_STOP("MXM 2", &mxm2);
867889

@@ -877,6 +899,8 @@ GrB_Info LAGraph_CFL_reachability_adv(
877899
Matrix *C = &delta_matrices[i];
878900

879901
rsub(C, A);
902+
// matrix_print_lazy(A);
903+
// matrix_print_lazy(C);
880904
}
881905
TIMER_STOP("WISE 3 (MASK)", &rsubt);
882906

@@ -915,7 +939,20 @@ GrB_Info LAGraph_CFL_reachability_adv(
915939
#endif
916940

917941
for (int32_t i = 0; i < nonterms_count; i++) {
918-
outputs[i] = matrices[i].base;
942+
if (matrices[i].base_matrices_count == 0) {
943+
outputs[i] = matrices[i].base;
944+
} else {
945+
GrB_Matrix _acc;
946+
GrB_Matrix_new(&_acc, GrB_BOOL, matrices[i].size, matrices[i].size);
947+
Matrix acc = matrix_from_base(_acc);
948+
949+
for (size_t j = 0; j < matrices[i].base_matrices_count; j++) {
950+
matrix_wise_empty(&acc, &acc, &matrices[i].base_matrices[j], false);
951+
}
952+
953+
outputs[i] = acc.base;
954+
}
955+
// outputs[i] = matrices[i].base;
919956
}
920957

921958
LG_FREE_WORK;

0 commit comments

Comments
 (0)