|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Binsparse Developers |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <binsparse/binsparse.h> |
| 8 | + |
| 9 | +int check_array_equivalence(bsp_array_t array1, bsp_array_t array2) { |
| 10 | + if (array1.size != array2.size) { |
| 11 | + fprintf(stderr, "Array sizes do not match. %zu != %zu\n", array1.size, |
| 12 | + array2.size); |
| 13 | + return 1; |
| 14 | + } |
| 15 | + |
| 16 | + if (array1.size == 0) { |
| 17 | + return 0; |
| 18 | + } |
| 19 | + |
| 20 | + bsp_matrix_market_type_t mm_type1 = BSP_MM_REAL; |
| 21 | + |
| 22 | + if ((array1.type >= BSP_UINT8 && array1.type <= BSP_INT64) || |
| 23 | + array1.type == BSP_BINT8) { |
| 24 | + mm_type1 = BSP_MM_INTEGER; |
| 25 | + } else if (array1.type >= BSP_FLOAT32 && array1.type <= BSP_FLOAT64) { |
| 26 | + mm_type1 = BSP_MM_REAL; |
| 27 | + } else if (array1.type == BSP_COMPLEX_FLOAT32 || |
| 28 | + array1.type == BSP_COMPLEX_FLOAT64) { |
| 29 | + mm_type1 = BSP_MM_COMPLEX; |
| 30 | + } else { |
| 31 | + fprintf(stderr, "Unhandled array type.\n"); |
| 32 | + return 2; |
| 33 | + } |
| 34 | + |
| 35 | + bsp_matrix_market_type_t mm_type2 = BSP_MM_REAL; |
| 36 | + |
| 37 | + if ((array2.type >= BSP_UINT8 && array2.type <= BSP_INT64) || |
| 38 | + array2.type == BSP_BINT8) { |
| 39 | + mm_type2 = BSP_MM_INTEGER; |
| 40 | + } else if (array2.type >= BSP_FLOAT32 && array2.type <= BSP_FLOAT64) { |
| 41 | + mm_type2 = BSP_MM_REAL; |
| 42 | + } else if (array2.type == BSP_COMPLEX_FLOAT32 || |
| 43 | + array2.type == BSP_COMPLEX_FLOAT64) { |
| 44 | + mm_type2 = BSP_MM_COMPLEX; |
| 45 | + } else { |
| 46 | + fprintf(stderr, "Unhandled array type.\n"); |
| 47 | + return 2; |
| 48 | + } |
| 49 | + |
| 50 | + if (mm_type1 != mm_type2) { |
| 51 | + fprintf(stderr, "Array types do not match.\n"); |
| 52 | + return 3; |
| 53 | + } |
| 54 | + |
| 55 | + for (size_t i = 0; i < array1.size; i++) { |
| 56 | + if (mm_type1 == BSP_MM_INTEGER) { |
| 57 | + size_t value1, value2; |
| 58 | + bsp_array_read(array1, i, value1); |
| 59 | + bsp_array_read(array2, i, value2); |
| 60 | + |
| 61 | + if (value1 != value2) { |
| 62 | + fprintf(stderr, "Array values are not equal. (%zu != %zu)\n", value1, |
| 63 | + value2); |
| 64 | + return 4; |
| 65 | + } |
| 66 | + } else if (mm_type1 == BSP_MM_REAL) { |
| 67 | + double value1, value2; |
| 68 | + bsp_array_read(array1, i, value1); |
| 69 | + bsp_array_read(array2, i, value2); |
| 70 | + |
| 71 | + if (value1 != value2) { |
| 72 | + fprintf(stderr, "Array values are not equal. (%.17lg != %.17lg)\n", |
| 73 | + value1, value2); |
| 74 | + return 4; |
| 75 | + } |
| 76 | + } else if (mm_type1 == BSP_MM_COMPLEX) { |
| 77 | + double _Complex value1, value2; |
| 78 | + bsp_array_read(array1, i, value1); |
| 79 | + bsp_array_read(array2, i, value2); |
| 80 | + |
| 81 | + if (value1 != value2) { |
| 82 | + fprintf(stderr, |
| 83 | + "Array values are not equal. (%.17lg + i%.17lg != %.17lg + " |
| 84 | + "i%.17lg)\n", |
| 85 | + __real__ value1, __imag__ value1, __real__ value2, |
| 86 | + __imag__ value2); |
| 87 | + return 4; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +int main(int argc, char** argv) { |
| 96 | + if (argc < 3) { |
| 97 | + printf( |
| 98 | + "usage: ./check_equivalence [file1.{mtx/hdf5}] [file2.{mtx/hdf5}]\n"); |
| 99 | + |
| 100 | + printf(" Note: the second argument will be read in parallel if it is a " |
| 101 | + "Binsparse file.\n"); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + char* file1 = argv[1]; |
| 106 | + char* file2 = argv[2]; |
| 107 | + |
| 108 | + int num_threads = 6; |
| 109 | + |
| 110 | + bsp_fdataset_info_t info1 = bsp_parse_fdataset_string(argv[1]); |
| 111 | + bsp_fdataset_info_t info2 = bsp_parse_fdataset_string(argv[2]); |
| 112 | + |
| 113 | + printf("Matrix 1: %s and %s\n", info1.fname, |
| 114 | + (info1.dataset == NULL) ? "root" : info1.dataset); |
| 115 | + printf("Matrix 2: %s and %s\n", info2.fname, |
| 116 | + (info2.dataset == NULL) ? "root" : info2.dataset); |
| 117 | + |
| 118 | + fflush(stdout); |
| 119 | + |
| 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); |
| 123 | + |
| 124 | + bool perform_suitesparse_declamping = true; |
| 125 | + if (perform_suitesparse_declamping && |
| 126 | + strcmp(bsp_get_file_extension(file1), ".mtx") == 0) { |
| 127 | + bsp_matrix_declamp_values(matrix1); |
| 128 | + } |
| 129 | + |
| 130 | + if (perform_suitesparse_declamping && |
| 131 | + strcmp(bsp_get_file_extension(file2), ".mtx") == 0) { |
| 132 | + bsp_matrix_declamp_values(matrix2); |
| 133 | + } |
| 134 | + |
| 135 | + // If matrices are not the same format, try to convert. |
| 136 | + if (matrix1.format != matrix2.format) { |
| 137 | + if (matrix1.format != BSP_COOR) { |
| 138 | + bsp_matrix_t intermediate = bsp_convert_matrix(matrix1, BSP_COOR); |
| 139 | + bsp_destroy_matrix_t(matrix1); |
| 140 | + matrix1 = intermediate; |
| 141 | + } |
| 142 | + |
| 143 | + if (matrix2.format != BSP_COOR) { |
| 144 | + bsp_matrix_t intermediate = bsp_convert_matrix(matrix2, BSP_COOR); |
| 145 | + bsp_destroy_matrix_t(matrix2); |
| 146 | + matrix2 = intermediate; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if (matrix1.format != matrix2.format) { |
| 151 | + fprintf(stderr, "Formats do not match. (%s != %s)\n", |
| 152 | + bsp_get_matrix_format_string(matrix1.format), |
| 153 | + bsp_get_matrix_format_string(matrix2.format)); |
| 154 | + fprintf(stderr, "FAIL!\n"); |
| 155 | + return 1; |
| 156 | + } |
| 157 | + |
| 158 | + if (matrix1.structure != matrix2.structure) { |
| 159 | + fprintf(stderr, "Structures do not match. (%s != %s)\n", |
| 160 | + bsp_get_structure_string(matrix1.structure), |
| 161 | + bsp_get_structure_string(matrix2.structure)); |
| 162 | + fprintf(stderr, "FAIL!\n"); |
| 163 | + return 2; |
| 164 | + } |
| 165 | + |
| 166 | + if (matrix1.nrows != matrix2.nrows || matrix1.ncols != matrix2.ncols) { |
| 167 | + fprintf(stderr, "Dimensions do not match. (%zu, %zu) != (%zu, %zu)\n", |
| 168 | + matrix1.nrows, matrix1.ncols, matrix2.nrows, matrix2.ncols); |
| 169 | + fprintf(stderr, "FAIL!\n"); |
| 170 | + return 3; |
| 171 | + } |
| 172 | + |
| 173 | + if (matrix1.nnz != matrix2.nnz) { |
| 174 | + fprintf(stderr, "Number of stored values does not match. %zu != %zu\n", |
| 175 | + matrix1.nnz, matrix2.nnz); |
| 176 | + fprintf(stderr, "FAIL!\n"); |
| 177 | + return 4; |
| 178 | + } |
| 179 | + |
| 180 | + if (matrix1.is_iso != matrix2.is_iso) { |
| 181 | + fprintf(stderr, "ISO-ness does not match. %s != %s\n", |
| 182 | + (matrix1.is_iso) ? "true" : "false", |
| 183 | + (matrix2.is_iso) ? "true" : "false"); |
| 184 | + fprintf(stderr, "FAIL!\n"); |
| 185 | + return 5; |
| 186 | + } |
| 187 | + |
| 188 | + if (check_array_equivalence(matrix1.values, matrix2.values) != 0) { |
| 189 | + fprintf(stderr, "Value arrays not equivalent.\n"); |
| 190 | + fprintf(stderr, "FAIL!\n"); |
| 191 | + return 6; |
| 192 | + } |
| 193 | + |
| 194 | + if (check_array_equivalence(matrix1.indices_0, matrix2.indices_0) != 0) { |
| 195 | + fprintf(stderr, "Indices_0 arrays not equivalent.\n"); |
| 196 | + fprintf(stderr, "FAIL!\n"); |
| 197 | + return 7; |
| 198 | + } |
| 199 | + |
| 200 | + if (check_array_equivalence(matrix1.indices_1, matrix2.indices_1) != 0) { |
| 201 | + fprintf(stderr, "Indices_1 arrays not equivalent.\n"); |
| 202 | + fprintf(stderr, "FAIL!\n"); |
| 203 | + return 8; |
| 204 | + } |
| 205 | + |
| 206 | + if (check_array_equivalence(matrix1.pointers_to_1, matrix2.pointers_to_1) != |
| 207 | + 0) { |
| 208 | + fprintf(stderr, "Pointers_to_1 arrays not equivalent.\n"); |
| 209 | + fprintf(stderr, "FAIL!\n"); |
| 210 | + return 9; |
| 211 | + } |
| 212 | + |
| 213 | + printf("The files are equivalent.\n"); |
| 214 | + printf("OK!\n"); |
| 215 | + |
| 216 | + return 0; |
| 217 | +} |
0 commit comments