Skip to content

Commit ba2fe4a

Browse files
committed
Support variable compression levels
1 parent 0100ebb commit ba2fe4a

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

examples/mtx2bsp.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ int main(int argc, char** argv) {
55

66
if (argc < 3) {
77
printf("usage: ./mtx2bsp [input.mtx] [output.bsp.h5]:[optional: group] "
8-
"[optional: format]\n");
8+
"[optional: format] [optional: compression level 0-9]\n");
99
printf("\n");
1010
printf("Description: Convert a Matrix Market file to a Binsparse HDF5 "
1111
"file.\n");
@@ -29,6 +29,13 @@ int main(int argc, char** argv) {
2929
"example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake CSR\n");
3030
printf(" - Same as previous example, but matrix will use CSR "
3131
"format.\n");
32+
printf("example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake CSR "
33+
"5\n");
34+
printf(" - Same as previous example, but will use GZip compression "
35+
"level 5.\n");
36+
printf(" 0 is no compression, 1-9 correspond to GZip compression "
37+
"levels.\n");
38+
printf(" Default is 9.\n");
3239
return 1;
3340
}
3441

@@ -49,6 +56,12 @@ int main(int argc, char** argv) {
4956
format_name = argv[3];
5057
}
5158

59+
int compression_level = 9;
60+
61+
if (argc >= 5) {
62+
compression_level = atoi(argv[4]);
63+
}
64+
5265
char* input_file_extension = bsp_get_file_extension(input_fname);
5366
char* output_file_extension = bsp_get_file_extension(output_fname);
5467

@@ -90,6 +103,8 @@ int main(int argc, char** argv) {
90103
printf("File has very long comments, not printing.\n");
91104
}
92105

106+
printf("Printing with compression level %d.\n", compression_level);
107+
93108
cJSON* user_json = cJSON_CreateObject();
94109

95110
assert(user_json != NULL);
@@ -115,7 +130,8 @@ int main(int argc, char** argv) {
115130
bsp_print_matrix_info(matrix);
116131

117132
printf(" === Writing to %s... ===\n", output_fname);
118-
bsp_write_matrix(output_fname, matrix, group_name, user_json);
133+
bsp_write_matrix(output_fname, matrix, group_name, user_json,
134+
compression_level);
119135
printf(" === Done writing. ===\n");
120136

121137
bsp_destroy_matrix_t(matrix);

examples/simple_matrix_write.c

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

18-
bsp_write_matrix("test.hdf5", mat, NULL, NULL);
18+
bsp_write_matrix("test.hdf5", mat, NULL, NULL, 9);
1919

2020
return 0;
2121
}

examples/simple_write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int main(int argc, char** argv) {
1313
bsp_array_write(array, i, i);
1414
}
1515

16-
bsp_write_array(f, "test", array);
16+
bsp_write_array(f, "test", array, 0);
1717

1818
H5Fclose(f);
1919
bsp_destroy_array_t(array);

include/binsparse/hdf5_wrapper.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
// Write an array to a dataset / file
99
// Returns 0 on success, nonzero on error.
10-
int bsp_write_array(hid_t f, char* label, bsp_array_t array) {
10+
int bsp_write_array(hid_t f, char* label, bsp_array_t array,
11+
int compression_level) {
1112
if (array.type == BSP_COMPLEX_FLOAT32 || array.type == BSP_COMPLEX_FLOAT64) {
1213
array = bsp_complex_array_to_fp(array);
1314
}
@@ -28,7 +29,9 @@ int bsp_write_array(hid_t f, char* label, bsp_array_t array) {
2829

2930
H5Pset_chunk(dcpl, 1, (hsize_t[]){chunk_size});
3031

31-
H5Pset_deflate(dcpl, 9);
32+
if (compression_level > 0) {
33+
H5Pset_deflate(dcpl, compression_level);
34+
}
3235

3336
hid_t dset =
3437
H5Dcreate2(f, label, hdf5_standard_type, fspace, lcpl, dcpl, H5P_DEFAULT);

include/binsparse/write_matrix.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,32 @@ char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
8181
return string;
8282
}
8383

84-
int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json) {
85-
int result = bsp_write_array(f, "values", matrix.values);
84+
int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json,
85+
int compression_level) {
86+
int result = bsp_write_array(f, "values", matrix.values, compression_level);
8687

8788
if (result != 0)
8889
return result;
8990

9091
if (matrix.indices_0.size > 0) {
91-
result = bsp_write_array(f, "indices_0", matrix.indices_0);
92+
result =
93+
bsp_write_array(f, "indices_0", matrix.indices_0, compression_level);
9294
if (result != 0) {
9395
return result;
9496
}
9597
}
9698

9799
if (matrix.indices_1.size > 0) {
98-
result = bsp_write_array(f, "indices_1", matrix.indices_1);
100+
result =
101+
bsp_write_array(f, "indices_1", matrix.indices_1, compression_level);
99102
if (result != 0) {
100103
return result;
101104
}
102105
}
103106

104107
if (matrix.pointers_to_1.size > 0) {
105-
result = bsp_write_array(f, "pointers_to_1", matrix.pointers_to_1);
108+
result = bsp_write_array(f, "pointers_to_1", matrix.pointers_to_1,
109+
compression_level);
106110
if (result != 0) {
107111
return result;
108112
}
@@ -117,10 +121,10 @@ int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json) {
117121
}
118122

119123
int bsp_write_matrix(char* fname, bsp_matrix_t matrix, char* group,
120-
cJSON* user_json) {
124+
cJSON* user_json, int compression_level) {
121125
if (group == NULL) {
122126
hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
123-
bsp_write_matrix_to_group(f, matrix, user_json);
127+
bsp_write_matrix_to_group(f, matrix, user_json, compression_level);
124128
H5Fclose(f);
125129
} else {
126130
hid_t f;
@@ -130,7 +134,7 @@ int bsp_write_matrix(char* fname, bsp_matrix_t matrix, char* group,
130134
f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
131135
}
132136
hid_t g = H5Gcreate1(f, group, H5P_DEFAULT);
133-
bsp_write_matrix_to_group(g, matrix, user_json);
137+
bsp_write_matrix_to_group(g, matrix, user_json, compression_level);
134138
H5Gclose(g);
135139
H5Fclose(f);
136140
}

0 commit comments

Comments
 (0)