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

Generated by: LCOV version 1.14