-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
221 lines (189 loc) · 4.3 KB
/
main.c
File metadata and controls
221 lines (189 loc) · 4.3 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
#include <upc.h>
#include <stdio.h>
/* --------- Defines ---------- */// Shared matrixes
shared float A[THREADS];
shared float B[THREADS];
shared float C[THREADS];
// Index of master id
#define MASTER 0
// Variable to print logs
// 0: logs invisible
// 1: logs visible
bool VERBOSE = 0;
/* --------- Function definitions ---------- */
/*
* Function: print_matrix
* --------------------
* prints out given matrix:
*
* mat: Matrix to print out
* dim: dimention of the matrix
* returns: void
*/
void print_matrix(shared float mat[], const int dim);
/*
* Function: initialize_matrix
* ----------------------------
* One dimentional matrix created from csv file (comma delivered).
*
* file_pointer: Pointer to *.csv file from which will be imported matrix
* matrix_data: Pointer to matrix which have to be filled.
*
* returns: Void
*/
void initialize_matrix(FILE *file_pointer, shared float mat[]);
/*
* Function: save_matrix
* --------------------
* saves matrix:
*
* file_pointer: Pointer to *.csv file from which will be imported matrix
* mat: Matrix to print out
* dim: dimention of the matrix
*
* returns: void
*/
void save_matrix(FILE *file_pointer,
const int dim,
shared float mat[]);
/* --------- Function declarations ---------- */
int main (int argc, char *argv[])
{
int myid;
int dim = (int)sqrt(THREADS);
FILE* file_pointer;
// --------- Set verbose --------- */
if(argc > 1 && strcmp(argv[1], "-v") == 0)
{
VERBOSE = 1;
}
if(MYTHREAD == MASTER)
{
/* --------- Initialize matrices ---------- */
file_pointer = fopen("A.csv", "r");
initialize_matrix(file_pointer, A);
if(VERBOSE)
{
fprintf(stdout, "Matrix A: \n");
print_matrix(A, dim);
}
fclose(file_pointer);
file_pointer = fopen("B.csv", "r");
initialize_matrix(file_pointer, B);
if(VERBOSE)
{
fprintf(stdout, "Matrix B: \n");
print_matrix(B, dim);
}
fclose(file_pointer);
fflush(stdout);
}
upc_barrier;
upc_forall(myid = 0; myid < THREADS; myid++; &C[myid])
{
//dim
int i = myid / dim;
//column
int j = myid % dim;
//initial skewing
int Ai = i;
int Aj = (j + i) % dim;
int Bi = (i + j) % dim;
int Bj = j;
for(int shift = 0; shift < dim; shift++)
{
Aj = (Aj + 1) % dim;
Bi = (Bi + 1) % dim;
int indexA = Ai * dim + Aj;
int indexB = Bi * dim + Bj;
C[myid] += A[indexA] * B[indexB];
}
}
upc_barrier;
if(MYTHREAD == MASTER)
{
if(VERBOSE)
{
fprintf(stdout, "Output matrix: \n");
print_matrix(C, dim);
}
file_pointer = fopen("C.csv","w");
save_matrix(file_pointer, dim, C);
fclose(file_pointer);
}
return 0;
}
void initialize_matrix(FILE *file_pointer, shared float mat[])
{
char buffer[1024];
char *line,
*record;
int row = 0,
col = 0;
while((line = fgets(buffer,sizeof(buffer), file_pointer)) != NULL)
{
if(row == 0)
{
for(int i = 0; i < strlen(line); i++)
{
if(line[i] == ',')
{
col++;
}
}
}
row++;
}
fseek(file_pointer, 0, SEEK_SET);
int k=0;
int i = 0;
while((line = fgets(buffer,sizeof(buffer), file_pointer)) != NULL && i++ < row)
{
record = strtok(line, ",");
int j = 0;
while(record != NULL && j++ < col)
{
mat[k] = atof(record);
record = strtok(NULL,",");
k++;
}
}
}
void print_matrix(shared float mat[], const int dim)
{
int cnt = 0;
for(int i = 0; i < dim; i++)
{
for(int j = 0; j < dim; j++)
{
if(VERBOSE)
{
fprintf(stdout, "%1.2f\t", mat[cnt]);
}
cnt++;
}
if(VERBOSE)
{
fprintf(stdout, "\n");
}
}
}
void save_matrix(FILE *file_pointer,
const int dim,
shared float mat[])
{
int cnt = 0;
if(VERBOSE)
{
fprintf(stdout, "Saving matrix to output file.\n");
}
for(int i = 0; i < dim; i++)
{
for(int j = 0; j < dim; j++)
{
fprintf(file_pointer, "%1.2f,", mat[cnt]);
cnt++;
}
fprintf(file_pointer, "\n");
}
}