Skip to content

Commit b37c626

Browse files
committed
Add error handling for Binsparse matrix IO functions.
1 parent 13a5471 commit b37c626

14 files changed

+155
-110
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ This library provides a C interface for reading and writing binsparse matrices.
1818
#include <binsparse/binsparse.h>
1919

2020
int main(int argc, char** argv) {
21-
bsp_matrix_t mat = bsp_read_matrix("chesapeake.bsp.hdf5");
21+
bsp_matrix_t mat;
22+
bsp_read_matrix(&mat, "chesapeake.bsp.hdf5", NULL);
2223

2324
if (mat.format == BSP_COO) {
2425
float* values = mat.values.data;
@@ -33,6 +34,7 @@ int main(int argc, char** argv) {
3334
bsp_get_matrix_format_string(mat.format));
3435
}
3536

37+
bsp_destroy_matrix_t(&mat);
3638
return 0;
3739
}
3840
```

examples/benchmark_read.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ int main(int argc, char** argv) {
7979

8080
// If running warm cache experiments, read once to warm cache.
8181
if (!cold_cache) {
82-
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
82+
bsp_matrix_t mat;
83+
BSP_CHECK(bsp_read_matrix(&mat, file_name, NULL));
8384
bsp_destroy_matrix_t(&mat);
8485
}
8586

@@ -88,7 +89,8 @@ int main(int argc, char** argv) {
8889
flush_cache();
8990
}
9091
double begin = gettime();
91-
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
92+
bsp_matrix_t mat;
93+
BSP_CHECK(bsp_read_matrix(&mat, file_name, NULL));
9294
double end = gettime();
9395
durations[i] = end - begin;
9496
nbytes = bsp_matrix_nbytes(mat);

examples/benchmark_read_parallel.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ int main(int argc, char** argv) {
8282

8383
// If running warm cache experiments, read once to warm cache.
8484
if (!cold_cache) {
85-
bsp_matrix_t mat = bsp_read_matrix_parallel(file_name, NULL, num_threads);
85+
bsp_matrix_t mat;
86+
BSP_CHECK(bsp_read_matrix_parallel(&mat, file_name, NULL, num_threads));
8687
bsp_destroy_matrix_t(&mat);
8788
}
8889

@@ -92,7 +93,8 @@ int main(int argc, char** argv) {
9293
}
9394
fflush(stdout);
9495
double begin = gettime();
95-
bsp_matrix_t mat = bsp_read_matrix_parallel(file_name, NULL, num_threads);
96+
bsp_matrix_t mat;
97+
BSP_CHECK(bsp_read_matrix_parallel(&mat, file_name, NULL, num_threads));
9698
double end = gettime();
9799
durations[i] = end - begin;
98100
nbytes = bsp_matrix_nbytes(mat);

examples/benchmark_write.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ int main(int argc, char** argv) {
9797

9898
double durations[num_trials];
9999

100-
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
100+
bsp_matrix_t mat;
101+
BSP_CHECK(bsp_read_matrix(&mat, file_name, NULL));
101102
size_t nbytes = bsp_matrix_nbytes(mat);
102103

103104
char output_filename[2048];
@@ -124,7 +125,8 @@ int main(int argc, char** argv) {
124125
printf("Writing to file %s\n", output_filename);
125126

126127
double begin = gettime();
127-
bsp_write_matrix(output_filename, mat, NULL, NULL, compression_level);
128+
BSP_CHECK(
129+
bsp_write_matrix(output_filename, mat, NULL, NULL, compression_level));
128130

129131
if (flush_each_write) {
130132
flush_writes();

examples/bsp2mtx.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ int main(int argc, char** argv) {
1919
char* output_fname = argv[2];
2020

2121
printf(" === Reading file... ===\n");
22-
bsp_matrix_t matrix = bsp_read_matrix(input_fname, NULL);
23-
printf(" === Done writing. ===\n");
22+
bsp_matrix_t matrix;
23+
BSP_CHECK(bsp_read_matrix(&matrix, input_fname, NULL));
24+
printf(" === Done reading. ===\n");
2425
if (matrix.format != BSP_COO) {
2526
matrix = bsp_convert_matrix(matrix, BSP_COO);
2627
}

examples/check_equivalence.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ int main(int argc, char** argv) {
110110
printf("Matrix 2: %s and %s\n", info2.fname,
111111
(info2.dataset == NULL) ? "root" : info2.dataset);
112112

113-
bsp_matrix_t matrix1 = bsp_read_matrix(info1.fname, info1.dataset);
114-
bsp_matrix_t matrix2 = bsp_read_matrix(info2.fname, info2.dataset);
113+
bsp_matrix_t matrix1, matrix2;
114+
BSP_CHECK(bsp_read_matrix(&matrix1, info1.fname, info1.dataset));
115+
BSP_CHECK(bsp_read_matrix(&matrix2, info2.fname, info2.dataset));
115116

116117
bool perform_suitesparse_declamping = true;
117118
if (perform_suitesparse_declamping &&

examples/check_equivalence_parallel.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ int main(int argc, char** argv) {
117117

118118
fflush(stdout);
119119

120-
bsp_matrix_t matrix1 = bsp_read_matrix(info1.fname, info1.dataset);
121-
bsp_matrix_t matrix2 =
122-
bsp_read_matrix_parallel(info2.fname, info2.dataset, num_threads);
120+
bsp_matrix_t matrix1, matrix2;
121+
BSP_CHECK(bsp_read_matrix(&matrix1, info1.fname, info1.dataset));
122+
BSP_CHECK(bsp_read_matrix_parallel(&matrix2, info2.fname, info2.dataset,
123+
num_threads));
123124

124125
bool perform_suitesparse_declamping = true;
125126
if (perform_suitesparse_declamping &&

examples/mtx2bsp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ int main(int argc, char** argv) {
163163

164164
printf(" === Writing to %s... ===\n", output_fname);
165165
begin = gettime();
166-
bsp_write_matrix(output_fname, matrix, group_name, user_json,
167-
compression_level);
166+
BSP_CHECK(bsp_write_matrix(output_fname, matrix, group_name, user_json,
167+
compression_level));
168168
end = gettime();
169169
duration = end - begin;
170170
printf("%lf seconds writing Binsparse file...\n", duration);

examples/simple_matrix_read.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
int main(int argc, char** argv) {
1010
const char* file_name = "test.hdf5";
1111

12-
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
12+
bsp_matrix_t mat;
13+
BSP_CHECK(bsp_read_matrix(&mat, file_name, NULL));
1314

1415
if (mat.format == BSP_COO) {
1516
float* values = (float*) mat.values.data;

examples/simple_matrix_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(int argc, char** argv) {
2121
printf("%d, %d: %f\n", rowind[i], colind[i], values[i]);
2222
}
2323

24-
bsp_write_matrix("test.hdf5", mat, NULL, NULL, 9);
24+
BSP_CHECK(bsp_write_matrix("test.hdf5", mat, NULL, NULL, 9));
2525

2626
return 0;
2727
}

0 commit comments

Comments
 (0)