-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_openmp.c
58 lines (53 loc) · 1.08 KB
/
matrix_openmp.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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define M 1500
#define P 500
#define N 300
int main(int argc, char const *argv[])
{
int **matrix1,**matrix2,**result;
matrix1=(int**) malloc (M*sizeof(int*));
matrix2=(int**) malloc (P*sizeof(int*));
result=(int**) malloc (M*sizeof(int*));
for (int i = 0; i < M; ++i)
{
matrix1[i]=(int *)malloc(P*sizeof(int));
result[i]=(int *)malloc(N*sizeof(int));
}
for (int i = 0; i < P; ++i)
{
matrix2[i]=(int *)malloc(N*sizeof(int));
}
//fill matrices with numbers
for (int i = 0; i < M; ++i)
{
for(int j=0;j<P;j++){
matrix1[i][j]=rand()%20;
}
}
for (int i = 0; i < P; ++i)
{
for(int j=0;j<N;j++){
matrix2[i][j]=rand()%20;
}
}
double begin,end,sum;
int i,j,k;
begin=omp_get_wtime();
#pragma omp parallel for private(i,j,k,sum) shared(matrix1,matrix2,result)
for (int i = 0; i < M; ++i)
{
for (int j = 0; j < N; ++j)
{
sum=0.0;
for(int k=0;k<P;k++){
sum+=matrix1[i][k]*matrix2[k][j];
}
result[i][j]=sum;
}
}
end=omp_get_wtime();
printf("Time elapsed: %f seconds\n", end - begin);
return 0;
}