Skip to content

Commit

Permalink
Fix add matrix test
Browse files Browse the repository at this point in the history
The tolerance in single precision was too tight.

Signed-off-by: Nicolas Bock <[email protected]>
  • Loading branch information
nicolasbock committed Aug 22, 2019
1 parent 6a75f78 commit efbfdc1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions tests/C-tests/add_matrix_typed.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <stdlib.h>
#include <stdio.h>
#if defined(SINGLE_REAL) || defined(SINGLE_COMPLEX)
#define REL_TOL 1e-6
#define REL_TOL 2e-6
#else
#define REL_TOL 1e-12
#endif
Expand All @@ -30,33 +30,44 @@ int TYPED_FUNC(
double beta = 0.8;
double threshold = 0.0;

LOG_DEBUG("rel. tolerance = %e\n", REL_TOL);
LOG_INFO("rel. tolerance = %e\n", REL_TOL);

A = bml_random_matrix(matrix_type, matrix_precision, N, M, sequential);
B = bml_copy_new(A);
C = bml_random_matrix(matrix_type, matrix_precision, N, M, sequential);

LOG_INFO("A\n");
bml_print_bml_matrix(A, 0, N, 0, N);
LOG_INFO("B\n");
bml_print_bml_matrix(B, 0, N, 0, N);
LOG_INFO("C\n");
bml_print_bml_matrix(C, 0, N, 0, N);

bml_add(B, C, alpha, beta, threshold);

A_dense = bml_export_to_dense(A, dense_row_major);
B_dense = bml_export_to_dense(B, dense_row_major);
C_dense = bml_export_to_dense(C, dense_row_major);

LOG_INFO("A_dense\n");
bml_print_dense_matrix(N, matrix_precision, dense_row_major, A_dense, 0,
N, 0, N);
LOG_INFO("B_dense\n");
bml_print_dense_matrix(N, matrix_precision, dense_row_major, B_dense, 0,
N, 0, N);
LOG_INFO("C_dense\n");
bml_print_dense_matrix(N, matrix_precision, dense_row_major, C_dense, 0,
N, 0, N);
for (int i = 0; i < N * N; i++)
{
double expected = alpha * A_dense[i] + beta * C_dense[i];
double rel_diff_val = (expected - B_dense[i]) / expected;
double rel_diff = fabs(rel_diff_val);
double abs_diff = fabs(expected - B_dense[i]);
double rel_diff = abs_diff / expected;
if (rel_diff > REL_TOL)
{
LOG_ERROR
("matrices are not identical; expected[%d] = %e, B[%d] = %e\n",
i, expected, i, B_dense[i]);
("matrices are not identical; expected[%d] = %e, B[%d] = %e, abs. diff = %e, |rel. diff| = %e\n",
i, expected, i, B_dense[i], abs_diff, rel_diff);
return -1;
}
}
Expand Down

0 comments on commit efbfdc1

Please sign in to comment.