-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatOper.cpp
More file actions
118 lines (96 loc) · 3.17 KB
/
MatOper.cpp
File metadata and controls
118 lines (96 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// matrix_operations.cpp
#include <iostream>
#include <cmath>
#include "MatOper.h"
using namespace std;
// 2D DYNAMIC ARRAY ALLOCATOR
float** ArrayAllocator(int x, int y) {
float** array = new float*[x];
for (int i = 0; i < x; ++i) {
array[i] = new float[y];
}
return array;
}
// 2D DYNAMIC ARRAY DEALLOCATOR
void ArrayDeAllocator(float** array, int x) {
for (int i = 0; i < x; ++i) {
delete[] array[i];
}
delete[] array;
}
// INITIALIZING THE INPUT ARRAY A
void InitializeMatrixA(float** matrix, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
matrix[i][j] = i/10000.0f - j/10000.0f;
}
}
}
// INITIALIZING THE INPUT ARRAY B
void InitializeMatrixB(float** matrix, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
matrix[i][j] = i/10000.0f + j/10000.0f;
}
}
}
// INITIALIZING THE INPUT ARRAY R
void InitializeMatrixR(float** matrix, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
matrix[i][j] = 0.0f;
}
}
}
// MATRIX-MATRIX MULTIPLICATION IN CPU
void MatrixMultiplier2D(float** matrixA, float** matrixB, float** matrixR, int ax, int ay, int bx, int by) {
float alpha = 1.0f;
float beta = 0.0f;
// CHECK THE VALIDITY OF MATRICES FOR MULTIPLICATION
if (ay != bx) {
cout << "Error: The number of columns in matrix A (" << ay
<< ") does not match the number of rows in matrix B (" << bx << ")." << endl;
return;
}
for (int i = 0; i < ax; ++i) {
for (int j = 0; j < by; ++j) {
matrixR[i][j] = beta*(matrixR[i][j]);
for (int k = 0; k < ay; ++k) {
matrixR[i][j] += alpha*(matrixA[i][k] * matrixB[k][j]);
}
}
}
// Print the dimensions of the result matrix
cout << "Matrix multiplication completed." << endl;
cout << "The result matrix has dimensions: " << ax << " x " << by << endl;
}
// Function to calculate and print error metrics
void ErrCalculator(float** normal_result, float** bfp_result, int RX, int RY) {
float sum_absolute_error = 0.0f;
float sum_relative_error = 0.0f;
int count = 0;
for (int i = 0; i < RX; ++i) {
for (int j = 0; j < RY; ++j) {
float abs_error = abs(normal_result[i][j] - bfp_result[i][j]);
sum_absolute_error += abs_error;
if (abs(normal_result[i][j]) > 1e-30) { // Avoid division by zero
float relative_error = abs_error / abs(normal_result[i][j]);
sum_relative_error += relative_error;
}
++count;
}
}
cout << "Mean absolute error: " << sum_absolute_error / count << endl;
cout << "Mean relative error: " << sum_relative_error / count << endl;
}
// Calculate statistics
void StatCalculator(float* times, float& avg_time, float& std_dev, int NRuns) {
for (int i = 0; i < NRuns; ++i) {
avg_time += times[i];
}
avg_time /= NRuns;
for (int i = 0; i < NRuns; ++i) {
std_dev += pow(times[i] - avg_time, 2);
}
std_dev = sqrt(std_dev / NRuns);
}