-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
229 lines (215 loc) · 8.51 KB
/
test.cpp
File metadata and controls
229 lines (215 loc) · 8.51 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*************************************************************************
> File Name: test.cpp
> Author: Binbin Li
> Mail:
> Created Time: Sun 02 Oct 2016 03:20:27 PM EDT
************************************************************************/
#include <iostream>
#include <mpi.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
using namespace std;
void communicate(
const int& bot, const int& top, const int& right, const int& left,
const int& squareRowId, const int& squareColId, double **localArray,
const int& width, const int& rank, const int& size);
void update(int& rank, double **localArray, double **prevArray, const int& top,
const int& bot, const int& left, const int& right, const int& m, const int& n);
int main(int argc, char** argv) {
int rank, size;
MPI_Init(0, 0);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
// Compute the size of each square
int m = atoi(argv[1]), n = atoi(argv[2]);
int width = (int) sqrt(size);
int rowSize = m / width, colSize = n / width;
if (rowSize * width < m) ++rowSize;
if (colSize * width < n) ++colSize;
int squareRowId = rank / width, squareColId = rank % width;
// Compute upperleft index and bottomright index
int left = squareColId * colSize;
int right = min((squareColId + 1) * colSize - 1, n - 1);
int top = squareRowId * rowSize;
int bot = min((squareRowId + 1) * rowSize - 1, m - 1);
// Initialize the array
double **localArray = new double*[bot - top + 3];
double **prevArray = new double*[bot - top + 3];
for (int i = 0; i < bot - top + 3; ++i) {
localArray[i] = new double [right - left + 3];
prevArray[i] = new double [right - left + 3];
for (int j = 0; j < right - left + 3; ++j) {
localArray[i][j] = 0;
}
memcpy(prevArray[i], localArray[i], sizeof(double) * (right - left + 3));
}
vector<double> jVal(right - left + 1, 0);
int iIdx, jIdx;
for (int i = top; i <= bot; ++i) {
double a = i * sin(i);
iIdx = i - top + 1;
if (i == top) {
for (int j = left; j <= right; ++j) {
jVal[j - left] = j * cos(j);
localArray[iIdx][j - left + 1] = a + jVal[j - left] + sqrt(i + j);
}
} else {
for (int j = left; j <= right; ++j) {
localArray[iIdx][j - left + 1] = a + jVal[j - left] + sqrt(i + j);
}
}
}
double startTime = MPI_Wtime();
// Update in 10 iterations
for (int iteration = 1; iteration <= 10; ++iteration) {
// Communicate with neighbors
communicate(bot, top, right, left, squareRowId, squareColId, localArray, width, rank, size);
// Update the array
update(rank, localArray, prevArray, top, bot, left, right, m, n);
//MPI_Barrier(MPI_COMM_WORLD);
}
double sum = 0;
double squareSum = 0;
for (int i = 1; i < bot - top + 2; ++i) {
for (int j = 1; j < right - left + 2; ++j) {
sum += localArray[i][j];
squareSum += localArray[i][j] * localArray[i][j];
}
}
for (int i = 0; i < bot - top + 3; ++i) {
delete [] localArray[i];
delete [] prevArray[i];
}
delete [] localArray;
delete [] prevArray;
// Send result to processor 0
if (rank != 0) {
MPI_Request request;
MPI_Isend(&sum, 1, MPI_DOUBLE, 0, 2, MPI_COMM_WORLD, &request);
MPI_Isend(&squareSum, 1, MPI_DOUBLE, 0, 1, MPI_COMM_WORLD, &request);
}
else {
double totalSum = sum, totalSquareSum = squareSum;
MPI_Status status;
for (int i = 1; i < size; ++i) {
MPI_Recv(&sum, 1, MPI_DOUBLE, i, 2, MPI_COMM_WORLD, &status);
MPI_Recv(&squareSum, 1, MPI_DOUBLE, i , 1, MPI_COMM_WORLD, &status);
totalSum += sum;
totalSquareSum += squareSum;
}
cout << "Sum: " << totalSum << endl;
cout << "Square Sum: " << totalSquareSum << endl;
}
if (rank == 0) cout << "Time: " << MPI_Wtime() - startTime << endl << endl;;
MPI_Finalize();
return 0;
}
void communicate(
const int& bot, const int& top, const int& right, const int& left,
const int& squareRowId, const int& squareColId, double **localArray,
const int& width, const int& rank, const int& size) {
MPI_Status status;
int height = bot - top + 3, length = right - left + 3;
// Send to below in the even row
if (squareRowId % 2 == 0 && rank + width < size) {
MPI_Ssend(localArray[bot - top + 1], length, MPI_DOUBLE, rank + width, 0, MPI_COMM_WORLD);
}
// Receive from upper in the odd row
if (squareRowId % 2 == 1) {
MPI_Recv(localArray[0], length, MPI_DOUBLE, rank - width, 0, MPI_COMM_WORLD, &status);
}
// Send to below in the odd row
if (squareRowId % 2 == 1 && squareRowId != width - 1) {
MPI_Ssend(localArray[bot - top + 1], length, MPI_DOUBLE, rank + width, 0, MPI_COMM_WORLD);
}
// Receive from upper in the even row
if (squareRowId % 2 == 0 && squareRowId != 0) {
MPI_Recv(localArray[0], length, MPI_DOUBLE, rank - width, 0, MPI_COMM_WORLD, &status);
}
// Send to upper in the even row
if (squareRowId % 2 == 0 && squareRowId != 0) {
MPI_Ssend(localArray[1], length, MPI_DOUBLE, rank - width, 0, MPI_COMM_WORLD);
}
// Receive from below in the odd row
if (squareRowId % 2 == 1 && squareRowId != width - 1) {
MPI_Recv(localArray[height - 1], length, MPI_DOUBLE, rank + width, 0, MPI_COMM_WORLD, &status);
}
// Send to upper in the odd row
if (squareRowId % 2 == 1) {
MPI_Ssend(localArray[1], length, MPI_DOUBLE, rank - width, 0, MPI_COMM_WORLD);
}
// Receive from below in the even row
if (squareRowId % 2 == 0 && squareRowId != width - 1) {
MPI_Recv(localArray[height - 1], length, MPI_DOUBLE, rank + width, 0, MPI_COMM_WORLD, &status);
}
double *rowVec = new double [height];
// Send to right in the even column
if (squareColId % 2 == 0 && squareColId != width - 1) {
for (int i = 0; i < height; ++i) rowVec[i] = localArray[i][length - 2];
MPI_Ssend(rowVec, height, MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
}
// Receive from left in the odd column
if (squareColId % 2 == 1) {
MPI_Recv(rowVec, height, MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, &status);
for (int i = 0; i < height; ++i) localArray[i][0] = rowVec[i];
}
// Send to right in the odd column
if (squareColId % 2 == 1 && squareColId != width - 1) {
for (int i = 0; i < height; ++i) rowVec[i] = localArray[i][length - 2];
MPI_Ssend(rowVec, height, MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD);
}
// Receive from left in the even column
if (squareColId % 2 == 0 && squareColId != 0) {
MPI_Recv(rowVec, height, MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD, &status);
for (int i = 0; i < height; ++i) localArray[i][0] = rowVec[i];
}
// Send to left in the even column
if (squareColId % 2 == 0 && squareColId != 0) {
for (int i = 0; i < height; ++i) rowVec[i] = localArray[i][1];
MPI_Ssend(rowVec, height, MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD);
}
// Receive from right in the odd column
if (squareColId % 2 == 1 && squareColId != width - 1) {
MPI_Recv(rowVec, height, MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD, &status);
for (int i = 0; i < height; ++i) localArray[i][length - 1] = rowVec[i];
}
// Send to left in the odd column
if (squareColId % 2 == 1) {
for (int i = 0; i < height; ++i) rowVec[i] = localArray[i][1];
MPI_Ssend(rowVec, height, MPI_DOUBLE, rank - 1, 0, MPI_COMM_WORLD);
}
// Receive from right in the even column
if (squareColId % 2 == 0 && squareColId != width - 1) {
MPI_Recv(rowVec, height, MPI_DOUBLE, rank + 1, 0, MPI_COMM_WORLD, &status);
for (int i = 0; i < height; ++i) localArray[i][length - 1] = rowVec[i];
}
delete [] rowVec;
}
void update(
int& rank, double **localArray, double **prevArray, const int& top,
const int& bot, const int& left, const int& right, const int& m, const int& n) {
int topBorder = (top == 0) ? 2 : 1;
int botBorder = (bot == m - 1) ? bot - top : bot - top + 1;
int leftBorder = (left == 0) ? 2 : 1;
int rightBorder = (right == n - 1) ? right - left : right - left + 1;
for (int i = 0; i < bot - top + 3; ++i) {
memcpy(prevArray[i], localArray[i], sizeof(double) * (right - left + 3));
}
for (int i = topBorder - 1; i <= botBorder + 1; ++i) {
for (int j = leftBorder - 1; j <= rightBorder + 1; ++j) {
double x = prevArray[i][j];
double y = x;
for (int k = 1; k <= 10; ++k) {
y += sin(x * k) / pow(2.0, k);
}
prevArray[i][j] = y;
}
}
for (int i = topBorder; i <= botBorder; ++i) {
for (int j = leftBorder; j <= rightBorder; ++j) {
double z = (prevArray[i-1][j] + prevArray[i][j-1] + prevArray[i][j] + prevArray[i][j+1] + prevArray[i+1][j]) / 5.0;
localArray[i][j] = max(-100.0, min(100.0, z));
}
}
}