From 99e5568e3643ddd261c8f26bc062a959e7947a65 Mon Sep 17 00:00:00 2001 From: Nicolas Bock Date: Thu, 15 Jul 2021 06:42:23 +0200 Subject: [PATCH] Add tests for `bml_add_norm` Closes: lanl/bml#531 Signed-off-by: Nicolas Bock --- tests/C-tests/add_matrix_typed.c | 42 ++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/tests/C-tests/add_matrix_typed.c b/tests/C-tests/add_matrix_typed.c index 576b76395..ad0cd5242 100644 --- a/tests/C-tests/add_matrix_typed.c +++ b/tests/C-tests/add_matrix_typed.c @@ -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; @@ -39,6 +41,7 @@ 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); @@ -46,16 +49,21 @@ int TYPED_FUNC( 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) @@ -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");