|
8 | 8 | #include <sys/resource.h> |
9 | 9 | #include <time.h> |
10 | 10 | #include <unistd.h> |
| 11 | +#include <math.h> |
11 | 12 |
|
12 | 13 | #include "common.h" |
13 | 14 |
|
@@ -62,6 +63,15 @@ static int read_kernel_file(const char *filename, uint8_t **data, |
62 | 63 | return 0; |
63 | 64 | } |
64 | 65 |
|
| 66 | +static bool float_compare(float f1, float f2) |
| 67 | +{ |
| 68 | + static constexpr auto epsilon = 1.0e-06f; |
| 69 | + |
| 70 | + if (abs(f1 - f2) <= epsilon) |
| 71 | + return true; |
| 72 | + return abs(f1 - f2) <= epsilon * fmax(abs(f1), abs(f2)); |
| 73 | +} |
| 74 | + |
65 | 75 | static void sgemm_cpu(float *C, const float *A, const float *B, int M, int N, |
66 | 76 | int K) { |
67 | 77 | for (int m = 0; m < M; ++m) { |
@@ -172,9 +182,9 @@ int main(int argc, char **argv) { |
172 | 182 | } |
173 | 183 | srand(time(NULL)); |
174 | 184 | for (int i = 0; i < M * K; i++) |
175 | | - A[i] = (int)((float)rand() / (float)RAND_MAX); |
| 185 | + A[i] = ((float)rand() / (float)RAND_MAX); |
176 | 186 | for (int i = 0; i < N * K; i++) |
177 | | - B[i] = (int)((float)rand() / (float)RAND_MAX); |
| 187 | + B[i] = ((float)rand() / (float)RAND_MAX); |
178 | 188 |
|
179 | 189 | // create buffers |
180 | 190 | a_memobj = |
@@ -265,7 +275,7 @@ int main(int argc, char **argv) { |
265 | 275 | int errors = 0; |
266 | 276 |
|
267 | 277 | for (size_t i = 0; i < size_t(M * N); i++) |
268 | | - if (C_cpu[i] != C[i]) |
| 278 | + if (!float_compare(C_cpu[i], C[i])) |
269 | 279 | errors++; |
270 | 280 | if (errors != 0) |
271 | 281 | printf("FAILED! - %d errors\n", errors); |
|
0 commit comments