-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.c
More file actions
383 lines (282 loc) · 10.1 KB
/
Matrix.c
File metadata and controls
383 lines (282 loc) · 10.1 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
/*
* Program to perform three different numerical matrix operations in two ways each.
One naive way, and a second smart one with tiling, to improve cache locality and
hence execution time.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
/* function prototypes */
float** read_matrix(float **array, int *arr_size, const char *filename);
void write_matrix(float **array, int arr_size, const char *filename);
float ** naive_matmul(float **arr1, float **arr2, float **arr3, int arr_size);
float ** tiled_matmul(float **arr1, float **arr2, float **arr3, int arr_size, int tile_size);
void naive_transpose(float **array, int arr_size);
void tiled_transpose(float **array, int arr_size, int tile_size);
void transpose(float **array, int index_x, int index_y, int subarr_size);
void exchange (float **array, int index_x, int index_y, int index_x2, int index_y2, int tile_size);
void naive_stencil(float **array, int arr_size);
void tiled_stencil(float **array, int arr_size, int tile_size);
/* main function */
int main(int argc, char *argv[]){
int tilesize, i,j, len ;
float **arr_A, **arr_B, **arr_C, **arr_A1, **arr_E, **arr_D1;
int arrA_size, arrB_size, arrC_size, arrE_size, arrA1_size;
struct timeval tim;
if (argc < 2) {
fprintf(stderr,"Name of file or tile size not provided.\n", argv[0]);
exit(0);
}
char *filename;
filename = (char*) malloc (20);
strcpy (filename, argv[1]);
tilesize = atoi(argv[2]);
strcat (filename, "_A.txt");
arr_A = read_matrix(arr_A, &arrA_size, filename); //Read array A from file
/* Naive Transpose */
gettimeofday(&tim, NULL);
double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
naive_transpose (arr_A, arrA_size);
gettimeofday(&tim, NULL);
double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf Naive Transpose - seconds elapsed\n", t2-t1);
double t3 = t2-t1;
write_matrix(arr_A, arrA_size, "out_A.txt");
arr_A = read_matrix(arr_A, &arrA_size, filename); //Read array A from file
/* do tiled matrix transpose here */
gettimeofday(&tim, NULL);
t1=tim.tv_sec+(tim.tv_usec/1000000.0);
tiled_transpose(arr_A, arrA_size, tilesize);
gettimeofday(&tim, NULL);
t2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf Tiled Transpose - seconds elapsed\n", t2-t1);
double t4 = t2-t1;
printf("%.6lf Transpose - Speedup\n", t3/t4);
write_matrix(arr_A, arrA_size, "out_A_t.txt");
/* Elementary Matrix Multiplication */
arr_A1 = read_matrix(arr_A1, &arrA1_size, filename);
strcpy (filename, argv[1]);
strcat (filename, "_B.txt");
arr_B = read_matrix(arr_B, &arrB_size, filename); //Read array B and C from file
if (arrB_size !=arrA1_size){
fprintf(stderr,"Matrix multiplication not possible with differently sized arrays");
exit(0);
}
gettimeofday(&tim, NULL);
t1=tim.tv_sec+(tim.tv_usec/1000000.0);
arr_C = naive_matmul(arr_A1, arr_B, arr_C, arrB_size);
gettimeofday(&tim, NULL);
t2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf Naive Transpose - seconds elapsed\n", t2-t1);
t3 = t2-t1;
write_matrix(arr_C, arrB_size, "out_C.txt");
/* do Tiled Matrix Multiplication here */
gettimeofday(&tim, NULL);
t1=tim.tv_sec+(tim.tv_usec/1000000.0);
arr_D1 = tiled_matmul(arr_A1, arr_B, arr_D1, arrB_size, tilesize);
gettimeofday(&tim, NULL);
t2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.6lf Naive Transpose - seconds elapsed\n", t2-t1);
t4 = t2-t1;
printf("%.6lf Multiplication - Speedup\n", t3/t4);
write_matrix(arr_D1, arrB_size, "out_C_t.txt");
/* 5-point naive stencil over 2D matrix*/
strcpy (filename, argv[1]);
strcat (filename, "_E.txt");
arr_E = read_matrix(arr_E, &arrE_size, filename);
naive_stencil(arr_E, arrE_size);
write_matrix(arr_E, arrE_size, "out_E.txt");
/* 5-point tiled stencil over 2D matrix */
arr_E = read_matrix(arr_E, &arrE_size, filename);
tiled_stencil(arr_E, arrE_size, tilesize);
write_matrix(arr_E, arrE_size, "out_E_t.txt");
/* Free allocated memory */
for (i = 0; i < arrA_size; i++){
free(arr_A[i]);
}
free(arr_A);
for (i = 0; i < arrB_size; i++){
free(arr_B[i]);
}
free(arr_B);
for (i = 0; i < arrC_size; i++){
free(arr_C[i]);
}
free(arr_C);
return 0;
}
/* Function to read a matrix from a text file
* Takes filename as input
* Populates the passed in array and returns it, also populates arr_size which is passed by reference.
*/
float** read_matrix(float **array, int *arr_size, const char *filename){
int i, j;
FILE *file = fopen (filename, "r" ); //Open a file for reading
if (file!=NULL)
{
fscanf(file, "%d", arr_size); //Read size of array from file
array = (float**)malloc((*arr_size)*sizeof(float*));
for (i= 0; i< (*arr_size); i++)
array[i]= (float*)malloc((*arr_size)*sizeof(float)); //allocate memory for array
while(!feof(file))
{
for (i=0;i<(*arr_size);i++)
for (j=0;j<(*arr_size);j++)
{
fscanf(file, "%f", &array[i][j]);
}
}
}
else
{
fprintf(stderr,"File not found! - %s \n",filename);
exit(0);
}
return array;
}
/* Function to write a matrix to a text file.
* Takes matrix, its size and filename as input.
*/
void write_matrix(float **array, int arr_size, const char *filename){
int i, j;
FILE *file_w = fopen (filename, "w" ); //Open a file for writing
fprintf(file_w, "%d", arr_size); //write size of array into file
fprintf(file_w, "%s", "\n");
for (i=0; i<arr_size; i++)
for (j=0; j<arr_size; j++)
{
fprintf(file_w, "%.2f", array[i][j]);
fprintf(file_w, "\n");
}
}
/*
* Function to calculate transpose of a given matrix without using tiles.
* Takes matrix and its size as input.
*/
void naive_transpose(float **array, int arr_size){
int i, j, temp;
for (i=0; i<arr_size; i++) {
for (j=i+1; j<arr_size; j++) {
temp=array[j][i];
array[j][i]=array[i][j];
array[i][j] = temp;
}
}
}
/* Function to multiply 2 matrices the naive way.
* Takes 2 matrices as input along with their size and the tile size as specified by the user.
* Returns product matrix.
*/
float ** naive_matmul(float **arr1, float **arr2, float **arr3, int arr_size){
int i, j, k;
arr3 = (float**)malloc(arr_size*sizeof(float*));
for (i= 0; i< arr_size; i++)
arr3[i]= (float*)malloc(arr_size*sizeof(float)); //allocate memory for array
for (i=0; i<arr_size; i++){
for (j=0; j<arr_size; j++){
for (k=0; k<arr_size; k++){
arr3[i][j] = arr1[i][k]*arr2[k][j] + arr3[i][j];
}
}
}
return arr3;
}
/* Function to multiply 2 matrices using cache efficient tile algorithm.
* Takes 2 matrices as input along with their size and the tile size as specified by the user.
* Returns product matrix.
*/
float ** tiled_matmul(float **arr1, float **arr2, float **arr3, int arr_size, int tile_size){
int i, j, k, i1, j1, k1;
arr3 = (float**)malloc(arr_size*sizeof(float*));
for (i= 0; i< arr_size; i++)
arr3[i]= (float*)malloc(arr_size*sizeof(float)); //allocate memory for resultant array
for (i=0; i<arr_size; i+=tile_size){
for (j=0; j<arr_size; j+=tile_size){
for (k=0; k<arr_size; k+=tile_size){
for (i1=i; i1<i+tile_size; i1++){
for (j1=j; j1<j+tile_size; j1++){
for (k1=k; k1<k+tile_size; k1++){
arr3[i1][j1] = arr1[i1][k1]*arr2[k1][j1] + arr3[i1][j1];
}
}
}
}
}
}
return arr3;
}
/* Function to transpose an array using cache efficient tile algorithm.
* Takes matrix as input along with its size and the tile size as specified by the user.
*/
void tiled_transpose(float **array, int arr_size, int tile_size){
int i, j, p, q, temp;
for (i=0; i<arr_size; i+=tile_size) {
//for diagonals
for (p=i; p<i+tile_size; p++) {
for (q=p+1; q<i+tile_size; q++) {
temp=array[q][p];
array[q][p]=array[p][q];
array[p][q] = temp;
}
}
for (j=i+tile_size; j< arr_size; j+=tile_size) {
transpose (array, j, i, tile_size);
transpose (array, i, j, tile_size);
exchange (array, j, i , i, j, tile_size);
}
}
}
/* Helper function to calculate transpose of a sub array inside the matrix.
* Takes the matrix, and the index of the starting point of the sub array as input.
*/
void transpose(float **array, int index_x, int index_y, int tile_size){
int p, q, temp;
for (p=index_x+1; p<index_x+tile_size; p++) {
for (q=index_y+1; q<index_y+tile_size; q++) {
// cout<<"Exchanging arr("<<index_x<<","<<q<<"and arr("<<p<<","<<index_y<<")"<<endl;
temp=array[index_x][q];
array[index_x][q]=array[p][index_y];
array[p][index_y] = temp;
}
}
}
/* Helper function to exchange 2 sub arrays inside the matrix.
* Takes the matrix, and the indices of the starting points of the 2 sub arrays as input.
*/
void exchange (float **array, int index_x, int index_y, int index_x2, int index_y2, int tile_size){
int i, j, k, m, temp;
for (i=index_x, k= index_x2; i< index_x + tile_size; i++, k++) {
for (j=index_y, m= index_y2; j<index_y + tile_size; j++, m++){
temp = array[i][j];
array[i][j] = array[k][m];
array[k][m] = temp;
}
}
}
/* Function to perform the 5-point stencil on a matrix the naive way.
* Takes the matrix and its size as input
*/
void naive_stencil(float **array, int arr_size){
int i, j;
for(i=1;i<arr_size-1;i++) {
for(j=1;j<arr_size-1;j++) {
array[i][j] = (array[i][j-1]+ array[i][j+1]+ array[i-1][j]+ array[i+1][j]+ array[i][j])/5;
}
}
}
/* Function to perform the 5-point stencil on a matrix using cache efficient tile algorithm.
* Takes the matrix and its size as input.
*/
void tiled_stencil(float **array, int arr_size, int tile_size){
int i, j, p, q;
for (i=1; i<arr_size-1; i+=tile_size) {
for (j=1; j<arr_size-1; j+=tile_size) {
for(p=i;p<i+tile_size;p++) {
for(q=j;q<j+tile_size;q++) {
array[p][q] = (array[p][q-1] + array[p][q+1] + array[p-1][q] + array[p+1][q] + array[p][q])/5;
}
}
}
}
}