-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMM_Pthread.cpp
More file actions
238 lines (208 loc) · 6.45 KB
/
MM_Pthread.cpp
File metadata and controls
238 lines (208 loc) · 6.45 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <float.h>
#include <time.h>
#include <math.h>
#include <sys/time.h> // for clock_gettime()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct thread_args {
float* A;
float* B;
float* C;
int rows;
int cols;
int numThreads;
int threadId;
};
bool printprogress = false;
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
void* MatrixMulti_Partial_Pthread_worker(void* data)
{
struct thread_args* my_data = (struct thread_args*) data;
float* A = my_data->A;
float* B = my_data->B;
float* C = my_data->C;
int numThreads = my_data->numThreads;
int threadId = my_data->threadId;
int rows = my_data->rows;
int cols = my_data->cols;
//assign rows consecutively on the threads
int start_row, end_row;
start_row = (threadId*rows) / numThreads;
end_row = ((threadId+1)*rows) / numThreads;
for (int i = start_row; i < end_row; ++i)
{
if(printprogress)
printf("row = %d\n", i);
for (int j = 0; j < cols; ++j) {
float sum = 0;
for (int k = 0; k < cols; ++k) {
float a = A[i * cols + k];
float b = B[k * cols + j];
sum += a * b;
}
C[i * cols + j] = sum;
}
}
pthread_exit(NULL);
return 0;
}
void MatrixMulti_Partial_Pthread(float* A, float* B, float* C, int rows, int cols, const int numThreads)
{
pthread_t *thread = (pthread_t*)malloc(sizeof(pthread_t)*numThreads);
struct thread_args *thread_args_array = (struct thread_args *)malloc(sizeof(struct thread_args)*numThreads);
int i;
for(i = 0; i<numThreads; i++)
{
thread_args_array[i].threadId = i;
thread_args_array[i].numThreads = numThreads;
thread_args_array[i].A = A;
thread_args_array[i].B = B;
thread_args_array[i].C = C;
thread_args_array[i].rows = rows;
thread_args_array[i].cols = cols;
pthread_create(&thread[i], NULL, MatrixMulti_Partial_Pthread_worker, (void *)&thread_args_array[i]);
}
for(i = 0; i<numThreads; i++)
{
pthread_join(thread[i], NULL);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void MatrixMulti_Serial(float* A, float* B, float* C, int N)
{
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j) {
float sum = 0;
for (int k = 0; k < N; ++k) {
float a = A[i * N + k];
float b = B[k * N + j];
sum += a * b;
}
C[i * N + j] = sum;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void MatrixMulti_Partial(float* A, float* B, float* C, int rows, int N)
{
for (int i = 0; i < rows; ++i)
for (int j = 0; j < N; ++j) {
float sum = 0;
for (int k = 0; k < N; ++k) {
float a = A[i * N + k];
float b = B[k * N + j];
sum += a * b;
}
C[i * N + j] = sum;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
void RandomInit(float* data, int N)
{
for (int i=0; i<N; i++)
{
data[i] = (float)(rand()%100)+1;
}
}
void printMatrix(float* A, int N){
int i, j;
for (i=0; i<N; i++){
printf("| ");
for(j=0; j<N; j++)
printf("%7.2f ", A[i * N + j]);
printf("|\n");
}
}
void copyMatrix(float* A, int N, float* B){
int i, j;
for (i=0; i<N; i++){
for (j=0; j<N; j++){
B[i * N + j] = A[i * N + j];
}
}
}
bool isMatrixSame(float* A, int N, float* B){
int i, j;
for (i=0; i<N; i++){
for (j=0; j<N; j++){
if((fabs(A[i * N + j] - B[i * N + j]) > 0.0001))
return false;
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
if(argc < 5)
{
printf("Arguments are incorect\n");
printf("Four arguemnts are required: printmatrix(0,1) checkrequired(0,1) matrixsize(postive integer number) numThreads(postive integter number)\n");
printf("Example: ./exe 0 1 2048 4\n");
return 0;
}
//initialize variables from arguments
int printMatrixRequired = atoi(argv[1]);
int checkRequired = atoi(argv[2]);
int N = atoi(argv[3]);
int numThreads = atoi(argv[4]);
printprogress = printMatrixRequired;
float* A_master, * B_master, * C_master;
int matrix_size = N*N;
//clock_t start, end;
double cpu_time_used;
struct timeval start, end;
gettimeofday(&start, NULL);
if(N % numThreads != 0)
{
printf("Matrix size should be multiple of the number of processes!\n");
return 0;
}
printf("Allocate Matrix 'A', 'B', 'C' for master node\n");
A_master = (float*)malloc(matrix_size*sizeof(float));
B_master = (float*)malloc(matrix_size*sizeof(float));
C_master = (float*)malloc(matrix_size*sizeof(float));
RandomInit(A_master, matrix_size);
RandomInit(B_master, matrix_size);
// Matrix Multiplication Per Process
printf("MatrixMulti\n");
MatrixMulti_Partial_Pthread(A_master, B_master, C_master, N, N, numThreads);
gettimeofday(&end, NULL);
cpu_time_used = ((end.tv_sec - start.tv_sec) * 1000000u + end.tv_usec - start.tv_usec) / 1.e6;
printf("Elapsed Time= %f seconds \n", cpu_time_used);
if(printMatrixRequired)
{
printf("A=\n");
printMatrix(A_master,N);
printf("B=\n");
printMatrix(B_master,N);
printf("C=\n");
printMatrix(C_master,N);
}
if(checkRequired)
{
printf("Checking Correctness....\n");
gettimeofday(&start, NULL);
printf("N = %d\n", N);
float* C_serial = (float*)malloc(matrix_size*sizeof(float));
MatrixMulti_Serial(A_master, B_master, C_serial, N);
gettimeofday(&end, NULL);
cpu_time_used = ((end.tv_sec - start.tv_sec) * 1000000u + end.tv_usec - start.tv_usec) / 1.e6;
printf("Sequntial Elapsed Time= %f seconds \n", cpu_time_used);
if(printMatrixRequired)
{
printf("CSequntial=\n");
printMatrix(C_serial,N);
}
if(isMatrixSame(C_master, N, C_serial))
printf("Correct!\n");
else
printf("Incorrect!\n");
}
}