-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
89 lines (68 loc) · 1.91 KB
/
main.cpp
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
/*
*
* Adarsh Baghel
*
*/
#include <mpi.h>
#include <bits/stdc++.h>
using namespace std;
double *createMatrix(int n,int m){
double *matrix;
matrix = new double[n*m];
for (int h=0; h<n*m; h++)
matrix[h] = h+1;
return matrix;
}
int main(int argc, char *argv[])
{
int pid, nProc;
double mytime, maxtime, mintime, avgtime;
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &nProc);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
double dotProduct;
int n;
double *a,*b;
if(pid==0){
cin>>n;
if(true){
a = createMatrix(n,n);
b = createMatrix(n,n);
}
else{
a = new double [n];
b = new double [n];
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=0; i<n; i++)
cin>>b[i];
}
}
MPI_Bcast(&n, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
mytime = MPI_Wtime(); /*get time just before work section */
//#####################################
double va[n/nProc];
double vb[n/nProc];
double sum=0;
MPI_Scatter(a, n/nProc, MPI_DOUBLE, va, n/nProc, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatter(b, n/nProc, MPI_DOUBLE, vb, n/nProc, MPI_DOUBLE, 0, MPI_COMM_WORLD);
for(int i=0; i<n/nProc; i++){
sum+=va[i]*vb[i];
}
MPI_Reduce(&sum, &dotProduct, 1, MPI_DOUBLE, MPI_SUM, 0,MPI_COMM_WORLD);
//#####################################
mytime = MPI_Wtime() - mytime; /*get time just after work section*/
if (pid == 0) {
printf("Dot Product: %lf\n", dotProduct);
}
MPI_Reduce(&mytime, &maxtime, 1, MPI_DOUBLE,MPI_MAX, 0, MPI_COMM_WORLD);
MPI_Reduce(&mytime, &mintime, 1, MPI_DOUBLE, MPI_MIN, 0,MPI_COMM_WORLD);
MPI_Reduce(&mytime, &avgtime, 1, MPI_DOUBLE, MPI_SUM, 0,MPI_COMM_WORLD);
MPI_Barrier(MPI_COMM_WORLD);
if (pid == 0){
avgtime /= nProc;
printf("Min: %lf Max: %lf Avg: %lf\n", mintime, maxtime, avgtime);
}
MPI_Finalize();
return 0;
}