forked from daverio/LATfield2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLATfield2_Lattice.hpp
executable file
·435 lines (345 loc) · 10.9 KB
/
LATfield2_Lattice.hpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#ifndef LATFIELD2_LATTICE_HPP
#define LATFIELD2_LATTICE_HPP
/*! \file LATfield2_Lattice.hpp
\brief LATfield2_Lattice.hpp contains the class Lattice definition.
\author David Daverio, Neil Bevis
*/
//CONSTANTS=====================
int Lattice::initialized = 1; //Status flag for initialized
//CONSTRUCTORS===================
Lattice::Lattice()
{
status_=0;
arch_saved_=false;
}
Lattice::Lattice(int dim, const int* size, int halo)
{
status_=0;
arch_saved_=false;
this->initialize(dim, size, halo);
}
Lattice::Lattice(int dim, const int size, int halo)
{
status_=0;
arch_saved_=false;
int* sizeArray=new int[dim];
for(int i=0; i<dim; i++) { sizeArray[i]=size; }
this->initialize(dim, sizeArray, halo);
delete[] sizeArray;
}
//DESTRUCTOR=========================
Lattice::~Lattice()
{
if((status_ & initialized) > 0)
{
delete[] size_;
delete[] sizeLocal_;
delete[] jump_;
delete[] sizeLocalAllProcDim0_;
delete[] sizeLocalAllProcDim1_;
}
}
//INITIALIZE=========================
void Lattice::initialize(int dim, const int size, int halo)
{
int* sizeArray=new int[dim];
for(int i=0; i<dim; i++) { sizeArray[i]=size; }
this->initialize(dim, sizeArray, halo);
}
void Lattice::initialize(int dim, const int* size, int halo)
{
int i;
if((status_ & initialized) > 0)
{
delete[] size_;
delete[] sizeLocal_;
delete[] jump_;
delete[] sizeLocalAllProcDim0_;
delete[] sizeLocalAllProcDim1_;
}
//Store input lattice properties
dim_ =dim;
size_=new int[dim_];
for(i=0;i<dim_;i++) size_[i]=size[i];
halo_=halo;
//Calculate local size
sizeLocal_=new int[dim_];
sizeLocal_[dim_-1]=int(ceil( (parallel.grid_size()[0]-parallel.grid_rank()[0])*size_[dim_-1]/float(parallel.grid_size()[0]) ));
sizeLocal_[dim_-1]-=int(ceil((parallel.grid_size()[0]-parallel.grid_rank()[0]-1)*size_[dim_-1]/float(parallel.grid_size()[0]) ));
sizeLocal_[dim_-2]=int(ceil( (parallel.grid_size()[1]-parallel.grid_rank()[1])*size_[dim_-2]/float(parallel.grid_size()[1]) ));
sizeLocal_[dim_-2]-=int(ceil((parallel.grid_size()[1]-parallel.grid_rank()[1]-1)*size_[dim_-2]/float(parallel.grid_size()[1]) ));
for(i=0;i<dim_-2;i++) sizeLocal_[i]=size_[i];
sizeLocalAllProcDim0_ = new int[parallel.grid_size()[0]];
sizeLocalAllProcDim1_ = new int[parallel.grid_size()[1]];
for(i=0;i<parallel.grid_size()[0];i++)
{
sizeLocalAllProcDim0_[i] = int(ceil( (parallel.grid_size()[0]-i)*size_[dim_-1]/float(parallel.grid_size()[0]) ));
sizeLocalAllProcDim0_[i] -= int(ceil((parallel.grid_size()[0]-i-1)*size_[dim_-1]/float(parallel.grid_size()[0]) ));
}
for(i=0;i<parallel.grid_size()[1];i++)
{
sizeLocalAllProcDim1_[i] = int(ceil( (parallel.grid_size()[1]-i)*size_[dim_-2]/float(parallel.grid_size()[1]) ));
sizeLocalAllProcDim1_[i] -= int(ceil((parallel.grid_size()[1]-i-1)*size_[dim_-2]/float(parallel.grid_size()[1]) ));
}
//Calculate index jumps
jump_=new long[dim_];
jump_[0]=1;
for(i=1;i<dim_;i++) jump_[i]=jump_[i-1]*(sizeLocal_[i-1]+2*halo_);
//Calculate number of sites in lattice
sitesLocal_=1;
sitesLocalGross_=1;
for(i=0;i<dim_;i++)
{
sitesLocal_*=sizeLocal_[i];
sitesLocalGross_*=sizeLocal_[i]+(2*halo_);
}
sites_=sitesLocal_;
sitesGross_=sitesLocalGross_;
parallel.sum(sites_);
parallel.sum(sitesGross_);
//Calculate index of first and last local sites on lattice
siteFirst_=0;
siteLast_=sitesLocalGross_-1;
for(i=0;i<dim_;i++)
{
siteFirst_+=jump_[i]*halo_;
siteLast_-=jump_[i]*halo_;
}
////calculate coordSkip
//Get each processor to tell the others in his dim0_group its local sizeLocal_[dim-1])
int* sizes_dim0=new int[parallel.grid_size()[0]];
for(i=0;i<parallel.grid_size()[0];i++)
{
if(i==parallel.grid_rank()[0]) { sizes_dim0[i]=sizeLocal_[dim_-1]; }
parallel.broadcast_dim0(sizes_dim0[i],i);
}
//Sum up sizes for the processors of less than or equal rank
coordSkip_[0]=0;
for(i=0; i<parallel.grid_rank()[0]; i++)coordSkip_[0]+=sizes_dim0[i];
//Get each processor to tell the others in his dim1_group its local sizeLocal_[dim-2])
int* sizes_dim1=new int[parallel.grid_size()[1]];
for(i=0;i<parallel.grid_size()[1];i++)
{
if(i==parallel.grid_rank()[1]) { sizes_dim1[i]=sizeLocal_[dim_-2]; }
parallel.broadcast_dim1(sizes_dim1[i],i);
}
//Sum up sizes for the processors of less than or equal rank
coordSkip_[1]=0;
for(i=0; i<parallel.grid_rank()[1]; i++)coordSkip_[1]+=sizes_dim1[i];
////calculate sitesSkip : sitesskip used for fastread , fastload (function witch need to be coded :-) )
sitesSkip_=coordSkip_[0];
for(i=0;i<dim_-1;i++)sitesSkip_*=size_[i];
long siteSkiptemp= coordSkip_[1]*sizeLocal_[dim_-1];
for(i=0;i<dim_-2;i++)siteSkiptemp*=size_[i];
sitesSkip_+=siteSkiptemp;
//calculate sitesSkip2d :
int* sizes1 = new int[parallel.grid_size()[1]];
int* sizes0 = new int[parallel.grid_size()[0]];
long* offset1 = new long[parallel.grid_size()[1]];
long* offset0 = new long[parallel.grid_size()[0]];
int b=1;
int n;
for(i=0;i<dim_-2;i++)b*=sizeLocal_[i];
//calulate offset in dim-2
for(i=0;i<parallel.grid_size()[1];i++)sizes1[i]=sizes_dim1[i] * b;
for(n=0;n<parallel.grid_size()[1];n++)offset1[n]=0;
for(n=1;n<parallel.grid_size()[1];n++)
{
for(i=0;i<n;i++)offset1[n]+=sizes1[i];
}
//calulate offset in dim-1
for(i=0;i<parallel.grid_size()[0];i++)sizes0[i]=size_[dim_-2] * sizes_dim0[i] * b;
for(n=0;n<parallel.grid_size()[0];n++)offset0[n]=0;
for(n=1;n<parallel.grid_size()[0];n++)
{
for(i=0;i<n;i++)offset0[n]+=sizes0[i];
}
sitesSkip2d_ = offset0[parallel.grid_rank()[0]] + offset1[parallel.grid_rank()[1]];
//Set status
status_ = status_ | initialized;
//Free memory
delete[] sizes_dim0;
delete[] sizes_dim1;
// WV: Free more memory!
delete[] sizes1;
delete[] sizes0;
delete[] offset1;
delete[] offset0;
}
#ifdef FFT3D
void Lattice::initializeRealFFT(Lattice & lat_real, int halo)
{
if(lat_real.dim()!=3)
{
if(parallel.isRoot())
{
cerr<<"Latfield2d::Lattice::initializeRealFFT : fft curently work only for 3d cubic lattice"<<endl;
cerr<<"Latfield2d::Lattice::initializeRealFFT : coordinate lattice have not 3 dimensions"<<endl;
cerr<<"Latfield2d : Abort Process Requested"<<endl;
}
parallel.abortForce();
}
if(lat_real.size(0)!=lat_real.size(1) | lat_real.size(2)!=lat_real.size(1))
{
if(parallel.isRoot())
{
cerr<<"Latfield2d::Lattice::initializeRealFFT : fft curently work only for 3d cubic lattice"<<endl;
cerr<<"Latfield2d::Lattice::initializeRealFFT : coordinate lattice is not cubic"<<endl;
cerr<<"Latfield2d : Abort Process Requested"<<endl;
}
parallel.abortForce();
}
int lat_size[3];
lat_size[0]=lat_real.size(0)/2+1;
lat_size[1]=lat_real.size(0);
lat_size[2]=lat_real.size(0);
this->initialize(3, lat_size, halo);
}
void Lattice::initializeComplexFFT(Lattice & lat_real, int halo)
{
if(lat_real.dim()!=3)
{
if(parallel.isRoot())
{
cerr<<"Latfield2d::Lattice::initializeRealFFT : fft curently work only for 3d cubic lattice"<<endl;
cerr<<"Latfield2d::Lattice::initializeRealFFT : coordinate lattice have not 3 dimensions"<<endl;
cerr<<"Latfield2d : Abort Process Requested"<<endl;
}
parallel.abortForce();
}
if(lat_real.size(0)!=lat_real.size(1) | lat_real.size(2)!=lat_real.size(1))
{
if(parallel.isRoot())
{
cerr<<"Latfield2d::Lattice::initializeRealFFT : fft curently work only for 3d cubic lattice"<<endl;
cerr<<"Latfield2d::Lattice::initializeRealFFT : coordinate lattice is not cubic"<<endl;
cerr<<"Latfield2d : Abort Process Requested"<<endl;
}
parallel.abortForce();
}
int lat_size[3];
lat_size[0]=lat_real.size(0);
lat_size[1]=lat_real.size(0);
lat_size[2]=lat_real.size(0);
this->initialize(3, lat_size, halo);
}
#endif
void Lattice::save_arch(const string filename)
{
fstream file;
int p,i;
for( p=0; p<parallel.size(); p++ )
{
if( parallel.rank()==p )
{
if( parallel.rank()==0)
{
//Truncate file if first process
file.open(filename.c_str(), fstream::out | fstream::trunc);
}
else
{
//Append to file if another process
file.open(filename.c_str(), fstream::out | fstream::app);
}
if(!file.is_open())
{
cerr<<"Latfield::Lattice::save_arch - Could not open file for writing"<<endl;
cerr<<"Latfield::Lattice::save_arch - File: "<<filename<<endl;
parallel.abortRequest();
}
if( parallel.rank()==0)
{
file<<"# Architerctur of the lattice"<<endl;
file<<"# Number of dimension :"<<endl;
file<<this->dim()<<endl;
file<<"############################"<<endl;
}
file<<"############################"<<endl;
file<<"# Ranks of processor, world, dim-1, dim-2 :"<<endl;
file<<parallel.rank()<<" "<<parallel.grid_rank()[0]<<" "<<parallel.grid_rank()[1] <<endl;
file<<"# Local size :"<<endl;
for(i=0;i<this->dim();i++)
{
file<<this->sizeLocal()[i]<<endl;
}
file<<"############################"<<endl;
file.close();
}
MPI_Barrier(MPI_COMM_WORLD);
}
arch_saved_=true;
if(parallel.rank()==0)cout<<"Architecture saved in : "<<filename<<endl;
}
int Lattice::getRank(int* coord)
{
int n,m;
int temp;
bool flag;
temp=0;
flag = true;
for(n=0;flag;n++)
{
temp+= sizeLocalAllProcDim0_[n];
if(temp>coord[dim_-1])flag=false;
}
n-=1;
temp=0;
flag = true;
for(m=0;flag;m++)
{
temp+= sizeLocalAllProcDim1_[m];
if(temp>coord[dim_-2])flag=false;
}
m-=1;
return parallel.grid2world(n,m);
}
int Lattice::getRankDim0(int coord)
{
int n;
int temp;
bool flag;
temp=0;
flag = true;
for(n=0;flag;n++)
{
temp+= sizeLocalAllProcDim0_[n];
if(temp>coord)flag=false;
}
return n-1;
}
int Lattice::getRankDim1(int coord)
{
int m;
int temp;
bool flag;
temp=0;
flag = true;
for(m=0;flag;m++)
{
temp+= sizeLocalAllProcDim1_[m];
if(temp>coord)flag=false;
}
return m-1;
}
//MISCELLANEOUS======================
bool Lattice::is_arch_saved() {return arch_saved_;};
int Lattice::dim() { return dim_; };
int* Lattice::size() { return size_; };
int Lattice::size(int i) { return size_[i]; }
long Lattice::sites() { return sites_; }
long Lattice::sitesGross() { return sitesGross_; }
int Lattice::halo() { return halo_; }
int* Lattice::sizeLocal() { return sizeLocal_; };
int Lattice::sizeLocal(int i) { return sizeLocal_[i]; }
long Lattice::sitesLocal() { return sitesLocal_; }
long Lattice::sitesLocalGross() { return sitesLocalGross_; }
long Lattice::jump(int i) { return jump_[i]; }
long Lattice::sitesSkip() { return sitesSkip_; }
long Lattice::sitesSkip2d() { return sitesSkip2d_; }
long* Lattice::coordSkip() { return coordSkip_; }
long Lattice::siteFirst() { return siteFirst_; }
long Lattice::siteLast() { return siteLast_; }
int * Lattice::sizeLocalAllProcDim0(){ return sizeLocalAllProcDim0_; }
int * Lattice::sizeLocalAllProcDim1(){ return sizeLocalAllProcDim1_; }
#endif