-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix_Multiplication_with_processes.c
More file actions
414 lines (335 loc) · 11 KB
/
Matrix_Multiplication_with_processes.c
File metadata and controls
414 lines (335 loc) · 11 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<errno.h>
#include<string.h>
pid_t cur;
int rs[5], re[5], cs[5], ce[5];
int r1, r2, c1, c2;
struct Parsed{
int num;
char *file1, *file2;
};
void usage(){
printf("Usage: Take parameters for matrix multiplication\n");
printf(" Options:\n");
printf(" -i \tTakes filename path\n");
printf(" -j \tTakes filename path\n");
printf(" -n \tThe number of integers in a matrix is 2^n * 2^n\n");
exit(0);
}
struct Parsed parseCommandLine(int argc, char *argv[]){
int opt, count=0;
struct Parsed ret;
int done[3] = {0,0,0};
char *tmp;
while((opt = getopt(argc, argv, ":i:j:n:")) != -1)
{
switch(opt)
{
case 'i':
if (done[0]!=0){
printf("Invalid command line argument, i used twice\n");
usage();
}
ret.file1 = strdup(optarg);
done[0] = 1;
break;
case 'j':
if (done[1]!=0){
printf("Invalid command line argument, j used twice\n");
usage();
}
ret.file2 = strdup(optarg);
done[1] = 1;
break;
case 'n':
if (done[2]!=0){
printf("Invalid command line argument, n used twice\n");
usage();
}
done[2] = 1;
count = strtol(optarg, &tmp, 10);
if (*tmp != '\0' || count>30 || count<0){
printf("The option -n requires a number between 0 to 30 as an input\n"); usage();
}
ret.num = count;
break;
case ':':
printf("option needs a value\n");
usage();
break;
case '?':
printf("unknown option: %c\n", optopt);
usage();
}
}
if(done[0]!=1 || done[1]!=1 || done[2]!=1){
printf("Too few arguments\n");
usage();
}
return ret;
};
void parent(int fd1[], int fd2[], int ind, int n, float Mat1[n][n], float Mat2[n][n], float Mat[n][n]){
// printf("Parent %d\n", ind);
int count;
if (ind==1){
close(fd1[0]);
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c1, sizeof(float));
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c1, sizeof(float));
for(int i=rs[1]; i<re[1]; i++) for(int j=cs[1]; j<ce[1]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[1]; i<re[1]; i++) for(int j=cs[1]; j<ce[1]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c2, sizeof(float));
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c1, sizeof(float));
for(int i=rs[2]; i<re[2]; i++) for(int j=cs[2]; j<ce[2]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[3]; i<re[3]; i++) for(int j=cs[3]; j<ce[3]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
close(fd2[1]); wait(NULL); // do something about it
read(fd2[0], &count, sizeof(int));
if(count==0) return;
for(int i=rs[1]; i<re[1]; i++)
for(int j=cs[1]; j<ce[1]; j++)
read(fd2[0], &Mat[i][j], sizeof(float));
}
else if (ind==2){
close(fd1[0]);
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c1, sizeof(float));
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c2, sizeof(float));
for(int i=rs[1]; i<re[1]; i++) for(int j=cs[1]; j<ce[1]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[2]; i<re[2]; i++) for(int j=cs[2]; j<ce[2]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c2, sizeof(float));
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c2, sizeof(float));
for(int i=rs[2]; i<re[2]; i++) for(int j=cs[2]; j<ce[2]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[4]; i<re[4]; i++) for(int j=cs[4]; j<ce[4]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
close(fd2[1]); // do something about it
read(fd2[0], &count, sizeof(int));
if(count==0) return;
for(int i=rs[2]; i<re[2]; i++)
for(int j=cs[2]; j<ce[2]; j++)
read(fd2[0], &Mat[i][j], sizeof(float));
}
else if (ind==3){
close(fd1[0]);
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c1, sizeof(float));
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c1, sizeof(float));
for(int i=rs[3]; i<re[3]; i++) for(int j=cs[3]; j<ce[3]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[1]; i<re[1]; i++) for(int j=cs[1]; j<ce[1]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c2, sizeof(float));
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c1, sizeof(float));
for(int i=rs[4]; i<re[4]; i++) for(int j=cs[4]; j<ce[4]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[3]; i<re[3]; i++) for(int j=cs[3]; j<ce[3]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
close(fd2[1]); wait(NULL); // do something about it
read(fd2[0], &count, sizeof(int));
if(count==0) return;
for(int i=rs[3]; i<re[3]; i++)
for(int j=cs[3]; j<ce[3]; j++)
read(fd2[0], &Mat[i][j], sizeof(float));
}
else if (ind==4){
close(fd1[0]);
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c1, sizeof(float));
write(fd1[1], &r1, sizeof(float)); write(fd1[1], &c2, sizeof(float));
for(int i=rs[3]; i<re[3]; i++) for(int j=cs[3]; j<ce[3]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[2]; i<re[2]; i++) for(int j=cs[2]; j<ce[2]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
close(fd1[0]);
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c2, sizeof(float));
write(fd1[1], &r2, sizeof(float)); write(fd1[1], &c2, sizeof(float));
for(int i=rs[4]; i<re[4]; i++) for(int j=cs[4]; j<ce[4]; j++) write(fd1[1], &Mat1[i][j], sizeof(float));
for(int i=rs[4]; i<re[4]; i++) for(int j=cs[4]; j<ce[4]; j++) write(fd1[1], &Mat2[i][j], sizeof(float));
close(fd2[1]); wait(NULL); // do something about it
read(fd2[0], &count, sizeof(int));
if(count==0) return;
for(int i=rs[4]; i<re[4]; i++)
for(int j=cs[4]; j<ce[4]; j++)
read(fd2[0], &Mat[i][j], sizeof(float));
}
}// parent
void child(int fd1[], int fd2[], int ind){
int R1, C1, R2, C2;
int count = 0;
switch(ind){
case 1: if(r1==0 || c1==0){
close(fd2[1]); write(fd2[0], &count, sizeof(int)); exit(0);
}
case 2: if(r1==0 || c2==0){
close(fd2[1]); write(fd2[0], &count, sizeof(int)); exit(0);
}
case 3: if(r2==0 || c1==0){
close(fd2[1]); write(fd2[0], &count, sizeof(int)); exit(0);
}
case 4: if(r2==0 || c2==0){
close(fd2[1]); write(fd2[0], &count, sizeof(int)); exit(0);
}
}
close(fd1[1]);
read(fd1[0], &R1, sizeof(float)); read(fd1[0], &C1, sizeof(float));
read(fd1[0], &R2, sizeof(float)); read(fd1[0], &C2, sizeof(float));
// answer matrix
float Mat3[R1][C2]; for(int i=0;i<R1;i++) for(int c=0; c<C2; c++) Mat3[i][c] = 0.0;
float Mat1[R1][C1], Mat2[R2][C2];
for(int r=0; r<R1; r++) for(int c=0; c<C1; c++) read(fd1[0], &Mat1[r][c], sizeof(float));
for(int r=0; r<R2; r++) for(int c=0; c<C2; c++) read(fd1[0], &Mat2[r][c], sizeof(float));
// matrix multiplication
for(int i=0; i<R1; i++)
for(int k=0; k<C2; k++)
for(int j=0; j<C1; j++)
Mat3[i][k] += Mat1[i][j] * Mat2[j][k];
read(fd1[0], &R1, sizeof(float)); read(fd1[0], &C1, sizeof(float));
read(fd1[0], &R2, sizeof(float)); read(fd1[0], &C2, sizeof(float));
float Mat4[R1][C1], Mat5[R2][C2];
for(int r=0; r<R1; r++) for(int c=0; c<C1; c++) read(fd1[0], &Mat4[r][c], sizeof(float));
for(int r=0; r<R2; r++) for(int c=0; c<C2; c++) read(fd1[0], &Mat5[r][c], sizeof(float));
for(int i=0; i<R1; i++)
for(int k=0; k<C2; k++)
for(int j=0; j<C1; j++)
Mat3[i][k] += Mat4[i][j] * Mat5[j][k];
close(fd2[0]);
count = R1*C2;
write(fd2[1], &count, sizeof(int));
for(int i=0; i<R1; i++) for(int j=0; j<C2; j++) write(fd2[1], &Mat3[i][j], sizeof(float));
exit(0);
}
int setpipe(int n, int m, int fd1[n][2], int fd2[n][2]){
for(int i=1; i<=m;i++){
if ( pipe(fd1[i-1])==-1 ){
perror("Pipe Failed for fd1");
return 1;
}
if ( pipe(fd2[i-1])==-1 ){
perror("pipe Failed for fd2");
return 1;
}
}
rs[1] = 0; re[1] = n/2; cs[1] = 0, ce[1] = n/2;
rs[2] = 0; re[2] = n/2; cs[2] = n/2, ce[2] = n;
rs[3] = n/2; re[3] = n; cs[3] = 0, ce[3] = n/2;
rs[4] = n/2; re[4] = n; cs[4] = n/2, ce[4] = n;
r1 = n/2; r2=n-n/2;
c1 = n/2; c2=n-n/2;
return 0;
}
void readfile(char * filename, int times, float arr[]){
FILE * fp; fp = fopen(filename, "r");
char tmp[200]; strcpy(tmp, filename);
int i=0;
if( fp == 0 )
{
switch(errno)
{
case EPERM:
perror("Operation not permitted");
break;
case ENOENT:
case ENFILE:
strcat(tmp, " not found");
perror(tmp);
break;
case EACCES:
perror(filename);
break;
case ENAMETOOLONG:
perror("Filename is too long");
break;
default:
fprintf(stderr,"Unknown error\n");
}
exit(0);
}
while(i<times && !feof(fp))
fscanf(fp, "%f", &arr[i++]);
if(i < times) { printf("Not enough values in %s \n", filename); exit(0);}
fclose(fp);
}
void get_val(float arr1[], float arr2[], int size, struct Parsed values){
readfile(values.file1, size , arr1);
readfile(values.file2, size , arr2);
}
void handler(int signum){
kill(cur, SIGTERM);
printf("\nSignal Ctrl + C Caught\n");
printf("Killing open child processes\n");
printf("bye\n");
exit(signum);
}
int main(int argc, char *argv[]){
signal(SIGINT, handler);
struct Parsed values = parseCommandLine(argc, argv);
int PROC = 4, fd1[PROC][2], fd2[PROC][2], N=1;
pid_t pid;
for(int i=0; i<values.num; i++) N *= 2; // N is 2^n
float arr1[N*N], arr2[N*N];
get_val(arr1, arr2, N*N, values);
float MAT1[N][N], MAT2[N][N], MAT3[N][N];
for(int i=0; i<N; i++)
for(int j=0 ;j<N; j++)
MAT1[i][j] = arr1[i*N+j];
for(int i=0; i<N; i++)
for(int j=0 ;j<N; j++)
MAT2[i][j] = arr2[i*N+j];
if(setpipe(N, 4, fd1, fd2)) exit(0);
// -----------------------------------------------
// process 1
pid = fork();
if (pid < 0 ){
perror("forking failed for process 1"); exit(0);
}
else if (pid==0){
// child process
child(fd1[0], fd2[0], 1);
}
else if (pid > 0) {
cur = pid;
parent(fd1[0], fd2[0], 1, N, MAT1, MAT2, MAT3);
signal(SIGCHLD, SIG_IGN);
pid = fork();
}
// process 2
if (pid < 0 ){
perror("forking failed for process 2"); exit(0);
}
else if (pid==0){
// child process
child(fd1[1], fd2[1], 2);
}
else if (pid > 0) {
cur = pid;
parent(fd1[1], fd2[1], 2, N, MAT1, MAT2, MAT3);
signal(SIGCHLD, SIG_IGN);
pid = fork();
}
// process 3
if (pid < 0 ){
perror("forking failed for process 3"); exit(0);
}
else if (pid==0){
// child process
child(fd1[2], fd2[2], 3);
}
else if (pid > 0) {
cur = pid;
parent(fd1[2], fd2[2], 3, N, MAT1, MAT2, MAT3);
signal(SIGCHLD, SIG_IGN);
pid = fork();
}
// process 4
if (pid < 0 ){
perror("forking failed for process 4"); exit(0);
}
else if (pid==0){
// child process
child(fd1[3], fd2[3], 4);
}
else if (pid > 0) {
cur = pid;
parent(fd1[3], fd2[3], 4, N, MAT1, MAT2, MAT3);
signal(SIGCHLD, SIG_IGN);
}
// ----------------------------------------
for(int i=0; i<N; i++){
for(int j=0; j<N; j++) printf("%-20f ", MAT3[i][j]);
printf("\n");
}
return 0;
}