44int 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 );
0 commit comments