openGPMP
Open Source Mathematics Package
Functions
mtx_x86.c File Reference
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <random>

Go to the source code of this file.

Functions

void matrix_addition (float *A, float *B, float *C, int matrixSize)
 
void initialize_matrix_random (float *matrix, int matrixSize)
 
void print_matrix (const char *name, float *matrix, int matrixSize)
 
int main ()
 

Function Documentation

◆ initialize_matrix_random()

void initialize_matrix_random ( float *  matrix,
int  matrixSize 
)

Definition at line 8 of file mtx_x86.c.

8  {
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 }
constexpr int matrixSize
Definition: mtx.cpp:6

References matrixSize.

Referenced by main().

◆ main()

int main ( void  )

Definition at line 29 of file mtx_x86.c.

29  {
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
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23

References python.linalg::A, python.linalg::B, python.linalg::C, initialize_matrix_random(), matrix_addition(), and matrixSize.

◆ matrix_addition()

void matrix_addition ( float *  A,
float *  B,
float *  C,
int  matrixSize 
)

Referenced by main().

◆ print_matrix()

void print_matrix ( const char *  name,
float *  matrix,
int  matrixSize 
)

Definition at line 18 of file mtx_x86.c.

18  {
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 }

References matrixSize.