-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolve_old.cpp
More file actions
330 lines (290 loc) · 10.4 KB
/
solve_old.cpp
File metadata and controls
330 lines (290 loc) · 10.4 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Solves the Aliev-Panfilov model using an explicit numerical scheme.
* Based on code orginally provided by Xing Cai, Simula Research Laboratory
*
* Modified and restructured by Scott B. Baden, UCSD
*
*/
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <math.h>
#include "time.h"
#include "apf.h"
#include "Plotting.h"
#ifdef _MPI_
#include <mpi.h>
#endif
#define NEIGHBOR 1
#define FINAL 2
using namespace std;
void repNorms(ofstream& logfile, double l2norm, double mx, double dt, int m,int n, int niter, int stats_freq);
// Reports statistics about the computation: the L2 Norm and the Infinity NOrm
// These values should not vary (except to within roundoff)
// when we use different numbers of processes to solve the problem
// The L2 norm of an array is computed by taking sum of the squares
// of each element, normalizing by dividing by the number of points
// and then taking the sequare root of the result
//
// The Linf norm is simply the maximum (absolute) value over
// all points in the array
double stats(double **E, int m, int n, double *_mx){
double mx = -1;
double l2norm = 0;
int i, j;
for (j=1; j<=m+1; j++)
for (i=1; i<=n+1; i++) {
l2norm += E[j][i]*E[j][i];
double fe = fabs(E[j][i]);
if (fe > mx)
mx = fe;
}
// In the parallel version, you must sum all the local contributoins
// before dividing by (m+1)*(n+1)
l2norm /= (double) ((m+1)*(n+1));
l2norm = sqrt(l2norm);
*_mx = mx;
return l2norm;
}
// Added px and py to solve
int solve(ofstream& logfile, double ***_E, double ***_E_prev, double **R, int m, int n, int niters, double alpha, double dt, int plot_freq, Plotter *plotter, int stats_freq, int px, int py){
// Simulated time is different from the integer timestep number
double t = 0.0;
int siz = m*n*sizeof(double);
char * buffer = new char[siz];
int position = 0;
double **E = *_E, **E_prev = *_E_prev;
int niter;
int rank =0, np=1;
#ifdef _MPI_
MPI_Comm_size(MPI_COMM_WORLD,&np);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Request send, recv, send2, recv2;
#endif
cout << "Inside solve from rank " << rank << "/" << np << endl;
// We continue to sweep over the mesh until the simulation has reached
// the desired simulation Time
// This is different from the number of iterations
/* Setting up our boundaries for i, not pretty but it works */
int i_s = (((n+1)*rank)/py)+1;
i_s += rank==0?0:1;
int i_e = rank==py-1?n+1:(((n+1)*(rank+1))/py)+1;
// cout << "i_s = " << i_s << " rank = " << rank << " n = " << n+1 << endl;
//cout << "i_e = " << i_e << " rank = " << rank << " n = " << n+1 << endl;
for (niter = 0; niter < niters; niter++){
#ifdef DEBUG
double mx;
double l2norm = stats(E_prev,m,n,&mx);
repNorms(logfile,l2norm,mx,dt,m,n,niter, stats_freq);
if (plot_freq)
plotter->updatePlot(E, niter, m+1, n+1, WAIT);
// splot(E_prev,niter,m+1,n+1,WAIT);
#endif
/*
* Copy data from boundary of the computational box to the
* padding region, set up for differencing computational box's boundary
*
* These are physical boundary conditions, and are not to be confused
* with ghost cells that we would use in an MPI implementation
*
* The reason why we copy boundary conditions is to avoid
* computing single sided differences at the boundaries
* which increase the running time of solve()
*
*/
int i,j;
/*
for (j=1; j<=m+1; j++) {
E_prev[j][0] = E_prev[j][2];
E_prev[j][n+2] = E_prev[j][n];
}
for (i=i_s; i<=i_e; i++) {
E_prev[0][i] = E_prev[2][i];
E_prev[m+2][i] = E_prev[m][i];
}*/
// Solve for the excitation, a PDE
for (j=1; j<=m+1; j++){
E_prev[j][0] = E_prev[j][2];
E_prev[j][n+2] = E_prev[j][n];
for (i=i_s; i<=i_e; i++) {
E_prev[0][i] = E_prev[2][i];
E_prev[m+2][i] = E_prev[m][i];
E[j][i] = E_prev[j][i]+alpha*(E_prev[j][i+1]+E_prev[j][i-1]-4*E_prev[j][i]+E_prev[j+1][i]+E_prev[j-1][i]);
}
/* 1 | 2 | 3 0 | 1 | 2 r/3= 0
*----------- ---------
* 4 | 5 | 6 3 | 4 | 5 = 1
*----------- ---------
* 7 | 8 | 9 6 | 7 | 8 = 2
* 1 >1 0 */
// Left edge
if ((rank+1)%py== 1) {
/* This sends the boundary of the first thread (the items at the end of first thread) */
MPI_Send(&E[j][i_e], 1, MPI_DOUBLE, rank+1, NEIGHBOR, MPI_COMM_WORLD);
/* This recieves the boundary of the second thread (i+1) */
MPI_Recv(&E[j][i_e+1], 1, MPI_DOUBLE, rank+1, NEIGHBOR, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
// Middle
} else if ((rank+1)%py>1) {
/* This sends the boundary of the second thread (the items at the end of the second thread) */
MPI_Send(&E[j][i_s], 1, MPI_DOUBLE, rank-1, NEIGHBOR, MPI_COMM_WORLD);
/* This recieves the boundary of the first thread needed for computation (i-1) */
MPI_Recv(&E[j][i_s-1], 1, MPI_DOUBLE, rank-1, NEIGHBOR, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
MPI_Send(&E[j][i_e], 1, MPI_DOUBLE, rank+1, NEIGHBOR, MPI_COMM_WORLD);
/* This recieves the boundary of the second thread (i+1) */
MPI_Recv(&E[j][i_e+1], 1, MPI_DOUBLE, rank+1, NEIGHBOR, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
// Right Edge
} else if ((rank+1)%py==0) {
/* This sends the boundary of the second thread (the items at the end of the second thread) */
MPI_Send(&E[j][i_s], 1, MPI_DOUBLE, rank-1, NEIGHBOR, MPI_COMM_WORLD);
/* This recieves the boundary of the first thread needed for computation (i-1) */
MPI_Recv(&E[j][i_s-1], 1, MPI_DOUBLE, rank-1, NEIGHBOR, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
}
}
/* Printing our table each iteration. Just for debugging
cout << "------------------------n = " << niter << ", rank = " << rank << "-------------------------\n";
for (j=1; j<=m+1; j++){
for (i=1; i<=n+1; i++) {
cout << E[j][i] << " ";
}
cout << "\n";
}*/
/*
* Solve the ODE, advancing excitation and recovery variables
* to the next timtestep
*/
int new_i_s = i_s==1?i_s:i_s-1;
int new_i_e = i_e==n+1?i_e:i_e+1;
for (j=1; j<=m+1; j++){
double *RR = &R[j][new_i_s];
double *EE = &E[j][new_i_s];
for (i=new_i_s; i<=new_i_e; i++, EE++,RR++) {
EE[0] += -dt*(kk*EE[0]*(EE[0]-a)*(EE[0]-1)+EE[0]*RR[0]);
RR[0] += dt*(epsilon+M1* RR[0]/( EE[0]+M2))*(-RR[0]-kk*EE[0]*(EE[0]-b-1));
}
}
if (stats_freq){
double mx;
double l2norm = stats(E_prev,m,n,&mx);
repNorms(logfile,l2norm,mx,dt,m,n,niter, stats_freq);
}
if (plot_freq){
if (!(niter % plot_freq)){
// splot(E,niter,m+1,n+1,WAIT);
/* *************************** */
/* Gather matrix for plot */
/* *************************** */
/* We're going to let thread 0 do the return value, so that means every other thread
* needs to pack up it's own working segment and send it to thread 0 */
if (rank != 0) {
position = 0;
/* Packing up our rank so we know which part to fill in */
MPI_Pack(&rank, 1, MPI_INT, buffer, siz, &position, MPI_COMM_WORLD);
/* Packing up our array */
for (int j=1; j<=m+1; j++){
for (int i=i_s; i<=i_e; i++) {
MPI_Pack(&E_prev[j][i],1, MPI_DOUBLE, buffer, siz, &position, MPI_COMM_WORLD);
}
}
/* Then we send to thread 0 */
MPI_Send(buffer, siz, MPI_PACKED,0,FINAL, MPI_COMM_WORLD);
}else {
// IN THREAD 0
int sofar = 0;
int in_rank;
int in_i_s;
int in_i_e;
double in_val;
/* We loop until we've recieved communication from every thread */
while (sofar < np-1) {
position=0; // Reset buffer iterator each time we recieve a message
/* We recieve our packed stuff from some other thread */
MPI_Recv(buffer, siz, MPI_PACKED, MPI_ANY_SOURCE,FINAL, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
/* We unpack the rank first */
MPI_Unpack(buffer, siz, &position, &in_rank, 1, MPI_INT, MPI_COMM_WORLD);
/* Use the rank to figure out the thread's boundary so we can fill our own array
* with it's array elements */
in_i_s = (((n+1)*in_rank)/py)+1;
in_i_s += in_rank==0?0:1;
in_i_e = in_rank==py-1?n+1:(((n+1)*(in_rank+1))/py)+1;
/* Now we unpack each value in the array and add it to our own */
for (int j=1; j<=m+1; j++){
for (int i=in_i_s; i<=in_i_e; i++) {
MPI_Unpack(buffer, siz, &position, &in_val, 1, MPI_DOUBLE, MPI_COMM_WORLD);
E_prev[j][i] = in_val;
}
}
sofar++; // Move to the next process
}
plotter->updatePlot(E, niter, m+1, n+1, WAIT);
}
}
}
// Swap current and previous
double **tmp = E; E = E_prev; E_prev = tmp;
}
// Store them into the pointers passed in
/* *************************** */
/* Gather matrix before return */
/* *************************** */
/* We're going to let thread 0 do the return value, so that means every other thread
* needs to pack up it's own working segment and send it to thread 0 */
if (rank != 0) {
position = 0;
/* Packing up our rank so we know which part to fill in */
MPI_Pack(&rank, 1, MPI_INT, buffer, siz, &position, MPI_COMM_WORLD);
/* Packing up our array */
for (int j=1; j<=m+1; j++){
for (int i=i_s; i<=i_e; i++) {
MPI_Pack(&E_prev[j][i],1, MPI_DOUBLE, buffer, siz, &position, MPI_COMM_WORLD);
}
}
/* Then we send to thread 0 */
MPI_Send(buffer, siz, MPI_PACKED,0,FINAL, MPI_COMM_WORLD);
}else {
// IN THREAD 0
int sofar = 0;
int in_rank;
int in_i_s;
int in_i_e;
double in_val;
/* We loop until we've recieved communication from every thread */
while (sofar < np-1) {
position=0; // Reset buffer iterator each time we recieve a message
/* We recieve our packed stuff from some other thread */
MPI_Recv(buffer, siz, MPI_PACKED, MPI_ANY_SOURCE,FINAL, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
/* We unpack the rank first */
MPI_Unpack(buffer, siz, &position, &in_rank, 1, MPI_INT, MPI_COMM_WORLD);
/* Use the rank to figure out the thread's boundary so we can fill our own array
* with it's array elements */
in_i_s = (((n+1)*in_rank)/py)+1;
in_i_s += in_rank==0?0:1;
in_i_e = in_rank==py-1?n+1:(((n+1)*(in_rank+1))/py)+1;
/* Now we unpack each value in the array and add it to our own */
for (int j=1; j<=m+1; j++){
for (int i=in_i_s; i<=in_i_e; i++) {
MPI_Unpack(buffer, siz, &position, &in_val, 1, MPI_DOUBLE, MPI_COMM_WORLD);
E_prev[j][i] = in_val;
}
}
sofar++; // Move to the next process
}
/* Print out final array for debugging
cout << "------------------------FINAL-------------------------\n";
for (int j=1; j<=m+1; j++){
for (int i=1; i<=n+1; i++) {
cout << E_prev[j][i] << " ";
}
cout << "\n";
}*/
}
*_E = E;
*_E_prev = E_prev;
return niter;
}