openGPMP
Open Source Mathematics Package
mtx.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 #include <openGPMP/linalg.hpp>
4 #include <random>
5 #include <vector>
6 constexpr int matrixSize = 8912;
7 
8 void run_mtx_add() {
10 
11  std::chrono::steady_clock::time_point gen_start_time =
12  std::chrono::steady_clock::now();
13 
14  std::vector<std::vector<int>> A(matrixSize, std::vector<int>(matrixSize));
15  std::vector<std::vector<int>> B(matrixSize, std::vector<int>(matrixSize));
16  std::vector<std::vector<int>> C(matrixSize, std::vector<int>(matrixSize));
17 
18  // initialize random number generator
19  std::random_device rd;
20  std::mt19937 gen(rd());
21  std::uniform_int_distribution<int> distribution(1, 100);
22 
23  // populate matrices A and B with random values
24  for (int i = 0; i < matrixSize; ++i) {
25  for (int j = 0; j < matrixSize; ++j) {
26  A[i][j] = distribution(gen);
27  B[i][j] = distribution(gen);
28  }
29  }
30 
31  std::chrono::steady_clock::time_point start_time_mtx =
32  std::chrono::steady_clock::now();
33  // perform matrix addition
34  mtx.mtx_add(A, B, C);
35  std::chrono::steady_clock::time_point start_time_std_mtx =
36  std::chrono::steady_clock::now();
37 
38  mtx.std_mtx_add(A, B, C);
39  /*
40  std::cout << "Matrix A:" << std::endl;
41  for (int i = 0; i < matrixSize; ++i) {
42  for (int j = 0; j < matrixSize; ++j) {
43  std::cout << A[i][j] << " ";
44  }
45  std::cout << std::endl;
46  }
47 
48  std::cout << "Matrix B:" << std::endl;
49  for (int i = 0; i < matrixSize; ++i) {
50  for (int j = 0; j < matrixSize; ++j) {
51  std::cout << B[i][j] << " ";
52  }
53  std::cout << std::endl;
54  }
55  // Display the result
56  std::cout << "Matrix C after addition:" << std::endl;
57  for (int i = 0; i < matrixSize; ++i) {
58  for (int j = 0; j < matrixSize; ++j) {
59  std::cout << C[i][j] << " ";
60  }
61  std::cout << std::endl;
62  }
63  */
64  std::chrono::steady_clock::time_point end_time =
65  std::chrono::steady_clock::now();
66 
67  std::cout << "Generating random matrices (VECTORS) - Time elapsed: "
68  << std::chrono::duration_cast<std::chrono::milliseconds>(
69  start_time_mtx - gen_start_time)
70  .count()
71  << " ms" << std::endl;
72 
73  std::cout << "SIMD - Time elapsed: "
74  << std::chrono::duration_cast<std::chrono::milliseconds>(
75  start_time_std_mtx - start_time_mtx)
76  .count()
77  << " ms" << std::endl;
78 
79  std::cout << "NAIVE - Time elapsed: "
80  << std::chrono::duration_cast<std::chrono::milliseconds>(
81  end_time - start_time_std_mtx)
82  .count()
83  << " ms" << std::endl;
84 }
85 
88 
89  std::chrono::steady_clock::time_point gen_start_time =
90  std::chrono::steady_clock::now();
91 
92  std::vector<std::vector<float>> A(matrixSize,
93  std::vector<float>(matrixSize));
94  std::vector<std::vector<float>> B(matrixSize,
95  std::vector<float>(matrixSize));
96  std::vector<std::vector<float>> C(matrixSize,
97  std::vector<float>(matrixSize));
98 
99  // initialize random number generator
100  std::random_device rd;
101  std::mt19937 gen(rd());
102  std::uniform_real_distribution<float> distribution(1.0, 100.0);
103 
104  // populate matrices A and B with random values
105  for (int i = 0; i < matrixSize; ++i) {
106  for (int j = 0; j < matrixSize; ++j) {
107  A[i][j] = distribution(gen);
108  B[i][j] = distribution(gen);
109  }
110  }
111 
112  std::chrono::steady_clock::time_point start_time_mtx =
113  std::chrono::steady_clock::now();
114  // perform matrix addition
115  mtx.mtx_add(A, B, C);
116  std::chrono::steady_clock::time_point start_time_std_mtx =
117  std::chrono::steady_clock::now();
118 
119  mtx.std_mtx_add(A, B, C);
120  /*
121  std::cout << "Matrix A:" << std::endl;
122  for (int i = 0; i < matrixSize; ++i) {
123  for (int j = 0; j < matrixSize; ++j) {
124  std::cout << A[i][j] << " ";
125  }
126  std::cout << std::endl;
127  }
128 
129  std::cout << "Matrix B:" << std::endl;
130  for (int i = 0; i < matrixSize; ++i) {
131  for (int j = 0; j < matrixSize; ++j) {
132  std::cout << B[i][j] << " ";
133  }
134  std::cout << std::endl;
135  }
136  // Display the result
137  std::cout << "Matrix C after addition:" << std::endl;
138  for (int i = 0; i < matrixSize; ++i) {
139  for (int j = 0; j < matrixSize; ++j) {
140  std::cout << C[i][j] << " ";
141  }
142  std::cout << std::endl;
143  }
144  */
145  std::chrono::steady_clock::time_point end_time =
146  std::chrono::steady_clock::now();
147 
148  std::cout << "Generating random matrices (VECTORS, FLOATS) - Time elapsed: "
149  << std::chrono::duration_cast<std::chrono::milliseconds>(
150  start_time_mtx - gen_start_time)
151  .count()
152  << " ms" << std::endl;
153 
154  std::cout << "SIMD - Time elapsed: "
155  << std::chrono::duration_cast<std::chrono::milliseconds>(
156  start_time_std_mtx - start_time_mtx)
157  .count()
158  << " ms" << std::endl;
159 
160  std::cout << "NAIVE - Time elapsed: "
161  << std::chrono::duration_cast<std::chrono::milliseconds>(
162  end_time - start_time_std_mtx)
163  .count()
164  << " ms" << std::endl;
165 }
166 
169 
170  std::chrono::steady_clock::time_point gen_start_time =
171  std::chrono::steady_clock::now();
172 
173  std::vector<std::vector<double>> A(matrixSize,
174  std::vector<double>(matrixSize));
175  std::vector<std::vector<double>> B(matrixSize,
176  std::vector<double>(matrixSize));
177  std::vector<std::vector<double>> C(matrixSize,
178  std::vector<double>(matrixSize));
179 
180  // initialize random number generator
181  std::random_device rd;
182  std::mt19937 gen(rd());
183  std::uniform_real_distribution<double> distribution(1.0, 100.0);
184 
185  // populate matrices A and B with random values
186  for (int i = 0; i < matrixSize; ++i) {
187  for (int j = 0; j < matrixSize; ++j) {
188  A[i][j] = distribution(gen);
189  B[i][j] = distribution(gen);
190  }
191  }
192 
193  std::chrono::steady_clock::time_point start_time_mtx =
194  std::chrono::steady_clock::now();
195  // perform matrix addition
196  mtx.mtx_add(A, B, C);
197  std::chrono::steady_clock::time_point start_time_std_mtx =
198  std::chrono::steady_clock::now();
199 
200  mtx.std_mtx_add(A, B, C);
201  /*
202  std::cout << "Matrix A:" << std::endl;
203  for (int i = 0; i < matrixSize; ++i) {
204  for (int j = 0; j < matrixSize; ++j) {
205  std::cout << A[i][j] << " ";
206  }
207  std::cout << std::endl;
208  }
209 
210  std::cout << "Matrix B:" << std::endl;
211  for (int i = 0; i < matrixSize; ++i) {
212  for (int j = 0; j < matrixSize; ++j) {
213  std::cout << B[i][j] << " ";
214  }
215  std::cout << std::endl;
216  }
217  // Display the result
218  std::cout << "Matrix C after addition:" << std::endl;
219  for (int i = 0; i < matrixSize; ++i) {
220  for (int j = 0; j < matrixSize; ++j) {
221  std::cout << C[i][j] << " ";
222  }
223  std::cout << std::endl;
224  }
225  */
226  std::chrono::steady_clock::time_point end_time =
227  std::chrono::steady_clock::now();
228 
229  std::cout
230  << "Generating random matrices (VECTORS, DOUBLES) - Time elapsed: "
231  << std::chrono::duration_cast<std::chrono::milliseconds>(
232  start_time_mtx - gen_start_time)
233  .count()
234  << " ms" << std::endl;
235 
236  std::cout << "SIMD - Time elapsed: "
237  << std::chrono::duration_cast<std::chrono::milliseconds>(
238  start_time_std_mtx - start_time_mtx)
239  .count()
240  << " ms" << std::endl;
241 
242  std::cout << "NAIVE - Time elapsed: "
243  << std::chrono::duration_cast<std::chrono::milliseconds>(
244  end_time - start_time_std_mtx)
245  .count()
246  << " ms" << std::endl;
247 }
248 
251 
252  std::chrono::steady_clock::time_point gen_start_time =
253  std::chrono::steady_clock::now();
254 
255  /*new int A[matrixSize * matrixSize];
256  new int B[matrixSize * matrixSize];
257  new int C[matrixSize * matrixSize];
258  */
259 
260  int *A = new int[matrixSize * matrixSize];
261  int *C = new int[matrixSize * matrixSize];
262  int *B = new int[matrixSize * matrixSize];
263 
264  // initialize random number generator
265  std::random_device rd;
266  std::mt19937 gen(rd());
267  std::uniform_int_distribution<int> distribution(1, 100);
268 
269  // populate matrices A and B with random values
270  for (int i = 0; i < matrixSize; ++i) {
271  for (int j = 0; j < matrixSize; ++j) {
272  A[i * matrixSize + j] = distribution(gen);
273  B[i * matrixSize + j] = distribution(gen);
274  }
275  }
276 
277  std::chrono::steady_clock::time_point start_time_mtx =
278  std::chrono::steady_clock::now();
279  // perform matrix addition
280  mtx.mtx_add(A, B, C, matrixSize, matrixSize);
281  std::chrono::steady_clock::time_point start_time_std_mtx =
282  std::chrono::steady_clock::now();
283 
284  mtx.std_mtx_add(A, B, C, matrixSize, matrixSize);
285 
286  std::chrono::steady_clock::time_point end_time =
287  std::chrono::steady_clock::now();
288 
289  std::cout << "Generating random matrices (ARRAYS, INT) - Time elapsed: "
290  << std::chrono::duration_cast<std::chrono::milliseconds>(
291  start_time_mtx - gen_start_time)
292  .count()
293  << " ms" << std::endl;
294 
295  std::cout << "SIMD - Time elapsed: "
296  << std::chrono::duration_cast<std::chrono::milliseconds>(
297  start_time_std_mtx - start_time_mtx)
298  .count()
299  << " ms" << std::endl;
300 
301  std::cout << "NAIVE - Time elapsed: "
302  << std::chrono::duration_cast<std::chrono::milliseconds>(
303  end_time - start_time_std_mtx)
304  .count()
305  << " ms" << std::endl;
306 
307  delete[] A;
308  delete[] B;
309  delete[] C;
310 }
311 
312 void fill_matrix_random(float *matrix, std::size_t rows, std::size_t cols) {
313  std::random_device rd;
314  std::mt19937 gen(rd());
315  std::uniform_real_distribution<float> distribution(1.0, 100.0);
316 
317  for (std::size_t i = 0; i < rows; ++i) {
318  for (std::size_t j = 0; j < cols; ++j) {
319  matrix[i * cols + j] = distribution(gen);
320  }
321  }
322 }
323 
324 void print_matrix(const float *matrix, std::size_t rows, std::size_t cols) {
325  for (std::size_t i = 0; i < rows; ++i) {
326  for (std::size_t j = 0; j < cols; ++j) {
327  std::cout << matrix[i * cols + j] << " ";
328  }
329  std::cout << std::endl;
330  }
331 }
332 
333 void mtx_add_f90() {
335 
336  std::chrono::steady_clock::time_point gen_start_time =
337  std::chrono::steady_clock::now();
338 
339  float *A = new float[matrixSize * matrixSize];
340  float *B = new float[matrixSize * matrixSize];
341  float *C = new float[matrixSize * matrixSize];
342  // Initialize A and B matrices with random values
345 
346  std::chrono::steady_clock::time_point mtx_start_time =
347  std::chrono::steady_clock::now();
348 
349  // Call the Fortran matrix addition function through the C++ wrapper
350  mtx.mtx_add_f90(A, B, C, matrixSize);
351 
352  std::chrono::steady_clock::time_point end_time =
353  std::chrono::steady_clock::now();
354 
355  // Perform any further operations or print the result as needed
356  /*std::cout << "Matrix A:" << std::endl;
357  print_matrix(A, matrixSize, matrixSize);
358 
359  std::cout << "\nMatrix B:" << std::endl;
360  print_matrix(B, matrixSize, matrixSize);
361 
362  std::cout << "\nMatrix C:" << std::endl;
363  print_matrix(C, matrixSize, matrixSize);*/
364 
365  std::cout << "Generating random matrices (ARRAYS) - Time elapsed: "
366  << std::chrono::duration_cast<std::chrono::milliseconds>(
367  mtx_start_time - gen_start_time)
368  .count()
369  << " ms" << std::endl;
370 
371  std::cout << "mtx add using Fortran subroutine & flattened matrices: "
372  << std::chrono::duration_cast<std::chrono::milliseconds>(
373  end_time - mtx_start_time)
374  .count()
375  << " ms" << std::endl;
376 
377  delete[] A;
378  delete[] B;
379  delete[] C;
380 }
381 
382 int main() {
383  run_mtx_add();
384 
385  run_mtx_add_flt();
386 
388 
389  run_mtx_add_arr();
390 
391  mtx_add_f90();
392  return 0;
393 }
Mtx class offers matrix arithmetic operations. Some methods are element-wise while others make use of...
Definition: mtx.hpp:57
User API for OpenGPMP LINEAR ALGEBRA MODULE.
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23
void run_mtx_add_doub()
Definition: mtx.cpp:167
void print_matrix(const float *matrix, std::size_t rows, std::size_t cols)
Definition: mtx.cpp:324
void run_mtx_add_arr()
Definition: mtx.cpp:249
void run_mtx_add_flt()
Definition: mtx.cpp:86
constexpr int matrixSize
Definition: mtx.cpp:6
void run_mtx_add()
Definition: mtx.cpp:8
int main()
Definition: mtx.cpp:382
void fill_matrix_random(float *matrix, std::size_t rows, std::size_t cols)
Definition: mtx.cpp:312
void mtx_add_f90()
Definition: mtx.cpp:333