forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_grid.cpp
More file actions
468 lines (401 loc) · 11.1 KB
/
parallel_grid.cpp
File metadata and controls
468 lines (401 loc) · 11.1 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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "parallel_grid.h"
#include "parallel_global.h"
Parallel_Grid::Parallel_Grid()
{
this->allocate = false;
this->allocate_final_scf = false; //LiuXh add 20180619
}
Parallel_Grid::~Parallel_Grid()
{
//if(this->allocate) //LiuXh modify 20180619
if(this->allocate || this->allocate_final_scf) //LiuXh add 20180619
{
for(int ip=0; ip<GlobalV::KPAR; ip++)
{
delete[] numz[ip];
delete[] startz[ip];
delete[] whichpro[ip];
}
delete[] numz;
delete[] startz;
delete[] whichpro;
}
}
void Parallel_Grid::init(
const int &ncx_in,
const int &ncy_in,
const int &ncz_in,
const int &nczp_in,
const int &nrxx_in,
const int &nbz_in,
const int &bz_in)
{
#ifndef __MPI
return;
#endif
ModuleBase::TITLE("Parallel_Grid","init");
this->ncx = ncx_in;
this->ncy = ncy_in;
this->ncz = ncz_in;
this->nczp = nczp_in;
this->nrxx = nrxx_in;
this->nbz = nbz_in;
this->bz = bz_in;
if(nczp<0)
{
GlobalV::ofs_warning << " nczp = " << nczp << std::endl;
ModuleBase::WARNING_QUIT("Parallel_Grid::init","nczp<0");
}
assert(ncx > 0);
assert(ncy > 0);
assert(ncz > 0);
this->ncxy = ncx * ncy;
this->ncxyz = ncxy * ncz;
// (2)
assert(allocate==false);
assert(GlobalV::KPAR > 0);
this->nproc_in_pool = new int[GlobalV::KPAR];
int nprocgroup;
if(GlobalV::CALCULATION.substr(0,3)=="sto") nprocgroup = GlobalV::NPROC_IN_STOGROUP;
else nprocgroup = GlobalV::NPROC;
const int remain_pro = nprocgroup%GlobalV::KPAR;
for(int i=0; i<GlobalV::KPAR; i++)
{
nproc_in_pool[i] = nprocgroup/GlobalV::KPAR;
if(i<remain_pro) this->nproc_in_pool[i]++;
}
this->numz = new int*[GlobalV::KPAR];
this->startz = new int*[GlobalV::KPAR];
this->whichpro = new int*[GlobalV::KPAR];
this->numdata = new int*[GlobalV::KPAR];
this->startdata = new int*[GlobalV::KPAR];
for(int ip=0; ip<GlobalV::KPAR; ip++)
{
const int nproc = nproc_in_pool[ip];
this->numz[ip] = new int[nproc];
this->startz[ip] = new int[nproc];
this->whichpro[ip] = new int[this->ncz];
this->numdata[ip] = new int[nproc];
this->startdata[ip] = new int[nproc];
ModuleBase::GlobalFunc::ZEROS(this->numz[ip], nproc);
ModuleBase::GlobalFunc::ZEROS(this->startz[ip], nproc);
ModuleBase::GlobalFunc::ZEROS(this->whichpro[ip], this->ncz);
ModuleBase::GlobalFunc::ZEROS(this->numdata[ip], nproc);
ModuleBase::GlobalFunc::ZEROS(this->startdata[ip], nproc);
}
this->allocate = true;
this->z_distribution();
return;
}
void Parallel_Grid::z_distribution(void)
{
assert(allocate);
int* startp = new int[GlobalV::KPAR];
startp[0] = 0;
for(int ip=0; ip<GlobalV::KPAR; ip++)
{
// GlobalV::ofs_running << "\n now POOL=" << ip;
const int nproc = nproc_in_pool[ip];
if(ip>0) startp[ip] = startp[ip-1] + nproc_in_pool[ip-1];
// (1) how many z on each 'proc' in each 'pool'
for(int iz=0; iz<nbz; iz++)
{
const int proc = iz % nproc;
numz[ip][proc]+=bz;
}
// for(int proc=0; proc<nproc; proc++)
// {
// GlobalV::ofs_running << "\n proc=" << proc << " numz=" << numz[ip][proc];
// }
// (2) start position of z in each 'proc' in each 'pool'
startz[ip][0] = 0;
for (int proc=1; proc<nproc; proc++)
{
startz[ip][proc] = startz[ip][proc-1] + numz[ip][proc-1];
}
// for(int proc=0; proc<nproc; proc++)
// {
// GlobalV::ofs_running << "\n proc=" << proc << " startz=" << startz[ip][proc];
// }
// (3) each z belongs to which 'proc' ( global index )
for(int iz=0; iz<ncz; iz++)
{
for(int proc=0; proc<nproc; proc++)
{
if(iz>=startz[ip][nproc-1])
{
whichpro[ip][iz] = startp[ip] + nproc-1;
break;
}
else if(iz>=startz[ip][proc] && iz<startz[ip][proc+1])
{
whichpro[ip][iz] = startp[ip] + proc;
break;
}
}
}
// for(int iz=0; iz<ncz; iz++)
// {
// GlobalV::ofs_running << "\n iz=" << iz << " whichpro=" << whichpro[ip][iz];
// }
//(4)
for(int proc=0; proc<nproc; proc++)
{
numdata[ip][proc] = numz[ip][proc]*ncxy;
}
//(5)
startdata[ip][0]=0;
for(int proc=1; proc<nproc; proc++)
{
startdata[ip][proc]=startdata[ip][proc-1]+numdata[ip][proc-1];
}
}
delete[] startp;
return;
}
#ifdef __MPI
void Parallel_Grid::zpiece_to_all(double *zpiece, const int &iz, double *rho)
{
if(GlobalV::CALCULATION.substr(0,3)=="sto")
{
this->zpiece_to_stogroup(zpiece,iz,rho);
return;
}
assert(allocate);
//ModuleBase::TITLE("Parallel_Grid","zpiece_to_all");
MPI_Status ierror;
const int znow = iz - this->startz[GlobalV::MY_POOL][GlobalV::RANK_IN_POOL];
const int proc = this->whichpro[GlobalV::MY_POOL][iz];
if(GlobalV::MY_POOL==0)
{
// case 1: the first part of rho in processor 0.
// and send zpeice to to other pools.
if(proc == 0 && GlobalV::MY_RANK ==0)
{
for(int ir=0; ir<ncxy; ir++)
{
rho[ir*nczp+znow] = zpiece[ir];
}
for(int ipool=1; ipool < GlobalV::KPAR; ipool++)
{
MPI_Send(zpiece, ncxy, MPI_DOUBLE, this->whichpro[ipool][iz], iz, MPI_COMM_WORLD);
}
}
// case 2: processor n (n!=0) receive rho from processor 0.
// and the receive tag is iz.
else if(proc == GlobalV::RANK_IN_POOL )
{
MPI_Recv(zpiece, ncxy, MPI_DOUBLE, 0, iz, MPI_COMM_WORLD,&ierror);
for(int ir=0; ir<ncxy; ir++)
{
rho[ir*nczp + znow] = zpiece[ir];
}
}
// case 2: > first part rho: processor 0 send the rho
// to all pools. The tag is iz, because processor may
// send more than once, and the only tag to distinguish
// them is iz.
else if(GlobalV::RANK_IN_POOL==0)
{
for(int ipool=0; ipool < GlobalV::KPAR; ipool++)
{
MPI_Send(zpiece, ncxy, MPI_DOUBLE, this->whichpro[ipool][iz], iz, MPI_COMM_WORLD);
}
}
}// GlobalV::MY_POOL == 0
else
{
//GlobalV::ofs_running << "\n Receive charge density iz=" << iz << std::endl;
// the processors in other pools always receive rho from
// processor 0. the tag is 'iz'
if(proc == GlobalV::MY_RANK )
{
MPI_Recv(zpiece, ncxy, MPI_DOUBLE, 0, iz, MPI_COMM_WORLD,&ierror);
for(int ir=0; ir<ncxy; ir++)
{
rho[ir*nczp+znow] = zpiece[ir];
}
}
}
//GlobalV::ofs_running << "\n iz = " << iz << " Done.";
return;
}
#endif
#ifdef __MPI
void Parallel_Grid::zpiece_to_stogroup(double *zpiece, const int &iz, double *rho)
{
assert(allocate);
//TITLE("Parallel_Grid","zpiece_to_all");
MPI_Status ierror;
const int znow = iz - this->startz[GlobalV::MY_POOL][GlobalV::RANK_IN_POOL];
const int proc = this->whichpro[GlobalV::MY_POOL][iz];
if(GlobalV::MY_POOL==0)
{
// case 1: the first part of rho in processor 0.
// and send zpeice to to other pools.
if(proc == 0 && GlobalV::RANK_IN_STOGROUP ==0)
{
for(int ir=0; ir<ncxy; ir++)
{
rho[ir*nczp+znow] = zpiece[ir];
}
for(int ipool=1; ipool < GlobalV::KPAR; ipool++)
{
MPI_Send(zpiece, ncxy, MPI_DOUBLE, this->whichpro[ipool][iz], iz, STO_WORLD);
}
}
// case 2: processor n (n!=0) receive rho from processor 0.
// and the receive tag is iz.
else if(proc == GlobalV::RANK_IN_POOL )
{
MPI_Recv(zpiece, ncxy, MPI_DOUBLE, 0, iz, STO_WORLD,&ierror);
for(int ir=0; ir<ncxy; ir++)
{
rho[ir*nczp + znow] = zpiece[ir];
}
}
// case 2: > first part rho: processor 0 send the rho
// to all pools. The tag is iz, because processor may
// send more than once, and the only tag to distinguish
// them is iz.
else if(GlobalV::RANK_IN_POOL==0)
{
for(int ipool=0; ipool < GlobalV::KPAR; ipool++)
{
MPI_Send(zpiece, ncxy, MPI_DOUBLE, this->whichpro[ipool][iz], iz, STO_WORLD);
}
}
}// MY_POOL == 0
else
{
//ofs_running << "\n Receive charge density iz=" << iz << endl;
// the processors in other pools always receive rho from
// processor 0. the tag is 'iz'
if(proc == GlobalV::RANK_IN_STOGROUP )
{
MPI_Recv(zpiece, ncxy, MPI_DOUBLE, 0, iz, STO_WORLD,&ierror);
for(int ir=0; ir<ncxy; ir++)
{
rho[ir*nczp+znow] = zpiece[ir];
}
}
}
//ofs_running << "\n iz = " << iz << " Done.";
return;
}
void Parallel_Grid::reduce_to_fullrho(double *rhotot, double *rhoin)
{
//ModuleBase::TITLE("Parallel_Grid","reduce_to_fullrho");
// if not the first pool, wait here until processpr 0
// send the Barrier command.
if(GlobalV::MY_POOL!=0)
{
MPI_Barrier(MPI_COMM_WORLD);
return;
}
double* zpiece = new double[this->ncxy];
for(int iz=0; iz<this->ncz; iz++)
{
const int znow = iz - this->startz[GlobalV::MY_POOL][GlobalV::RANK_IN_POOL];
const int proc = this->whichpro[GlobalV::MY_POOL][iz];
ModuleBase::GlobalFunc::ZEROS(zpiece, this->ncxy);
int tag = iz;
MPI_Status ierror;
// case 1: the first part of rho in processor 0.
if(proc == 0 && GlobalV::RANK_IN_POOL ==0)
{
for(int ir=0; ir<ncxy; ir++)
{
zpiece[ir] = rhoin[ir*this->nczp + znow];
}
}
// case 2: > first part rho: send the rho to
// processor 0.
else if(proc == GlobalV::RANK_IN_POOL )
{
for(int ir=0; ir<ncxy; ir++)
{
zpiece[ir] = rhoin[ir*this->nczp + znow];
}
MPI_Send(zpiece, ncxy, MPI_DOUBLE, 0, tag, POOL_WORLD);
}
// case 2: > first part rho: processor 0 receive the rho
// from other processors
else if(GlobalV::RANK_IN_POOL==0)
{
MPI_Recv(zpiece, ncxy, MPI_DOUBLE, proc, tag, POOL_WORLD, &ierror);
}
if(GlobalV::MY_RANK==0)
{
for(int ix=0; ix<this->ncx; ix++)
{
for(int iy=0; iy<this->ncy; iy++)
{
const int ir = ix * this->ncy + iy;
rhotot[ix * ncy * ncz + iy * ncz + iz] = zpiece[ir];
}
}
}
}
delete[] zpiece;
MPI_Barrier(MPI_COMM_WORLD);
return;
}
#endif
void Parallel_Grid::init_final_scf(const int &ncx_in, const int &ncy_in, const int &ncz_in, const int &nczp_in,
const int &nrxx_in, const int &nbz_in, const int &bz_in)
{
#ifndef __MPI
return;
#endif
ModuleBase::TITLE("Parallel_Grid","init");
this->ncx = ncx_in;
this->ncy = ncy_in;
this->ncz = ncz_in;
this->nczp = nczp_in;
this->nrxx = nrxx_in;
this->nbz = nbz_in;
this->bz = bz_in;
if(nczp<0)
{
GlobalV::ofs_warning << " nczp = " << nczp << std::endl;
ModuleBase::WARNING_QUIT("Parallel_Grid::init","nczp<0");
}
assert(ncx > 0);
assert(ncy > 0);
assert(ncz > 0);
this->ncxy = ncx * ncy;
this->ncxyz = ncxy * ncz;
// (2)
assert(allocate_final_scf==false);
assert(GlobalV::KPAR > 0);
this->nproc_in_pool = new int[GlobalV::KPAR];
const int remain_pro = GlobalV::NPROC%GlobalV::KPAR;
for(int i=0; i<GlobalV::KPAR; i++)
{
nproc_in_pool[i] = GlobalV::NPROC/GlobalV::KPAR;
if(i<remain_pro) this->nproc_in_pool[i]++;
}
this->numz = new int*[GlobalV::KPAR];
this->startz = new int*[GlobalV::KPAR];
this->whichpro = new int*[GlobalV::KPAR];
this->numdata = new int*[GlobalV::KPAR];
this->startdata = new int*[GlobalV::KPAR];
for(int ip=0; ip<GlobalV::KPAR; ip++)
{
const int nproc = nproc_in_pool[ip];
this->numz[ip] = new int[nproc];
this->startz[ip] = new int[nproc];
this->whichpro[ip] = new int[this->ncz];
this->numdata[ip] = new int[nproc];
this->startdata[ip] = new int[nproc];
ModuleBase::GlobalFunc::ZEROS(this->numz[ip], nproc);
ModuleBase::GlobalFunc::ZEROS(this->startz[ip], nproc);
ModuleBase::GlobalFunc::ZEROS(this->whichpro[ip], this->ncz);
ModuleBase::GlobalFunc::ZEROS(this->numdata[ip], nproc);
ModuleBase::GlobalFunc::ZEROS(this->startdata[ip], nproc);
}
this->allocate_final_scf = true;
this->z_distribution();
return;
}