-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsum3Dmatrix.cpp
110 lines (73 loc) · 1.83 KB
/
sum3Dmatrix.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<iostream>
#include<mpi.h>
#include<random>
int main( int arg, char **argv)
{
int rank, size;
MPI_Init(&arg, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
int root_rank =0;
// 3D matrix ///
double starttime = MPI_Wtime();
int n=100, m=100, k=2400;
double **A= new double*[k];
double **B= new double*[k];
double **C= new double*[k];
int end = n*m;
srand(23);
for(int i=0; i<k; i++){
A[i] = new double[end];
B[i] = new double[end];
C[i] = new double[end];
for(int j=0;j<n*m;j++){
A[i][j] = double( rand())/double(RAND_MAX);
B[i][j] = double(rand())/double(RAND_MAX);
C[i][j] =0;
}
}
// A+B
// Let's scatter the table
int count = int(end/size); // size of matrix to sum
int start= count*rank;
int stop = start+ count ;
int len = count*size; // size of the array which will gather process operations.
// row iteration//
double *my_value = new double[len];
double *sum = new double[count];
for(int l=0;l<k;l++)
{
int j=0;
for(int i=start;i<stop; i++)
{
sum[j]=A[l][i]+B[l][i];
j++;
}
MPI_Gather(sum,count, MPI_DOUBLE,my_value,count,MPI_DOUBLE,root_rank,MPI_COMM_WORLD);
if(rank==root_rank)
{
for(int i=0;i<len;++i){
C[l][i]=my_value[i];
}
// it is time to check whehter there not operation leave right
if(end>len){
for(int i=len;i<end;i++) C[l][i] = A[l][i] + B[l][i];
}
}
// wait for all the process
MPI_Barrier(MPI_COMM_WORLD); // blocks untill all the processes in the communicator have reached this routine
} // close for l
delete[] sum;
// free space //
for(int i=0; i<k;i++){
delete A[i];
delete C[i];
delete B[i];
}
delete[] A;
delete[] B;
delete[] C;
if(rank ==0) std::cout<<" Runtime is "<< MPI_Wtime()-starttime<<std::endl;
MPI_Finalize();
return 0;
}