-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
329 lines (278 loc) · 8.1 KB
/
main.c
File metadata and controls
329 lines (278 loc) · 8.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include "parser.h"
#define MAX_MATRIX_SIZE 500
float a[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE];
float b[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE];
float c[MAX_MATRIX_SIZE][MAX_MATRIX_SIZE];
int a_dimensions[2]; // [0] --> x dimension, [1] --> y dimension
int b_dimensions[2];
int c_dimensions[2];
int num_of_threads_in_element_calculation = 0;
int num_of_threads_in_row_calculation = 0;
unsigned long without_threads_calculation_time = 0;
unsigned long elements_calculation_time = 0;
unsigned long rows_calculation_time = 0;
int error_flag = 0;
void run();
void read_matrix_from_file(char *file_name, int mat_num);
void write_matrix_to_file(char *file_name);
void post_read();
void calculate_without_threads();
void calculate_element_by_element();
void calculate_row_by_row();
void *elements_calculation_thread(void *cell);
void *rows_calculation_thread(void *row);
void print_statistics();
/**
A struct to hold row and column params of an index. This struct is being
called by the thread creation function
*/
struct cell
{
int i; // row
int j; // column
};
int main()
{
run();
if(error_flag == 1)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/**
Works like the main function of the program, it calls almost all other functions
*/
void run()
{
char **files = get_files_list();
read_matrix_from_file(files[1], 1);
if (error_flag == 1)
return;
read_matrix_from_file(files[2], 2);
if (error_flag == 1)
return;
post_read();
if (error_flag == 1)
return;
calculate_without_threads();
calculate_element_by_element();
calculate_row_by_row();
write_matrix_to_file(files[3]);
print_statistics();
}
/**
Reads a matrix from file
*/
void read_matrix_from_file(char *file_name, int mat_num)
{
FILE *file = fopen(file_name, "r");
if(!file)
{
error_flag = 1;
perror("Can't find the input file. Terminating...\n");
return;
}
int length, width, i, j;
float in;
fscanf(file, "%d", &length);
fscanf(file, "%d", &width);
if(mat_num == 1)
{
printf("Reading first matrix...\n");
a_dimensions[0] = length;
a_dimensions[1] = width;
}
else
{
printf("Reading second matrix...\n");
b_dimensions[0] = length;
b_dimensions[1] = width;
}
for(i = 0; i < length; i++)
{
for(j = 0; j < width; j++)
{
fscanf(file, "%f", &in);
if(in == EOF)
{
error_flag = 1;
perror("Can't find all the matrix element, please check the input file again.\n");
return;
}
mat_num == 1 ? (a[i][j] = in) : (b[i][j] = in);
}
}
fclose(file);
}
/**
Writes a matrix to file
*/
void write_matrix_to_file(char *file_name)
{
printf("Writing matrix to the output file...\n");
int i, j;
FILE *file = fopen(file_name, "w");
fprintf(file,"%d %d\n",c_dimensions[0], c_dimensions[1]);
for(i = 0; i < c_dimensions[0]; i++)
{
for(j = 0; j< c_dimensions[1]; j++)
{
fprintf(file, "%f ", c[i][j]);
}
fprintf(file, "\n");
}
fclose(file);
}
/**
Check if the two matrices multiplication is valid, and assign the dimensions to c matrix
*/
void post_read()
{
if(a_dimensions[1] != b_dimensions[0])
{
error_flag = 1;
perror("Number of columns of first matrix is not equle number of rows of the second one, can't multiply those two matrices.\n");
return;
}
c_dimensions[0] = a_dimensions[0];
c_dimensions[1] = b_dimensions[1];
}
/**
Multiply the two matrices in one thread.
*/
void calculate_without_threads()
{
printf("Calculating matrix without threads...\n");
struct timeval stop, start;
gettimeofday(&start, NULL); //start checking time
int i, j, k;
float sum = 0.0;
for(i = 0; i < c_dimensions[0]; i++) // rows
{
for(j = 0; j < c_dimensions[1]; j++) // columns
{
sum = 0.0;
for(k = 0; k < a_dimensions[1]; k++) //multiply row by column
{
sum += a[i][k] * b[k][j] * 1.0;
}
// set the value to it's index
c[i][j] = sum;
}
}
gettimeofday(&stop, NULL); //end checking time
without_threads_calculation_time = stop.tv_usec - start.tv_usec;
}
/**
Multiply the two matrices by calculating each element in a separate thread.
*/
void calculate_element_by_element()
{
printf("Calculating matrix element by element...\n");
struct timeval stop, start;
gettimeofday(&start, NULL); //start checking time
int i,j;
pthread_t threads_ids[c_dimensions[0] * c_dimensions[1]];
for(i = 0; i < c_dimensions[0]; i++) // rows
{
for(j = 0; j < c_dimensions[1]; j++) // columns
{
struct cell *index = (struct cell *) malloc(sizeof(struct cell));
index->i = i;
index->j = j;
pthread_t tid; //Thread ID
pthread_attr_t attr; //thread attributes
pthread_attr_init(&attr);
pthread_create(&tid, &attr, elements_calculation_thread, index);
threads_ids[num_of_threads_in_element_calculation++] = tid;
}
}
//parent should wait for all thread to complete
for (i = 0; i< num_of_threads_in_element_calculation; i++)
{
pthread_join(threads_ids[i], NULL);
}
gettimeofday(&stop, NULL); //end checking time
elements_calculation_time = stop.tv_usec - start.tv_usec;
}
/**
Multiply the two matrices by calculating each row in a separate thread.
*/
void calculate_row_by_row()
{
printf("Calculating matrix row by row...\n");
struct timeval stop, start;
gettimeofday(&start, NULL); //start checking time
int i;
pthread_t threads_ids[c_dimensions[0]];
for(i = 0; i < c_dimensions[0]; i++) // rows
{
pthread_t tid; //Thread ID
pthread_attr_t attr; //thread attributes
pthread_attr_init(&attr);
pthread_create(&tid, &attr, rows_calculation_thread, (void *)i);
threads_ids[num_of_threads_in_row_calculation++] = tid;
}
//parent should wait for all thread to complete
for (i = 0; i< num_of_threads_in_row_calculation; i++)
{
pthread_join(threads_ids[i], NULL);
}
gettimeofday(&stop, NULL); //end checking time
rows_calculation_time = stop.tv_usec - start.tv_usec;
}
/**
This function is being called by the created thread while calculating the multiplication
element by element.
*/
void *elements_calculation_thread(void *cell)
{
struct cell *current_cell = cell;
int k;
float sum = 0.0;
//multiply row by column
for(k = 0; k< a_dimensions[1]; k++)
{
sum += a[current_cell->i][k] * b[k][current_cell->j] * 1.0;
}
// set the value to it's index
c[current_cell->i][current_cell->j] = sum;
pthread_exit(0);
}
/**
This function is being called by the created thread while calculating the multiplication
row by row.
*/
void *rows_calculation_thread(void *row)
{
int i = (int)row, j, k;
float sum = 0.0;
//multiply row by column
for(j = 0; j < c_dimensions[1]; j++)
{
sum = 0;
for(k = 0; k < a_dimensions[1]; k++)
{
sum += a[i][k] * b[k][j] * 1.0;
}
// set the value to it's index
c[i][j] = sum;
}
pthread_exit(0);
}
/**
Prints the final statistics to the stdout.
*/
void print_statistics()
{
printf("\nFinal Statestics...\n\n");
printf("Calculation without threads took: %lu microseconds.\n", without_threads_calculation_time);
printf("Calculating element by element took: %lu microseconds.\n", elements_calculation_time);
printf("Calculating row by row took: %lu microseconds.\n\n", rows_calculation_time);
printf("Number of threads created during element by element calculation: %d\n", num_of_threads_in_element_calculation);
printf("Number of threads created during row by row calculation: %d\n", num_of_threads_in_row_calculation);
}