Skip to content

Commit f9f7047

Browse files
committed
get_nvals v1.1 and first comment
1 parent 3d89e8c commit f9f7047

2 files changed

Lines changed: 48 additions & 16 deletions

File tree

experimental/algorithm/LAGraph_CFL_AllPaths.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "LG_internal.h"
1717
#include <LAGraphX.h>
1818

19+
//Merging two ordered arrays of internal vertices in the add function
1920
static GrB_Index* merge_all_paths(GrB_Index* n, const void* left, const GrB_Index na, const void* right, const GrB_Index nb){
2021
GrB_Index* a = (GrB_Index*) left;
2122
GrB_Index* b = (GrB_Index*) right;
@@ -68,6 +69,7 @@ void clear_elem_all_paths(AllPathsElem *z){
6869

6970
void add_all_paths(AllPathsElem *z, AllPathsElem *x, AllPathsElem *y)
7071
{
72+
//temp is needed to avoid freeing the memory of z in case z == x or z == y
7173
AllPathsElem temp;
7274
temp.middle = merge_all_paths(&temp.n, x->middle, x->n, y->middle, y->n);
7375
clear_elem_all_paths(x);
@@ -95,7 +97,7 @@ void set_all_paths(AllPathsElem *z, const AllPathsElem *x, const bool *edge_exis
9597
if (edge_exist && *edge_exist){
9698
temp.middle = malloc(sizeof(GrB_Index));
9799
temp.n = 1;
98-
temp.middle[0] = GrB_INDEX_MAX;
100+
temp.middle[0] = GrB_INDEX_MAX; //GrB_INDEX_MAX - marker of A->eps and A->t
99101
}
100102

101103
z->middle = merge_all_paths(&z->n, x->middle, x->n, temp.middle, temp.n);
@@ -113,16 +115,18 @@ void set_all_paths(AllPathsElem *z, const AllPathsElem *x, const bool *edge_exis
113115
" z->n = 1; \n" \
114116
"}"
115117

118+
//Adding the count of all internal vertices in a reduction
116119
void add_get_nvals_all_paths(AllPathsElem *z, const AllPathsElem *x, const AllPathsElem *y)
117120
{
118121
z->n = x->n + y->n;
119122
z->middle = NULL;
120123
}
121124

125+
//Made global so that the get_nvals_all_paths matches the GrB_Matrix_nvals signature
122126
static GrB_Type* AllPaths_type_get_nvals = NULL;
123127
static GrB_Monoid AllPaths_monoid_get_nvals = NULL;
124128

125-
//A function that replaces GrB_Matrix_nvals for counting non-zero elements
129+
//A function that replaces GrB_Matrix_nvals in Reachability to check if new vertices have been added to the matrix.
126130
GrB_Info get_nvals_all_paths(GrB_Index *nvals, const GrB_Matrix A){
127131
GrB_Scalar s = NULL;
128132
GrB_Scalar_new(&s, *AllPaths_type_get_nvals);
@@ -141,6 +145,28 @@ GrB_Info get_nvals_all_paths(GrB_Index *nvals, const GrB_Matrix A){
141145
return GrB_SUCCESS;
142146
}
143147

148+
//To test the non-reduction approach in the future
149+
GrB_Info get_nvals_all_paths2(GrB_Index *nvals, const GrB_Matrix A){
150+
GrB_Index accum = 0;
151+
GxB_Iterator iterator;
152+
GxB_Iterator_new(&iterator);
153+
GrB_Info info = GxB_Matrix_Iterator_attach(iterator, A, NULL);
154+
info = GxB_Matrix_Iterator_seek(iterator, 0);
155+
AllPathsElem val;
156+
157+
while (info != GxB_EXHAUSTED)
158+
{
159+
GxB_Iterator_get_UDT(iterator, (void*) &val);
160+
accum+=val.n;
161+
info = GxB_Matrix_Iterator_next(iterator);
162+
}
163+
164+
GrB_free(&iterator);
165+
*nvals = accum;
166+
167+
return GrB_SUCCESS;
168+
}
169+
144170
GrB_Info LAGraph_CFL_AllPaths(
145171
// Output
146172
GrB_Matrix *outputs, // Array of matrices containing results.

experimental/test/test_CFL_AllPaths.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ void print_outputs(void)
156156
}
157157
}
158158

159+
//Cleaning of internal elements before free matrix
160+
void free_AllPaths_matrix(GrB_Matrix* ptr_output){
161+
GrB_Matrix output = *ptr_output;
162+
GxB_Iterator iterator;
163+
GxB_Iterator_new(&iterator);
164+
GrB_Info info = GxB_Matrix_Iterator_attach(iterator, output, NULL);
165+
info = GxB_Matrix_Iterator_seek(iterator, 0);
166+
AllPathsElem val;
167+
while (info != GxB_EXHAUSTED)
168+
{
169+
GxB_Iterator_get_UDT(iterator, (void*) &val);
170+
if (val.middle) free(val.middle);
171+
info = GxB_Matrix_Iterator_next(iterator);
172+
}
173+
174+
GrB_free(&iterator);
175+
GrB_free(ptr_output);
176+
}
177+
159178
void free_workspace() {
160179

161180
if (adj_matrices != NULL)
@@ -171,20 +190,7 @@ void free_workspace() {
171190
{
172191
for (size_t i = 0; i < grammar.nonterms_count; i++)
173192
{
174-
GxB_Iterator iterator;
175-
GxB_Iterator_new(&iterator);
176-
GrB_Info info = GxB_Matrix_Iterator_attach(iterator, outputs[i], NULL);
177-
info = GxB_Matrix_Iterator_seek(iterator, 0);
178-
AllPathsElem val;
179-
while (info != GxB_EXHAUSTED)
180-
{
181-
GxB_Iterator_get_UDT(iterator, (void*) &val);
182-
if (val.middle) free(val.middle);
183-
info = GxB_Matrix_Iterator_next(iterator);
184-
}
185-
186-
GrB_free(&iterator);
187-
GrB_free(&outputs[i]);
193+
free_AllPaths_matrix(&outputs[i]);
188194
}
189195
}
190196
LAGraph_Free ((void **) &outputs, msg);

0 commit comments

Comments
 (0)