Skip to content

Commit 098be75

Browse files
committed
Commit file
1 parent e72f602 commit 098be75

File tree

1 file changed

+215
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)