-
Notifications
You must be signed in to change notification settings - Fork 0
/
CartesianDecomp.cxx
314 lines (265 loc) · 7.83 KB
/
CartesianDecomp.cxx
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
/*
____ _ __ ____ __ ____
/ __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
_\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
/___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
Copyright 2012 SciberQuest Inc.
*/
#include "CartesianDecomp.h"
#include "CartesianDataBlock.h"
#include "CartesianDataBlockIODescriptor.h"
#include "Tuple.hxx"
#include "SQMacros.h"
#ifndef SQTK_WITHOUT_MPI
#include <mpi.h>
#endif
#define CartesianDecompDEBUG
// Search the decomposition for the block conaining the point. Return
// non-zero if an error occured or the point was contained by any block
// in the decomposition.
//*****************************************************************************
int DecompSearch(
CartesianDecomp *decomp, // decomp to search
int *dext, // decomp coordinate range to search
int q, // coordinate direction
const double *pt, // find index of block comntaining this point
int *I // return the index (should be initialize 0).
)
{
const int qq=2*q;
// get the median in q coordinate direction
I[q]=(dext[qq]+dext[qq+1])/2;
CartesianBounds &bounds=decomp->GetBlock(I)->GetBounds();
if ( pt[q]>=bounds[qq]
&& pt[q]<=bounds[qq+1] )
{
// point is inside some block on I[q] coordinate
return 0;
}
else
if ( pt[q]<bounds[qq] )
{
// reduce search space to the lower half of the range.
dext[qq+1]=I[q]-1;
// failed to locate the point, in any block along qth coordinate.
if (dext[qq+1]<0)
{
return 1;
}
}
else
// if ( pt[q]>bounds[qq+1] )
{
// reduce search space to upper half of the range.
dext[qq]=I[q]+1;
// failed to locate the point, in any block along qth coordinate.
if (dext[qq]>(decomp->GetDecompDimensions()[q]))
{
return 1;
}
}
// continue the search in the reduced space.
return DecompSearch(decomp,dext,q,pt,I);
}
//-----------------------------------------------------------------------------
CartesianDecomp::CartesianDecomp()
{
#ifdef SQTK_WITHOUT_MPI
sqErrorMacro(
cerr,
<< "This class requires MPI however it was built without MPI.");
#endif
this->Mode=CartesianExtent::DIM_MODE_3D;
this->DecompDims[0]=
this->DecompDims[1]=
this->DecompDims[2]=0;
this->PeriodicBC[0]=
this->PeriodicBC[1]=
this->PeriodicBC[2]=0;
this->NGhosts=1;
}
//-----------------------------------------------------------------------------
CartesianDecomp::~CartesianDecomp()
{
this->ClearDecomp();
this->ClearIODescriptors();
}
//-----------------------------------------------------------------------------
void CartesianDecomp::ClearDecomp()
{
size_t nBlocks=this->Decomp.size();
for (size_t i=0; i<nBlocks; ++i)
{
delete this->Decomp[i];
}
this->Decomp.clear();
}
//-----------------------------------------------------------------------------
void CartesianDecomp::ClearIODescriptors()
{
size_t nBlocks=this->IODescriptors.size();
for (size_t i=0; i<nBlocks; ++i)
{
delete this->IODescriptors[i];
}
this->IODescriptors.clear();
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetFileExtent(
int ilo,
int ihi,
int jlo,
int jhi,
int klo,
int khi)
{
int ext[6]={ilo,ihi,jlo,jhi,klo,khi};
this->SetFileExtent(ext);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetFileExtent(const CartesianExtent &ext)
{
this->SetFileExtent(ext.GetData());
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetFileExtent(const int ext[6])
{
this->FileExtent.Set(ext);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::ComputeDimensionMode()
{
this->Mode
= CartesianExtent::GetDimensionMode(this->FileExtent,this->NGhosts);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetBounds(
double xlo,
double xhi,
double ylo,
double yhi,
double zlo,
double zhi)
{
this->Bounds.Set(xlo,xhi,ylo,yhi,zlo,zhi);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetBounds(const double bounds[6])
{
this->Bounds.Set(bounds);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetBounds(const CartesianBounds &bounds)
{
this->Bounds=bounds;
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetExtent(
int ilo,
int ihi,
int jlo,
int jhi,
int klo,
int khi)
{
this->Extent.Set(ilo,ihi,jlo,jhi,klo,khi);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetExtent(const int ext[6])
{
this->Extent.Set(ext);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetExtent(const CartesianExtent &ext)
{
this->Extent=ext;
}
//-----------------------------------------------------------------------------
int CartesianDecomp::SetDecompDims(int nBlocks)
{
#ifndef SQTK_WITHOUT_MPI
if (nBlocks==0)
{
sqErrorMacro(cerr,"0 is an invald number of blocks.");
return 0;
}
// create a partitioning yeilding the desired number
// of blocks distributed over 3 dimensions.
int decompDims[3]={0};
MPI_Dims_create(nBlocks,3,decompDims);
this->SetDecompDims(decompDims);
#endif
return 1;
}
//-----------------------------------------------------------------------------
int CartesianDecomp::SetDecompDims(int ni,int nj,int nk)
{
int decompDims[3]={ni,nj,nk};
return this->SetDecompDims(decompDims);
}
//-----------------------------------------------------------------------------
int CartesianDecomp::SetDecompDims(const int decompDims[3])
{
if (decompDims[0]<1)
{
sqErrorMacro(cerr,"Decomp dims cannot be zero.");
return 0;
}
// special case, if user explicitly passes both
// y and z dims zero, we generate a decomposition
// using the x dimension as the number of blocks.
if (decompDims[1]<1 && decompDims[2]<1)
{
return this->SetDecompDims(decompDims[0]);
}
// in any other case a zero entry in any of the dims
// is an error.
else
if (decompDims[0]<1 || decompDims[1]<1 || decompDims[2]<1)
{
sqErrorMacro(cerr,
<< "Invald decomp dims requested "
<< Tuple<int>(decompDims,3) << ".");
return 0;
}
this->DecompDims[0]=decompDims[0];
this->DecompDims[1]=decompDims[1];
this->DecompDims[2]=decompDims[2];
// save ni*nj for faster indexing.
this->DecompDims[3]=this->DecompDims[0]*this->DecompDims[1];
return 1;
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetPeriodicBC(int px, int py, int pz)
{
int periodic[3]={px,py,pz};
this->SetPeriodicBC(periodic);
}
//-----------------------------------------------------------------------------
void CartesianDecomp::SetPeriodicBC(const int periodic[3])
{
this->PeriodicBC[0]=periodic[0];
this->PeriodicBC[1]=periodic[1];
this->PeriodicBC[2]=periodic[2];
}
//-----------------------------------------------------------------------------
CartesianDataBlock *CartesianDecomp::GetBlock(const double *pt)
{
int I[3]={0};
int dext[6]={
0,this->DecompDims[0]-1,
0,this->DecompDims[1]-1,
0,this->DecompDims[2]-1};
if ( DecompSearch(this,dext,0,pt,I)
|| DecompSearch(this,dext,1,pt,I)
|| DecompSearch(this,dext,2,pt,I) )
{
sqErrorMacro(cerr,
"Point "
<< Tuple<double>(pt,3)
<< " not found in "
<< this->Bounds << ".");
return 0;
}
return this->GetBlock(I);
}