1616#include <LAGraphX.h>
1717
1818//Merging two ordered arrays of internal vertices in the add function
19- static GrB_Index * merge_all_paths (GrB_Index * n , const void * left , const GrB_Index na , const void * right , const GrB_Index nb ){
20- GrB_Index * a = (GrB_Index * ) left ;
21- GrB_Index * b = (GrB_Index * ) right ;
22-
23- if (na == 0 && nb == 0 ) {
24- * n = 0 ;
25- return NULL ;
26- }
27-
19+ static GrB_Index * merge_all_paths (GrB_Index * n , const GrB_Index * a , const size_t na , const GrB_Index * b , const size_t nb )
20+ {
2821 GrB_Index * tmp = malloc ((na + nb ) * sizeof (GrB_Index ));
2922
30- GrB_Index ia = 0 , ib = 0 , outn = 0 ;
23+ size_t ia = 0 , ib = 0 , outn = 0 ;
24+ // Handle the first elements of both arrays to initialize tmp and avoid checking if tmp is empty in the loop
3125 if (na > 0 && nb > 0 ) {
3226 if (a [0 ] < b [0 ]) {
3327 tmp [outn ++ ] = a [ia ++ ];
@@ -66,56 +60,98 @@ static GrB_Index* merge_all_paths(GrB_Index* n, const void* left, const GrB_Inde
6660 if (tmp [outn - 1 ] != vb ) tmp [outn ++ ] = vb ;
6761 }
6862
69- GrB_Index * sh = realloc (tmp , outn * sizeof (GrB_Index ));
70- if (sh ) tmp = sh ;
71-
63+ tmp = realloc (tmp , outn * sizeof (GrB_Index ));
7264 * n = outn ;
65+
7366 return tmp ;
7467}
7568
76- void clear_elem_all_paths (AllPathsElem * z ){
77- if (z -> middle ){
78- free (z -> middle );
79- z -> middle = NULL ;
69+ static GrB_Index * insert_all_paths (size_t * n , const GrB_Index * arr , size_t len , GrB_Index value )
70+ {
71+ // Array is ordered, so we can use binary search to find the position to insert value
72+ size_t l = 0 , r = len , m = 0 ;
73+ while (l < r ) {
74+ m = l + (r - l ) / 2 ;
75+ if (arr [m ] < value ) l = m + 1 ;
76+ else r = m ;
8077 }
81- z -> n = 0 ;
78+
79+ // If value is already in the array, return the original array
80+ if (l < len && arr [l ] == value ) {
81+ GrB_Index * tmp = malloc (len * sizeof (GrB_Index ));
82+ memcpy (tmp , arr , len * sizeof (GrB_Index ));
83+ * n = len ;
84+ return tmp ;
85+ }
86+
87+ GrB_Index * tmp = malloc ((len + 1 ) * sizeof (GrB_Index ));
88+ memcpy (tmp , arr , l * sizeof (GrB_Index ));
89+ tmp [l ] = value ;
90+ memcpy (tmp + l + 1 , arr + l , (len - l ) * sizeof (GrB_Index ));
91+
92+ * n = len + 1 ;
93+ return tmp ;
8294}
8395
8496void add_all_paths (AllPathsElem * z , AllPathsElem * x , AllPathsElem * y )
8597{
86- //temp is needed to avoid freeing the memory of z in case z == x or z == y
87- AllPathsElem temp ;
88- temp .middle = merge_all_paths (& temp .n , x -> middle , x -> n , y -> middle , y -> n );
89- clear_elem_all_paths (x );
90- clear_elem_all_paths (y );
91- z -> middle = temp .middle ;
92- z -> n = temp .n ;
98+ // temp is needed to avoid freeing the memory of z in case z == x or z == y
99+ AllPathsElem temp ;
100+
101+ // x->n and y->n are not equal to zero
102+ if (x -> n == 1 && y -> n == 1 ) {
103+ if (x -> data .single_elem == y -> data .single_elem ) {
104+ temp .n = 1 ;
105+ temp .data .single_elem = x -> data .single_elem ;
106+ } else {
107+ temp .n = 2 ;
108+ temp .data .middle = malloc (2 * sizeof (GrB_Index ));
109+ if (x -> data .single_elem < y -> data .single_elem ) {
110+ temp .data .middle [0 ] = x -> data .single_elem ;
111+ temp .data .middle [1 ] = y -> data .single_elem ;
112+ } else {
113+ temp .data .middle [0 ] = y -> data .single_elem ;
114+ temp .data .middle [1 ] = x -> data .single_elem ;
115+ }
116+ }
117+ } else if (x -> n == 1 ) {
118+ temp .data .middle = insert_all_paths (& temp .n , y -> data .middle , y -> n , x -> data .single_elem );
119+ } else if (y -> n == 1 ) {
120+ temp .data .middle = insert_all_paths (& temp .n , x -> data .middle , x -> n , y -> data .single_elem );
121+ } else {
122+ temp .data .middle = merge_all_paths (& temp .n , x -> data .middle , x -> n , y -> data .middle , y -> n );
123+ }
124+
125+ if (x -> n > 1 ){
126+ free (x -> data .middle );
127+ }
128+ if (y -> n > 1 ){
129+ free (y -> data .middle );
130+ }
131+
132+ * z = temp ;
93133}
94134
95135void mult_all_paths (AllPathsElem * z ,
96136 const AllPathsElem * x , GrB_Index ix , GrB_Index jx ,
97137 const AllPathsElem * y , GrB_Index iy , GrB_Index jy ,
98138 const void * theta )
99139{
100- z -> middle = malloc (sizeof (GrB_Index ));
101- z -> middle [0 ] = jx ;
140+ z -> data .single_elem = jx ;
102141 z -> n = 1 ;
103142}
104143
105144void set_all_paths (AllPathsElem * z , const AllPathsElem * x , const bool * edge_exist )
106145{
107- AllPathsElem temp ;
108- temp . middle = NULL ;
109- temp . n = 0 ;
110-
111- if ( edge_exist && * edge_exist ){
112- temp . middle = malloc ( sizeof ( GrB_Index ));
113- temp . n = 1 ;
114- temp . middle [ 0 ] = GrB_INDEX_MAX ; //GrB_INDEX_MAX - marker of A->eps and A->t
146+ if ( * edge_exist )
147+ {
148+ z -> data . single_elem = GrB_INDEX_MAX ; // A special value to indicate that this path corresponds to a terminal rule (A->t) or an epsilon rule (A->eps)
149+ z -> n = 1 ;
150+ }
151+ else {
152+ z -> n = x -> n ;
153+ z -> data . single_elem = x -> data . single_elem ;
115154 }
116-
117- z -> middle = merge_all_paths (& z -> n , x -> middle , x -> n , temp .middle , temp .n );
118- clear_elem_all_paths (& temp );
119155}
120156
121157#define MULT_PATH_INDEX_DEFN \
@@ -124,26 +160,25 @@ void set_all_paths(AllPathsElem *z, const AllPathsElem *x, const bool *edge_exis
124160" const AllPathsElem *y, GrB_Index iy, GrB_Index jy, \n" \
125161" const void *theta) \n" \
126162"{ \n" \
127- " z->middle = malloc(sizeof(GrB_Index)); \n" \
128- " z->middle[0] = jx; \n" \
163+ " z->data.single_elem = jx; \n" \
129164" z->n = 1; \n" \
130165"}"
131166
132167//Adding the count of all internal vertices in a reduction
133168void add_get_nvals_all_paths (AllPathsElem * z , const AllPathsElem * x , const AllPathsElem * y )
134169{
135170 z -> n = x -> n + y -> n ;
136- z -> middle = NULL ;
137171}
138172
139173//Made global so that the get_nvals_all_paths matches the GrB_Matrix_nvals signature
140- static GrB_Type * AllPaths_type_get_nvals = NULL ;
174+ static GrB_Type AllPaths_type = NULL ;
141175static GrB_Monoid AllPaths_monoid_get_nvals = NULL ;
142176
143177//A function that replaces GrB_Matrix_nvals in Reachability to check if new vertices have been added to the matrix.
144- GrB_Info get_nvals_all_paths (GrB_Index * nvals , const GrB_Matrix A ){
178+ static GrB_Info get_nvals_all_paths (GrB_Index * nvals , const GrB_Matrix A )
179+ {
145180 GrB_Scalar s = NULL ;
146- GrB_Scalar_new (& s , * AllPaths_type_get_nvals );
181+ GrB_Scalar_new (& s , AllPaths_type );
147182 GrB_Info info = GrB_reduce (s , NULL , AllPaths_monoid_get_nvals , A , NULL );
148183
149184 if (info != GrB_SUCCESS )
@@ -152,50 +187,23 @@ GrB_Info get_nvals_all_paths(GrB_Index *nvals, const GrB_Matrix A){
152187 return info ;
153188 }
154189
155- AllPathsElem result = {0 , NULL };
156- GrB_Info extract_info = GrB_Scalar_extractElement_UDT (& result , s );
157-
158- if (extract_info == GrB_NO_VALUE )
190+ AllPathsElem result = {0 };
191+ info = GrB_Scalar_extractElement_UDT (& result , s );
192+ if (info == GrB_NO_VALUE )
159193 {
160194 result .n = 0 ;
161195 }
162- else if (extract_info != GrB_SUCCESS )
196+ else if (info != GrB_SUCCESS )
163197 {
164198 GrB_free (& s );
165- return extract_info ;
199+ return info ;
166200 }
167201
168202 * nvals = result .n ;
169203 GrB_free (& s );
170204 return GrB_SUCCESS ;
171205}
172206
173- //To test the non-reduction approach in the future
174- GrB_Info get_nvals_all_paths2 (GrB_Index * nvals , const GrB_Matrix A ){
175- GrB_Index accum = 0 ;
176- GxB_Iterator iterator ;
177- GxB_Iterator_new (& iterator );
178- GrB_Info info = GxB_Matrix_Iterator_attach (iterator , A , NULL );
179- info = GxB_Matrix_Iterator_seek (iterator , 0 );
180- AllPathsElem val ;
181-
182- while (info != GxB_EXHAUSTED )
183- {
184- GxB_Iterator_get_UDT (iterator , (void * ) & val );
185- accum += val .n ;
186- info = GxB_Matrix_Iterator_next (iterator );
187- }
188-
189- GrB_free (& iterator );
190- * nvals = accum ;
191-
192- return GrB_SUCCESS ;
193- }
194-
195- // all_paths_ptr_t is a pointer to the type of elements of the outputs matrices.
196- // Use GrB_free(all_paths_ptr_t) after you finish working with the outputs matrices.
197- // Important: Do not free all_paths_ptr_t until all work with the output matrices is complete.
198- // Accessing matrices after freeing their type is undefined behavior.
199207GrB_Info LAGraph_CFL_AllPaths (
200208 // Output
201209 GrB_Matrix * outputs , // Array of matrices containing results.
@@ -207,6 +215,8 @@ GrB_Info LAGraph_CFL_AllPaths(
207215 // There are paths from I to M by nonterminal N1 and from M to J by nonterminal N2,
208216 // and A->N1 N2 where outputs[k] corresponds to nonterminal A.
209217 // GrB_INDEX_MAX in the array is a special value for A->eps and A->t.
218+ // AllPaths type - elements of the output matrices.
219+ GrB_Type * all_paths_ptr_t , // Pass a pointer to GrB_Type.
210220 // Input
211221 const GrB_Matrix * adj_matrices , // Array of adjacency matrices representing the graph.
212222 // The length of this array is equal to the count of
@@ -216,9 +226,6 @@ GrB_Info LAGraph_CFL_AllPaths(
216226 // is an edge between nodes i and j with the label of
217227 // the terminal corresponding to index 't' (where t is
218228 // in the range [0, terms_count - 1]).
219- GrB_Type * all_paths_ptr_t , // AllPaths type - elements of the output matrices.
220- // Pass a pointer to GrB_Type and
221- // free it after you finish working with outputs matrices.
222229 int64_t terms_count , // The total number of terminal symbols in the CFG.
223230 int64_t nonterms_count , // The total number of non-terminal symbols in the CFG.
224231 const LAGraph_rule_WCNF * rules , // The rules of the CFG.
@@ -230,7 +237,6 @@ GrB_Info LAGraph_CFL_AllPaths(
230237 return (GrB_NOT_IMPLEMENTED );
231238#else
232239 // Semiring components
233- GrB_Type AllPaths_type = NULL ;
234240 GrB_BinaryOp AllPaths_add = NULL ;
235241 GrB_BinaryOp AllPaths_add_get_nvals = NULL ;
236242 GrB_Monoid AllPaths_monoid = NULL ;
@@ -244,12 +250,11 @@ GrB_Info LAGraph_CFL_AllPaths(
244250 GrB_free (all_paths_ptr_t );
245251 GRB_TRY (GrB_Type_new (all_paths_ptr_t , sizeof (AllPathsElem )));
246252 AllPaths_type = * all_paths_ptr_t ;
247- AllPaths_type_get_nvals = all_paths_ptr_t ;
248253
249254 GRB_TRY (GrB_Scalar_new (& Theta , GrB_BOOL ));
250255 GRB_TRY (GrB_Scalar_setElement_BOOL (Theta , false));
251256
252- AllPathsElem bottom = {0 , NULL };
257+ AllPathsElem bottom = {0 };
253258 GRB_TRY (GrB_Scalar_new (& bottom_scalar , AllPaths_type ));
254259 GRB_TRY (GrB_Scalar_setElement_UDT (bottom_scalar , (void * )(& bottom )));
255260
@@ -304,19 +309,62 @@ GrB_Info LAGraph_CFL_AllPaths(
304309 AllPaths_add_get_nvals ,
305310 (void * )(& bottom )));
306311
307- CFL_Semiring semiring = {.type = AllPaths_type ,
312+ CFL_Semiring semiring = {
313+ .type = AllPaths_type ,
308314 .semiring = AllPaths_semiring ,
309315 .add = AllPaths_add ,
310316 .mult = AllPaths_mult ,
311317 .init_path = AllPaths_set ,
312318 .bottom_scalar = bottom_scalar ,
313- .get_nvals = get_nvals_all_paths };
319+ .get_nvals = get_nvals_all_paths };
314320
315321 LG_TRY (LAGraph_CFPQ_core (outputs , adj_matrices , terms_count , nonterms_count , rules , rules_count , & semiring , msg ));
316322
317- AllPaths_type = NULL ;
318- AllPaths_type_get_nvals = NULL ;
319323 LG_FREE_WORK ;
320324 return GrB_SUCCESS ;
321325#endif
322326}
327+
328+ // Helper function to free the output matrix of LAGraph_CFL_AllPaths, which contains elements of type AllPathsElem with dynamically allocated arrays of intermediate vertices.
329+ static void free_AllPaths_matrix (GrB_Matrix * ptr_output )
330+ {
331+ GxB_Iterator iterator ;
332+ GxB_Iterator_new (& iterator );
333+ GrB_Info info = GxB_Matrix_Iterator_attach (iterator , * ptr_output , NULL );
334+ info = GxB_Matrix_Iterator_seek (iterator , 0 );
335+ AllPathsElem val ;
336+
337+ while (info != GxB_EXHAUSTED )
338+ {
339+ GxB_Iterator_get_UDT (iterator , (void * )& val );
340+ if (val .n > 1 && val .data .middle != NULL ) {
341+ free (val .data .middle );
342+ }
343+ info = GxB_Matrix_Iterator_next (iterator );
344+ }
345+
346+ GrB_free (& iterator );
347+ GrB_free (ptr_output );
348+ }
349+
350+ // Free outputs and all_paths_ptr_t after you have finished working with the output matrices from LAGraph_CFL_AllPaths.
351+ GrB_Info LAGraph_CFL_AllPaths_free_outputs (GrB_Matrix * outputs , int64_t nonterms_count , GrB_Type * all_paths_ptr_t )
352+ {
353+ #if GxB_IMPLEMENTATION < GxB_VERSION (9 , 4 , 5 )
354+ return (GrB_NOT_IMPLEMENTED );
355+ #else
356+ if (outputs ) {
357+ for (size_t i = 0 ; i < nonterms_count ; i ++ ) {
358+ if (outputs [i ] == NULL )
359+ continue ;
360+ free_AllPaths_matrix (& outputs [i ]);
361+ outputs [i ] = NULL ;
362+ }
363+ free (outputs );
364+ }
365+ outputs = NULL ;
366+ GrB_free (all_paths_ptr_t );
367+ return GrB_SUCCESS ;
368+ #endif
369+ }
370+
0 commit comments