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(MatrixArrayTestI8, AdditionComparisonSmall) {
23 1 : INFO_COUT << "MATRIX (as Arrays) INT8" << std::endl;
24 :
25 1 : int mtx_size = 400;
26 : // define input matrices A and B
27 1 : int8_t *A = new int8_t[mtx_size * mtx_size];
28 1 : int8_t *B = new int8_t[mtx_size * mtx_size];
29 1 : int8_t *expected = new int8_t[mtx_size * mtx_size];
30 1 : int8_t *result = new int8_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 401 : for (int i = 0; i < mtx_size; ++i) {
39 160400 : for (int j = 0; j < mtx_size; ++j) {
40 160000 : A[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
41 160000 : B[i * mtx_size + j] = static_cast<int8_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(MatrixArrayTestI8, AdditionComparisonLarge) {
61 1 : int mtx_size = 1024;
62 : // define input matrices A and B
63 1 : int8_t *A = new int8_t[mtx_size * mtx_size];
64 1 : int8_t *B = new int8_t[mtx_size * mtx_size];
65 1 : int8_t *expected = new int8_t[mtx_size * mtx_size];
66 1 : int8_t *result = new int8_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<int8_t>(distribution(gen));
77 1048576 : B[i * mtx_size + j] = static_cast<int8_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(MatrixArrayTestI8, 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 : int8_t *A = new int8_t[mtx_size * mtx_size];
101 1 : int8_t *B = new int8_t[mtx_size * mtx_size];
102 1 : int8_t *expected = new int8_t[mtx_size * mtx_size];
103 1 : int8_t *result = new int8_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<int8_t>(distribution(gen));
114 1048576 : B[i * mtx_size + j] = static_cast<int8_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 :
124 1 : mtx.std_mtx_add(A, B, expected, mtx_size, mtx_size);
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(MatrixArrayTestI8, SubtractionComparisonSmall) {
150 1 : int mtx_size = 400;
151 : // define input matrices A and B
152 1 : int8_t *A = new int8_t[mtx_size * mtx_size];
153 1 : int8_t *B = new int8_t[mtx_size * mtx_size];
154 1 : int8_t *expected = new int8_t[mtx_size * mtx_size];
155 1 : int8_t *result = new int8_t[mtx_size * mtx_size];
156 :
157 : // initialize random number generator
158 1 : std::random_device rd;
159 1 : std::mt19937 gen(rd());
160 1 : std::uniform_int_distribution<int> distribution(1, 100);
161 :
162 : // populate matrices A and B with random values
163 401 : for (int i = 0; i < mtx_size; ++i) {
164 160400 : for (int j = 0; j < mtx_size; ++j) {
165 160000 : A[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
166 160000 : B[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
167 : }
168 : }
169 :
170 : gpmp::linalg::Mtx mtx;
171 : // expected result using the naive implementation
172 1 : mtx.std_mtx_sub(A, B, expected, mtx_size, mtx_size);
173 :
174 : // result using the intrinsics implementation
175 1 : mtx.mtx_sub(A, B, result, mtx_size, mtx_size);
176 :
177 : // compare the results
178 1 : ASSERT_TRUE(mtx_verif(expected, result, mtx_size, mtx_size));
179 1 : delete[] A;
180 1 : delete[] B;
181 1 : delete[] expected;
182 1 : delete[] result;
183 1 : }
184 :
185 4 : TEST(MatrixArrayTestI8, SubtractionComparisonLarge) {
186 1 : int mtx_size = 1024;
187 : // define input matrices A and B
188 1 : int8_t *A = new int8_t[mtx_size * mtx_size];
189 1 : int8_t *B = new int8_t[mtx_size * mtx_size];
190 1 : int8_t *expected = new int8_t[mtx_size * mtx_size];
191 1 : int8_t *result = new int8_t[mtx_size * 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 * mtx_size + j] = static_cast<int8_t>(distribution(gen));
202 1048576 : B[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
203 : }
204 : }
205 :
206 : gpmp::linalg::Mtx mtx;
207 : // expected result using the naive implementation
208 1 : mtx.std_mtx_sub(A, B, expected, mtx_size, mtx_size);
209 :
210 : // result using the intrinsics implementation
211 1 : mtx.mtx_sub(A, B, result, mtx_size, mtx_size);
212 :
213 : // compare the results
214 1 : ASSERT_TRUE(mtx_verif(expected, result, mtx_size, mtx_size));
215 1 : delete[] A;
216 1 : delete[] B;
217 1 : delete[] expected;
218 1 : delete[] result;
219 1 : }
220 :
221 4 : TEST(MatrixArrayTestI8, SubtractionPerformanceComparison) {
222 1 : int mtx_size = 1024;
223 1 : TEST_COUT << "Matrix size : " << mtx_size << std::endl;
224 : // define input matrices A and B
225 1 : int8_t *A = new int8_t[mtx_size * mtx_size];
226 1 : int8_t *B = new int8_t[mtx_size * mtx_size];
227 1 : int8_t *expected = new int8_t[mtx_size * mtx_size];
228 1 : int8_t *result = new int8_t[mtx_size * mtx_size];
229 :
230 : // initialize random number generator
231 1 : std::random_device rd;
232 1 : std::mt19937 gen(rd());
233 1 : std::uniform_int_distribution<int> distribution(1, 100);
234 :
235 : // populate matrices A and B with random values
236 1025 : for (int i = 0; i < mtx_size; ++i) {
237 1049600 : for (int j = 0; j < mtx_size; ++j) {
238 1048576 : A[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
239 1048576 : B[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
240 : }
241 : }
242 :
243 : gpmp::linalg::Mtx mtx;
244 :
245 1 : auto start_std = std::chrono::high_resolution_clock::now();
246 :
247 : // expected result using the naive implementation
248 :
249 1 : mtx.std_mtx_sub(A, B, expected, mtx_size, mtx_size);
250 1 : auto end_std = std::chrono::high_resolution_clock::now();
251 1 : std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
252 :
253 1 : auto start_intrin = std::chrono::high_resolution_clock::now();
254 :
255 : // result using the intrinsics implementation
256 1 : mtx.mtx_sub(A, B, result, mtx_size, mtx_size);
257 1 : auto end_intrin = std::chrono::high_resolution_clock::now();
258 : std::chrono::duration<double> elapsed_seconds_intrin =
259 1 : end_intrin - start_intrin;
260 :
261 1 : TEST_COUT << "INTRINSIC Matrix Subtraction Time : "
262 1 : << elapsed_seconds_intrin.count() << " seconds" << std::endl;
263 1 : TEST_COUT << "STANDARD Matrix Subtraction Time : "
264 1 : << elapsed_seconds_std.count() << " seconds" << std::endl;
265 :
266 : // compare the results
267 1 : ASSERT_TRUE(mtx_verif(expected, result, mtx_size, mtx_size));
268 1 : delete[] A;
269 1 : delete[] B;
270 1 : delete[] expected;
271 1 : delete[] result;
272 1 : }
273 :
274 : /*
275 : TEST(MatrixArrayTestI8, MultiplicationPerformanceComparison) {
276 : int mtx_size = 1024;
277 : TEST_COUT << "Matrix size : " << mtx_size << std::endl;
278 : // define input matrices A and B
279 : int8_t *A = new int8_t[mtx_size * mtx_size];
280 : int8_t *B = new int8_t[mtx_size * mtx_size];
281 : int8_t *expected = new int8_t[mtx_size * mtx_size];
282 : int8_t *result = new int8_t[mtx_size * mtx_size];
283 :
284 : // initialize random number generator
285 : std::random_device rd;
286 : std::mt19937 gen(rd());
287 : std::uniform_int_distribution<int> distribution(1, 5);
288 :
289 : // populate matrices A and B with random values
290 : for (int i = 0; i < mtx_size; ++i) {
291 : for (int j = 0; j < mtx_size; ++j) {
292 : A[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
293 : B[i * mtx_size + j] = static_cast<int8_t>(distribution(gen));
294 : }
295 : }
296 :
297 : gpmp::linalg::Mtx mtx;
298 :
299 : auto start_std = std::chrono::high_resolution_clock::now();
300 :
301 : // expected result using the naive implementation
302 : mtx.std_mtx_mult(A, B, expected, mtx_size, mtx_size, mtx_size);
303 :
304 : auto end_std = std::chrono::high_resolution_clock::now();
305 : std::chrono::duration<double> elapsed_seconds_std = end_std - start_std;
306 :
307 : auto start_intrin = std::chrono::high_resolution_clock::now();
308 :
309 : // result using the intrinsics implementation
310 : mtx.mtx_mult(A, B, result, mtx_size, mtx_size, mtx_size);
311 : auto end_intrin = std::chrono::high_resolution_clock::now();
312 : std::chrono::duration<double> elapsed_seconds_intrin =
313 : end_intrin - start_intrin;
314 :
315 : TEST_COUT << "INTRINSIC Matrix Multiplication Time : "
316 : << elapsed_seconds_intrin.count() << " seconds" << std::endl;
317 : TEST_COUT << "STANDARD Matrix Multiplication Time : "
318 : << elapsed_seconds_std.count() << " seconds" << std::endl;
319 :
320 : // compare the results
321 : ASSERT_TRUE(mtx_verif(expected, result, mtx_size, mtx_size));
322 : delete[] A;
323 : delete[] B;
324 : delete[] expected;
325 : delete[] result;
326 : }
327 : */
328 :
329 : } // namespace
|