Skip to content

Commit

Permalink
Add tests for bml_add_norm
Browse files Browse the repository at this point in the history
Closes: lanl#531
Signed-off-by: Nicolas Bock <[email protected]>
  • Loading branch information
nicolasbock committed Jul 15, 2021
1 parent eaae21f commit 99e5568
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions tests/C-tests/add_matrix_typed.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ int TYPED_FUNC(
const int M)
{
bml_matrix_t *A = NULL;
bml_matrix_t *B = NULL;
bml_matrix_t *B_1 = NULL;
bml_matrix_t *B_2 = NULL;
bml_matrix_t *C = NULL;

REAL_T *A_dense = NULL;
REAL_T *B_dense = NULL;
REAL_T *B_1_dense = NULL;
REAL_T *B_2_dense = NULL;
REAL_T *C_dense = NULL;

bml_distribution_mode_t distrib_mode = sequential;
Expand All @@ -39,23 +41,29 @@ int TYPED_FUNC(
double alpha = 1.2;
double beta = 0.8;
double threshold = 0.0;
double trace_norm = 0.0;

int max_row = MIN(N, PRINT_THRESHOLD);
int max_col = MIN(N, PRINT_THRESHOLD);

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

A = bml_random_matrix(matrix_type, matrix_precision, N, M, distrib_mode);
B = bml_copy_new(A);
B_1 = bml_copy_new(A);
B_2 = bml_copy_new(A);
C = bml_random_matrix(matrix_type, matrix_precision, N, M, distrib_mode);

LOG_DEBUG("bml_add...\n");
bml_add(B, C, alpha, beta, threshold);
bml_add(B_1, C, alpha, beta, threshold);

LOG_DEBUG("bml_add_norm...\n");
trace_norm = bml_add_norm(B_2, C, alpha, beta, threshold);

LOG_INFO("alpha = %f \n ", alpha);
LOG_INFO("beta = %f \n ", beta);
A_dense = bml_export_to_dense(A, dense_row_major);
B_dense = bml_export_to_dense(B, dense_row_major);
B_1_dense = bml_export_to_dense(B_1, dense_row_major);
B_2_dense = bml_export_to_dense(B_2, dense_row_major);
C_dense = bml_export_to_dense(C, dense_row_major);

if (bml_getMyRank() == 0)
Expand All @@ -67,29 +75,43 @@ int TYPED_FUNC(
bml_print_dense_matrix(N, matrix_precision, dense_row_major, C_dense,
0, max_row, 0, max_col);
LOG_INFO("B = alpha A + beta C \n");
bml_print_dense_matrix(N, matrix_precision, dense_row_major, B_dense,
bml_print_dense_matrix(N, matrix_precision, dense_row_major, B_1_dense,
0, max_row, 0, max_col);

double expected_trace_norm = 0.0;
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_val = (expected - B_1_dense[i]) / expected;
double rel_diff = fabs(rel_diff_val);
if (rel_diff > REL_TOL)
{
LOG_ERROR
("matrices are not identical; expected[%d] = %e, B[%d] = %e\n",
i, expected, i, B_dense[i]);
i, expected, i, B_1_dense[i]);
return -1;
}
expected_trace_norm += expected * expected;
}
expected_trace_norm = sqrt(expected_trace_norm);
double rel_diff_val = (expected_trace_norm - trace_norm) / expected_trace_norm;
double rel_diff = fabs(rel_diff_val);
if (rel_diff > REL_TOL)
{
LOG_ERROR
("trace norm is incorrect; expected = %e but found = %e\n",
expected_trace_norm, trace_norm);
return -1;
}
bml_free_memory(A_dense);
bml_free_memory(B_dense);
bml_free_memory(B_1_dense);
bml_free_memory(B_2_dense);
bml_free_memory(C_dense);
}

bml_deallocate(&A);
bml_deallocate(&B);
bml_deallocate(&B_1);
bml_deallocate(&B_2);
bml_deallocate(&C);

LOG_INFO("add matrix test passed\n");
Expand Down

0 comments on commit 99e5568

Please sign in to comment.