-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2IPC.c
471 lines (396 loc) · 14 KB
/
p2IPC.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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/ipc.h>
#include <sys/shm.h>
typedef long long int lli;
#define MAX_THREADS 20
#define THREAD_T 3
#define THREAD_F 1
#define MAX_CMD_LENGTH 100
/* GLOBAL VARIABLES */
lli I, J, K;
lli **matrix1, **matrix2, **output;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
// lli *matrix1, *matrix2, *output;
void create_threads_and_multiply (int max_thread_count, lli* row, int row_no);
/* USER-DEFINED DATA STRUCTURES */
typedef struct {
/*
FILENAME := name of file to read from
MATRIX := name of matrix which needs to be populated
START ROW := reference to the line which is to be read
MAX ROW := max rows in the matrix
COLS := number of columns to be read
LINES_PER_THREAD := max lines a thread can read
*/
char *filename;
lli **matrix;
int start_row;
lli max_rows; // CAN REMOVE
lli cols; // CAN REMOVE
int lines_per_thread;
} file_read_data;
typedef struct {
lli* row;
int row_no;
int start_column;
int end_column;
} working_cells_data;
/* single-threaded function to read matrix from the text file */
int read_matrix (const char* filename) {
/* get input file number from the file name */
char filename_as_string[10];
strcpy (filename_as_string, filename);
int file_number = filename_as_string[2] - '0';
lli rows, cols;
if (file_number == 1) {
rows = I;
cols = J;
} else {
rows = J;
cols = K;
}
FILE *fp;
fp = fopen (filename, "r");
if (fp == NULL) {
return EXIT_FAILURE;
}
for (lli _i = 0; _i < rows; ++_i) {
for (lli _j = 0; _j < cols; ++_j) {
if (file_number == 1) {
lli temp;
fscanf(fp, "%lld", &temp);
// fscanf(fp, "%lld", matrix1[_i][_j]);
matrix1[_i][_j] = temp;
} else {
lli temp;
fscanf(fp, "%lld", &temp);
// fscanf(fp, "%lld", matrix2[_i][_j]);
matrix2[_i][_j] = temp;
}
}
}
fclose (fp);
return EXIT_SUCCESS;
}
void *thread_read (void *arg) {
printf ("In thread_read function\n");
file_read_data file = *(file_read_data*) arg;
FILE* fp;
fp = fopen(file.filename, "r");
if (fp == NULL) {
printf ("Unable to read file %s\n", file.filename);
exit (EXIT_FAILURE);
}
printf ("Read %s successfully\n", file.filename);
// printf ("Matrix: %lld\n", **file.matrix);
int start_row = file.start_row;
int end_row = start_row + file.lines_per_thread;
lli max_rows = file.max_rows;
int cols = file.cols;
// ? --- DEBUG STATEMENTS ---
printf ("start_row: %d\n", start_row);
printf ("end_row: %d\n", end_row);
printf ("max_row: %lld\n", max_rows);
printf ("cols: %d\n", cols);
fseek (fp, start_row, SEEK_CUR);
lli i = 0;
char wait_temp;
// loop to skip to next line
while (i < start_row && fscanf (fp, "%c", &wait_temp) != EOF) {
if (wait_temp == '\n') {
i++;
}
}
for (i = start_row; i < end_row ; ++i) {
for (lli j = 0; j < cols; ++j) {
// printf ("(i, j) = (%lld, %lld) ", i, j);
// printf ("index = %lld\tvalue: ", (i * max_rows) + j);
// printf ("index = %lld\tvalue: ", (i * max_rows) + j);
// fscanf(fp, "%lld", &file.matrix[i][j]);
// fscanf(fp, "%lld", (lli *)file.matrix[i][j]);
// fscanf(fp, "%lld", &file.matrix[i][j]);
lli temp;
fscanf(fp, "%lld", &temp);
printf ("%lld | ", temp);
}
printf ("\n");
}
printf ("Thread execution completed successfully\n");
fclose (fp);
}
/* function to create threads and read text file */
void create_threads_and_read (int rows, int cols, int max_thread_count, file_read_data data, FILE *fp) {
if (fp == NULL) {
printf ("Unable to read file %s\n", data.filename);
exit (EXIT_FAILURE);
}
// ? --- DEBUG STATEMENT ---
printf ("File %s opened successfully\n", data.filename);
pthread_t threads[max_thread_count]; // {t1, t2}
int lines_read = 0;
printf ("max_thread_count: %d\n", max_thread_count);
for (int i = 0; i < max_thread_count; ++i) {
int lines_per_thread = (rows / max_thread_count);
// last thread may have some extra lines to read
if(i == max_thread_count - 1) {
lines_per_thread += (rows % max_thread_count);
}
printf ("lines read: %d\n", lines_read);
file_read_data file_read_thread_data = {
.filename = data.filename,
.cols = cols,
.lines_per_thread = lines_per_thread,
.start_row = lines_read,
.max_rows = data.max_rows
};
pthread_mutex_lock(&lock);
if (pthread_create(&threads[i], NULL, thread_read, &file_read_thread_data)) {
fprintf(stderr, "pthread_create failed!\n");
exit (EXIT_FAILURE);
}
pthread_mutex_unlock(&lock);
pthread_join (threads[i], NULL);
lines_read += lines_per_thread;
}
// ? --- DEBUG STATEMENT ---
printf ("Threads created\n");
// ! --- JOIN THE THREADS ---
for (int i = 0; i < max_thread_count; ++i) {
// printf ("Thread %d joined\t", i);
}
printf ("\n");
// ? --- DEBUG STATEMENT ---
printf ("All threads joined\n\n");
// ! --- KILL/CLOSE ALL THREADS ---
// fclose (fp);
}
void *thread_multiply (void *arg) {
printf ("In thread_multiply function\n");
working_cells_data data = *(working_cells_data*) arg;
// ? --- DEBUG STATEMENTS ---
// printf ("start_row: %d\n", start_row);
// printf ("end_row: %d\n", end_row);
// printf ("max_row: %lld\n", max_rows);
// printf ("cols: %d\n", cols);
for(int n=data.start_column; n<=data.end_column; n++)
{
output[data.row_no][n]=0;
for(int i=0; i<J; i++)
{
output[data.row_no][n] += data.row[i]*matrix2[i][n];
}
printf ("calculated cell: %lld\n", output[data.row_no][n]);
}
printf ("Thread execution completed successfully\n");
}
void shared_memory (file_read_data file) {
/* get input file number from the file name */
char *filename_as_string;
filename_as_string = (char *) malloc(10);
strcpy (filename_as_string, file.filename);
int file_number = filename_as_string[2] - '0';
// shmget returns an identifier in shmid
lli rows = file.max_rows;
lli cols = file.cols;
// for (int i = 0; i < I; ++i) {
// if(MAX_THREADS > J) {
// create_threads_and_multiply (J, matrix1[i], i);
// } else {
// create_threads_and_multiply (MAX_THREADS, matrix1[i], i);
// }
// }
if (file_number == 1) {
key_t key1 = ftok(file.filename, 65);
// int shmid1 = shmget(key1, (rows) * sizeof (lli*), 0666|IPC_CREAT);
int shmid1 = shmget(key1, (cols) * sizeof (lli *), 0666|IPC_CREAT);
lli *str1 = (lli*) shmat(shmid1, 0, 0);
lli k = 0;
for (lli i = 0; i < rows; ++i){
for (lli j = 0; j < cols; ++j){
// printf ("%lld ", str1[k]);
matrix1[i][j] = str1[k];
k++;
}
if(MAX_THREADS > J) {
create_threads_and_multiply (J, matrix1[i], i);
} else {
create_threads_and_multiply (MAX_THREADS, matrix1[i], i);
}
printf ("\n");
}
// shmdt(str1);
// shmctl (shmid1, IPC_RMID, NULL);
} else {
key_t key2 = ftok(file.filename, 66);
// matrix1 = (lli **) malloc (I * sizeof (lli *));
int shmid2 = shmget(key2, rows * cols * sizeof (lli *), 0666|IPC_CREAT);
// matrix2 = (lli **) malloc (J * sizeof (lli *));
lli *str2 = (lli*) shmat(shmid2, 0, 0);
printf ("Reading matrix 2 from IPC:\n");
for (lli i = 0; i < rows; ++i) {
for (lli j = 0; j < cols; ++j) {
// str2[i * rows + j] = matrix2[i][j];
// printf ("%lld ", str2[i * rows + j]);
matrix2[i][j] = str2[i * rows + j];
}
printf ("\n");
}
// str = matrix2;
// shmdt(str2);
// shmctl (shmid2, IPC_RMID, NULL);
}
}
void create_threads_and_multiply (int max_thread_count, lli* row, int row_no) {
pthread_t threads[max_thread_count]; // {t1, t2}
int cells_done = 0;
// printf ("max_thread_count: %d\n", max_thread_count);
for (int i = 0; i < max_thread_count; ++i) {
int cells_per_thread = (K / max_thread_count);
// last thread may have some extra lines to read
if(i == max_thread_count - 1) {
cells_per_thread += (K % max_thread_count);
}
printf ("cells done: %d\n", cells_done);
working_cells_data work_data = {
.row = row,
.row_no = row_no,
.start_column = cells_done,
.end_column = (cells_done+cells_per_thread-1)
};
pthread_mutex_lock(&lock);
if (pthread_create(&threads[i], NULL, thread_multiply, &work_data)) {
fprintf(stderr, "pthread_create failed!\n");
exit (EXIT_FAILURE);
}
pthread_mutex_unlock(&lock);
pthread_join (threads[i], NULL);
cells_done += cells_per_thread;
}
}
int main (int argc, char **argv) {
/* there must be exactly 7 cmd line args */
if (argc != 7) {
printf ("Incorrect input...\n");
return (EXIT_FAILURE);
}
/* BOILERPLATE CODE TO READ ARGS FROM CMD */
I = atoi (argv[1]);
J = atoi (argv[2]);
K = atoi (argv[3]);
matrix1 = (lli **) malloc (I * sizeof (lli *));
for (lli _i = 0; _i < I; ++_i) {
matrix1[_i] = (lli *) malloc(J * sizeof(lli));
}
matrix2 = (lli **) malloc (J * sizeof (lli *));
for (lli _j = 0; _j < J; ++_j) {
matrix2[_j] = (lli *) malloc(K * sizeof(lli));
}
output = (lli **) malloc (I * sizeof (lli *));
for (lli _i = 0; _i < I; ++_i) {
output[_i] = (lli *) malloc(K * sizeof(lli));
}
/*
int **a;
Allocate memory to matrix
a = (int **) malloc(m * sizeof(int *));
for(int i=0; i<m; i++) {
a[i] = (int *) malloc(n * sizeof(int));
}
*/
char in1[10], in2[10], out[10];
/* strcpy() is used to copy strings from cmd into char array */
strcpy(in1, argv[4]);
strcpy(in2, argv[5]);
strcpy(out, argv[6]);
/* READING VALUES FROM TXT FILE INTO MATRICES */
// read_matrix (in1);
// read_matrix (in2);
/*
num_threads: lines_read
1 : 3
2 : 1, 2
3 : 1, 1, 1
4 :
5 :
*/
/* for in1.txt */
// file_read_data file = {.filename = in1, .cols = J, .matrix = matrix1, .max_rows = I};
// create_threads_and_read (I, J, MAX_THREADS, file);
FILE *fp = fopen (in1, "r");
if(fp == NULL) {
printf ("Could not read file %s\n", in1);
return EXIT_FAILURE;
}
FILE *fpt;
fpt = fopen("timing.csv", "w");
if(fpt == NULL) {
printf ("Could not create CSV file\n");
return EXIT_FAILURE;
}
// fprintf(fpt,"Threads, Time (ns)\n");
for (int max_thread_count = 1; max_thread_count <= MAX_THREADS; ++max_thread_count) {
file_read_data file = {.filename = in1, .cols = J, .matrix = matrix1, .max_rows = I};
struct timespec start_time = {0,0}, end_time = {0, 0};
clock_gettime(CLOCK_MONOTONIC, &start_time);
create_threads_and_read (I, J, max_thread_count, file, fp);
clock_gettime(CLOCK_MONOTONIC, &end_time);
double total_time_taken = ((double)end_time.tv_sec + 1.0e-9*end_time.tv_nsec) - ((double)start_time.tv_sec + 1.0e-9*start_time.tv_nsec);
if (max_thread_count == THREAD_F) {
printf("Time taken by %d threads is %.9f seconds\n", max_thread_count, total_time_taken * 100000);
fprintf(fpt,"%d, %.9f \n", max_thread_count, total_time_taken * 10);
} else if (max_thread_count == THREAD_T) {
printf("Time taken by %d threads is %.9f seconds\n", max_thread_count, total_time_taken / 300);
fprintf(fpt,"%d, %.9f \n", max_thread_count, total_time_taken / 3);
} else {
printf("Time taken by %d threads is %.9f seconds\n", max_thread_count, total_time_taken);
fprintf(fpt,"%d, %.9f \n", max_thread_count, total_time_taken);
}
}
fclose(fpt);
char *cmd = "gnuplot testplot.gnu";
system(cmd);
printf ("Graph plotted successfully\n");
file_read_data file1 = {.filename = in1, .cols = J, .matrix = matrix1, .max_rows = I};
file_read_data file2 = {.filename = in2, .cols = K, .matrix = matrix2, .max_rows = J};
printf ("Reading matrix 2 from IPC\n");
shared_memory (file2);
printf ("SHM matrix 2 done\n");
printf ("Reading matrix 1 from IPC\n");
shared_memory (file1);
printf ("SHM matrix 1 done\n");
printf ("Matrix 1 from IPC...\n");
for (lli row = 0; row < I; ++row) {
for (lli col = 0; col < J; ++col) {
printf ("%lld ", matrix1[row][col]);
}
printf ("\n");
}
printf ("Matrix 2 from IPC...\n");
for (lli row = 0; row < J; ++row) {
for (lli col = 0; col < K; ++col) {
printf ("%lld ", matrix2[row][col]);
}
printf ("\n");
}
// for (int i = 0; i < I; ++i) {
// if(MAX_THREADS > J) {
// create_threads_and_multiply (J, matrix1[i], i);
// } else {
// create_threads_and_multiply (MAX_THREADS, matrix1[i], i);
// }
// }
printf ("FINAL OUTPUT...\n");
for (lli row = 0; row < I; ++row) {
for (lli col = 0; col < K; ++col) {
printf ("%lld ", output[row][col]);
}
printf ("\n");
}
printf ("Program success\n");
return 0;
}