Skip to content

Commit 0f7d17b

Browse files
revise LAGraph memory wrappers: return int, add msg
1 parent 844fc57 commit 0f7d17b

17 files changed

+212
-247
lines changed

src/algorithm/LAGr_Betweenness.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
if (S [i] == NULL) break ; \
6363
GrB_free (&(S [i])) ; \
6464
} \
65-
LAGraph_Free ((void **) &S) ; \
65+
LAGraph_Free ((void **) &S, NULL) ; \
6666
} \
6767
}
6868

@@ -167,8 +167,7 @@ int LAGr_Betweenness
167167
frontier, A, GrB_DESC_RSC)) ;
168168

169169
// Allocate memory for the array of S matrices
170-
S = (GrB_Matrix *) LAGraph_Malloc (n+1, sizeof (GrB_Matrix)) ;
171-
LG_ASSERT (S != NULL, GrB_OUT_OF_MEMORY) ;
170+
LG_TRY (LAGraph_Malloc ((void **) &S, n+1, sizeof (GrB_Matrix), msg)) ;
172171
S [0] = NULL ;
173172

174173
// =========================================================================

src/algorithm/LAGr_TriangleCount.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static int tricount_prep
9797
GrB_free (&L) ; \
9898
GrB_free (&T) ; \
9999
GrB_free (&U) ; \
100-
LAGraph_Free ((void **) &P) ; \
100+
LAGraph_Free ((void **) &P, NULL) ; \
101101
}
102102

103103
int LAGr_TriangleCount
@@ -260,7 +260,7 @@ int LAGr_TriangleCount
260260
A = T ;
261261

262262
// free workspace
263-
LAGraph_Free ((void **) &P) ;
263+
LG_TRY (LAGraph_Free ((void **) &P, NULL)) ;
264264
}
265265

266266
//--------------------------------------------------------------------------

src/algorithm/LG_BreadthFirstSearch_SSGrB.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ int LG_BreadthFirstSearch_SSGrB
108108
LAGRAPH_PROPERTY_MISSING, "G->AT is required") ;
109109
}
110110

111+
// FIXME: if AT is not present, do push-only?
112+
111113
// direction-optimization requires G->AT and G->rowdegree
112114
LG_ASSERT_MSG (Degree != NULL,
113115
LAGRAPH_PROPERTY_MISSING, "G->rowdegree is required") ;

src/algorithm/LG_CC_Boruvka.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,24 @@ void my_select_func (void *z, const void *x,
7777
//------------------------------------------------------------------------------
7878

7979
#undef LG_FREE_ALL
80-
#define LG_FREE_ALL \
81-
{ \
82-
LG_FREE_WORK ; \
83-
GrB_free (&parent) ; \
80+
#define LG_FREE_ALL \
81+
{ \
82+
LG_FREE_WORK ; \
83+
GrB_free (&parent) ; \
8484
}
8585

8686
#undef LG_FREE_WORK
87-
#define LG_FREE_WORK \
88-
{ \
89-
LAGraph_Free ((void **) &I) ; \
90-
LAGraph_Free ((void **) &Px) ; \
91-
LAGraph_Free ((void **) &mem) ; \
92-
GrB_free (&gp) ; \
93-
GrB_free (&mnp) ; \
94-
GrB_free (&ccmn) ; \
95-
GrB_free (&ramp) ; \
96-
GrB_free (&mask) ; \
97-
GrB_free (&select_op) ; \
87+
#define LG_FREE_WORK \
88+
{ \
89+
LAGraph_Free ((void **) &I, NULL) ; \
90+
LAGraph_Free ((void **) &Px, NULL) ; \
91+
LAGraph_Free ((void **) &mem, NULL) ; \
92+
GrB_free (&gp) ; \
93+
GrB_free (&mnp) ; \
94+
GrB_free (&ccmn) ; \
95+
GrB_free (&ramp) ; \
96+
GrB_free (&mask) ; \
97+
GrB_free (&select_op) ; \
9898
}
9999

100100
int LG_CC_Boruvka
@@ -141,14 +141,12 @@ int LG_CC_Boruvka
141141
GRB_TRY (GrB_Vector_new (&ccmn, GrB_UINT64, n)) ; // cc's min neighbor
142142
GRB_TRY (GrB_Vector_new (&mask, GrB_BOOL, n)) ; // various uses
143143

144-
mem = (GrB_Index *) LAGraph_Malloc (3*n, sizeof (GrB_Index)) ;
145-
Px = (GrB_Index *) LAGraph_Malloc (n, sizeof (GrB_Index)) ;
146-
LG_ASSERT (Px != NULL && mem != NULL, GrB_OUT_OF_MEMORY) ;
144+
LG_TRY (LAGraph_Malloc ((void **) &mem, 3*n, sizeof (GrB_Index), msg)) ;
145+
LG_TRY (LAGraph_Malloc ((void **) &Px, n, sizeof (GrB_Index), msg)) ;
147146

148147
#if !LAGRAPH_SUITESPARSE
149148
// I is not needed for SuiteSparse and remains NULL
150-
I = (GrB_Index *) LAGraph_Malloc (n, sizeof (GrB_Index)) ;
151-
LG_ASSERT (I != NULL, GrB_OUT_OF_MEMORY) ;
149+
LG_TRY (LAGraph_Malloc ((void **) &I, n, sizeof (GrB_Index), msg)) ;
152150
#endif
153151

154152
// parent = 0:n-1, and copy to ramp

src/algorithm/LG_CC_FastSV6.c

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,32 @@ static inline GrB_Info fastsv
165165
// components in the graph G is the number of representatives.
166166

167167
#undef LG_FREE_WORK
168-
#define LG_FREE_WORK \
169-
{ \
170-
LAGraph_Free ((void **) &Tp) ; \
171-
LAGraph_Free ((void **) &Tj) ; \
172-
LAGraph_Free ((void **) &Tx) ; \
173-
LAGraph_Free ((void **) &Cp) ; \
174-
LAGraph_Free ((void **) &Px) ; \
175-
LAGraph_Free ((void **) &Cx) ; \
176-
LAGraph_Free ((void **) &ht_key) ; \
177-
LAGraph_Free ((void **) &ht_count) ; \
178-
LAGraph_Free ((void **) &count) ; \
179-
LAGraph_Free ((void **) &range) ; \
180-
GrB_free (&C) ; \
181-
GrB_free (&T) ; \
182-
GrB_free (&t) ; \
183-
GrB_free (&y) ; \
184-
GrB_free (&gp) ; \
185-
GrB_free (&mngp) ; \
186-
GrB_free (&gp_new) ; \
168+
#define LG_FREE_WORK \
169+
{ \
170+
LAGraph_Free ((void **) &Tp, NULL) ; \
171+
LAGraph_Free ((void **) &Tj, NULL) ; \
172+
LAGraph_Free ((void **) &Tx, NULL) ; \
173+
LAGraph_Free ((void **) &Cp, NULL) ; \
174+
LAGraph_Free ((void **) &Px, NULL) ; \
175+
LAGraph_Free ((void **) &Cx, NULL) ; \
176+
LAGraph_Free ((void **) &ht_key, NULL) ; \
177+
LAGraph_Free ((void **) &ht_count, NULL) ; \
178+
LAGraph_Free ((void **) &count, NULL) ; \
179+
LAGraph_Free ((void **) &range, NULL) ; \
180+
GrB_free (&C) ; \
181+
GrB_free (&T) ; \
182+
GrB_free (&t) ; \
183+
GrB_free (&y) ; \
184+
GrB_free (&gp) ; \
185+
GrB_free (&mngp) ; \
186+
GrB_free (&gp_new) ; \
187187
}
188188

189189
#undef LG_FREE_ALL
190-
#define LG_FREE_ALL \
191-
{ \
192-
LG_FREE_WORK ; \
193-
GrB_free (&parent) ; \
190+
#define LG_FREE_ALL \
191+
{ \
192+
LG_FREE_WORK ; \
193+
GrB_free (&parent) ; \
194194
}
195195

196196
#endif
@@ -293,9 +293,8 @@ int LG_CC_FastSV6 // SuiteSparse:GraphBLAS method, with GxB extensions
293293
nthreads = LAGRAPH_MAX (nthreads, 1) ;
294294
// ]
295295

296-
Cx = (void *) LAGraph_Calloc (1, sizeof (bool)) ;
297-
Px = (GrB_Index *) LAGraph_Malloc (n, sizeof (GrB_Index)) ;
298-
LG_ASSERT (Px != NULL && Cx != NULL, GrB_OUT_OF_MEMORY) ;
296+
LG_TRY (LAGraph_Calloc ((void **) &Cx, 1, sizeof (bool), msg)) ;
297+
LG_TRY (LAGraph_Malloc ((void **) &Px, n, sizeof (GrB_Index), msg)) ;
299298

300299
// create Cp = 0:n (always 64-bit) and the empty C matrix
301300
GRB_TRY (GrB_Matrix_new (&C, GrB_BOOL, n, n)) ;
@@ -390,13 +389,11 @@ int LG_CC_FastSV6 // SuiteSparse:GraphBLAS method, with GxB extensions
390389
GrB_Index Tp_size = (n+1) * sizeof (GrB_Index) ;
391390
GrB_Index Tj_size = nvals * sizeof (GrB_Index) ;
392391
GrB_Index Tx_size = sizeof (bool) ;
393-
Tp = (GrB_Index *) LAGraph_Malloc (n+1, sizeof (GrB_Index)) ;
394-
Tj = (GrB_Index *) LAGraph_Malloc (nvals, sizeof (GrB_Index)) ;
395-
Tx = (bool *) LAGraph_Calloc (1, sizeof (bool)) ;
396-
range = (int64_t *) LAGraph_Malloc (nthreads + 1, sizeof (int64_t)) ;
397-
count = (GrB_Index *) LAGraph_Calloc (nthreads + 1, sizeof (GrB_Index));
398-
LG_ASSERT (Tp != NULL && Tj != NULL && Tx != NULL && range != NULL
399-
&& count != NULL, GrB_OUT_OF_MEMORY) ;
392+
LG_TRY (LAGraph_Malloc ((void **) &Tp, n+1, sizeof (GrB_Index), msg)) ;
393+
LG_TRY (LAGraph_Malloc ((void **) &Tj, nvals, sizeof (GrB_Index), msg)) ;
394+
LG_TRY (LAGraph_Calloc ((void **) &Tx, 1, sizeof (bool), msg)) ;
395+
LG_TRY (LAGraph_Malloc ((void **) &range, nthreads + 1, sizeof (int64_t), msg)) ;
396+
LG_TRY (LAGraph_Calloc ((void **) &count, nthreads + 1, sizeof (GrB_Index), msg));
400397

401398
//----------------------------------------------------------------------
402399
// define parallel tasks to construct T
@@ -487,9 +484,8 @@ int LG_CC_FastSV6 // SuiteSparse:GraphBLAS method, with GxB extensions
487484
#define NEXT(x) ((x + 23) & (HASH_SIZE-1))
488485

489486
// allocate and initialize the hash table
490-
ht_key = (GrB_Index *) LAGraph_Malloc (HASH_SIZE, sizeof (GrB_Index)) ;
491-
ht_count = (int *) LAGraph_Calloc (HASH_SIZE, sizeof (int)) ;
492-
LG_ASSERT (ht_key != NULL && ht_count != NULL, GrB_OUT_OF_MEMORY) ;
487+
LG_TRY (LAGraph_Malloc ((void **) &ht_key, HASH_SIZE, sizeof (GrB_Index), msg)) ;
488+
LG_TRY (LAGraph_Calloc ((void **) &ht_count, HASH_SIZE, sizeof (int), msg)) ;
493489
for (int k = 0 ; k < HASH_SIZE ; k++)
494490
{
495491
ht_key [k] = UINT64_MAX ;

src/benchmark/LAGraph_demo.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@
5151
// binwrite: write a matrix to a binary file
5252
//------------------------------------------------------------------------------
5353

54-
#define LG_FREE_ALL \
55-
{ \
56-
GrB_free (A) ; \
57-
LAGraph_Free ((void **) &Ap) ; \
58-
LAGraph_Free ((void **) &Ab) ; \
59-
LAGraph_Free ((void **) &Ah) ; \
60-
LAGraph_Free ((void **) &Ai) ; \
61-
LAGraph_Free ((void **) &Ax) ; \
54+
#define LG_FREE_ALL \
55+
{ \
56+
GrB_free (A) ; \
57+
LAGraph_Free ((void **) &Ap, NULL) ; \
58+
LAGraph_Free ((void **) &Ab, NULL) ; \
59+
LAGraph_Free ((void **) &Ah, NULL) ; \
60+
LAGraph_Free ((void **) &Ai, NULL) ; \
61+
LAGraph_Free ((void **) &Ax, NULL) ; \
6262
}
6363

6464
#define FWRITE(p,s,n) \
6565
{ \
6666
if (fwrite (p, s, n, f) != n) \
6767
{ \
68-
CATCH (-1001) ; /* file I/O error */ \
68+
CATCH (LAGRAPH_IO_ERROR) ; \
6969
} \
7070
}
7171

@@ -571,9 +571,9 @@ static inline int binread // returns 0 if successful, -1 if failure
571571
Ah_len = nvec ;
572572
Ai_len = nvals ;
573573
Ax_len = nvals ;
574-
Ap = LAGraph_Malloc (Ap_len, sizeof (GrB_Index)) ;
575-
Ah = LAGraph_Malloc (Ah_len, sizeof (GrB_Index)) ;
576-
Ai = LAGraph_Malloc (Ai_len, sizeof (GrB_Index)) ;
574+
LAGraph_Malloc ((void **) &Ap, Ap_len, sizeof (GrB_Index), msg) ;
575+
LAGraph_Malloc ((void **) &Ah, Ah_len, sizeof (GrB_Index), msg) ;
576+
LAGraph_Malloc ((void **) &Ai, Ai_len, sizeof (GrB_Index), msg) ;
577577
Ap_size = Ap_len * sizeof (GrB_Index) ;
578578
Ah_size = Ah_len * sizeof (GrB_Index) ;
579579
Ai_size = Ai_len * sizeof (GrB_Index) ;
@@ -584,8 +584,8 @@ static inline int binread // returns 0 if successful, -1 if failure
584584
Ap_len = nvec+1 ;
585585
Ai_len = nvals ;
586586
Ax_len = nvals ;
587-
Ap = LAGraph_Malloc (Ap_len, sizeof (GrB_Index)) ;
588-
Ai = LAGraph_Malloc (Ai_len, sizeof (GrB_Index)) ;
587+
LAGraph_Malloc ((void **) &Ap, Ap_len, sizeof (GrB_Index), msg) ;
588+
LAGraph_Malloc ((void **) &Ai, Ai_len, sizeof (GrB_Index), msg) ;
589589
Ap_size = Ap_len * sizeof (GrB_Index) ;
590590
Ai_size = Ai_len * sizeof (GrB_Index) ;
591591
ok = (Ap != NULL && Ai != NULL) ;
@@ -594,7 +594,7 @@ static inline int binread // returns 0 if successful, -1 if failure
594594
{
595595
Ab_len = nrows*ncols ;
596596
Ax_len = nrows*ncols ;
597-
Ab = LAGraph_Malloc (nrows*ncols, sizeof (int8_t)) ;
597+
LAGraph_Malloc ((void **) &Ab, nrows*ncols, sizeof (int8_t), msg) ;
598598
Ab_size = Ab_len * sizeof (GrB_Index) ;
599599
ok = (Ab != NULL) ;
600600
}
@@ -606,7 +606,7 @@ static inline int binread // returns 0 if successful, -1 if failure
606606
{
607607
CATCH (DEAD_CODE) ; // this "cannot" happen
608608
}
609-
Ax = LAGraph_Malloc (iso ? 1 : Ax_len, typesize) ;
609+
LAGraph_Malloc ((void **) &Ax, iso ? 1 : Ax_len, typesize, msg) ;
610610
Ax_size = (iso ? 1 : Ax_len) * typesize ;
611611
ok = ok && (Ax != NULL) ;
612612
if (!ok) CATCH (GrB_OUT_OF_MEMORY) ; // out of memory

src/benchmark/cc_demo.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
GrB_Index countCC (GrB_Vector f, GrB_Index n)
3939
{
4040
GrB_Index nCC = 0;
41-
GrB_Index *w_val = (GrB_Index *) LAGraph_Malloc (n, sizeof (GrB_Index)) ;
41+
GrB_Index *w_val = NULL ;
42+
LAGraph_Malloc ((void **) &w_val, n, sizeof (GrB_Index), NULL) ;
4243
if (w_val == NULL) { printf ("out of memory\n") ; abort ( ) ; }
44+
GrB_Index *i_val = NULL ;
4345
#if LAGRAPH_SUITESPARSE
4446
// SuiteSparse:GraphBLAS allows NULL inputs to GrB_Vector_extractTuples
45-
GrB_Index *i_val = NULL ;
4647
#else
47-
GrB_Index *i_val = (GrB_Index *) LAGraph_Malloc (n, sizeof (GrB_Index)) ;
48+
LAGraph_Malloc ((void **) &i_val, n, sizeof (GrB_Index), NULL) ;
4849
if (i_val == NULL) { printf ("out of memory\n") ; abort ( ) ; }
4950
#endif
5051
GrB_Vector_extractTuples (i_val, w_val, &n, f) ;
@@ -55,8 +56,8 @@ GrB_Index countCC (GrB_Vector f, GrB_Index n)
5556
nCC++ ;
5657
}
5758
}
58-
LAGraph_Free ((void **) &i_val) ;
59-
LAGraph_Free ((void **) &w_val) ;
59+
LAGraph_Free ((void **) &i_val, NULL) ;
60+
LAGraph_Free ((void **) &w_val, NULL) ;
6061
return nCC;
6162
}
6263

src/test/LG_check_bfs.c

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111

1212
//------------------------------------------------------------------------------
1313

14-
#define LG_FREE_WORK \
15-
{ \
16-
LAGraph_Free ((void **) &queue) ; \
17-
LAGraph_Free ((void **) &level_check) ; \
18-
LAGraph_Free ((void **) &level_in) ; \
19-
LAGraph_Free ((void **) &parent_in) ; \
20-
LAGraph_Free ((void **) &visited) ; \
21-
LAGraph_Free ((void **) &neighbors) ; \
22-
GrB_free (&Row) ; \
14+
#define LG_FREE_WORK \
15+
{ \
16+
LAGraph_Free ((void **) &queue, NULL) ; \
17+
LAGraph_Free ((void **) &level_check, NULL) ; \
18+
LAGraph_Free ((void **) &level_in, NULL) ; \
19+
LAGraph_Free ((void **) &parent_in, NULL) ; \
20+
LAGraph_Free ((void **) &visited, NULL) ; \
21+
LAGraph_Free ((void **) &neighbors, NULL) ; \
22+
GrB_free (&Row) ; \
2323
}
2424

25-
#define LG_FREE_ALL \
26-
{ \
27-
LG_FREE_WORK ; \
28-
LAGraph_Free ((void **) &Ap) ; \
29-
LAGraph_Free ((void **) &Aj) ; \
30-
LAGraph_Free ((void **) &Ax) ; \
25+
#define LG_FREE_ALL \
26+
{ \
27+
LG_FREE_WORK ; \
28+
LAGraph_Free ((void **) &Ap, NULL) ; \
29+
LAGraph_Free ((void **) &Aj, NULL) ; \
30+
LAGraph_Free ((void **) &Ax, NULL) ; \
3131
}
3232

3333
#include "LG_internal.h"
@@ -74,25 +74,22 @@ int LG_check_bfs
7474
// allocate workspace
7575
//--------------------------------------------------------------------------
7676

77-
queue = LAGraph_Malloc (n, sizeof (int64_t)) ;
78-
level_check = LAGraph_Malloc (n, sizeof (int64_t)) ;
79-
LG_ASSERT (queue != NULL && level_check != NULL , GrB_OUT_OF_MEMORY) ;
77+
LG_TRY (LAGraph_Malloc ((void **) &queue, n, sizeof (int64_t), msg)) ;
78+
LG_TRY (LAGraph_Malloc ((void **) &level_check, n, sizeof (int64_t), msg)) ;
8079

8180
//--------------------------------------------------------------------------
8281
// get the contents of the Level and Parent vectors
8382
//--------------------------------------------------------------------------
8483

8584
if (Level != NULL)
8685
{
87-
level_in = LAGraph_Malloc (n, sizeof (int64_t)) ;
88-
LG_ASSERT (level_in != NULL, GrB_OUT_OF_MEMORY) ;
86+
LG_TRY (LAGraph_Malloc ((void **) &level_in, n, sizeof (int64_t), msg)) ;
8987
LG_TRY (LG_check_vector (level_in, Level, n, -1)) ;
9088
}
9189

9290
if (Parent != NULL)
9391
{
94-
parent_in = LAGraph_Malloc (n, sizeof (int64_t)) ;
95-
LG_ASSERT (parent_in != NULL, GrB_OUT_OF_MEMORY) ;
92+
LG_TRY (LAGraph_Malloc ((void **) &parent_in, n, sizeof (int64_t), msg)) ;
9693
LG_TRY (LG_check_vector (parent_in, Parent, n, -1)) ;
9794
}
9895

@@ -120,8 +117,7 @@ int LG_check_bfs
120117
queue [0] = src ;
121118
int64_t head = 0 ;
122119
int64_t tail = 1 ;
123-
visited = LAGraph_Calloc (n, sizeof (bool)) ;
124-
LG_ASSERT (visited != NULL, GrB_OUT_OF_MEMORY) ;
120+
LG_TRY (LAGraph_Calloc ((void **) &visited, n, sizeof (bool), msg)) ;
125121
visited [src] = true ; // src is visited, and is level 0
126122

127123
for (int64_t i = 0 ; i < n ; i++)
@@ -132,8 +128,7 @@ int LG_check_bfs
132128

133129
#if !LAGRAPH_SUITESPARSE
134130
GRB_TRY (GrB_Vector_new (&Row, GrB_BOOL, n)) ;
135-
neighbors = LAGraph_Malloc (n, sizeof (GrB_Index)) ;
136-
LG_ASSERT (neighbors != NULL, GrB_OUT_OF_MEMORY) ;
131+
LG_TRY (LAGraph_Malloc ((void **) &neighbors, n, sizeof (GrB_Index), msg)) ;
137132
#endif
138133

139134
while (head < tail)

0 commit comments

Comments
 (0)