Skip to content

Commit 8405a28

Browse files
committed
Update bsp_matrix_t to have proper error handling, update parsing
functions.
1 parent d1801b9 commit 8405a28

File tree

9 files changed

+258
-138
lines changed

9 files changed

+258
-138
lines changed

examples/bsp-ls.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ void print_group_info(hid_t g, const char* name) {
1818
H5E_END_TRY;
1919

2020
if (bsp_json != H5I_INVALID_HID) {
21-
char* json_string = bsp_read_attribute(g, "binsparse");
21+
char* json_string;
22+
bsp_error_t error = bsp_read_attribute(&json_string, g, "binsparse");
23+
if (error != BSP_SUCCESS) {
24+
printf("Error reading binsparse attribute: %s\n",
25+
bsp_get_error_string(error));
26+
return;
27+
}
2228

2329
cJSON* j = cJSON_Parse(json_string);
2430

examples/simple_read.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ int main(int argc, char** argv) {
1111

1212
hid_t f = H5Fopen(file_name, H5F_ACC_RDWR, H5P_DEFAULT);
1313

14-
bsp_array_t array = bsp_read_array(f, "test");
14+
bsp_array_t array;
15+
bsp_error_t error = bsp_read_array(&array, f, "test");
16+
if (error != BSP_SUCCESS) {
17+
printf("Error reading array: %s\n", bsp_get_error_string(error));
18+
H5Fclose(f);
19+
return 1;
20+
}
1521

1622
int* values = (int*) array.data;
1723

examples/simple_write.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ int main(int argc, char** argv) {
1212
hid_t f = H5Fcreate(file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
1313

1414
bsp_array_t array;
15-
bsp_error_t error = bsp_construct_array_t(&array, 1000, BSP_INT32);
16-
if (error != BSP_SUCCESS) {
17-
printf("Error: Failed to allocate array\n");
18-
H5Fclose(f);
19-
return 1;
20-
}
15+
bsp_construct_array_t(&array, 1000, BSP_INT32);
2116

2217
int* values = (int*) array.data;
2318

include/binsparse/hdf5_wrapper.h

Lines changed: 77 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121

2222
// Write an array to a dataset / file
2323
// Returns 0 on success, nonzero on error.
24-
static inline int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
25-
int compression_level) {
24+
static inline bsp_error_t bsp_write_array(hid_t f, const char* label,
25+
bsp_array_t array,
26+
int compression_level) {
2627
if (array.type == BSP_COMPLEX_FLOAT32 || array.type == BSP_COMPLEX_FLOAT64) {
2728
bsp_error_t error = bsp_complex_array_to_fp(&array);
2829
if (error != BSP_SUCCESS) {
29-
return -3; // Type conversion error
30+
return BSP_ERROR_TYPE;
3031
}
3132
}
3233

@@ -59,52 +60,63 @@ static inline int bsp_write_array(hid_t f, const char* label, bsp_array_t array,
5960
H5Dcreate2(f, label, hdf5_standard_type, fspace, lcpl, dcpl, H5P_DEFAULT);
6061

6162
if (dset == H5I_INVALID_HID) {
62-
return -1;
63+
H5Sclose(fspace);
64+
H5Pclose(lcpl);
65+
H5Pclose(dcpl);
66+
return BSP_ERROR_IO;
6367
}
6468

6569
hid_t hdf5_native_type = bsp_get_hdf5_native_type(array.type);
6670

6771
hid_t r = H5Dwrite(dset, hdf5_native_type, H5S_ALL, fspace, H5P_DEFAULT,
6872
array.data);
6973

70-
if (r == H5I_INVALID_HID) {
71-
return -2;
74+
if (r < 0) {
75+
H5Dclose(dset);
76+
H5Sclose(fspace);
77+
H5Pclose(lcpl);
78+
H5Pclose(dcpl);
79+
return BSP_ERROR_IO;
7280
}
7381

7482
H5Sclose(fspace);
83+
H5Dclose(dset);
7584
H5Pclose(lcpl);
7685
H5Pclose(dcpl);
7786

78-
return 0;
87+
return BSP_SUCCESS;
7988
}
8089

8190
#if __STDC_VERSION__ >= 201112L
82-
static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
91+
static inline bsp_error_t bsp_read_array_parallel(bsp_array_t* array, hid_t f,
92+
const char* label,
8393
int num_threads) {
8494
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
8595

8696
if (dset == H5I_INVALID_HID) {
87-
bsp_array_t empty_array;
88-
bsp_construct_default_array_t(&empty_array);
89-
return empty_array;
97+
H5Dclose(dset);
98+
bsp_construct_default_array_t(array);
99+
return BSP_ERROR_IO;
90100
}
91101

92102
hid_t fspace = H5Dget_space(dset);
93103

94104
if (fspace == H5I_INVALID_HID) {
95-
bsp_array_t empty_array;
96-
bsp_construct_default_array_t(&empty_array);
97-
return empty_array;
105+
H5Sclose(fspace);
106+
H5Dclose(dset);
107+
bsp_construct_default_array_t(array);
108+
return BSP_ERROR_IO;
98109
}
99110

100111
hsize_t dims[3];
101112

102113
int r = H5Sget_simple_extent_dims(fspace, dims, NULL);
103114

104115
if (r < 0) {
105-
bsp_array_t empty_array;
106-
bsp_construct_default_array_t(&empty_array);
107-
return empty_array;
116+
H5Sclose(fspace);
117+
H5Dclose(dset);
118+
bsp_construct_default_array_t(array);
119+
return BSP_ERROR_IO;
108120
}
109121

110122
hid_t hdf5_type = H5Dget_type(dset);
@@ -113,10 +125,9 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
113125

114126
// Array will be written into a POSIX shared memory.
115127
bsp_shm_t array_shm = bsp_shm_new(dims[0] * bsp_type_size(type));
116-
bsp_array_t array;
117-
array.type = type;
118-
array.size = dims[0];
119-
array.allocator = bsp_shm_allocator;
128+
array->type = type;
129+
array->size = dims[0];
130+
array->allocator = bsp_shm_allocator;
120131

121132
bsp_shm_t active_children_shm = bsp_shm_new(sizeof(_Atomic int));
122133

@@ -140,35 +151,35 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
140151
}
141152
}
142153

143-
array.data = bsp_shm_attach(array_shm);
154+
array->data = bsp_shm_attach(array_shm);
144155
if (thread_num == 0) {
145156
bsp_shm_delete(array_shm);
146157
}
147158

148-
hsize_t chunk_size = (array.size + num_threads - 1) / num_threads;
149-
hsize_t start = (chunk_size * thread_num < array.size)
159+
hsize_t chunk_size = (array->size + num_threads - 1) / num_threads;
160+
hsize_t start = (chunk_size * thread_num < array->size)
150161
? chunk_size * thread_num
151-
: array.size;
162+
: array->size;
152163
hsize_t count =
153-
(start + chunk_size <= array.size) ? chunk_size : array.size - start;
164+
(start + chunk_size <= array->size) ? chunk_size : array->size - start;
154165

155166
if (count > 0) {
156167
H5Sselect_hyperslab(fspace, H5S_SELECT_SET, &start, NULL, &count, NULL);
157168

158169
hid_t memspace_id = H5Screate_simple(1, &count, NULL);
159170

160171
H5Dread(dset, bsp_get_hdf5_native_type(type), memspace_id, fspace,
161-
H5P_DEFAULT, ((char*) array.data) + start * bsp_type_size(type));
172+
H5P_DEFAULT, ((char*) array->data) + start * bsp_type_size(type));
162173
H5Sclose(memspace_id);
163174
}
164175

165-
H5Dclose(dset);
166176
H5Sclose(fspace);
177+
H5Dclose(dset);
167178

168179
if (thread_num > 0) {
169180
atomic_fetch_add_explicit(active_children, -1, memory_order_relaxed);
170181
bsp_shm_detach(active_children);
171-
bsp_shm_detach(array.data);
182+
bsp_shm_detach(array->data);
172183
exit(0);
173184
}
174185

@@ -178,73 +189,69 @@ static inline bsp_array_t bsp_read_array_parallel(hid_t f, const char* label,
178189
}
179190
bsp_shm_detach(active_children);
180191

181-
return array;
192+
return BSP_SUCCESS;
182193
}
183194
#endif
184195

185-
static inline bsp_array_t bsp_read_array(hid_t f, const char* label) {
196+
static inline bsp_error_t bsp_read_array(bsp_array_t* array, hid_t f,
197+
const char* label) {
186198
hid_t dset = H5Dopen2(f, label, H5P_DEFAULT);
187199

188200
if (dset == H5I_INVALID_HID) {
189-
bsp_array_t empty_array;
190-
bsp_construct_default_array_t(&empty_array);
191-
return empty_array;
201+
bsp_construct_default_array_t(array);
202+
return BSP_ERROR_IO;
192203
}
193204

194205
hid_t fspace = H5Dget_space(dset);
195206

196207
if (fspace == H5I_INVALID_HID) {
197-
bsp_array_t empty_array;
198-
bsp_construct_default_array_t(&empty_array);
199-
return empty_array;
208+
H5Sclose(fspace);
209+
H5Dclose(dset);
210+
bsp_construct_default_array_t(array);
211+
return BSP_ERROR_IO;
200212
}
201213

202214
hsize_t dims[3];
203215

204216
int r = H5Sget_simple_extent_dims(fspace, dims, NULL);
205217

206218
if (r < 0) {
207-
bsp_array_t empty_array;
208-
bsp_construct_default_array_t(&empty_array);
209-
return empty_array;
219+
H5Dclose(dset);
220+
H5Sclose(fspace);
221+
bsp_construct_default_array_t(array);
222+
return BSP_ERROR_IO;
210223
}
211224

212225
hid_t hdf5_type = H5Dget_type(dset);
213226

214227
bsp_type_t type = bsp_get_bsp_type(hdf5_type);
215228

216-
bsp_array_t array;
217-
bsp_construct_default_array_t(&array);
218-
219-
bsp_error_t error = bsp_construct_array_t(&array, dims[0], type);
229+
bsp_error_t error = bsp_construct_array_t(array, dims[0], type);
220230
if (error != BSP_SUCCESS) {
221-
bsp_destroy_array_t(&array);
222231
H5Dclose(dset);
223232
H5Sclose(fspace);
224-
bsp_array_t empty_array;
225-
bsp_construct_default_array_t(&empty_array);
226-
return empty_array;
233+
bsp_construct_default_array_t(array);
234+
return BSP_ERROR_MEMORY;
227235
}
228236

229237
herr_t status = H5Dread(dset, bsp_get_hdf5_native_type(type), H5S_ALL,
230-
H5S_ALL, H5P_DEFAULT, array.data);
238+
H5S_ALL, H5P_DEFAULT, array->data);
231239

232240
if (status < 0) {
233-
bsp_destroy_array_t(&array);
234-
H5Dclose(dset);
241+
bsp_destroy_array_t(array);
235242
H5Sclose(fspace);
236-
bsp_array_t empty_array;
237-
bsp_construct_default_array_t(&empty_array);
238-
return empty_array;
243+
H5Dclose(dset);
244+
bsp_construct_default_array_t(array);
245+
return BSP_ERROR_IO;
239246
}
240247

241-
H5Dclose(dset);
242248
H5Sclose(fspace);
243-
return array;
249+
H5Dclose(dset);
250+
return BSP_SUCCESS;
244251
}
245252

246-
static inline void bsp_write_attribute(hid_t f, const char* label,
247-
const char* string) {
253+
static inline bsp_error_t bsp_write_attribute(hid_t f, const char* label,
254+
const char* string) {
248255
hid_t strtype = H5Tcopy(H5T_C_S1);
249256
H5Tset_size(strtype, strlen(string));
250257
H5Tset_cset(strtype, H5T_CSET_UTF8);
@@ -258,23 +265,28 @@ static inline void bsp_write_attribute(hid_t f, const char* label,
258265
H5Tclose(strtype);
259266
H5Aclose(attribute);
260267
H5Sclose(dataspace);
268+
269+
return BSP_SUCCESS;
261270
}
262271

263-
static inline char* bsp_read_attribute(hid_t f, const char* label) {
272+
static inline bsp_error_t bsp_read_attribute(char** string, hid_t f,
273+
const char* label) {
264274
hid_t attribute = H5Aopen(f, label, H5P_DEFAULT);
265-
hid_t strtype = H5Aget_type(attribute);
266275

267-
hid_t type_class = H5Tget_class(strtype);
268-
assert(type_class == H5T_STRING);
276+
if (attribute == H5I_INVALID_HID) {
277+
return BSP_ERROR_FORMAT;
278+
}
279+
280+
hid_t strtype = H5Aget_type(attribute);
269281

270282
size_t size = H5Tget_size(strtype);
271283

272-
char* string = (char*) malloc(size + 1);
284+
*string = (char*) malloc(size + 1);
273285

274-
H5Aread(attribute, strtype, string);
286+
H5Aread(attribute, strtype, *string);
275287

276288
H5Aclose(attribute);
277289
H5Tclose(strtype);
278290

279-
return string;
291+
return BSP_SUCCESS;
280292
}

include/binsparse/write_matrix.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ extern "C" {
1818
#ifdef BSP_USE_HDF5
1919
#include <hdf5.h>
2020

21-
int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json,
22-
int compression_level);
21+
bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
22+
cJSON* user_json, int compression_level);
2323
#endif
2424

25-
int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, const char* group,
26-
cJSON* user_json, int compression_level);
25+
bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
26+
const char* group, cJSON* user_json,
27+
int compression_level);
2728

2829
#ifdef __cplusplus
2930
}

0 commit comments

Comments
 (0)