LCOV - code coverage report
Current view: top level - tests/linalg - t_matrix_arr_f90.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 88 88 100.0 %
Date: 2024-05-13 05:06:06 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*************************************************************************
       2             :  * Testing Mtx class operations
       3             :  ************************************************************************/
       4             : #include "t_matrix.hpp"
       5             : #include <chrono>
       6             : #include <cmath>
       7             : #include <cstdint>
       8             : #include <gtest/gtest.h>
       9             : #include <iostream>
      10             : #include <limits.h>
      11             : #include <openGPMP/linalg/mtx.hpp>
      12             : #include <openGPMP/linalg/mtx_tmpl.hpp>
      13             : #include <string>
      14             : #include <vector>
      15             : 
      16             : using namespace gpmp;
      17             : #define TEST_COUT std::cerr << "\033[32m[          ] [ INFO ] \033[0m"
      18             : #define INFO_COUT                                                              \
      19             :     std::cerr << "\033[32m[          ] [ INFO ] \033[0m\033[1;34m\033[1m"
      20             : namespace {
      21             : 
      22           4 : TEST(FORTRAN90MatrixArrayTestI32, AdditionPerformanceComparison) {
      23           1 :     INFO_COUT << "MATRIX (as FORTRAN Arrays) INT32" << std::endl;
      24             : 
      25           1 :     int mtx_size = 1024;
      26           1 :     TEST_COUT << "Matrix size      : " << mtx_size << std::endl;
      27             :     // define input matrices A and B
      28           1 :     int *A = new int[mtx_size * mtx_size];
      29           1 :     int *B = new int[mtx_size * mtx_size];
      30           1 :     int *expected = new int[mtx_size * mtx_size];
      31           1 :     int *result = new int[mtx_size * mtx_size];
      32             : 
      33             :     // initialize random number generator
      34           1 :     std::random_device rd;
      35           1 :     std::mt19937 gen(rd());
      36           1 :     std::uniform_int_distribution<int> distribution(1, 100);
      37             : 
      38             :     // populate matrices A and B with random values
      39        1025 :     for (int i = 0; i < mtx_size; ++i) {
      40     1049600 :         for (int j = 0; j < mtx_size; ++j) {
      41     1048576 :             A[i * mtx_size + j] = distribution(gen);
      42     1048576 :             B[i * mtx_size + j] = distribution(gen);
      43             :         }
      44             :     }
      45             : 
      46             :     gpmp::linalg::Mtx mtx;
      47           1 :     auto start_std = std::chrono::high_resolution_clock::now();
      48             : 
      49             :     // expected result using the naive implementation
      50           1 :     mtx.std_mtx_add(A, B, expected, mtx_size, mtx_size);
      51             : 
      52           1 :     auto end_std = std::chrono::high_resolution_clock::now();
      53           1 :     std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
      54             : 
      55           1 :     auto start_f90 = std::chrono::high_resolution_clock::now();
      56             : 
      57             :     // result using the f90sics implementation
      58           1 :     mtx.mtx_add_f90(A, B, result, mtx_size);
      59           1 :     auto end_f90 = std::chrono::high_resolution_clock::now();
      60           1 :     std::chrono::duration<double> elapsed_seconds_f90 = end_f90 - start_f90;
      61             : 
      62           1 :     TEST_COUT << "FORTRAN90 Matrix Addition Time      : "
      63           1 :               << elapsed_seconds_f90.count() << " seconds" << std::endl;
      64           1 :     TEST_COUT << "STANDARD  Matrix Addition Time      : "
      65           1 :               << elapsed_seconds_std.count() << " seconds" << std::endl;
      66             : 
      67             :     // compare the results
      68           1 :     ASSERT_TRUE(mtx_verif(expected, result, mtx_size, mtx_size));
      69           1 :     delete[] A;
      70           1 :     delete[] B;
      71           1 :     delete[] expected;
      72           1 :     delete[] result;
      73           1 : }
      74             : 
      75           4 : TEST(FORTRAN90MatrixArrayTestI32, MultiplicationVerification) {
      76           1 :     int mtx_size = 4;
      77             : 
      78           1 :     TEST_COUT << "Matrix size      : " << mtx_size << std::endl;
      79             : 
      80           1 :     int A[] = {59, 84, 97, 92, 42, 26, 44, 81, 98, 18, 84, 97, 77, 100, 55, 21};
      81             : 
      82           1 :     int B[] = {36, 59, 39, 11, 89, 93, 16, 31, 80, 71, 25, 68, 71, 92, 29, 61};
      83           1 :     int exp[] = {23892,
      84             :                  26644,
      85             :                  8738,
      86             :                  15461,
      87             :                  13097,
      88             :                  15472,
      89             :                  5503,
      90             :                  9201,
      91             :                  18737,
      92             :                  22344,
      93             :                  9023,
      94             :                  13265,
      95             :                  17563,
      96             :                  19680,
      97             :                  6587,
      98             :                  8968};
      99           1 :     int *expected = new int[mtx_size * mtx_size];
     100           1 :     int *result = new int[mtx_size * mtx_size];
     101             : 
     102             :     gpmp::linalg::Mtx mtx;
     103           1 :     auto start_std = std::chrono::high_resolution_clock::now();
     104             : 
     105             :     // expected result using the naive implementation
     106           1 :     mtx.std_mtx_mult(A, B, expected, mtx_size, mtx_size, mtx_size);
     107             : 
     108           1 :     auto end_std = std::chrono::high_resolution_clock::now();
     109           1 :     std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
     110             : 
     111           1 :     auto start_f90 = std::chrono::high_resolution_clock::now();
     112             : 
     113             :     // result using the f90sics implementation
     114           1 :     mtx.mtx_mult_f90(A, B, result, mtx_size, mtx_size, mtx_size);
     115           1 :     auto end_f90 = std::chrono::high_resolution_clock::now();
     116           1 :     std::chrono::duration<double> elapsed_seconds_f90 = end_f90 - start_f90;
     117             : 
     118           1 :     TEST_COUT << "FORTRAN90 Matrix Multiplication Time      : "
     119           1 :               << elapsed_seconds_f90.count() << " seconds" << std::endl;
     120           1 :     TEST_COUT << "STANDARD  Matrix Multiplication Time      : "
     121           1 :               << elapsed_seconds_std.count() << " seconds" << std::endl;
     122             : 
     123             :     // compare the results
     124           1 :     ASSERT_TRUE(mtx_verif(exp, result, mtx_size, mtx_size));
     125           1 :     delete[] expected;
     126           1 :     delete[] result;
     127             : }
     128             : 
     129           4 : TEST(FORTRAN90MatrixArrayTestI32, MultiplicationPerformanceComparison) {
     130           1 :     int mtx_size = 1024;
     131             : 
     132           1 :     TEST_COUT << "Matrix size      : " << mtx_size << std::endl;
     133             :     // define input matrices A and B
     134           1 :     int *A = new int[mtx_size * mtx_size];
     135           1 :     int *B = new int[mtx_size * mtx_size];
     136             : 
     137           1 :     int *expected = new int[mtx_size * mtx_size];
     138           1 :     int *result = new int[mtx_size * mtx_size];
     139             : 
     140             :     // initialize random number generator
     141           1 :     std::random_device rd;
     142           1 :     std::mt19937 gen(rd());
     143           1 :     std::uniform_int_distribution<int> distribution(1, 100);
     144             : 
     145             :     // populate matrices A and B with random values
     146        1025 :     for (int i = 0; i < mtx_size; ++i) {
     147     1049600 :         for (int j = 0; j < mtx_size; ++j) {
     148     1048576 :             A[i * mtx_size + j] = distribution(gen);
     149     1048576 :             B[i * mtx_size + j] = distribution(gen);
     150             :         }
     151             :     }
     152             : 
     153             :     gpmp::linalg::Mtx mtx;
     154           1 :     auto start_std = std::chrono::high_resolution_clock::now();
     155             : 
     156             :     // expected result using the naive implementation
     157           1 :     mtx.std_mtx_mult(A, B, expected, mtx_size, mtx_size, mtx_size);
     158             : 
     159           1 :     auto end_std = std::chrono::high_resolution_clock::now();
     160           1 :     std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
     161             : 
     162           1 :     auto start_f90 = std::chrono::high_resolution_clock::now();
     163             : 
     164             :     // result using the f90sics implementation
     165           1 :     mtx.mtx_mult_f90(A, B, result, mtx_size, mtx_size, mtx_size);
     166           1 :     auto end_f90 = std::chrono::high_resolution_clock::now();
     167           1 :     std::chrono::duration<double> elapsed_seconds_f90 = end_f90 - start_f90;
     168             : 
     169           1 :     TEST_COUT << "FORTRAN90 Matrix Multiplication Time      : "
     170           1 :               << elapsed_seconds_f90.count() << " seconds" << std::endl;
     171           1 :     TEST_COUT << "STANDARD  Matrix Multiplication Time      : "
     172           1 :               << elapsed_seconds_std.count() << " seconds" << std::endl;
     173             : 
     174             :     // compare the results
     175           1 :     ASSERT_TRUE(mtx_verif(expected, result, mtx_size, mtx_size));
     176           1 :     delete[] A;
     177           1 :     delete[] B;
     178           1 :     delete[] expected;
     179           1 :     delete[] result;
     180           1 : }
     181             : 
     182             : } // namespace

Generated by: LCOV version 1.14