-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_helpers.cpp
176 lines (161 loc) · 5.59 KB
/
matrix_helpers.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// Created by eva on 30.11.20.
//
#include "matrix_helpers.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <chrono>
#include <omp.h>
using namespace std;
/**
* Fill a Matrix with random numbers (and sparsity)
*/
void fillRandomMatrix(long *matrix, int height, int width, int range) {
for (int r = 0; r < height; r++)
for (int c = 0; c < width; c++) {
matrix[r * width + c] = rand() % range;
}
}
/**
* Reset a Matrix with zeros
*/
void resetMatrix(long *matrix, int height, int width) {
for (int r = 0; r < height; r++)
for (int c = 0; c < width; c++) {
matrix[r * width + c] = 0;
}
}
/**
* Print given Matrix
*/
void printMatrix(long *matrix, int height, int width, string name) {
cout << "Matrix: " << name << "\n";
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
cout << matrix[r * width + c] << "\t";
}
cout << "\n";
}
cout << "\n";
}
/**
* Calculates the average value of a given long-array
* @param array
* @param length
* @return the average as double
*/
float calcAvg(float *array, int length) {
float sum = 0.0;
for (int i=0; i<length; i++) {
sum += array[i];
}
return (float) sum/length;
}
/**
* Prety print elapsed time of an algorithm
* @param duration the measured time
* @param algorithm the name for he algorithm
*/
void printTimeDiff(long duration, string algorithm) {
cout << "Duration for "<< algorithm << ": \t" << duration << " microseconds\n";
}
long checkCorrectness(long *matrix, long *reference, int height, int width) {
int row, col;
long diff = 0;
long diff_i = 0;
for (row=0; row<height; row++) {
for (col = 0; col < width; col++) {
if (matrix[row * width + col] != reference[row * width + col]) {
diff_i = abs(matrix[row * width + col] - reference[row * width + col]);
// printf("Difference at [%i][%i]: %ld\n", row, col, diff_i);
diff += diff_i;
}
}
}
return diff;
}
void printAverages(string *alg_names, float *alg_averages, int limit, int linepoint) {
//print averages
printf("Compare run times:\n");
printf("------------------\n");
for (int a = 0; a < limit; a++) {
printf("%s\t |%.0f| mls\n", alg_names[a].c_str(), alg_averages[a]);
if ((a%linepoint) == 1) printf("------------------\n");
}
}
/**
* Prints information and measures the time for a given algorithm
*
* @param func The matrix multiplication algorithm to be measured
* @param result The result matrix
* @param a Input matrix 1
* @param b Input matrix 2
* @param heightR y-Dimension of result matrix
* @param widthR x-Dimension of result matrix
* @param widthA x-Dimension of Input matrix 1
* @param duration an array to store teh time used on the algorithm
* @param loops Number of iterations the multiplication takes place
* @param print If the result matrix should be printed or not
* @param description The name of the algorithm
*
* @return the average of durations
*/
float doTheMeasuring(void func(long *result, long *a, long *b, int heightR, int widthR, int widthA),
long *result, long *a, long *b, int heightR, int widthR, int widthA,
int loops, bool print, long *refMatrix, string description){
float duration[loops];
long error;
bool errorBool = false;
printf("Alorithm: %s\n", description.c_str());
printf("----------------------------------\n");
for (int loop = 0; loop < loops; loop++) {
resetMatrix(result, heightR, widthR);
duration[loop] = 0;
auto begin = chrono::high_resolution_clock::now();
func(result, a, b, heightR, widthR, widthA);
auto end = chrono::high_resolution_clock::now();
error = checkCorrectness(result, refMatrix, heightR, widthR);
if (error > 0) errorBool = true;
duration[loop] = (float) chrono::duration_cast<chrono::microseconds>(end - begin).count() / 1000;
if (print) printMatrix(result, heightR, widthR, description);
printf("-- %i. Loop Duration: \t %.3f mls\n", loop, duration[loop]);
printf(" Error in calculation:\t %ld\n", error);
}
if (errorBool){
printf("Errors\n\n");
return -1;
}
float avg = calcAvg(duration, loops);
printf("Average Duration:\t %.0f mls\n\n", avg);
return avg;
}
float doIncreasingMeasuring(void func(long *result, long *a, long *b, int heightR, int widthR, int widthA, int threads),
long *result, long *a, long *b, int size,
int loops, long *refMatrix, string description, int threads){
float duration[loops];
long error;
bool errorBool = false;
printf("Alorithm: %s\n", description.c_str());
printf("----------------------------------\n");
for (int loop = 0; loop < loops; loop++) {
resetMatrix(result, size, size);
duration[loop] = 0;
auto begin = chrono::high_resolution_clock::now();
func(result, a, b, size, size, size, threads);
auto end = chrono::high_resolution_clock::now();
error = checkCorrectness(result, refMatrix, size, size);
if (error > 0) errorBool = true;
duration[loop] = (float) chrono::duration_cast<chrono::microseconds>(end - begin).count();
printf("-- %i. Loop Duration: \t %.3f mys\n", loop, duration[loop]);
printf(" Error in calculation:\t %ld\n", error);
}
if (errorBool){
printf("Errors\n\n");
return -1;
}
float avg = calcAvg(duration, loops);
printf("Average Duration:\t %.0f mys\n\n", avg);
return avg;
}