-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainMatrixMultiplication_Threads.hpp
97 lines (75 loc) · 2.19 KB
/
MainMatrixMultiplication_Threads.hpp
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
#include "igcl/igcl.hpp"
#include <cstring>
#include <iomanip>
using namespace std;
typedef unsigned int DATATYPE;
#define N_THREADS 2
#define MATSIZE 2048
int nTests = 32;
void fillMatrix(DATATYPE * mat, uint rows, uint cols) {
for (uint i = 0; i < rows; i++) {
for (uint j = 0; j < cols; j++) {
mat[i*cols+j] = (unsigned int) (double(rand()) / RAND_MAX * 10);
}
}
}
void transpose(DATATYPE * mat, uint size) { // only works for square matrices!
for (uint i = 0; i < size-1; i++) {
for (uint j = i+1; j < size; j++) {
swap(mat[i*size+j], mat[j*size+i]);
}
}
}
bool confirm(const DATATYPE * mat_a, const DATATYPE * mat_b, const DATATYPE * result, uint size) { // only works for square matrices!
#pragma omp parallel for
for (uint i = 0; i < size; i++) {
for (uint j = 0; j < size; j++) {
DATATYPE sum = 0;
for (uint k = 0; k < size; k++) {
sum += mat_a[i*size+k] * mat_b[j*size+k];
}
//cout << setw(5) << sum << ' ';
if (sum != result[i*size+j]) {
cout << "WROOOOOOOOOOOOOOOOOOONG!!! expected: " << sum << " got: " << result[i*size+j] << endl;
i = size;
break;
}
}
//cout << endl;
}
return true;
}
void runCoordinator(igcl::Coordinator * coord)
{
for(int test=0; test<nTests; test++) {
DATATYPE * mat_a, * mat_b, * mat_result;
mat_a = (DATATYPE *) malloc(MATSIZE * MATSIZE * sizeof(DATATYPE));
mat_b = (DATATYPE *) malloc(MATSIZE * MATSIZE * sizeof(DATATYPE));
mat_result = (DATATYPE *) malloc(MATSIZE * MATSIZE * sizeof(DATATYPE));
srand(0);
fillMatrix(mat_a, MATSIZE, MATSIZE);
fillMatrix(mat_b, MATSIZE, MATSIZE);
transpose(mat_b, MATSIZE);
timeval iniTime, endTime;
gettimeofday(&iniTime, NULL);
#pragma omp parallel for firstprivate(mat_a, mat_b, mat_result) num_threads(N_THREADS)
for (uint i = 0; i < MATSIZE; i++) {
for (uint j = 0; j < MATSIZE; j++) {
DATATYPE sum = 0;
for (uint k = 0; k < MATSIZE; k++) {
sum += mat_a[i*MATSIZE+k] * mat_b[j*MATSIZE+k];
}
mat_result[i*MATSIZE+j] = sum;
}
}
gettimeofday(&endTime, NULL);
printf("Time = %ld ms\n", timeDiff(iniTime, endTime));
//confirm(mat_a, mat_b, mat_result, MATSIZE);
free(mat_a);
free(mat_b);
free(mat_result);
}
}
void runPeer(igcl::Peer * peer)
{
}