-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix_Multiplication_Serial_Parallel.c
62 lines (55 loc) · 1.55 KB
/
Matrix_Multiplication_Serial_Parallel.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
/*
Author : Gopal Panigrahi
Date : 5 September 2020
Description : Two Large matrices are multiplied parallely
*/
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
void main()
{
long size = 1000;
/*
Create and initializes large matrices for multiplication
*/
int **first_matrix = (int**)malloc(size*sizeof(int*));
for(long i=0;i<size;i++){
first_matrix[i] = (int*)malloc(size*sizeof(int));
}
int **second_matrix = (int**)malloc(size*sizeof(int*));
for(long i=0;i<size;i++){
second_matrix[i] = (int*)malloc(size*sizeof(int));
}
int **result_matrix = (int**)malloc(size*sizeof(int*));
for(long i=0;i<size;i++){
result_matrix[i] = (int*)calloc(size,sizeof(int));
}
for(long i=0;i<size;i++){
for(long j=0;j<size;j++){
first_matrix[i][j] = second_matrix[i][j] = 1;
}
}
// SERIAL REGION
double start = omp_get_wtime();
for(long i=0;i<size;i++){
for(long j=0;j<size;j++){
for(long k=0;k<size;k++){
result_matrix[i][j] += first_matrix[i][k] + second_matrix[k][j];
}
}
}
double end = omp_get_wtime();
printf("Time Taken In Serial %lf \n",end-start);
// PARALLEL REGION
start = omp_get_wtime();
#pragma omp parallel for default(none) collapse(3) shared(first_matrix,second_matrix,result_matrix,size) schedule(auto)
for(long i=0;i<size;i++){
for(long j=0;j<size;j++){
for(long k=0;k<size;k++){
result_matrix[i][j] += first_matrix[i][k] + second_matrix[k][j];
}
}
}
end = omp_get_wtime();
printf("Time Taken In Parallel %lf \n",end-start);
}