-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathver1_pt.cpp
More file actions
166 lines (139 loc) · 4 KB
/
Copy pathver1_pt.cpp
File metadata and controls
166 lines (139 loc) · 4 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
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
// #include <pthread.h>
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
double **A;
double **B;
double **C;
double **D;
int n;
int threads;
int sqrt_threads;
int chunk_size;
struct thread_data {
int k;
int thread_id;
};
// Function implementing SUMMA algorithm
void* SUMMA(void *args)
{
int k;
int thread_id;
// struct thread_data *my_data;
thread_data *my_data = (thread_data *) args;
thread_id = my_data->thread_id;
k = my_data->k;
int chunk_i = thread_id/sqrt_threads;
int chunk_j = thread_id%sqrt_threads;
int max_chunk_i = min((int)(chunk_i+1)*chunk_size, n);
int max_chunk_j = min((int)(chunk_j+1)*chunk_size, n);
for (int i=chunk_i*chunk_size; i< max_chunk_i; ++i) {
for (int j=chunk_j*chunk_size; j< max_chunk_j; ++j) {
C[i][j] += A[i][k]*B[k][j];
}
}
pthread_exit(0);
}
void Serial(double** A, double** B, double** C, int n)
{
for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
C[i][j] += A[i][k]*B[k][j];
}
}
}
}
// Function to initialize the matrix with random floating point values
void Initialize(double** matrix, int n)
{
srand((unsigned int)time(NULL));
sleep(1);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
*(*(matrix+i)+j) = ((double)rand() / (double)RAND_MAX)*((double)RAND_MAX - 1);
}
}
}
//Function to print the matrix
void PrintMatrix(double** matrix, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << *(*(matrix+i)+j) << " ";
}
cout << endl;
}
}
int Validate(double** A, double** B, int n) {
int mistakes = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (A[i][j] != B[i][j]) mistakes++;
}
}
return mistakes;
}
int main(int argc, char const* argv[])
{
if (argc != 3) {
cout << "Usage: ./ver0 <matrix_size> <threads>" << endl;
return 1;
}
n = atoi(argv[1]);
threads = atoi(argv[2]);
sqrt_threads = sqrt(threads);
chunk_size = n/sqrt_threads;
A = (double **)malloc(sizeof(double*) * n);
B = (double **)malloc(sizeof(double*) * n);
C = (double **)malloc(sizeof(double*) * n);
D = (double **)malloc(sizeof(double*) * n);
for (int i = 0; i < n; i++)
{
A[i]= (double*)malloc(sizeof(double) * n);
B[i]= (double*)malloc(sizeof(double) * n);
C[i]= (double*)malloc(sizeof(double) * n);
D[i]= (double*)malloc(sizeof(double) * n);
}
Initialize(A, n);
Initialize(B, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
*(*(C+i)+j) = 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
*(*(D+i)+j) = 0;
}
}
// cout << "Matrix A:" << endl;
// PrintMatrix(A, n);
// cout << "Matrix B:" << endl;
// PrintMatrix(B, n);
pthread_t workers[threads];
struct thread_data thread_data_array[threads];
auto now = chrono::system_clock::now();
for (long k = 0; k < n; ++k) {
for (long thread_id = 0; thread_id < threads; ++thread_id) {
thread_data_array[thread_id].k = k;
thread_data_array[thread_id].thread_id = thread_id;
pthread_create(workers + thread_id, NULL, SUMMA, (void *) &thread_data_array[thread_id]);
}
for (int thread_id = 0; thread_id < threads; ++thread_id)
pthread_join(workers[thread_id], NULL);
}
cout << "millisec=" << std::chrono::duration_cast<std::chrono::milliseconds>(chrono::system_clock::now() - now).count() << "\n";
now = chrono::system_clock::now();
Serial(A, B, D, n);
cout << "millisec=" << std::chrono::duration_cast<std::chrono::milliseconds>(chrono::system_clock::now() - now).count() << "\n";
cout << Validate(C, D, n) << endl;
return 0;
}