LCOV - code coverage report
Current view: top level - tests/linalg - t_matrix_vector_f64.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 63 63 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(MatrixVectorTestF64, AdditionComparisonSmall) {
      23           1 :     INFO_COUT << "MATRIX (as Vectors) FLOAT64" << std::endl;
      24             : 
      25           1 :     int mtx_size = 64;
      26             :     // define input matrices A and B
      27           2 :     std::vector<std::vector<double>> A(mtx_size, std::vector<double>(mtx_size));
      28           2 :     std::vector<std::vector<double>> B(mtx_size, std::vector<double>(mtx_size));
      29             :     // define matrix to store expected result in (from std_mtx_add())
      30             :     std::vector<std::vector<double>> expected(mtx_size,
      31           2 :                                               std::vector<double>(mtx_size));
      32             :     // define matrix to store actual result (from mtx_add())
      33             :     std::vector<std::vector<double>> result(mtx_size,
      34           2 :                                             std::vector<double>(mtx_size));
      35             : 
      36             :     // initialize random number generator
      37           1 :     std::random_device rd;
      38           1 :     std::mt19937 gen(rd());
      39           1 :     std::uniform_real_distribution<double> distribution(1.0, 100.0);
      40             : 
      41             :     // populate matrices A and B with random values
      42          65 :     for (int i = 0; i < mtx_size; ++i) {
      43        4160 :         for (int j = 0; j < mtx_size; ++j) {
      44        4096 :             A[i][j] = distribution(gen);
      45        4096 :             B[i][j] = distribution(gen);
      46             :         }
      47             :     }
      48             : 
      49             :     gpmp::linalg::Mtx mtx;
      50             :     // expected result using the naive implementation
      51           1 :     mtx.std_mtx_add(A, B, expected);
      52             : 
      53             :     // result using the intrinsics implementation
      54           1 :     mtx.mtx_add(A, B, result);
      55             : 
      56             :     /*
      57             :         std::cout << "Matrix EXPECTED after addition:" << std::endl;
      58             :         for (int i = 0; i < mtx_size; ++i) {
      59             :             for (int j = 0; j < mtx_size; ++j) {
      60             :                 std::cout << expected[i][j] << " ";
      61             :             }
      62             :             std::cout << std::endl;
      63             :         }
      64             : 
      65             :         std::cout << "Matrix RESULT after addition:" << std::endl;
      66             :         for (int i = 0; i < mtx_size; ++i) {
      67             :             for (int j = 0; j < mtx_size; ++j) {
      68             :                 std::cout << result[i][j] << " ";
      69             :             }
      70             :             std::cout << std::endl;
      71             :         }
      72             :     */
      73             : 
      74             :     // compare the results
      75           1 :     ASSERT_TRUE(mtx_verif(expected, result));
      76           1 : }
      77             : 
      78           4 : TEST(MatrixVectorTestF64, AdditionComparisonLarge) {
      79           1 :     int mtx_size = 1024;
      80             : 
      81             :     // define input matrices A and B
      82           2 :     std::vector<std::vector<double>> A(mtx_size, std::vector<double>(mtx_size));
      83           2 :     std::vector<std::vector<double>> B(mtx_size, std::vector<double>(mtx_size));
      84             :     std::vector<std::vector<double>> expected(mtx_size,
      85           2 :                                               std::vector<double>(mtx_size));
      86             :     std::vector<std::vector<double>> result(mtx_size,
      87           2 :                                             std::vector<double>(mtx_size));
      88             : 
      89             :     // initialize random number generator
      90           1 :     std::random_device rd;
      91           1 :     std::mt19937 gen(rd());
      92           1 :     std::uniform_real_distribution<double> distribution(1.0, 100.0);
      93             : 
      94             :     // populate matrices A and B with random values
      95        1025 :     for (int i = 0; i < mtx_size; ++i) {
      96     1049600 :         for (int j = 0; j < mtx_size; ++j) {
      97     1048576 :             A[i][j] = distribution(gen);
      98     1048576 :             B[i][j] = distribution(gen);
      99             :         }
     100             :     }
     101             : 
     102             :     gpmp::linalg::Mtx mtx;
     103             :     // expected result using the naive implementation
     104           1 :     mtx.std_mtx_add(A, B, expected);
     105             : 
     106             :     // result using the intrinsics implementation
     107           1 :     mtx.mtx_add(A, B, result);
     108             : 
     109             :     // compare the results
     110           1 :     ASSERT_TRUE(mtx_verif(expected, result));
     111           1 : }
     112             : 
     113           4 : TEST(MatrixVectorTestF64, AdditionPerformanceComparison) {
     114           1 :     int mtx_size = 1024;
     115           1 :     TEST_COUT << "Matrix size      : " << mtx_size << std::endl;
     116             : 
     117             :     // define input matrices A and B
     118           2 :     std::vector<std::vector<double>> A(mtx_size, std::vector<double>(mtx_size));
     119           2 :     std::vector<std::vector<double>> B(mtx_size, std::vector<double>(mtx_size));
     120             :     std::vector<std::vector<double>> expected(mtx_size,
     121           2 :                                               std::vector<double>(mtx_size));
     122             :     std::vector<std::vector<double>> result(mtx_size,
     123           2 :                                             std::vector<double>(mtx_size));
     124             : 
     125             :     // initialize random number generator
     126           1 :     std::random_device rd;
     127           1 :     std::mt19937 gen(rd());
     128           1 :     std::uniform_real_distribution<double> distribution(1.0, 100.0);
     129             : 
     130             :     // populate matrices A and B with random values
     131        1025 :     for (int i = 0; i < mtx_size; ++i) {
     132     1049600 :         for (int j = 0; j < mtx_size; ++j) {
     133     1048576 :             A[i][j] = distribution(gen);
     134     1048576 :             B[i][j] = distribution(gen);
     135             :         }
     136             :     }
     137             : 
     138             :     gpmp::linalg::Mtx mtx;
     139           1 :     auto start_std = std::chrono::high_resolution_clock::now();
     140             : 
     141             :     // expected result using the naive implementation
     142           1 :     mtx.std_mtx_add(A, B, expected);
     143             : 
     144           1 :     auto end_std = std::chrono::high_resolution_clock::now();
     145           1 :     std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
     146             : 
     147           1 :     auto start_intrin = std::chrono::high_resolution_clock::now();
     148             : 
     149             :     // result using the intrinsics implementation
     150           1 :     mtx.mtx_add(A, B, result);
     151           1 :     auto end_intrin = std::chrono::high_resolution_clock::now();
     152             :     std::chrono::duration<double> elapsed_seconds_intrin =
     153           1 :         end_intrin - start_intrin;
     154             : 
     155           1 :     TEST_COUT << "INTRINSIC Matrix Addition Time      : "
     156           1 :               << elapsed_seconds_intrin.count() << " seconds" << std::endl;
     157           1 :     TEST_COUT << "STANDARD  Matrix Addition Time      : "
     158           1 :               << elapsed_seconds_std.count() << " seconds" << std::endl;
     159             : 
     160             :     // compare the results
     161           1 :     ASSERT_TRUE(mtx_verif(expected, result));
     162           1 : }
     163             : } // namespace

Generated by: LCOV version 1.14