-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_threads.c
74 lines (62 loc) · 1.66 KB
/
simple_threads.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
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#define size_n 5
#define size_m 5
int matrix_a[size_n][size_m];
int matrix_b[size_n][size_m];
int matrix_c[size_n][size_m] = {0};
int matrix_d[size_n][size_m] = {0};
int i_global = 0;
void fill_matrix(int matrix[size_n][size_m]){
for (int i=0; i<size_n; i++){
for (int j=0; j<size_m; j++){
matrix[i][j] = i * size_n + j + 1;
}
}
}
void print_matrix(int matrix[size_n][size_m]){
for(int i=0; i<size_n; i++){
for(int j=0; j<size_n; j++){
printf("%i ", matrix[i][j]);
}
printf("\n");
}
}
void* multiply_row(void* params){
int i = i_global++;
for (int j=0; j<size_m; j++){
for (int k=0; k<size_m; k++){
matrix_c[i][j] += matrix_a[i][k] * matrix_b[k][j];
}
}
return NULL;
}
int main(){
pthread_t threads[size_n];
fill_matrix(matrix_a);
fill_matrix(matrix_b);
clock_t begin = clock();
for (int i=0; i<size_n; i++){
pthread_create(&threads[i], NULL, multiply_row, NULL);
}
for (int i=0; i<size_n; i++){
pthread_join(threads[i], NULL);
}
clock_t end = clock();
double exec_time = (double) (end - begin);
// print_matrix(matrix_c);
printf("Execution time: %f\n", exec_time);
begin = clock();
for (int i=0; i<size_n; i++){
for (int j=0; j<size_m; j++){
for (int k=0; k<size_m; k++){
matrix_d[i][j] += matrix_a[i][k] * matrix_b[k][j];
}
}
}
end = clock();
// print_matrix(matrix_d);
exec_time = (double) (end - begin);
printf("Execution time: %f\n", exec_time);
}