-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.c
484 lines (459 loc) · 19.6 KB
/
matrix.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
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "matrix.h"
struct GLOBAL_PARAM param;
void usage(char *err) {
char *usage = "\nusage: matrix {routines} [options] -f matrix1 [matrix2]\n"
"routines:\n"
" --sm scalar\tperform scalar multiplcation with value scalar\n"
" --tr\t\tcompute the matrix trace value\n"
" --ad\t\tperform matrix addition on two matrices, matrix2 must be specified\n"
" --ts\t\ttranspose the given matrix\n"
" --mm\t\tperform matrix multiplication on two matrices, matrix2 must be specified\n"
"options:\n"
" -t threads\tspecify the number of execution threads to use\n"
" -l\t\tif present, results will be logged to an output file\n"
" -s\t\tif present, results will not contain the matrix data\n";
if (err != NULL) {
fprintf(stderr, "matrix: %s", err);
}
printf("%s", usage);
}
int main(int argc, char *argv[]) {
// CLA variables
enum VAR_TYPE type = INVALID;
struct ROUTINE routine;
routine.type = UNDEF;
int rows, cols;
char *data = NULL;
char *data2 = NULL;
char *filename = NULL;
char *filename2 = NULL;
bool log = false;
bool silent = false;
int arg = 1; // Argument pointer
param.threads = -1;
// Timing and outfile name variables
time_t t = time(NULL);
struct tm tm = *localtime(&t);
float load_time, routine_time;
struct timeval start, end;
// CLA processing
if (argc < 4) {
usage("invalid number of arguments supplied\n");
exit(EXIT_FAILURE);
}
while (arg < argc) {
if (strcmp(argv[arg], "--sm") == 0) {
if (routine.type != UNDEF) {
usage("only one routine may be specified\n");
exit(EXIT_FAILURE);
} else if ((arg + 1) == argc) { // Expect scalar value
usage("no scalar value supplied when using --sm\n");
exit(EXIT_FAILURE);
}
routine.type = SM;
arg++;
type = numeric_type(argv[arg]); // Check scalar input type
if (type == INVALID) {
usage("scalar value must be numeric\n");
exit(EXIT_FAILURE);
} else {
routine.scalar = strtod(argv[arg], NULL);
if (errno == ERANGE) {
fprintf(stderr, "matrix: failed to convert scalar value '%s' to double\n", argv[arg]);
exit(EXIT_FAILURE);
}
}
} else if (strcmp(argv[arg], "--tr") == 0) {
if (routine.type != UNDEF) {
usage("only one routine may be specified\n");
exit(EXIT_FAILURE);
}
routine.type = TR;
} else if (strcmp(argv[arg], "--ad") == 0) {
if (routine.type != UNDEF) {
usage("only one routine may be specified\n");
exit(EXIT_FAILURE);
}
routine.type = AD;
} else if (strcmp(argv[arg], "--ts") == 0) {
if (routine.type != UNDEF) {
usage("only one routine may be specified\n");
exit(EXIT_FAILURE);
}
routine.type = TS;
} else if (strcmp(argv[arg], "--mm") == 0) {
if (routine.type != UNDEF) {
usage("only one routine may be specified\n");
exit(EXIT_FAILURE);
}
routine.type = MM;
} else if (strcmp(argv[arg], "-t") == 0) {
if ((arg + 1) == argc) { // Expect thread value
usage("number of execution threads must be specified when using -t\n");
exit(EXIT_FAILURE);
}
arg++;
type = numeric_type(argv[arg]); // Check thread input type
if (type != TYPE_INT) {
usage("invalid number of execution threads\n");
exit(EXIT_FAILURE);
} else if (param.threads != -1) {
usage("thread parameter should only be used once\n");
exit(EXIT_FAILURE);
}
param.threads = strtoimax(argv[arg], NULL, 10);
if (errno == EINVAL) {
fprintf(stderr, "matrix: error processing thread value '%s'\n", argv[arg]);
exit(EXIT_FAILURE);
} else if (errno == ERANGE) {
fprintf(stderr, "matrix: thread value out of range '%s'\n", argv[arg]);
exit(EXIT_FAILURE);
} else if (param.threads <= 0) {
usage("number of exeuction threads must be greater than 0\n");
exit(EXIT_FAILURE);
} else {
omp_set_num_threads(param.threads);
}
} else if (strcmp(argv[arg], "-l") == 0) {
if (log) { // Logfile already specified
usage("log parameter should only be used once\n");
exit(EXIT_FAILURE);
} else {
log = true;
}
} else if (strcmp(argv[arg], "-s") == 0) {
if (silent) { // Logfile already specified
usage("silent parameter should only be used once\n");
exit(EXIT_FAILURE);
} else {
silent = true;
}
} else if (strcmp(argv[arg], "-f") == 0) {
if (filename != NULL) { // File already specified
usage("file parameter should only be used onece\n");
exit(EXIT_FAILURE);
} else if ((arg + 1) == argc) { // Expect matrix file
usage("at least one matrix file must be specified after -f\n");
exit(EXIT_FAILURE);
} else if (strcmp(argv[arg+1], "--sm") == 0 || strcmp(argv[arg+1], "--tr") == 0 || strcmp(argv[arg+1], "--ad") == 0
|| strcmp(argv[arg+1], "--ts") == 0 || strcmp(argv[arg+1], "--mm") == 0 || strcmp(argv[arg+1], "-t") == 0
|| strcmp(argv[arg+1], "-l") == 0 || strcmp(argv[arg+1], "-s") == 0 || strcmp(argv[arg+1], "-f") == 0) {
usage("at least one matrix file must be specified after -f\n");
exit(EXIT_FAILURE);
} else {
arg++;
filename = argv[arg];
// Check for second matrix file if any
if ((arg + 1) != argc && strcmp(argv[arg+1], "--sm") != 0 && strcmp(argv[arg+1], "--tr") != 0 && strcmp(argv[arg+1], "--ad") != 0
&& strcmp(argv[arg+1], "--ts") != 0 && strcmp(argv[arg+1], "--mm") != 0 && strcmp(argv[arg+1], "-t") != 0
&& strcmp(argv[arg+1], "-l") != 0 && strcmp(argv[arg+1], "-s") != 0 && strcmp(argv[arg+1], "-f") != 0) {
arg++;
filename2 = argv[arg];
}
}
} else {
fprintf(stderr, "matrix: unknown parameter '%s'\n", argv[arg]);
usage(NULL);
exit(EXIT_FAILURE);
}
arg++;
}
// File validation
if (filename == NULL) {
usage("no matrix file specified\n");
exit(EXIT_FAILURE);
}
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "%s: no such file\n", filename);
exit(EXIT_FAILURE);
}
if (filename2 != NULL && routine.type != MM && routine.type != AD) {
usage("only one matrix input file is required with this routine\n");
exit(EXIT_FAILURE);
} else if (filename2 == NULL && (routine.type == MM || routine.type == AD)) {
usage("two matrix input files are required with this routine\n");
exit(EXIT_FAILURE);
}
// Thread validation
if (param.threads == -1) {
param.threads = 4; // Default value
}
switch (routine.type) {
case SM:
// Read input file
gettimeofday(&start, NULL);
type = read_mat_type(fp);
rows = read_mat_dim(fp);
cols = read_mat_dim(fp);
data = read_line(fp);
struct COO smresult = coo_format(rows, cols, type, data);
gettimeofday(&end, NULL);
load_time = get_time(start, end);
// Perform the scalar multiplication routine
gettimeofday(&start, NULL);
scalar_multiply(smresult, routine.scalar);
gettimeofday(&end, NULL);
routine_time = get_time(start, end);
if (log) {
char *output_file = get_output_name(tm, "sm");
FILE *output = fopen(output_file, "w"); // sample file
if(output == NULL) {
fprintf(stderr, "matrix: failed to generate output file\n");
exit(EXIT_FAILURE);
}
write_details(output, filename, filename2, rows, cols, routine.type, smresult.type);
smresult.type = TYPE_FLOAT; // Float scalar results in float matrix
if (!silent) {
write_coo_data(output, smresult);
}
write_times(output, load_time, routine_time);
printf("matrix: successfully logged results to '%s'\n", output_file);
fclose(output);
free(output_file);
} else {
write_details(stdout, filename, filename2, rows, cols, routine.type, smresult.type);
smresult.type = TYPE_FLOAT; // Float scalar results in float matrix
if (!silent) {
write_coo_data(stdout, smresult);
}
write_times(stdout, load_time, routine_time);
}
break;
case TR:
// Read input file
gettimeofday(&start, NULL);
type = read_mat_type(fp);
rows = read_mat_dim(fp);
cols = read_mat_dim(fp);
if (rows != cols) {
fprintf(stderr, "matrix: the trace routine can only be performed on square matrices\n");
exit(EXIT_FAILURE);
}
data = read_line(fp);
struct CSR trresult = csr_format(rows, cols, type, data);
gettimeofday(&end, NULL);
load_time = get_time(start, end);
// Perform the trace routine
union {
int i;
double f;
} trace_result;
if (trresult.type == TYPE_INT) {
gettimeofday(&start, NULL);
trace_result.i = trace(trresult);
gettimeofday(&end, NULL);
routine_time = get_time(start, end);
} else {
gettimeofday(&start, NULL);
trace_result.f = trace_f(trresult);
gettimeofday(&end, NULL);
routine_time = get_time(start, end);
}
if (log) {
char *output_file = get_output_name(tm, "tr");
FILE *output = fopen(output_file, "w"); // sample file
if(output == NULL) {
fprintf(stderr, "matrix: failed to generate output file\n");
exit(EXIT_FAILURE);
}
write_details(output, filename, filename2, rows, cols, routine.type, trresult.type);
// Write single trace value
if (!silent) {
if (trresult.type == TYPE_INT) {
fprintf(output, "%d\n", trace_result.i);
} else {
fprintf(output, "%f\n", trace_result.f);
}
}
write_times(output, load_time, routine_time);
printf("matrix: successfully logged results to '%s'\n", output_file);
fclose(output);
free(output_file);
} else {
write_details(stdout, filename, filename2, rows, cols, routine.type, trresult.type);
if (!silent) {
if (trresult.type == TYPE_INT) {
fprintf(stdout, "%d\n", trace_result.i);
} else {
fprintf(stdout, "%f\n", trace_result.f);
}
}
write_times(stdout, load_time, routine_time);
}
break;
case AD:
// Read input files
gettimeofday(&start, NULL);
type = read_mat_type(fp);
rows = read_mat_dim(fp);
cols = read_mat_dim(fp);
data = read_line(fp);
struct CSR ad1 = csr_format(rows, cols, type, data);
gettimeofday(&end, NULL);
load_time = get_time(start, end);
// Filename2 validation
FILE *fp2 = fopen(filename2, "r");
if (fp2 == NULL) {
fprintf(stderr, "%s: no such file\n", filename2);
exit(EXIT_FAILURE);
}
gettimeofday(&start, NULL);
type = read_mat_type(fp2);
rows = read_mat_dim(fp2);
cols = read_mat_dim(fp2);
data2 = read_line(fp2);
struct CSR ad2 = csr_format(rows, cols, type, data2);
gettimeofday(&end, NULL);
load_time += get_time(start, end);
// Check matrix constraints
if (ad1.rows != ad2.rows || ad1.cols != ad2.cols) {
fprintf(stderr, "matrix: the addition routine can only be performed on matrices with identical dimensions\n");
exit(EXIT_FAILURE);
} else if (ad1.type != ad2.type) {
fprintf(stderr, "matrix: the addition routine should only be performed on matrices of identical variable types\n");
exit(EXIT_FAILURE);
}
struct COO adresult;
gettimeofday(&start, NULL);
if (ad1.type == TYPE_INT) {
adresult = matrix_addition(ad1, ad2);
} else {
adresult = matrix_addition_f(ad1, ad2);
}
gettimeofday(&end, NULL);
routine_time = get_time(start, end);
if (log) {
char *output_file = get_output_name(tm, "ad");
FILE *output = fopen(output_file, "w"); // sample file
if(output == NULL) {
fprintf(stderr, "matrix: failed to generate output file\n");
exit(EXIT_FAILURE);
}
write_details(output, filename, filename2, rows, cols, routine.type, adresult.type);
if (!silent) {
write_coo_data(output, adresult);
}
write_times(output, load_time, routine_time);
printf("matrix: successfully logged results to '%s'\n", output_file);
fclose(output);
free(output_file);
} else {
write_details(stdout, filename, filename2, rows, cols, routine.type, adresult.type);
if (!silent) {
write_coo_data(stdout, adresult);
}
write_times(stdout, load_time, routine_time);
}
break;
case TS:
// Read input file
gettimeofday(&start, NULL);
type = read_mat_type(fp);
rows = read_mat_dim(fp);
cols = read_mat_dim(fp);
data = read_line(fp);
struct CSC ts1 = csc_format(rows, cols, type, data);
gettimeofday(&end, NULL);
load_time = get_time(start, end);
// Perform the scalar multiplication routine
gettimeofday(&start, NULL);
struct CSR tsresult = transpose(ts1);
gettimeofday(&end, NULL);
routine_time = get_time(start, end);
if (log) {
char *output_file = get_output_name(tm, "ts");
FILE *output = fopen(output_file, "w"); // sample file
if(output == NULL) {
fprintf(stderr, "matrix: failed to generate output file\n");
exit(EXIT_FAILURE);
}
write_details(output, filename, filename2, rows, cols, routine.type, tsresult.type);
if (!silent) {
write_csr_data(output, tsresult);
}
write_times(output, load_time, routine_time);
printf("matrix: successfully logged results to '%s'\n", output_file);
fclose(output);
free(output_file);
} else {
write_details(stdout, filename, filename2, rows, cols, routine.type, tsresult.type);
if (!silent) {
write_csr_data(stdout, tsresult);
}
write_times(stdout, load_time, routine_time);
}
break;
case MM:
// Read input files
gettimeofday(&start, NULL);
type = read_mat_type(fp);
rows = read_mat_dim(fp);
cols = read_mat_dim(fp);
data = read_line(fp);
struct CSR mm1 = csr_format(rows, cols, type, data);
gettimeofday(&end, NULL);
load_time = get_time(start, end);
// Filename2 validation
FILE *mmfp2 = fopen(filename2, "r");
if (mmfp2 == NULL) {
fprintf(stderr, "%s: no such file\n", filename2);
exit(EXIT_FAILURE);
}
gettimeofday(&start, NULL);
type = read_mat_type(mmfp2);
rows = read_mat_dim(mmfp2);
cols = read_mat_dim(mmfp2);
data2 = read_line(mmfp2);
struct CSC mm2 = csc_format(rows, cols, type, data2);
gettimeofday(&end, NULL);
load_time += get_time(start, end);
// Check matrix constraints
if (mm1.cols != mm2.rows) {
fprintf(stderr, "matrix: the multiplication routine can only be performed if the columns and rows on the first and second matrices are equal\n");
exit(EXIT_FAILURE);
} else if (mm1.type != mm2.type) {
fprintf(stderr, "matrix: the multiplication routine should only be performed on matrices of identical variable types\n");
exit(EXIT_FAILURE);
}
struct COO mmresult;
gettimeofday(&start, NULL);
if (mm1.type == TYPE_INT) {
mmresult = matrix_multiply(mm1, mm2);
} else {
mmresult = matrix_multiply_f(mm1, mm2);
}
gettimeofday(&end, NULL);
routine_time = get_time(start, end);
if (log) {
char *output_file = get_output_name(tm, "mm");
FILE *output = fopen(output_file, "w"); // sample file
if(output == NULL) {
fprintf(stderr, "matrix: failed to generate output file\n");
exit(EXIT_FAILURE);
}
write_details(output, filename, filename2, rows, cols, routine.type, mmresult.type);
if (!silent) {
write_coo_data(output, mmresult);
}
write_times(output, load_time, routine_time);
printf("matrix: successfully logged results to '%s'\n", output_file);
fclose(output);
free(output_file);
} else {
write_details(stdout, filename, filename2, rows, cols, routine.type, mmresult.type);
if (!silent) {
write_coo_data(stdout, mmresult);
}
write_times(stdout, load_time, routine_time);
}
break;
case UNDEF: // No routines specified
usage("no matrix algebra routine specified\n");
exit(EXIT_FAILURE);
}
fclose(fp);
free(data);
free(data2);
exit(EXIT_SUCCESS);
}