Skip to content

Commit d1801b9

Browse files
committed
Add error handling to bsp_matrix_t functions.
1 parent 761c7e3 commit d1801b9

File tree

13 files changed

+98
-59
lines changed

13 files changed

+98
-59
lines changed

examples/benchmark_read.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ int main(int argc, char** argv) {
8080
// If running warm cache experiments, read once to warm cache.
8181
if (!cold_cache) {
8282
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
83-
bsp_destroy_matrix_t(mat);
83+
bsp_destroy_matrix_t(&mat);
8484
}
8585

8686
for (size_t i = 0; i < num_trials; i++) {
@@ -92,7 +92,7 @@ int main(int argc, char** argv) {
9292
double end = gettime();
9393
durations[i] = end - begin;
9494
nbytes = bsp_matrix_nbytes(mat);
95-
bsp_destroy_matrix_t(mat);
95+
bsp_destroy_matrix_t(&mat);
9696

9797
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
9898
double gbytes_s = gbytes / durations[i];

examples/benchmark_read_parallel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main(int argc, char** argv) {
8383
// If running warm cache experiments, read once to warm cache.
8484
if (!cold_cache) {
8585
bsp_matrix_t mat = bsp_read_matrix_parallel(file_name, NULL, num_threads);
86-
bsp_destroy_matrix_t(mat);
86+
bsp_destroy_matrix_t(&mat);
8787
}
8888

8989
for (size_t i = 0; i < num_trials; i++) {
@@ -97,7 +97,7 @@ int main(int argc, char** argv) {
9797
durations[i] = end - begin;
9898
nbytes = bsp_matrix_nbytes(mat);
9999

100-
bsp_destroy_matrix_t(mat);
100+
bsp_destroy_matrix_t(&mat);
101101

102102
double gbytes = ((double) nbytes) / 1024 / 1024 / 1024;
103103
double gbytes_s = gbytes / durations[i];

examples/bsp2mtx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main(int argc, char** argv) {
2929
bsp_mmwrite(output_fname, matrix);
3030
printf(" === Done writing. ===\n");
3131

32-
bsp_destroy_matrix_t(matrix);
32+
bsp_destroy_matrix_t(&matrix);
3333

3434
return 0;
3535
}

examples/check_equivalence.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ int main(int argc, char** argv) {
128128
if (matrix1.format != matrix2.format) {
129129
if (matrix1.format != BSP_COOR) {
130130
bsp_matrix_t intermediate = bsp_convert_matrix(matrix1, BSP_COOR);
131-
bsp_destroy_matrix_t(matrix1);
131+
bsp_destroy_matrix_t(&matrix1);
132132
matrix1 = intermediate;
133133
}
134134

135135
if (matrix2.format != BSP_COOR) {
136136
bsp_matrix_t intermediate = bsp_convert_matrix(matrix2, BSP_COOR);
137-
bsp_destroy_matrix_t(matrix2);
137+
bsp_destroy_matrix_t(&matrix2);
138138
matrix2 = intermediate;
139139
}
140140
}

examples/check_equivalence_parallel.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ int main(int argc, char** argv) {
136136
if (matrix1.format != matrix2.format) {
137137
if (matrix1.format != BSP_COOR) {
138138
bsp_matrix_t intermediate = bsp_convert_matrix(matrix1, BSP_COOR);
139-
bsp_destroy_matrix_t(matrix1);
139+
bsp_destroy_matrix_t(&matrix1);
140140
matrix1 = intermediate;
141141
}
142142

143143
if (matrix2.format != BSP_COOR) {
144144
bsp_matrix_t intermediate = bsp_convert_matrix(matrix2, BSP_COOR);
145-
bsp_destroy_matrix_t(matrix2);
145+
bsp_destroy_matrix_t(&matrix2);
146146
matrix2 = intermediate;
147147
}
148148
}

examples/mtx2bsp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int main(int argc, char** argv) {
151151
if (format != BSP_COOR) {
152152
begin = gettime();
153153
bsp_matrix_t converted_matrix = bsp_convert_matrix(matrix, format);
154-
bsp_destroy_matrix_t(matrix);
154+
bsp_destroy_matrix_t(&matrix);
155155
matrix = converted_matrix;
156156
end = gettime();
157157
duration = end - begin;
@@ -170,7 +170,7 @@ int main(int argc, char** argv) {
170170
printf("%lf seconds writing Binsparse file...\n", duration);
171171
printf(" === Done writing. ===\n");
172172

173-
bsp_destroy_matrix_t(matrix);
173+
bsp_destroy_matrix_t(&matrix);
174174

175175
return 0;
176176
}

include/binsparse/array.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ typedef struct bsp_array_t {
2020
bsp_allocator_t allocator;
2121
} bsp_array_t;
2222

23-
static inline bsp_array_t bsp_construct_default_array_t() {
24-
bsp_array_t array;
25-
array.data = NULL;
26-
array.size = 0;
27-
array.allocator = bsp_default_allocator;
28-
return array;
23+
static inline bsp_error_t bsp_construct_default_array_t(bsp_array_t* array) {
24+
array->data = NULL;
25+
array->size = 0;
26+
array->allocator = bsp_default_allocator;
27+
return BSP_SUCCESS;
2928
}
3029

3130
static inline bsp_error_t bsp_construct_array_t(bsp_array_t* array, size_t size,

include/binsparse/convert_matrix.h

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
2020
// *Convert to COO* from another format.
2121
if (matrix.format == BSP_CSR) {
2222
// Convert CSR -> COOR
23-
bsp_matrix_t result = bsp_construct_default_matrix_t();
23+
bsp_matrix_t result;
24+
bsp_construct_default_matrix_t(&result);
2425

2526
result.format = BSP_COOR;
2627

@@ -40,7 +41,9 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
4041
bsp_error_t error =
4142
bsp_copy_construct_array_t(&result.values, matrix.values);
4243
if (error != BSP_SUCCESS) {
43-
return bsp_construct_default_matrix_t();
44+
bsp_matrix_t empty_result;
45+
bsp_construct_default_matrix_t(&empty_result);
46+
return empty_result;
4447
}
4548

4649
// There is a corner case with tall and skinny matrices where we need a
@@ -51,14 +54,18 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
5154
error = bsp_copy_construct_array_t(&result.indices_1, matrix.indices_1);
5255
if (error != BSP_SUCCESS) {
5356
bsp_destroy_array_t(&result.values);
54-
return bsp_construct_default_matrix_t();
57+
bsp_matrix_t empty_result;
58+
bsp_construct_default_matrix_t(&empty_result);
59+
return empty_result;
5560
}
5661
} else {
5762
error = bsp_construct_array_t(&result.indices_1, matrix.indices_1.size,
5863
index_type);
5964
if (error != BSP_SUCCESS) {
6065
bsp_destroy_array_t(&result.values);
61-
return bsp_construct_default_matrix_t();
66+
bsp_matrix_t empty_result;
67+
bsp_construct_default_matrix_t(&empty_result);
68+
return empty_result;
6269
}
6370

6471
for (size_t i = 0; i < matrix.indices_1.size; i++) {
@@ -72,7 +79,9 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
7279
if (error != BSP_SUCCESS) {
7380
bsp_destroy_array_t(&result.values);
7481
bsp_destroy_array_t(&result.indices_1);
75-
return bsp_construct_default_matrix_t();
82+
bsp_matrix_t empty_result;
83+
bsp_construct_default_matrix_t(&empty_result);
84+
return empty_result;
7685
}
7786

7887
for (size_t i = 0; i < matrix.nrows; i++) {
@@ -95,13 +104,14 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
95104
if (matrix.format != BSP_COOR) {
96105
bsp_matrix_t intermediate = bsp_convert_matrix(matrix, BSP_COOR);
97106
bsp_matrix_t result = bsp_convert_matrix(intermediate, format);
98-
bsp_destroy_matrix_t(intermediate);
107+
bsp_destroy_matrix_t(&intermediate);
99108
return result;
100109
} else {
101110
if (format == BSP_CSR) {
102111
// Convert COOR -> CSR
103112

104-
bsp_matrix_t result = bsp_construct_default_matrix_t();
113+
bsp_matrix_t result;
114+
bsp_construct_default_matrix_t(&result);
105115

106116
result.format = BSP_CSR;
107117

@@ -130,22 +140,28 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
130140
bsp_error_t error =
131141
bsp_copy_construct_array_t(&result.values, matrix.values);
132142
if (error != BSP_SUCCESS) {
133-
return bsp_construct_default_matrix_t();
143+
bsp_matrix_t empty_result;
144+
bsp_construct_default_matrix_t(&empty_result);
145+
return empty_result;
134146
}
135147

136148
if (index_type == matrix.indices_1.type) {
137149
error =
138150
bsp_copy_construct_array_t(&result.indices_1, matrix.indices_1);
139151
if (error != BSP_SUCCESS) {
140152
bsp_destroy_array_t(&result.values);
141-
return bsp_construct_default_matrix_t();
153+
bsp_matrix_t empty_result;
154+
bsp_construct_default_matrix_t(&empty_result);
155+
return empty_result;
142156
}
143157
} else {
144158
error =
145159
bsp_construct_array_t(&result.indices_1, matrix.nnz, index_type);
146160
if (error != BSP_SUCCESS) {
147161
bsp_destroy_array_t(&result.values);
148-
return bsp_construct_default_matrix_t();
162+
bsp_matrix_t empty_result;
163+
bsp_construct_default_matrix_t(&empty_result);
164+
return empty_result;
149165
}
150166

151167
for (size_t i = 0; i < matrix.nnz; i++) {
@@ -160,7 +176,9 @@ static inline bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
160176
if (error != BSP_SUCCESS) {
161177
bsp_destroy_array_t(&result.values);
162178
bsp_destroy_array_t(&result.indices_1);
163-
return bsp_construct_default_matrix_t();
179+
bsp_matrix_t empty_result;
180+
bsp_construct_default_matrix_t(&empty_result);
181+
return empty_result;
164182
}
165183

166184
bsp_array_t rowptr = result.pointers_to_1;

include/binsparse/generate.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ static inline void bsp_array_fill_random(bsp_array_t array, size_t bound) {
7070
static inline bsp_matrix_t bsp_generate_coo(size_t m, size_t n, size_t nnz,
7171
bsp_type_t value_type,
7272
bsp_type_t index_type) {
73-
bsp_matrix_t matrix = bsp_construct_default_matrix_t();
73+
bsp_matrix_t matrix;
74+
bsp_construct_default_matrix_t(&matrix);
7475
matrix.nrows = m;
7576
matrix.ncols = n;
7677
matrix.nnz = nnz;
7778

78-
bsp_error_t error;
79-
error = bsp_construct_array_t(&matrix.values, nnz, value_type);
79+
bsp_error_t error = bsp_construct_array_t(&matrix.values, nnz, value_type);
8080
if (error != BSP_SUCCESS) {
8181
// Return empty matrix on error
8282
return matrix;

include/binsparse/hdf5_wrapper.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,27 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
8484
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
8585

8686
if (dset == H5I_INVALID_HID) {
87-
return bsp_construct_default_array_t();
87+
bsp_array_t empty_array;
88+
bsp_construct_default_array_t(&empty_array);
89+
return empty_array;
8890
}
8991

9092
hid_t fspace = H5Dget_space(dset);
9193

9294
if (fspace == H5I_INVALID_HID) {
93-
return bsp_construct_default_array_t();
95+
bsp_array_t empty_array;
96+
bsp_construct_default_array_t(&empty_array);
97+
return empty_array;
9498
}
9599

96100
hsize_t dims[3];
97101

98102
int r = H5Sget_simple_extent_dims(fspace, dims, NULL);
99103

100104
if (r < 0) {
101-
return bsp_construct_default_array_t();
105+
bsp_array_t empty_array;
106+
bsp_construct_default_array_t(&empty_array);
107+
return empty_array;
102108
}
103109

104110
hid_t hdf5_type = H5Dget_type(dset);
@@ -180,33 +186,44 @@ static inline bsp_array_t bsp_read_array(hid_t f, const char* label) {
180186
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
181187

182188
if (dset == H5I_INVALID_HID) {
183-
return bsp_construct_default_array_t();
189+
bsp_array_t empty_array;
190+
bsp_construct_default_array_t(&empty_array);
191+
return empty_array;
184192
}
185193

186194
hid_t fspace = H5Dget_space(dset);
187195

188196
if (fspace == H5I_INVALID_HID) {
189-
return bsp_construct_default_array_t();
197+
bsp_array_t empty_array;
198+
bsp_construct_default_array_t(&empty_array);
199+
return empty_array;
190200
}
191201

192202
hsize_t dims[3];
193203

194204
int r = H5Sget_simple_extent_dims(fspace, dims, NULL);
195205

196206
if (r < 0) {
197-
return bsp_construct_default_array_t();
207+
bsp_array_t empty_array;
208+
bsp_construct_default_array_t(&empty_array);
209+
return empty_array;
198210
}
199211

200212
hid_t hdf5_type = H5Dget_type(dset);
201213

202214
bsp_type_t type = bsp_get_bsp_type(hdf5_type);
203215

204-
bsp_array_t array = bsp_construct_default_array_t();
216+
bsp_array_t array;
217+
bsp_construct_default_array_t(&array);
218+
205219
bsp_error_t error = bsp_construct_array_t(&array, dims[0], type);
206220
if (error != BSP_SUCCESS) {
221+
bsp_destroy_array_t(&array);
207222
H5Dclose(dset);
208223
H5Sclose(fspace);
209-
return bsp_construct_default_array_t();
224+
bsp_array_t empty_array;
225+
bsp_construct_default_array_t(&empty_array);
226+
return empty_array;
210227
}
211228

212229
herr_t status = H5Dread(dset, bsp_get_hdf5_native_type(type), H5S_ALL,
@@ -216,7 +233,9 @@ static inline bsp_array_t bsp_read_array(hid_t f, const char* label) {
216233
bsp_destroy_array_t(&array);
217234
H5Dclose(dset);
218235
H5Sclose(fspace);
219-
return bsp_construct_default_array_t();
236+
bsp_array_t empty_array;
237+
bsp_construct_default_array_t(&empty_array);
238+
return empty_array;
220239
}
221240

222241
H5Dclose(dset);

0 commit comments

Comments
 (0)