-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.c
242 lines (227 loc) · 7.32 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
/*
* matrix.c
* 21103CW2
*
* Created by Matthew Livingston on 23/01/2012.
* Copyright 2012 University of Bristol. All rights reserved.
*
*/
#include "coo.h"
#include "csr.h"
// Read in elements from a file into a coordinate list
void matrix_read(List * list, FILE * f) {
fscanf(f, "%d,", &list->rows);
fscanf(f, "%d\n", &list->cols);
int row, col, val;
while (fscanf(f, "%d", &row) != EOF) {
fscanf(f, ",%d", &col);
fscanf(f, ",%d", &val);
list = addRowOrdered(list, row, col, val);
}
}
// Write elements from a coordinate list into a file
void matrix_write(List * list, FILE * f) {
Node * node = list->head;
fprintf(f, "%d,%d\n", list->rows, list->cols);
while (node != NULL) {
fprintf(f, "%d,", node->row);
fprintf(f, "%d,", node->col);
fprintf(f, "%d\n", node->val);
node = node->next;
}
}
// Copy elements in coordinate list data structure into compressed sparse row structure
void convertCOOToCSR(List * list, CSR * csr) {
Node * node = list->head;
int i = 0, k = 0;
while (node != NULL) {
csr->val[i] = node->val;
csr->col_ind[i] = node->col;
if (csr->row_ptr[node->row] == -1) {
csr->row_ptr[node->row] = i;
}
node = node->next;
i++;
}
csr->row_ptr[0] = 0;
csr->row_ptr[csr->row_ptr_length-1] = csr->val_length;
for (k = csr->row_ptr_length-1; k > 0; k--) {
if (csr->row_ptr[k] == -1) csr->row_ptr[k] = csr->row_ptr[k+1];
}
csr->rows = list->rows;
csr->cols = list->cols;
}
void stage1(FILE * f, int row, int col) {
List * list = calloc(1, sizeof(List));
CSR * csr = calloc(1, sizeof(CSR));
matrix_read(list, f);
if (row > list->rows-1) {
fprintf(stdout, "Error: Row input is larger than matrix row size\n");
exit(1);
}
if (col > list->cols-1) {
fprintf(stdout, "Error: Column input is larger than matrix column size\n");
exit(1);
}
csr = initCSR(csr, list->size, list->rows);
convertCOOToCSR(list, csr);
printf("%d\n", searchElement(list->head, row, col));
printRow(csr, row);
printCol(csr, col);
free(freeList(list));
free(freeCSR(csr));
}
void stage2(FILE * f, FILE * fout) {
List * list = calloc(1, sizeof(List));
matrix_read(list, f);
list = transpose(list);
matrix_write(list, fout);
free(freeList(list));
fclose(fout);
}
void stage3(FILE * fout, FILE * input0, FILE * input1) {
List * list = calloc(1, sizeof(List));
CSR * matrix0 = calloc(1, sizeof(CSR));
CSR * matrix1 = calloc(1, sizeof(CSR));
List * outmatrix = calloc(1, sizeof(List));
matrix_read(list, input0);
matrix0 = initCSR(matrix0, list->size, list->rows);
convertCOOToCSR(list, matrix0);
list = freeList(list);
matrix_read(list, input1);
matrix1 = initCSR(matrix1, list->size, list->rows);
convertCOOToCSR(list, matrix1);
free(freeList(list));
if ((matrix0->rows != matrix1->rows) || (matrix0->cols != matrix1->cols)) {
printf("Abort: Input matrices are incompatible\n");
exit(1);
}
outmatrix = addCSR(matrix0, matrix1, outmatrix);
matrix_write(outmatrix, fout);
free(freeCSR(matrix0));
free(freeCSR(matrix1));
free(freeList(outmatrix));
}
void stage4(FILE * fout, FILE * input0, FILE * input1) {
List * list = calloc(1, sizeof(List));
CSR * matrix0 = calloc(1, sizeof(CSR));
CSR * matrix1 = calloc(1, sizeof(CSR));
List * outmatrix = calloc(1, sizeof(List));
matrix_read(list, input0);
matrix0 = initCSR(matrix0, list->size, list->rows);
convertCOOToCSR(list, matrix0);
list = freeList(list);
matrix_read(list, input1);
matrix1 = initCSR(matrix1, list->size, list->rows);
convertCOOToCSR(list, matrix1);
free(freeList(list));
if (matrix0->cols != matrix1->rows) {
printf("Abort: Input matrices are incompatible\n");
exit(1);
}
outmatrix = multiplyCSR(matrix0, matrix1, outmatrix);
matrix_write(outmatrix, fout);
free(freeCSR(matrix0));
free(freeCSR(matrix1));
free(freeList(outmatrix));
}
void stage5(int argc, char ** argv) {
List * list = calloc(1, sizeof(List));
CSR * matrix0 = calloc(1, sizeof(CSR));
CSR * matrix1 = calloc(1, sizeof(CSR));
List * outmatrix = calloc(1, sizeof(List));
FILE * input0 = fopen(argv[3], "r");
if (input0 == 0) { fprintf(stdout, "Could not open file %s\n", argv[3]); exit(1); }
FILE * input1;
matrix_read(list, input0);
matrix0 = initCSR(matrix0, list->size, list->rows);
convertCOOToCSR(list, matrix0);
list = freeList(list);
int i = 4;
do {
input1 = fopen(argv[i], "r");
if (input1 == 0) { fprintf(stdout, "Could not open file %s\n", argv[i]); exit(1); }
matrix_read(list, input1);
matrix1 = initCSR(matrix1, list->size, list->rows);
convertCOOToCSR(list, matrix1);
list = freeList(list);
if (matrix0->cols != matrix1->rows) {
printf("Abort: Input matrices are incompatible\n");
exit(1);
}
outmatrix = multiplyCSR(matrix0, matrix1, outmatrix);
if (i == argc-1) {
FILE * fout = fopen(argv[2], "w");
if (fout == 0) { fprintf(stdout, "Could not open file %s\n", argv[2]); exit(1); }
matrix_write(outmatrix, fout);
free(list);
free(freeCSR(matrix0));
free(freeCSR(matrix1));
free(freeList(outmatrix));
fclose(fout);
break;
}
matrix0 = freeCSR(matrix0);
matrix1 = freeCSR(matrix1);
matrix0 = initCSR(matrix0, outmatrix->size, outmatrix->rows);
convertCOOToCSR(outmatrix, matrix0);
outmatrix = freeList(outmatrix);
i++;
} while (i < argc);
}
int main(int argc, char * argv[]) {
if (strcmp(argv[1], "stage1") == 0) {
if (argc != 5) {
fprintf(stdout, "Please use the format: ./matrix stage1 ${INPUT} ${ROW} ${COL}\n");
exit(1);
}
FILE * f = fopen(argv[2], "r");
if (f == 0) { fprintf(stdout, "Could not open file %s\n", argv[2]); exit(1); }
stage1(f, atoi(argv[3]), atoi(argv[4]));
}
else if (strcmp(argv[1], "stage2") == 0) {
if (argc != 4) {
fprintf(stdout, "Please use the format: ./matrix stage2 ${OUTPUT} ${INPUT}\n");
exit(1);
}
FILE * f = fopen(argv[3], "r");
if (f == 0) { fprintf(stdout, "Could not open file %s\n", argv[3]); exit(1); }
FILE * fout = fopen(argv[2], "w");
if (fout == 0) { fprintf(stdout, "Could not open file %s\n", argv[2]); exit(1); }
stage2(f, fout);
}
else if (strcmp(argv[1], "stage3") == 0) {
if (argc != 5) {
fprintf(stdout, "Please use the format: ./matrix stage3 ${OUTPUT} ${INPUT_0} ${INPUT_1}\n");
exit(1);
}
FILE * fout = fopen(argv[2], "w");
if (fout == 0) { fprintf(stdout, "Could not open file %s\n", argv[2]); exit(1); }
FILE * input0 = fopen(argv[3], "r");
if (input0 == 0) { fprintf(stdout, "Could not open file %s\n", argv[3]); exit(1); }
FILE * input1 = fopen(argv[4], "r");
if (input1 == 0) { fprintf(stdout, "Could not open file %s\n", argv[4]); exit(1); }
stage3(fout, input0, input1);
}
else if (strcmp(argv[1], "stage4") == 0) {
if (argc != 5) {
fprintf(stdout, "Please use the format: ./matrix stage4 ${OUTPUT} ${INPUT_0} ${INPUT_1}\n");
exit(1);
}
FILE * fout = fopen(argv[2], "w");
if (fout == 0) { fprintf(stdout, "Could not open file %s\n", argv[2]); exit(1); }
FILE * input0 = fopen(argv[3], "r");
if (input0 == 0) { fprintf(stdout, "Could not open file %s\n", argv[3]); exit(1); }
FILE * input1 = fopen(argv[4], "r");
if (input1 == 0) { fprintf(stdout, "Could not open file %s\n", argv[4]); exit(1); }
stage4(fout, input0, input1);
}
else if (strcmp(argv[1], "stage5") == 0) {
if (argc < 5) {
fprintf(stdout, "Please use the format: ./matrix stage5 ${OUTPUT} ${INPUT_0} ${INPUT_1} ... ${INPUT_X}\n");
exit(1);
}
stage5(argc, argv);
}
return 0;
}