diff --git a/examples/cpp/benchmark_read.cpp b/examples/cpp/benchmark_read.cpp index 9c20b48..7ea978f 100644 --- a/examples/cpp/benchmark_read.cpp +++ b/examples/cpp/benchmark_read.cpp @@ -2,126 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include -#include -#include - -double gettime() { - struct timespec time; - clock_gettime(CLOCK_MONOTONIC, &time); - return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec; -} - -int compar(const void* a, const void* b) { - double x = *((const double*) a); - double y = *((const double*) b); - - double diff = x - y; - - if (diff > 0) { - return 1; - } else if (diff < 0) { - return -1; - } else { - return 0; - } -} - -double compute_variance(double* x, size_t n) { - double sum = 0; - - for (size_t i = 0; i < n; i++) { - sum += x[i]; - } - - double mean = sum / n; - - double sum_of_squares = 0; - for (size_t i = 0; i < n; i++) { - sum_of_squares += (x[i] - mean) * (x[i] - mean); - } - - return sum_of_squares / (n - 1); -} - -void flush_cache() { -#ifdef __APPLE__ - system("bash -c \"sync && sudo purge\""); -#elif __linux__ - system("bash -c \"sync\" && sudo echo 3 > /proc/sys/vm/drop_caches"); -#else - static_assert(false); -#endif -} - -int main(int argc, char** argv) { - if (argc < 2) { - fprintf(stderr, "usage: ./benchmark_read [file_name.h5]\n"); - return 1; - } - - char* file_name = argv[1]; - - printf("Opening %s\n", file_name); - - const int num_trials = 1; - - double durations[num_trials]; - - size_t nbytes = 0; - - // To flush the filesystem cache before each trial, change to `true`. - bool cold_cache = false; - - for (size_t i = 0; i < num_trials; i++) { - if (cold_cache) { - flush_cache(); - } - double begin = gettime(); - bsp_matrix_t mat = bsp_read_matrix(file_name, NULL); - double end = gettime(); - durations[i] = end - begin; - nbytes = bsp_matrix_nbytes(mat); - bsp_destroy_matrix_t(mat); - } - - printf("["); - for (size_t i = 0; i < num_trials; i++) { - printf("%lf", durations[i]); - if (i + 1 < num_trials) { - printf(", "); - } - } - printf("]\n"); - - qsort(durations, num_trials, sizeof(double), compar); - - double variance = compute_variance(durations, num_trials); - - double median_time = durations[num_trials / 2]; - - printf("Read file in %lf seconds\n", median_time); - - if (num_trials > 1) { - printf("Variance is %lf seconds, standard devication is %lf seconds\n", - variance, sqrt(variance)); - } - - double gbytes = ((double) nbytes) / 1024 / 1024 / 1024; - double gbytes_s = gbytes / median_time; - - printf("Achieved %lf GiB/s\n", gbytes_s); - - printf("["); - for (size_t i = 0; i < num_trials; i++) { - printf("%lf", durations[i]); - if (i + 1 < num_trials) { - printf(", "); - } - } - printf("]\n"); - - printf("FORPARSER: %s,%lf,%lf\n", file_name, median_time, gbytes_s); - - return 0; -} +#include "../benchmark_read.c" diff --git a/examples/cpp/benchmark_write.cpp b/examples/cpp/benchmark_write.cpp index 2bd5895..61c1a74 100644 --- a/examples/cpp/benchmark_write.cpp +++ b/examples/cpp/benchmark_write.cpp @@ -2,173 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include -#include -#include - -double gettime() { - struct timespec time; - clock_gettime(CLOCK_MONOTONIC, &time); - return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec; -} - -int compar(const void* a, const void* b) { - double x = *((const double*) a); - double y = *((const double*) b); - - double diff = x - y; - - if (diff > 0) { - return 1; - } else if (diff < 0) { - return -1; - } else { - return 0; - } -} - -double compute_variance(double* x, size_t n) { - double sum = 0; - - for (size_t i = 0; i < n; i++) { - sum += x[i]; - } - - double mean = sum / n; - - double sum_of_squares = 0; - for (size_t i = 0; i < n; i++) { - sum_of_squares += (x[i] - mean) * (x[i] - mean); - } - - return sum_of_squares / (n - 1); -} - -void flush_cache() { -#ifdef __APPLE__ - system("bash -c \"sync && sudo purge\""); -#elif __linux__ - system("bash -c \"sync\" && sudo echo 3 > /proc/sys/vm/drop_caches"); -#else - static_assert(false); -#endif -} - -void flush_writes() { -#ifdef __APPLE__ - system("bash -c \"sync\""); -#elif __linux__ - system("bash -c \"sync\""); -#else - static_assert(false); -#endif -} - -void delete_file(const char* file_name) { - char command[2048]; - snprintf(command, 2047, "rm %s", file_name); - system(command); -} - -int main(int argc, char** argv) { - if (argc < 2) { - fprintf(stderr, - "usage: ./benchmark_read [file_name.h5] [scratch_space] [optional: " - "compression_level]\n"); - return 1; - } - - char* file_name = argv[1]; - char* scratch_space = argv[2]; - - int compression_level = 0; - - if (argc >= 4) { - compression_level = atoi(argv[3]); - } - - printf("Opening %s\n", file_name); - - const int num_trials = 1; - - double durations[num_trials]; - - bsp_matrix_t mat = bsp_read_matrix(file_name, NULL); - size_t nbytes = bsp_matrix_nbytes(mat); - - char output_filename[2048]; - strncpy(output_filename, scratch_space, 2047); - strncpy(output_filename + strlen(scratch_space), "/benchmark_write_file_n.h5", - 2047 - strlen(scratch_space)); - - // Current output name logic does not do much. - assert(num_trials <= 10); - - // To flush the filesystem cache before each trial, change to `true`. - bool cold_cache = false; - - // To flush each write to the filesystem and include this in the timing, - // change to `true`. - bool flush_each_write = true; - - for (size_t i = 0; i < num_trials; i++) { - if (cold_cache) { - flush_cache(); - } - - output_filename[strlen(scratch_space) + 21] = '0' + i; - printf("Writing to file %s\n", output_filename); - - double begin = gettime(); - bsp_write_matrix(output_filename, mat, NULL, NULL, compression_level); - - if (flush_each_write) { - flush_writes(); - } - - double end = gettime(); - durations[i] = end - begin; - - delete_file(output_filename); - } - - printf("["); - for (size_t i = 0; i < num_trials; i++) { - printf("%lf", durations[i]); - if (i + 1 < num_trials) { - printf(", "); - } - } - printf("]\n"); - - qsort(durations, num_trials, sizeof(double), compar); - - double variance = compute_variance(durations, num_trials); - - double median_time = durations[num_trials / 2]; - - printf("Wrote file in %lf seconds\n", median_time); - - if (num_trials > 1) { - printf("Variance is %lf seconds, standard devication is %lf seconds\n", - variance, sqrt(variance)); - } - - double gbytes = ((double) nbytes) / 1024 / 1024 / 1024; - double gbytes_s = gbytes / median_time; - - printf("Achieved %lf GiB/s\n", gbytes_s); - - printf("["); - for (size_t i = 0; i < num_trials; i++) { - printf("%lf", durations[i]); - if (i + 1 < num_trials) { - printf(", "); - } - } - printf("]\n"); - - printf("FORPARSER: %s,%lf,%lf\n", file_name, median_time, gbytes_s); - - return 0; -} +#include "../benchmark_write.c" diff --git a/examples/cpp/bsp-ls.cpp b/examples/cpp/bsp-ls.cpp index 1143563..dad66db 100644 --- a/examples/cpp/bsp-ls.cpp +++ b/examples/cpp/bsp-ls.cpp @@ -2,108 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include - -herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo, - void* opdata); - -void print_group_info(hid_t g, const char* name) { - hid_t bsp_json; - - H5E_BEGIN_TRY { - bsp_json = H5Aopen(g, "binsparse", H5P_DEFAULT); - } - H5E_END_TRY; - - if (bsp_json != H5I_INVALID_HID) { - char* json_string = bsp_read_attribute(g, "binsparse"); - - cJSON* j = cJSON_Parse(json_string); - - assert(j != NULL); - assert(cJSON_IsObject(j)); - - cJSON* binsparse = cJSON_GetObjectItemCaseSensitive(j, "binsparse"); - assert(cJSON_IsObject(binsparse)); - - cJSON* version_ = cJSON_GetObjectItemCaseSensitive(binsparse, "version"); - - assert(version_ != NULL); - - assert(cJSON_IsString(version_)); - char* version_string = cJSON_GetStringValue(version_); - - // TODO: check version. - - cJSON* format_ = cJSON_GetObjectItemCaseSensitive(binsparse, "format"); - assert(format_ != NULL); - char* format_string = cJSON_GetStringValue(format_); - - cJSON* nnz_ = - cJSON_GetObjectItemCaseSensitive(binsparse, "number_of_stored_values"); - assert(nnz_ != NULL); - size_t nnz = cJSON_GetNumberValue(nnz_); - - cJSON* shape_ = cJSON_GetObjectItemCaseSensitive(binsparse, "shape"); - assert(shape_ != NULL); - - assert(cJSON_GetArraySize(shape_) == 2); - - cJSON* nrows_ = cJSON_GetArrayItem(shape_, 0); - assert(nrows_ != NULL); - - size_t nrows = cJSON_GetNumberValue(nrows_); - - cJSON* ncols_ = cJSON_GetArrayItem(shape_, 1); - assert(ncols_ != NULL); - - size_t ncols = cJSON_GetNumberValue(ncols_); - - char full_group_path[2048]; - size_t size = H5Iget_name(g, full_group_path, 2048); - - printf("Group \"%s\": Version %s Binsparse matrix. Format %s, %zu x %zu. " - "%zu stored values.\n", - full_group_path, version_string, format_string, nrows, ncols, nnz); - - cJSON* data_types = - cJSON_GetObjectItemCaseSensitive(binsparse, "data_types"); - assert(data_types != NULL); - - cJSON* item; - cJSON_ArrayForEach(item, data_types) { - printf(" %s: %s\n", item->string, cJSON_Print(item)); - } - } - - H5Literate(g, H5_INDEX_NAME, H5_ITER_INC, NULL, visit_group, NULL); -} - -herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo, - void* opdata) { - hid_t g = H5Oopen(loc_id, name, H5P_DEFAULT); - H5I_type_t type = H5Iget_type(g); - if (type == H5I_GROUP) { - print_group_info(g, name); - } - H5Oclose(g); - return 0; -} - -int main(int argc, char** argv) { - - if (argc < 2) { - printf("usage: ./bsp-ls [file_name.mtx]\n"); - return 1; - } - - char* fname = argv[1]; - - hid_t f = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT); - - print_group_info(f, "/"); - - H5Fclose(f); - - return 0; -} +#include "../bsp-ls.c" diff --git a/examples/cpp/bsp2mtx.cpp b/examples/cpp/bsp2mtx.cpp index bc59692..0e35eff 100644 --- a/examples/cpp/bsp2mtx.cpp +++ b/examples/cpp/bsp2mtx.cpp @@ -2,29 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include -#include - -int main(int argc, char** argv) { - - if (argc < 3) { - printf( - "usage: ./bsp2mtx [inputfile_name.mtx] [outputfile_name.bsp.hdf5]\n"); - return 1; - } - - char* input_fname = argv[1]; - char* output_fname = argv[2]; - - printf(" === Reading file... ===\n"); - bsp_matrix_t matrix = bsp_read_matrix(input_fname, NULL); - printf(" === Done writing. ===\n"); - - printf(" === Writing to %s... ===\n", output_fname); - bsp_mmwrite(output_fname, matrix); - printf(" === Done writing. ===\n"); - - bsp_destroy_matrix_t(matrix); - - return 0; -} +#include "../bsp2mtx.c" diff --git a/examples/cpp/check_equivalence.cpp b/examples/cpp/check_equivalence.cpp index 2d3ef85..05ae09c 100644 --- a/examples/cpp/check_equivalence.cpp +++ b/examples/cpp/check_equivalence.cpp @@ -2,206 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include - -int check_array_equivalence(bsp_array_t array1, bsp_array_t array2) { - if (array1.size != array2.size) { - fprintf(stderr, "Array sizes do not match. %zu != %zu\n", array1.size, - array2.size); - return 1; - } - - if (array1.size == 0) { - return 0; - } - - bsp_matrix_market_type_t mm_type1 = BSP_MM_REAL; - - if ((array1.type >= BSP_UINT8 && array1.type <= BSP_INT64) || - array1.type == BSP_BINT8) { - mm_type1 = BSP_MM_INTEGER; - } else if (array1.type >= BSP_FLOAT32 && array1.type <= BSP_FLOAT64) { - mm_type1 = BSP_MM_REAL; - } else if (array1.type == BSP_COMPLEX_FLOAT32 || - array1.type == BSP_COMPLEX_FLOAT64) { - mm_type1 = BSP_MM_COMPLEX; - } else { - fprintf(stderr, "Unhandled array type.\n"); - return 2; - } - - bsp_matrix_market_type_t mm_type2 = BSP_MM_REAL; - - if ((array2.type >= BSP_UINT8 && array2.type <= BSP_INT64) || - array2.type == BSP_BINT8) { - mm_type2 = BSP_MM_INTEGER; - } else if (array2.type >= BSP_FLOAT32 && array2.type <= BSP_FLOAT64) { - mm_type2 = BSP_MM_REAL; - } else if (array2.type == BSP_COMPLEX_FLOAT32 || - array2.type == BSP_COMPLEX_FLOAT64) { - mm_type2 = BSP_MM_COMPLEX; - } else { - fprintf(stderr, "Unhandled array type.\n"); - return 2; - } - - if (mm_type1 != mm_type2) { - fprintf(stderr, "Array types do not match.\n"); - return 3; - } - - for (size_t i = 0; i < array1.size; i++) { - if (mm_type1 == BSP_MM_INTEGER) { - size_t value1, value2; - bsp_array_read(array1, i, value1); - bsp_array_read(array2, i, value2); - - if (value1 != value2) { - fprintf(stderr, "Array values are not equal. (%zu != %zu)\n", value1, - value2); - return 4; - } - } else if (mm_type1 == BSP_MM_REAL) { - double value1, value2; - bsp_array_read(array1, i, value1); - bsp_array_read(array2, i, value2); - - if (value1 != value2) { - fprintf(stderr, "Array values are not equal. (%.17lg != %.17lg)\n", - value1, value2); - return 4; - } - } else if (mm_type1 == BSP_MM_COMPLEX) { - double _Complex value1, value2; - bsp_array_read(array1, i, value1); - bsp_array_read(array2, i, value2); - - if (value1 != value2) { - fprintf(stderr, - "Array values are not equal. (%.17lg + i%.17lg != %.17lg + " - "i%.17lg)\n", - __real__ value1, __imag__ value1, __real__ value2, - __imag__ value2); - return 4; - } - } - } - - return 0; -} - -int main(int argc, char** argv) { - if (argc < 3) { - printf( - "usage: ./check_equivalence [file1.{mtx/hdf5}] [file2.{mtx/hdf5}]\n"); - return 1; - } - - char* file1 = argv[1]; - char* file2 = argv[2]; - - bsp_fdataset_info_t info1 = bsp_parse_fdataset_string(argv[1]); - bsp_fdataset_info_t info2 = bsp_parse_fdataset_string(argv[2]); - - printf("Matrix 1: %s and %s\n", info1.fname, - (info1.dataset == NULL) ? "root" : info1.dataset); - printf("Matrix 2: %s and %s\n", info2.fname, - (info2.dataset == NULL) ? "root" : info2.dataset); - - bsp_matrix_t matrix1 = bsp_read_matrix(info1.fname, info1.dataset); - bsp_matrix_t matrix2 = bsp_read_matrix(info2.fname, info2.dataset); - - bool perform_suitesparse_declamping = true; - if (perform_suitesparse_declamping && - strcmp(bsp_get_file_extension(file1), ".mtx") == 0) { - bsp_matrix_declamp_values(matrix1); - } - - if (perform_suitesparse_declamping && - strcmp(bsp_get_file_extension(file2), ".mtx") == 0) { - bsp_matrix_declamp_values(matrix2); - } - - // If matrices are not the same format, try to convert. - if (matrix1.format != matrix2.format) { - if (matrix1.format != BSP_COOR) { - bsp_matrix_t intermediate = bsp_convert_matrix(matrix1, BSP_COOR); - bsp_destroy_matrix_t(matrix1); - matrix1 = intermediate; - } - - if (matrix2.format != BSP_COOR) { - bsp_matrix_t intermediate = bsp_convert_matrix(matrix2, BSP_COOR); - bsp_destroy_matrix_t(matrix2); - matrix2 = intermediate; - } - } - - if (matrix1.format != matrix2.format) { - fprintf(stderr, "Formats do not match. (%s != %s)\n", - bsp_get_matrix_format_string(matrix1.format), - bsp_get_matrix_format_string(matrix2.format)); - fprintf(stderr, "FAIL!\n"); - return 1; - } - - if (matrix1.structure != matrix2.structure) { - fprintf(stderr, "Structures do not match. (%s != %s)\n", - bsp_get_structure_string(matrix1.structure), - bsp_get_structure_string(matrix2.structure)); - fprintf(stderr, "FAIL!\n"); - return 2; - } - - if (matrix1.nrows != matrix2.nrows || matrix1.ncols != matrix2.ncols) { - fprintf(stderr, "Dimensions do not match. (%zu, %zu) != (%zu, %zu)\n", - matrix1.nrows, matrix1.ncols, matrix2.nrows, matrix2.ncols); - fprintf(stderr, "FAIL!\n"); - return 3; - } - - if (matrix1.nnz != matrix2.nnz) { - fprintf(stderr, "Number of stored values does not match. %zu != %zu\n", - matrix1.nnz, matrix2.nnz); - fprintf(stderr, "FAIL!\n"); - return 4; - } - - if (matrix1.is_iso != matrix2.is_iso) { - fprintf(stderr, "ISO-ness does not match. %s != %s\n", - (matrix1.is_iso) ? "true" : "false", - (matrix2.is_iso) ? "true" : "false"); - fprintf(stderr, "FAIL!\n"); - return 5; - } - - if (check_array_equivalence(matrix1.values, matrix2.values) != 0) { - fprintf(stderr, "Value arrays not equivalent.\n"); - fprintf(stderr, "FAIL!\n"); - return 6; - } - - if (check_array_equivalence(matrix1.indices_0, matrix2.indices_0) != 0) { - fprintf(stderr, "Indices_0 arrays not equivalent.\n"); - fprintf(stderr, "FAIL!\n"); - return 7; - } - - if (check_array_equivalence(matrix1.indices_1, matrix2.indices_1) != 0) { - fprintf(stderr, "Indices_1 arrays not equivalent.\n"); - fprintf(stderr, "FAIL!\n"); - return 8; - } - - if (check_array_equivalence(matrix1.pointers_to_1, matrix2.pointers_to_1) != - 0) { - fprintf(stderr, "Pointers_to_1 arrays not equivalent.\n"); - fprintf(stderr, "FAIL!\n"); - return 9; - } - - printf("The files are equivalent.\n"); - printf("OK!\n"); - - return 0; -} +#include "../check_equivalence.c" diff --git a/examples/cpp/mtx2bsp.cpp b/examples/cpp/mtx2bsp.cpp index 61e0496..221c26a 100644 --- a/examples/cpp/mtx2bsp.cpp +++ b/examples/cpp/mtx2bsp.cpp @@ -2,173 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include -#include - -#include - -double gettime() { - struct timespec time; - clock_gettime(CLOCK_MONOTONIC, &time); - return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec; -} - -int main(int argc, char** argv) { - - if (argc < 3) { - printf("usage: ./mtx2bsp [input.mtx] [output.bsp.h5]:[optional: group] " - "[optional: format] [optional: compression level 0-9]\n"); - printf("\n"); - printf("Description: Convert a Matrix Market file to a Binsparse HDF5 " - "file.\n"); - printf(" Users can optionally provide an HDF5 group to store " - "the\n"); - printf(" file in as well as a specific format. The default " - "format\n"); - printf(" is row-sorted COO (COOR).\n"); - printf("\n"); - printf("example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5\n"); - printf(" - Convert Matrix Market file `chesapeake.mtx` to Binsparse " - "HDF5 file `chesapeake.bsp.h5`.\n"); - printf(" - Matrix will be stored in root group.\n"); - printf(" - Matrix will be stored in COOR format.\n"); - printf("\n"); - printf("example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake\n"); - printf(" - Same as previous example, but matrix will be stored in " - "HDF5 group `chesapeake`.\n"); - printf("\n"); - printf( - "example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake CSR\n"); - printf(" - Same as previous example, but matrix will use CSR " - "format.\n"); - printf("example: ./mtx2bsp chesapeake.mtx chesapeake.bsp.h5:chesapeake CSR " - "5\n"); - printf(" - Same as previous example, but will use GZip compression " - "level 5.\n"); - printf(" 0 is no compression, 1-9 correspond to GZip compression " - "levels.\n"); - printf(" Default is 9.\n"); - return 1; - } - - // NOTE: this binary automatically performs "declamping" of floating point - // values greater/less than 1e308/-1e308 in order to restore inf values - // in SuiteSparse Matrix Collection matrices. - bool perform_suitesparse_declamping = true; - - char* input_fname = argv[1]; - - bsp_fdataset_info_t info2 = bsp_parse_fdataset_string(argv[2]); - char* output_fname = info2.fname; - char* group_name = info2.dataset; - - char* format_name = NULL; - - if (argc >= 4) { - format_name = argv[3]; - } - - int compression_level = 9; - - if (argc >= 5) { - compression_level = atoi(argv[4]); - } - - const char* input_file_extension = bsp_get_file_extension(input_fname); - const char* output_file_extension = bsp_get_file_extension(output_fname); - - if (input_file_extension == NULL || - strcmp(input_file_extension, ".mtx") != 0) { - fprintf(stderr, - "error: input file \"%s\" is not a Matrix Market file. " - "(Its extension is not '.mtx'.)\n", - input_fname); - return 1; - } - - if (output_file_extension == NULL || - (strcmp(output_file_extension, ".h5") != 0 && - strcmp(output_file_extension, ".hdf5") != 0)) { - fprintf(stderr, - "error: output file \"%s\" is not an HDF5 file. " - "(Its extension is not '.h5' or '.hdf5'.)\n", - output_fname); - return 1; - } - - bsp_mm_metadata m = bsp_mmread_metadata(input_fname); - - bsp_matrix_format_t format = BSP_COOR; - if (format_name != NULL) { - format = bsp_get_matrix_format(format_name); - assert(format != 0); - } - - printf("%lu x %lu matrix with %lu nonzeros.\n", m.nrows, m.ncols, m.nnz); - printf( - "Matrix Market format is \"%s\" with type \"%s\" and structure \"%s\"\n", - m.format, m.type, m.structure); - - if (strlen(m.comments) < 100) { - printf("File has comments:\n\"%s\"\n", m.comments); - } else { - printf("File has very long comments, not printing.\n"); - } - - printf("Printing with compression level %d.\n", compression_level); - - cJSON* user_json = cJSON_CreateObject(); - - assert(user_json != NULL); - - cJSON_AddStringToObject(user_json, "comment", m.comments); - - printf(" === Reading file... ===\n"); - double begin = gettime(); - bsp_matrix_t matrix = bsp_mmread(input_fname); - double end = gettime(); - printf(" === Done reading. ===\n"); - - double duration = end - begin; - printf("%lf seconds reading Matrix Market file...\n", duration); - - if (perform_suitesparse_declamping) { - begin = gettime(); - bsp_matrix_declamp_values(matrix); - end = gettime(); - duration = end - begin; - printf("%lf seconds declamping...\n", duration); - } - - begin = gettime(); - matrix = bsp_matrix_minimize_values(matrix); - end = gettime(); - duration = end - begin; - printf("%lf seconds minimizing values...\n", duration); - - if (format != BSP_COOR) { - begin = gettime(); - bsp_matrix_t converted_matrix = bsp_convert_matrix(matrix, format); - bsp_destroy_matrix_t(matrix); - matrix = converted_matrix; - end = gettime(); - duration = end - begin; - printf("%lf seconds converting to %s format...\n", duration, - bsp_get_matrix_format_string(format)); - } - - bsp_print_matrix_info(matrix); - - printf(" === Writing to %s... ===\n", output_fname); - begin = gettime(); - bsp_write_matrix(output_fname, matrix, group_name, user_json, - compression_level); - end = gettime(); - duration = end - begin; - printf("%lf seconds writing Binsparse file...\n", duration); - printf(" === Done writing. ===\n"); - - bsp_destroy_matrix_t(matrix); - - return 0; -} +#include "../mtx2bsp.c" diff --git a/examples/cpp/simple_matrix_read.cpp b/examples/cpp/simple_matrix_read.cpp index 586955c..898ac99 100644 --- a/examples/cpp/simple_matrix_read.cpp +++ b/examples/cpp/simple_matrix_read.cpp @@ -2,22 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include - -int main(int argc, char** argv) { - const char* file_name = "test.hdf5"; - - bsp_matrix_t mat = bsp_read_matrix(file_name, NULL); - - if (mat.format == BSP_COO) { - float* values = (float*) mat.values.data; - int* rowind = (int*) mat.indices_0.data; - int* colind = (int*) mat.indices_1.data; - - for (size_t i = 0; i < mat.nnz; i++) { - printf("%d, %d: %f\n", rowind[i], colind[i], values[i]); - } - } - - return 0; -} +#include "../simple_matrix_read.c" diff --git a/examples/cpp/simple_matrix_write.cpp b/examples/cpp/simple_matrix_write.cpp index a12965e..96ea3ea 100644 --- a/examples/cpp/simple_matrix_write.cpp +++ b/examples/cpp/simple_matrix_write.cpp @@ -2,24 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include - -int main(int argc, char** argv) { - size_t m = 1000; - size_t n = 1000; - size_t nnz = 100; - - bsp_matrix_t mat = bsp_generate_coo(m, n, nnz, BSP_FLOAT32, BSP_INT32); - - float* values = (float*) mat.values.data; - int* rowind = (int*) mat.indices_0.data; - int* colind = (int*) mat.indices_1.data; - - for (size_t i = 0; i < nnz; i++) { - printf("%d, %d: %f\n", rowind[i], colind[i], values[i]); - } - - bsp_write_matrix("test.hdf5", mat, NULL, NULL, 9); - - return 0; -} +#include "../simple_matrix_write.c" diff --git a/examples/cpp/simple_read.cpp b/examples/cpp/simple_read.cpp index 2ab552f..3425ded 100644 --- a/examples/cpp/simple_read.cpp +++ b/examples/cpp/simple_read.cpp @@ -2,23 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include - -int main(int argc, char** argv) { - const char* file_name = "test.hdf5"; - - hid_t f = H5Fopen(file_name, H5F_ACC_RDWR, H5P_DEFAULT); - - bsp_array_t array = bsp_read_array(f, "test"); - - int* values = (int*) array.data; - - for (size_t i = 0; i < array.size; i++) { - printf("%lu: %d\n", i, values[i]); - } - - bsp_destroy_array_t(array); - H5Fclose(f); - - return 0; -} +#include "../simple_read.c" diff --git a/examples/cpp/simple_write.cpp b/examples/cpp/simple_write.cpp index e016073..e021683 100644 --- a/examples/cpp/simple_write.cpp +++ b/examples/cpp/simple_write.cpp @@ -2,25 +2,4 @@ // // SPDX-License-Identifier: BSD-3-Clause -#include - -int main(int argc, char** argv) { - const char* file_name = "test.hdf5"; - - hid_t f = H5Fcreate(file_name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); - - bsp_array_t array = bsp_construct_array_t(1000, BSP_INT32); - - int* values = (int*) array.data; - - for (size_t i = 0; i < array.size; i++) { - bsp_array_write(array, i, i); - } - - bsp_write_array(f, "test", array, 0); - - H5Fclose(f); - bsp_destroy_array_t(array); - - return 0; -} +#include "../simple_write.c"