-
Notifications
You must be signed in to change notification settings - Fork 0
/
matmul_hompi.c
295 lines (203 loc) · 5.69 KB
/
matmul_hompi.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <omp.h>
#include <torc.h>
#define N 1024 //Matrix size
#define NTASK M*M //Number of submatrices
#define L 4 //Number of worker threads (optimally equal to number of physical cores,
//or physical threads if multithreading technology is present)
#define NUM_OF_TESTS 1
//Function & data declarations
int **A, *A_data, **B, *B_data, **C, *C_data;
int *res_data, **res; // Reusable across tasks
int readmat(char *fname, int *mat, int n),
writemat(char *fname, int *mat, int n);
int M, S; // Must inform every node of these dimensions
void taskexecute(int wid, int *results);
void *threadworker(void *arg);
//Global variable declaration
int taskid = 0; //Next task's id
int ntask;
#pragma ompix taskdef in(m,s) inout(A_input[1024 * 1024], B_input[1024 * 1024])
void node_instat(int m, int s, int *A_input, int *B_input)
{
int i;
M = m;
S = s;
A_data = (int *) malloc(N * N * sizeof(int));
B_data = (int *) malloc(N * N * sizeof(int));
A = (int **) malloc (N * sizeof(int *));
B = (int **) malloc (N * sizeof(int *));
for (i = 0; i < N; i++)
{
A[i] = (int *) &(A_data[N * i]);
B[i] = (int *) &(B_data[N * i]);
}
for (i = 0; i < N * N; i++)
{
A_data[i] = A_input[i];
B_data[i] = B_input[i];
}
res_data = (int *) malloc (S * S * sizeof(int));
res = (int **) malloc (S * sizeof (int *));
return;
}
int **A, *A_data, **B, *B_data, **C, *C_data;
int *res_data, **res; // Reusable across tasks
#pragma ompix taskdef
void node_free_mem()
{
free(A[0]);
free(A);
free(B[0]);
free(B);
free(res[0]);
free(res);
}
#pragma ompix taskdef in (wid) out(results[S * S])
void taskexecute(int wid, int *results){
int i, j, k, x, y, outer_limit, inner_limit, r_i = 0;
int sum = 0;
x = wid / M;
y = wid % M;
outer_limit = (x + 1) * S; //Computed here once, so 'for' loops don't
inner_limit = (y + 1) * S; //have to perform these multiplications
//on each iteration.
for (i = x * S; i < outer_limit; i++){
for (j = y * S; j < inner_limit; j++){
for (k = 0, sum = 0; k < N; k++)
sum += A[i][k] * B[k][j];
results[r_i++] = sum;
}
}
return;
}
{
// Write retrieved results to final array
int i, j, x, y, outer_limit, inner_limit, r_i = 0;
// printf("Task %d callback\n", wid);
x = wid / M;
y = wid % M;
outer_limit = (x + 1) * S;
inner_limit = (y + 1) * S;
for (i = x * S; i < outer_limit; i++)
for (j = y * S; j < inner_limit; j++)
C[i][j] = results[r_i++];
}
int main(int argc, char *argv[])
{
int i, task_num, **master_res;
double start_time, end_time, test_times[NUM_OF_TESTS], avg_time = 0;
A_data = (int *) malloc (N * N * sizeof(int));
B_data = (int *) malloc (N * N * sizeof(int));
C_data = (int *) malloc (N * N * sizeof(int));
A = (int **) malloc (N * sizeof(int *));
B = (int **) malloc (N * sizeof(int *));
C = (int **) malloc (N * sizeof(int *));
for (i = 0; i < N; i++)
{
A[i] = (int *) &(A_data[N * i]);
B[i] = (int *) &(B_data[N * i]);
C[i] = (int *) &(C_data[N * i]);
}
if (argc == 1){
printf("Submatrix size must be entered in command "
"line arguments (32, 64, or 256)\n");
return (1);
}
S = atoi(argv[1]);
//M must be 4, 16, 32
//Therefore, s must be 256, 64, 32, respectively.
if (S != 256 && S != 64 && S != 32){
printf("Acceptable submatrix sizes: 32, 64, 256\n");
return (1);
}
M = N / S;
ntask = NTASK;
// Need this many result arrays
master_res = (int **) malloc (ntask * sizeof(int *));
for (i = 0; i < ntask; i++)
master_res[i] = (int *) malloc (S * S * sizeof(int));
/* Read matrices from files: "A_file", "B_file"
*/
if (readmat("Amat1024", (int *) A_data, N) < 0)
exit( 1 + printf("file problem\n") );
if (readmat("Bmat1024", (int *) B_data, N) < 0)
exit( 1 + printf("file problem\n") );
// Inform all worker nodes of the decided upon task dimensions
for (i = 1; i < torc_num_nodes(); i++)
{
#pragma ompix task atnode(i)
node_instat(M, S, A_data, B_data);
}
#pragma ompix tasksync
start_time = torc_gettime();
while(1){
task_num = taskid++;
if (task_num >= ntask)
break; //All tasks created
#pragma ompix task
taskexecute(task_num, master_res[task_num]);
}
#pragma ompix tasksync
end_time = torc_gettime();
test_times[i] = end_time - start_time;
avg_time += test_times[i];
// No need to wait for them, we can wait right before we finish
for (i = 1; i < torc_num_nodes(); i++)
{
#pragma ompix task atnode(i)
node_free_mem();
}
for (i = 0; i < NUM_OF_TESTS; ++i)
printf("Test #%d: %lf seconds\n", i + 1, test_times[i]);
printf("[S = %d] Average time: %f\n", S, avg_time / (double) NUM_OF_TESTS);
/* Save result in "Cmat1024Results"
*/
writemat("Cmat1024Results", (int *) C_data, N);
for (i = 0; i < ntask; i++)
free(master_res[i]);
free(master_res);
// First line frees the data, second one the int * array we used.
free(A[0]);
free(A);
free(B[0]);
free(B);
free(C[0]);
free(C);
#pragma ompix tasksync
return (0);
}
/* Utilities to read & write matrices from/to files
* VVD
*/
#define _mat(i,j) (mat[(i)*n + (j)])
int readmat(char *fname, int *mat, int n){
FILE *fp;
int i, j;
if ((fp = fopen(fname, "r")) == NULL)
return (-1);
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
if (fscanf(fp, "%d", &_mat(i,j)) == EOF){
fclose(fp);
return (-1);
};
}
}
fclose(fp);
return (0);
}
int writemat(char *fname, int *mat, int n){
FILE *fp;
int i, j;
if ((fp = fopen(fname, "w")) == NULL)
return (-1);
for (i = 0; i < n; i++, fprintf(fp, "\n")){
for (j = 0; j < n; j++)
fprintf(fp, " %d", _mat(i, j));
}
fclose(fp);
return (0);
}