-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
315 lines (263 loc) · 9.48 KB
/
test.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
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
#include <iostream>
#include "block.h"
#include "math.h"
#include "mpiTools.h"
using namespace std;
struct BoxLimit{
int iXbegin, iXend;
int iYbegin, iYend;
};
void getLongIndex (const int& i, const int&nY, int& iX, int& iY) {iX = i/nY; iY=i%nY;}
int getShortIndex (const int& iX, const int& iY, const int& nY) {return iX*nY+iY;}
void getPeriodicNeighbor (const int& iX, const int& iY, const int& iQ,const int&nX, const int& nY, int& iX_neighbor, int& iY_neighbor) {
iX_neighbor = (iX + lattice::Qvector[iQ][0]+nX)%nX;
iY_neighbor = (iY + lattice::Qvector[iQ][1]+nY)%nY;
}
int main(){
int nDomainNodeX=10;
int nDomainNodeY=10;
int nMortarLayer = 2;
int nDomainNode = nDomainNodeX*nDomainNodeY;
int nBlockNodeX = sqrt(nDomainNode/mpiTools.getSize())+nMortarLayer;
int nBlockNodeY = nBlockNodeX;
int nBlockX = nDomainNodeX/(nBlockNodeX-nMortarLayer);
int nBlockY = nDomainNodeY/(nBlockNodeY-nMortarLayer);
Block block(nBlockNodeX,nBlockNodeY);
// filling a node on top of block0 with 1.0
if (mpiTools.getRank()==0){
for (int iQ=0;iQ<lattice::nQ;++iQ){
block(5,5)[5] = 5.0;//(double) iQ+0.00001;
}
}
// rank is the address of each block
//cout<<"nBlockX,Y "<<nBlockX<<" "<<nBlockY<<endl;
//cout<<"nBlockNodeX,Y "<<nBlockNodeX<<" "<<nBlockNodeY<<endl;
// setting block neighbors
int iX,iY,iXnei,iYnei;
getLongIndex(mpiTools.getRank(),nBlockY,iX,iY);
for (int iQ=0;iQ<lattice::nQ;++iQ){
getPeriodicNeighbor(iX,iY,iQ,nBlockX,nBlockY,iXnei,iYnei);
block.getMPIneighbor(iQ)= getShortIndex(iXnei,iYnei,nBlockY);
// cout<<mpiTools.getRank()<<" iX="<<iX<<" iY="<<iY<<" iQ= "<<iQ
// <<" nei_rank="<<block.getMPIneighbor(iQ)<<" iXnei="<<iXnei<<" iYnei="<<iYnei<<endl;
}
// setting border limits(not mortar layer) for D2Q9 - check for loop limits of 0 if it runs!
BoxLimit blockLimit[lattice::nQ];
{
int NNX=block.getDim(0)-1;
int NNY=block.getDim(1)-1;
int BBegin = 1;
for (int iQ=0;iQ<lattice::nQ;++iQ){
if (lattice::Qvector[iQ][0]==1){
blockLimit[iQ].iXbegin = NNX-1;
blockLimit[iQ].iXend = NNX;
}else if(lattice::Qvector[iQ][0]==0){
blockLimit[iQ].iXbegin=BBegin;
blockLimit[iQ].iXend=NNX;
}else if(lattice::Qvector[iQ][0]==-1){
blockLimit[iQ].iXbegin=BBegin;
blockLimit[iQ].iXend=BBegin+1;
}
if (lattice::Qvector[iQ][1]==1){
blockLimit[iQ].iYbegin = NNY-1;
blockLimit[iQ].iYend = NNY;
}else if(lattice::Qvector[iQ][1]==0){
blockLimit[iQ].iYbegin=BBegin;
blockLimit[iQ].iYend=NNY;
}else if(lattice::Qvector[iQ][1]==-1){
blockLimit[iQ].iYbegin=BBegin;
blockLimit[iQ].iYend=BBegin+1;
}
if (mpiTools.getRank()==0){
// cout<<blockLimit[iQ].iXbegin<<" "<<blockLimit[iQ].iXend<<" "<<blockLimit[iQ].iYbegin<<" "<<blockLimit[iQ].iYend<<endl;
}
}
}
// setting Mortar Layer limits for D2Q9 - check for loop limits of 0 if it runs!
BoxLimit mortarLimit[lattice::nQ];
int NNX=block.getDim(0);
int NNY=block.getDim(1);
int BBegin = 0;
for (int iQ=0;iQ<lattice::nQ;++iQ){
if (lattice::Qvector[iQ][0]==1){
mortarLimit[iQ].iXbegin = NNX-1;
mortarLimit[iQ].iXend = NNX;
}else if(lattice::Qvector[iQ][0]==0){
mortarLimit[iQ].iXbegin=BBegin+1;
mortarLimit[iQ].iXend=NNX-1;
}else if(lattice::Qvector[iQ][0]==-1){
mortarLimit[iQ].iXbegin=BBegin;
mortarLimit[iQ].iXend=BBegin+1;
}
if (lattice::Qvector[iQ][1]==1){
mortarLimit[iQ].iYbegin = NNY-1;
mortarLimit[iQ].iYend = NNY;
}else if(lattice::Qvector[iQ][1]==0){
mortarLimit[iQ].iYbegin=BBegin+1;
mortarLimit[iQ].iYend=NNY-1;
}else if(lattice::Qvector[iQ][1]==-1){
mortarLimit[iQ].iYbegin=BBegin;
mortarLimit[iQ].iYend=BBegin+1;
}
if (mpiTools.getRank()==0){
// cout<<mortarLimit[iQ].iXbegin<<" "<<mortarLimit[iQ].iXend<<" "<<mortarLimit[iQ].iYbegin<<" "<<mortarLimit[iQ].iYend<<endl;
}
}
// creating node vectors of boundaries
//std::vector<Node> border[lattice::nQ];
/*
MPI_Datatype border[lattice::nQ];
int blocklength = 1;
int stride;
int iQ = 1;
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
if (iQ==2 or iQ==4){
stride = block.getDim(1);
} else {
stride = 1;
}
MPI_Type_vector(length,blocklength,stride,Node,&border[iQ]);
*/
// initialization of boundary buffer
class Distro{
double f[lattice::nQ]={0.};
public:
double& getF(const int& iQ){
return f[iQ];
}
Distro& operator= (const Distro& rhs){
for (int iQ=0;iQ<lattice::nQ;iQ++){
f[iQ] = rhs.f[iQ];
}
}
Distro& operator= (const Node& rhs){
for (int iQ=0;iQ<lattice::nQ;iQ++){
f[iQ] = rhs[iQ];
}
}
};
std::vector<Distro> boundarySendBuffer[lattice::nQ];
std::vector<Distro> boundaryRecvBuffer[lattice::nQ];
// boundaries start from iQ = 1. Because borderlimit[iQ=0] gives whole domain.
for (int iQ=1;iQ<lattice::nQ;++iQ){
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
for (int iBuffer=0;iBuffer<length;++iBuffer){
Distro distro;
boundarySendBuffer[iQ].push_back(distro);
boundaryRecvBuffer[iQ].push_back(distro);
}
}
// for (int iX=blockLimit[iBoundary].iXbegin;iX<blockLimit[iBoundary].iXend;++iX){
// for (int iY=blockLimit[iBoundary].iYbegin;iY<blockLimit[iBoundary].iYend;++iY){
// int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
// for (int iBuffer=0;iBuffer<length;++iBuffer){
// Send Boundary Buffers
/*
MPI_Request reqs,reqr;
MPI_Status status;
//for (int iQ=1;iQ<lattice::nQ;++iQ)
if (mpiTools.getRank()==0)
{
int iQ = 2;
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
// printing
for(int ib=0;ib<length;++ib)
{
for (int i=0;i<lattice::nQ;++i)
cout<<"Rank()="<<mpiTools.getRank()<<" node ID="<<ib<<" dir="<<i<<" f= "<<boundarySendBuffer[iQ][ib].getF(i)<<endl;
}
cout<<"-----------------------"<<endl;
// printing finished
MPI_Isend(&boundarySendBuffer[iQ][0].getF(0), length*lattice::nQ, MPI_DOUBLE,block.getMPIneighbor(iQ), iQ, MPI_COMM_WORLD,&reqs);
}
//for (int iQ=1;iQ<lattice::nQ;++iQ)
if(mpiTools.getRank()==1)
{
int iQ = 4;
int iOp = lattice::iOpposite[iQ];
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
MPI_Recv(&boundaryRecvBuffer[iQ][0].getF(0), length*lattice::nQ, MPI_DOUBLE,block.getMPIneighbor(iOp), iOp, MPI_COMM_WORLD,&status);
// printing
for(int ib=0;ib<length;++ib)
{
for (int i=0;i<lattice::nQ;++i)
cout<<" AfterRank"<<mpiTools.getRank()<<" node ID="<<ib<<" dir="<<i<<" f= "<<boundaryRecvBuffer[iQ][ib].getF(i)<<endl;
}
cout<<"-----------------------"<<endl;
// printing finished
}
*/
for (int t=0;t<10;++t){
// Copy domain boundaries to send buffer
for (int iQ=1;iQ<lattice::nQ;++iQ){
int iBuffer =0;
for (int iX=blockLimit[iQ].iXbegin;iX<blockLimit[iQ].iXend;++iX){
for (int iY=blockLimit[iQ].iYbegin;iY<blockLimit[iQ].iYend;++iY){
boundarySendBuffer[iQ][iBuffer] = block(iX,iY);
iBuffer++;
}
}
}
// Send & Recv
MPI_Request reqs,reqr;
MPI_Status status;
for (int iQ=1;iQ<lattice::nQ;++iQ)
{
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
MPI_Isend(&boundarySendBuffer[iQ][0].getF(0), length*lattice::nQ, MPI_DOUBLE,block.getMPIneighbor(iQ), iQ, MPI_COMM_WORLD,&reqs);
}
for (int iQ=1;iQ<lattice::nQ;++iQ)
{
int iOp = lattice::iOpposite[iQ];
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
MPI_Recv(&boundaryRecvBuffer[iQ][0].getF(0), length*lattice::nQ, MPI_DOUBLE,block.getMPIneighbor(iQ), iOp, MPI_COMM_WORLD,&status);
}
// Copying receive buffer into Domain mortar nodes
for (int iQ=1;iQ<lattice::nQ;++iQ){
int iBuffer =0;
for (int iX=mortarLimit[iQ].iXbegin;iX<mortarLimit[iQ].iXend;++iX){
for (int iY=mortarLimit[iQ].iYbegin;iY<mortarLimit[iQ].iYend;++iY){
// TODO :: overload this loop
for (int iQQ=0;iQQ<lattice::nQ;++iQQ){
block(iX,iY)[iQQ] = boundaryRecvBuffer[iQ][iBuffer].getF(iQQ);
}
iBuffer++;
}
}
}
block.stream();
//block.revStream(1,block.getDim(0),1,block.getDim(1));
MPI_Barrier(MPI_COMM_WORLD);
// Printing
for (int iX=0;iX<block.getDim(0);++iX){
for (int iY=0;iY<block.getDim(1);++iY){
for (int iQ=0;iQ<lattice::nQ;++iQ)
{
double f = block(iX,iY)[iQ];
if (f>0.){
cout<<" t="<<t<<" Rank="<<mpiTools.getRank()<<" node iX="<<iX<<" node iY="<<iY<<" dir="<<iQ<<" f= "<<f<<endl;
}
}
}
}
// printing
for (int iQ=1;iQ<lattice::nQ;++iQ)
{
int length = (blockLimit[iQ].iYend - blockLimit[iQ].iYbegin)*(blockLimit[iQ].iXend - blockLimit[iQ].iXbegin);
for(int ib=0;ib<length;++ib)
{
for (int i=0;i<lattice::nQ;++i)
{
double f = boundaryRecvBuffer[iQ][ib].getF(i);
if (f>0.){
// cout<<" rank="<<mpiTools.getRank()<<" BC dir="<<iQ<<" node ID="<<ib<<" dir="<<i<<" f= "<<f<<endl;
}
}
}
}
// printing finished
} //timestep loop
MPI_Finalize();
return 0;
}
// int loadBalancer (int N,int &begin,int &end){