LCOV - code coverage report
Current view: top level - tests/linalg - t_matrix_vector_i32.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 91 91 100.0 %
Date: 2024-05-13 05:06:06 Functions: 16 16 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             : // test case to compare the results of the intrinsics implementation with the
      23             : // naive implementation for matrix addition
      24           4 : TEST(MatrixVectorTestI32, AdditionComparisonSmall) {
      25           1 :     INFO_COUT << "MATRIX (as Vectors) INT32" << std::endl;
      26             : 
      27           1 :     int mtx_size = 64;
      28             :     // define input matrices A and B
      29           2 :     std::vector<std::vector<int>> A(mtx_size, std::vector<int>(mtx_size));
      30           2 :     std::vector<std::vector<int>> B(mtx_size, std::vector<int>(mtx_size));
      31             :     // define matrix to store expected result in (from std_mtx_add())
      32             :     std::vector<std::vector<int>> expected(mtx_size,
      33           2 :                                            std::vector<int>(mtx_size));
      34             :     // define matrix to store actual result (from mtx_add())
      35           2 :     std::vector<std::vector<int>> result(mtx_size, std::vector<int>(mtx_size));
      36             : 
      37             :     // initialize random number generator
      38           1 :     std::random_device rd;
      39           1 :     std::mt19937 gen(rd());
      40           1 :     std::uniform_int_distribution<int> distribution(1, 100);
      41             : 
      42             :     // populate matrices A and B with random values
      43          65 :     for (int i = 0; i < mtx_size; ++i) {
      44        4160 :         for (int j = 0; j < mtx_size; ++j) {
      45        4096 :             A[i][j] = distribution(gen);
      46        4096 :             B[i][j] = distribution(gen);
      47             :         }
      48             :     }
      49             : 
      50             :     gpmp::linalg::Mtx mtx;
      51             :     // expected result using the naive implementation
      52           1 :     mtx.std_mtx_add(A, B, expected);
      53             : 
      54             :     // result using the intrinsics implementation
      55           1 :     mtx.mtx_add(A, B, result);
      56             : 
      57             :     /*
      58             :         std::cout << "Matrix EXPECTED after addition:" << std::endl;
      59             :         for (int i = 0; i < mtx_size; ++i) {
      60             :             for (int j = 0; j < mtx_size; ++j) {
      61             :                 std::cout << expected[i][j] << " ";
      62             :             }
      63             :             std::cout << std::endl;
      64             :         }
      65             : 
      66             :         std::cout << "Matrix RESULT after addition:" << std::endl;
      67             :         for (int i = 0; i < mtx_size; ++i) {
      68             :             for (int j = 0; j < mtx_size; ++j) {
      69             :                 std::cout << result[i][j] << " ";
      70             :             }
      71             :             std::cout << std::endl;
      72             :         }
      73             :     */
      74             : 
      75             :     // compare the results
      76           1 :     ASSERT_TRUE(mtx_verif(expected, result));
      77           1 : }
      78             : 
      79           4 : TEST(MatrixVectorTestI32, AdditionComparisonLarge) {
      80           1 :     int mtx_size = 1024;
      81             : 
      82             :     // define input matrices A and B
      83           2 :     std::vector<std::vector<int>> A(mtx_size, std::vector<int>(mtx_size));
      84           2 :     std::vector<std::vector<int>> B(mtx_size, std::vector<int>(mtx_size));
      85             :     std::vector<std::vector<int>> expected(mtx_size,
      86           2 :                                            std::vector<int>(mtx_size));
      87           2 :     std::vector<std::vector<int>> result(mtx_size, std::vector<int>(mtx_size));
      88             : 
      89             :     // initialize random number generator
      90           1 :     std::random_device rd;
      91           1 :     std::mt19937 gen(rd());
      92           1 :     std::uniform_int_distribution<int> distribution(1, 100);
      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             :     /*
     110             :         std::cout << "Matrix EXPECTED after addition:" << std::endl;
     111             :         for (int i = 0; i < mtx_size; ++i) {
     112             :             for (int j = 0; j < mtx_size; ++j) {
     113             :                 std::cout << expected[i][j] << " ";
     114             :             }
     115             :             std::cout << std::endl;
     116             :         }
     117             : 
     118             :         std::cout << "Matrix RESULT after addition:" << std::endl;
     119             :         for (int i = 0; i < mtx_size; ++i) {
     120             :             for (int j = 0; j < mtx_size; ++j) {
     121             :                 std::cout << result[i][j] << " ";
     122             :             }
     123             :             std::cout << std::endl;
     124             :         }
     125             :     */
     126             : 
     127             :     // compare the results
     128           1 :     ASSERT_TRUE(mtx_verif(expected, result));
     129           1 : }
     130             : 
     131           4 : TEST(MatrixVectorTestI32, AdditionPerformanceComparison) {
     132           1 :     int mtx_size = 1024;
     133           1 :     TEST_COUT << "Matrix size      : " << mtx_size << std::endl;
     134             : 
     135             :     // define input matrices A and B
     136           2 :     std::vector<std::vector<int>> A(mtx_size, std::vector<int>(mtx_size));
     137           2 :     std::vector<std::vector<int>> B(mtx_size, std::vector<int>(mtx_size));
     138             :     std::vector<std::vector<int>> expected(mtx_size,
     139           2 :                                            std::vector<int>(mtx_size));
     140           2 :     std::vector<std::vector<int>> result(mtx_size, std::vector<int>(mtx_size));
     141             : 
     142             :     // initialize random number generator
     143           1 :     std::random_device rd;
     144           1 :     std::mt19937 gen(rd());
     145           1 :     std::uniform_int_distribution<int> distribution(1, 100);
     146             : 
     147             :     // populate matrices A and B with random values
     148        1025 :     for (int i = 0; i < mtx_size; ++i) {
     149     1049600 :         for (int j = 0; j < mtx_size; ++j) {
     150     1048576 :             A[i][j] = distribution(gen);
     151     1048576 :             B[i][j] = distribution(gen);
     152             :         }
     153             :     }
     154             : 
     155             :     gpmp::linalg::Mtx mtx;
     156             : 
     157           1 :     auto start_std = std::chrono::high_resolution_clock::now();
     158             : 
     159             :     // expected result using the naive implementation
     160           1 :     mtx.std_mtx_add(A, B, expected);
     161             : 
     162           1 :     auto end_std = std::chrono::high_resolution_clock::now();
     163           1 :     std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
     164             : 
     165           1 :     auto start_intrin = std::chrono::high_resolution_clock::now();
     166             : 
     167             :     // result using the intrinsics implementation
     168           1 :     mtx.mtx_add(A, B, result);
     169           1 :     auto end_intrin = std::chrono::high_resolution_clock::now();
     170             :     std::chrono::duration<double> elapsed_seconds_intrin =
     171           1 :         end_intrin - start_intrin;
     172             : 
     173           1 :     TEST_COUT << "INTRINSIC Matrix Addition Time      : "
     174           1 :               << elapsed_seconds_intrin.count() << " seconds" << std::endl;
     175           1 :     TEST_COUT << "STANDARD  Matrix Addition Time      : "
     176           1 :               << elapsed_seconds_std.count() << " seconds" << std::endl;
     177             : 
     178             :     // compare the results
     179           1 :     ASSERT_TRUE(mtx_verif(expected, result));
     180           1 : }
     181             : 
     182           4 : TEST(MatrixVectorTestI32, MultiplicationPerformanceComparison) {
     183           1 :     int mtx_size = 1024;
     184           1 :     TEST_COUT << "Matrix size      : " << mtx_size << std::endl;
     185             : 
     186             :     // define input matrices A and B
     187           2 :     std::vector<std::vector<int>> A(mtx_size, std::vector<int>(mtx_size));
     188           2 :     std::vector<std::vector<int>> B(mtx_size, std::vector<int>(mtx_size));
     189             :     std::vector<std::vector<int>> expected(mtx_size,
     190           2 :                                            std::vector<int>(mtx_size));
     191           2 :     std::vector<std::vector<int>> result(mtx_size, std::vector<int>(mtx_size));
     192             : 
     193             :     // initialize random number generator
     194           1 :     std::random_device rd;
     195           1 :     std::mt19937 gen(rd());
     196           1 :     std::uniform_int_distribution<int> distribution(1, 100);
     197             : 
     198             :     // populate matrices A and B with random values
     199        1025 :     for (int i = 0; i < mtx_size; ++i) {
     200     1049600 :         for (int j = 0; j < mtx_size; ++j) {
     201     1048576 :             A[i][j] = distribution(gen);
     202     1048576 :             B[i][j] = distribution(gen);
     203             :         }
     204             :     }
     205             : 
     206             :     gpmp::linalg::Mtx mtx;
     207             : 
     208           1 :     auto start_std = std::chrono::high_resolution_clock::now();
     209             : 
     210             :     // expected result using the naive implementation
     211           1 :     mtx.std_mtx_mult(A, B, expected);
     212             : 
     213           1 :     auto end_std = std::chrono::high_resolution_clock::now();
     214           1 :     std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
     215             : 
     216           1 :     auto start_intrin = std::chrono::high_resolution_clock::now();
     217             : 
     218             :     // result using the intrinsics implementation
     219           1 :     mtx.mtx_mult(A, B, result);
     220           1 :     auto end_intrin = std::chrono::high_resolution_clock::now();
     221             :     std::chrono::duration<double> elapsed_seconds_intrin =
     222           1 :         end_intrin - start_intrin;
     223             : 
     224           1 :     TEST_COUT << "INTRINSIC Matrix Multiplication Time      : "
     225           1 :               << elapsed_seconds_intrin.count() << " seconds" << std::endl;
     226           1 :     TEST_COUT << "STANDARD  Matrix Multiplication Time      : "
     227           1 :               << elapsed_seconds_std.count() << " seconds" << std::endl;
     228             : 
     229             :     // compare the results
     230           1 :     ASSERT_TRUE(mtx_verif(expected, result));
     231           1 : }
     232             : 
     233             : } // namespace

Generated by: LCOV version 1.14