Skip to content

Commit fd94fd1

Browse files
committed
Update mtx2bsp to support multiple formats
1 parent 695b841 commit fd94fd1

File tree

5 files changed

+114
-40
lines changed

5 files changed

+114
-40
lines changed

examples/check_equivalence.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -80,39 +80,6 @@ int check_array_equivalence(bsp_array_t array1, bsp_array_t array2) {
8080
return 0;
8181
}
8282

83-
typedef struct {
84-
char* fname;
85-
char* dataset;
86-
} bsp_fdataset_info_t;
87-
88-
bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
89-
size_t len = strlen(str);
90-
91-
int split = -1;
92-
for (int i = len - 1; i >= 0; i--) {
93-
if (str[i] == ':') {
94-
split = i;
95-
break;
96-
}
97-
}
98-
99-
if (split == -1) {
100-
bsp_fdataset_info_t info;
101-
info.fname = (char*) malloc(sizeof(char) * (len + 1));
102-
strcpy(info.fname, str);
103-
info.dataset = NULL;
104-
return info;
105-
} else {
106-
bsp_fdataset_info_t info;
107-
info.fname = (char*) malloc(sizeof(char) * (split + 1));
108-
strncpy(info.fname, str, split);
109-
info.fname[split] = '\0';
110-
info.dataset = (char*) malloc(sizeof(char) * (len - split));
111-
strcpy(info.dataset, &str[split + 1]);
112-
return info;
113-
}
114-
}
115-
11683
int main(int argc, char** argv) {
11784
if (argc < 3) {
11885
printf(

examples/mtx2bsp.c

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@
44
int main(int argc, char** argv) {
55

66
if (argc < 3) {
7-
printf("usage: ./mtx2bsp [inputfile_name.mtx] [outputfile_name.bsp.hdf5] "
8-
"[optional: dataset]\n");
7+
printf("usage: ./mtx2bsp [input.mtx] [output.bsp.h5]:[optional: group] "
8+
"[optional: format]\n");
9+
printf("\n");
10+
printf("Description: Convert a Matrix Market file to a Binsparse HDF5 "
11+
"file.\n");
12+
printf(" Users can optionally provide an HDF5 group to store "
13+
"the\n");
14+
printf(" file in as well as a specific format. The default "
15+
"format\n");
16+
printf(" is row-sorted COO (COOR).\n");
17+
printf("\n");
18+
printf("example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5\n");
19+
printf(" - Convert Matrix Market file `chesapeake.mtx` to Binsparse "
20+
"HDF5 file `chesapeake.bsp.h5`.\n");
21+
printf(" - Matrix will be stored in root group.\n");
22+
printf(" - Matrix will be stored in COOR format.\n");
23+
printf("\n");
24+
printf("example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake\n");
25+
printf(" - Same as previous example, but matrix will be stored in "
26+
"HDF5 group `chesapeake`.\n");
27+
printf("\n");
28+
printf(
29+
"example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake CSR\n");
30+
printf(" - Same as previous example, but matrix will use CSR "
31+
"format.\n");
932
return 1;
1033
}
1134

@@ -15,16 +38,47 @@ int main(int argc, char** argv) {
1538
bool perform_suitesparse_declamping = true;
1639

1740
char* input_fname = argv[1];
18-
char* output_fname = argv[2];
1941

20-
char* group_name = NULL;
42+
bsp_fdataset_info_t info2 = bsp_parse_fdataset_string(argv[2]);
43+
char* output_fname = info2.fname;
44+
char* group_name = info2.dataset;
45+
46+
char* format_name = NULL;
2147

2248
if (argc >= 4) {
23-
group_name = argv[3];
49+
format_name = argv[3];
50+
}
51+
52+
char* input_file_extension = bsp_get_file_extension(input_fname);
53+
char* output_file_extension = bsp_get_file_extension(output_fname);
54+
55+
if (input_file_extension == NULL ||
56+
strcmp(input_file_extension, ".mtx") != 0) {
57+
fprintf(stderr,
58+
"error: input file \"%s\" is not a Matrix Market file. "
59+
"(Its extension is not '.mtx'.)\n",
60+
input_fname);
61+
return 1;
62+
}
63+
64+
if (output_file_extension == NULL ||
65+
(strcmp(output_file_extension, ".h5") != 0 &&
66+
strcmp(output_file_extension, ".hdf5") != 0)) {
67+
fprintf(stderr,
68+
"error: output file \"%s\" is not an HDF5 file. "
69+
"(Its extension is not '.h5' or '.hdf5'.)\n",
70+
output_fname);
71+
return 1;
2472
}
2573

2674
bsp_mm_metadata m = bsp_mmread_metadata(input_fname);
2775

76+
bsp_matrix_format_t format = BSP_COOR;
77+
if (format_name != NULL) {
78+
format = bsp_get_matrix_format(format_name);
79+
assert(format != 0);
80+
}
81+
2882
printf("%lu x %lu matrix with %lu nonzeros.\n", m.nrows, m.ncols, m.nnz);
2983
printf(
3084
"Matrix Market format is \"%s\" with type \"%s\" and structure \"%s\"\n",
@@ -52,6 +106,12 @@ int main(int argc, char** argv) {
52106

53107
matrix = bsp_matrix_minimize_values(matrix);
54108

109+
if (format != BSP_COOR) {
110+
bsp_matrix_t converted_matrix = bsp_convert_matrix(matrix, format);
111+
bsp_destroy_matrix_t(matrix);
112+
matrix = converted_matrix;
113+
}
114+
55115
bsp_print_matrix_info(matrix);
56116

57117
printf(" === Writing to %s... ===\n", output_fname);

include/binsparse/convert_matrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
bsp_matrix_t bsp_convert_matrix(bsp_matrix_t matrix,
77
bsp_matrix_format_t format) {
8-
// If already in desired format, do nothing.
8+
// Throw an error if matrix already in desired format.
99
if (matrix.format == format) {
10-
return matrix;
10+
assert(false);
1111
}
1212

1313
if (format == BSP_COOR) {

include/binsparse/detail/detail.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#pragma once
22

33
#include <binsparse/detail/declamp_values.h>
4+
#include <binsparse/detail/parse_dataset.h>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
#include <string.h>
4+
5+
typedef struct {
6+
char* fname;
7+
char* dataset;
8+
} bsp_fdataset_info_t;
9+
10+
bsp_fdataset_info_t bsp_parse_fdataset_string(char* str) {
11+
size_t len = strlen(str);
12+
13+
int split = -1;
14+
for (int i = len - 1; i >= 0; i--) {
15+
if (str[i] == ':') {
16+
split = i;
17+
break;
18+
}
19+
}
20+
21+
if (split == -1) {
22+
bsp_fdataset_info_t info;
23+
info.fname = (char*) malloc(sizeof(char) * (len + 1));
24+
strcpy(info.fname, str);
25+
info.dataset = NULL;
26+
return info;
27+
} else {
28+
bsp_fdataset_info_t info;
29+
info.fname = (char*) malloc(sizeof(char) * (split + 1));
30+
strncpy(info.fname, str, split);
31+
info.fname[split] = '\0';
32+
info.dataset = (char*) malloc(sizeof(char) * (len - split));
33+
strcpy(info.dataset, &str[split + 1]);
34+
return info;
35+
}
36+
}
37+
38+
char* bsp_get_file_extension(char* file_name) {
39+
int64_t len = strlen(file_name);
40+
for (int64_t i = len - 1; i >= 0; i--) {
41+
if (file_name[i] == '.') {
42+
return &file_name[i];
43+
}
44+
}
45+
return NULL;
46+
}

0 commit comments

Comments
 (0)