-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab6_q2_ced17i031.c
113 lines (97 loc) · 2.7 KB
/
lab6_q2_ced17i031.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
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
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// size of array
#define n 400000000
int main(int argc, char* argv[])
{
double start;
double end;
double maximum_end = 0;
double minimum_start = 10000;
double sum;
int processid;
int np;
int elements_per_process;
int n_elements_recieved;
int SIZE;
double productproduct;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &processid);
MPI_Comm_size(MPI_COMM_WORLD, &np);
elements_per_process = n / np;
printf("\nthe element_per_process is %d\n",elements_per_process);
int count = 0;
SIZE = elements_per_process*np;
MPI_Barrier(MPI_COMM_WORLD);
start = MPI_Wtime();
if (processid == 0)
{
double product;
int location;
int i;
double* a = (double*) malloc(SIZE*sizeof(double));
double* b = (double*) malloc(SIZE*sizeof(double));
double* sum = (double*) malloc(SIZE*sizeof(double));
for( i=0; i<n; i++)
a[i] = (double) rand() + 0.00001534784187571354923459*(double) rand();//rand()%10;
for( i=0; i<n; i++)
b[i] = (double) rand() + 0.00001534784187571354923459*(double) rand();//rand()%10;
product=0.0;
count=0;
int t;
t = (int) n%np;
if(np>1)
{
printf("\nthe elements left %d\n",n%np);
if(n%np != 0)
{
for(i=np*t;i<n;i++)
{
product+=a[i]*b[i];
}
}
for ( i = 1; i < np; i++)
{
location = i * elements_per_process;
MPI_Send(&elements_per_process, 1, MPI_INT, i, 0,MPI_COMM_WORLD);
MPI_Send(&a[location],elements_per_process,MPI_DOUBLE, i, 1,MPI_COMM_WORLD);
MPI_Send(&b[location],elements_per_process, MPI_DOUBLE, i, 2,MPI_COMM_WORLD);
}
}
for (i = 0; i < elements_per_process; i++)
product+= a[i]*b[i];
printf("\nMaster rank finished\n");
MPI_Reduce(&product, &productproduct, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
int k;
}
else {
double product1;
double* a2 = (double*) malloc(SIZE*sizeof(double));
double* b2 = (double*) malloc(SIZE*sizeof(double));
product1=0.0;
MPI_Recv(&n_elements_recieved, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
MPI_Recv(a2, n_elements_recieved, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &status);
MPI_Recv(b2, n_elements_recieved, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD, &status);
int i;
for ( i = 0; i < n_elements_recieved; i++)
product1+= a2[i]*b2[i];
MPI_Reduce(&product1, &productproduct, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
printf("\nDATA SENT-%d\n",processid);
}
MPI_Barrier(MPI_COMM_WORLD);
end = MPI_Wtime();
MPI_Finalize();
if(minimum_start > start){
minimum_start = start;
}
if(maximum_end < end){
maximum_end = end;
}
if(processid == 0){
printf("\nExect time is %lf\n",end-start);
}
return 0;
}