openGPMP
Open Source Mathematics Package
mtx_x86.c
Go to the documentation of this file.
1 #include <chrono>
2 #include <cstdlib>
3 #include <iostream>
4 #include <random>
5 
6 extern "C" void matrix_addition(float *A, float *B, float *C, int matrixSize);
7 
8 void initialize_matrix_random(float *matrix, int matrixSize) {
9  std::random_device rd;
10  std::mt19937 gen(rd());
11  std::uniform_real_distribution<float> distribution(1.0, 100.0);
12 
13  for (int i = 0; i < matrixSize * matrixSize; ++i) {
14  matrix[i] = distribution(gen);
15  }
16 }
17 
18 void print_matrix(const char *name, float *matrix, int matrixSize) {
19  std::cout << name << ":\n";
20  for (int i = 0; i < matrixSize; ++i) {
21  for (int j = 0; j < matrixSize; ++j) {
22  std::cout << matrix[i * matrixSize + j] << "\t";
23  }
24  std::cout << "\n";
25  }
26  std::cout << "\n";
27 }
28 
29 int main() {
30  // Matrix dimensions (assuming 4x4 matrices)
31  int matrixSize = 8192;
32 
33  std::chrono::steady_clock::time_point gen_start_time =
34  std::chrono::steady_clock::now();
35 
36  // Allocate memory for matrices A, B, and C
37  float *A = new float[matrixSize * matrixSize];
38  float *B = new float[matrixSize * matrixSize];
39  float *C = new float[matrixSize * matrixSize];
40  /*
41  float *A = static_cast<float*>(aligned_alloc(16, matrixSize * matrixSize *
42  sizeof(float))); float *B = static_cast<float*>(aligned_alloc(16, matrixSize
43  * matrixSize * sizeof(float))); float *C =
44  static_cast<float*>(aligned_alloc(16, matrixSize * matrixSize *
45  sizeof(float)));
46  */
47 
48  // Initialize matrices A and B with random values
51 
52  std::chrono::steady_clock::time_point mtx_start_time =
53  std::chrono::steady_clock::now();
54 
55  // Print matrices A and B
56  // print_matrix("Matrix A", A, matrixSize);
57  // print_matrix("Matrix B", B, matrixSize);
58 
59  // Call the matrix addition function in assembly
61 
62  std::chrono::steady_clock::time_point end_time =
63  std::chrono::steady_clock::now();
64 
65  // Display the result matrix C
66  // print_matrix("Result Matrix C", C, matrixSize);
67 
68  std::cout << "Generating random matrices (ARRAYS) - Time elapsed: "
69  << std::chrono::duration_cast<std::chrono::milliseconds>(
70  mtx_start_time - gen_start_time)
71  .count()
72  << " ms" << std::endl;
73 
74  std::cout << "Mtx add using ASM - Time elapsed: "
75  << std::chrono::duration_cast<std::chrono::milliseconds>(
76  end_time - mtx_start_time)
77  .count()
78  << " ms" << std::endl;
79 
80  // Free allocated memory
81  delete[] A;
82  delete[] B;
83  delete[] C;
84 
85  return 0;
86 }
void matrix_addition(float *A, float *B, float *C, int matrixSize)
void initialize_matrix_random(float *matrix, int matrixSize)
Definition: mtx_x86.c:8
void print_matrix(const char *name, float *matrix, int matrixSize)
Definition: mtx_x86.c:18
int main()
Definition: mtx_x86.c:29
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23
constexpr int matrixSize
Definition: mtx.cpp:6