NAMD
PmeSolver.C
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <iomanip>
3 #include "Priorities.h"
4 #if !(defined(NAMD_HIP) || defined(NAMD_CUDA))
5 struct float2 {float x,y;};
6 #endif
7 #include "PmeSolver.h"
8 #include "PatchData.h"
9 
10 //
11 // Data flow for PmePencilXYZ
12 //
13 // dataSrc [xyz] dataDst
14 //
15 // dataDst [solve] dataDst
16 //
17 // dataDst [xyz] dataSrc
18 //
19 // dataSrc [force]
20 //
21 
23  __sdag_init();
24  setMigratable(false);
25  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
26  pmeKSpaceComputes[iGrid] = NULL;
27  fftComputes[iGrid] = NULL;
28  }
30 #ifdef NODEGROUP_FORCE_REGISTER
31 // #if false
32  CProxy_PatchData cpdata(CkpvAccess(BOCclass_group).patchData);
33  PatchData *patchData = cpdata.ckLocalBranch();
34  nodeReduction = patchData->reduction;
35 #endif
36 // multipleGridLock = CmiCreateLock();
37  doEnergy = false;
38  doVirial = false;
39  simulationStep = 0;
40 }
41 
42 PmePencilXYZ::PmePencilXYZ(CkMigrateMessage *m) {
43  NAMD_bug("PmePencilXYZ cannot be migrated");
44  //__sdag_init();
45  // setMigratable(false);
46  // fftCompute = NULL;
47  // pmeKSpaceCompute = NULL;
48  // reduction = ReductionMgr::Object()->willSubmit(REDUCTIONS_BASIC);
49 }
50 
52  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
53  if (pmeKSpaceComputes[iGrid] != NULL) delete pmeKSpaceComputes[iGrid];
54  if (fftComputes[iGrid] != NULL) delete fftComputes[iGrid];
55  }
56  delete reduction;
57 // CmiDestroyLock(multipleGridLock);
58 }
59 
60 void PmePencilXYZ::initFFT(PmeStartMsg *msg) {
61  if (fftComputes[0] == NULL)
62  NAMD_bug("PmePencilXYZ::initFFT, fftCompute not initialized");
63 // fftCompute->init(msg->dataGrid[0], msg->dataSizes[0], NULL, 0, Perm_X_Y_Z, pmeGrid, 3, 0, 0, 0);
64 // fftCompute2->init(msg->dataGrid[1], msg->dataSizes[1], NULL, 0, Perm_X_Y_Z, pmeGrid, 3, 0, 0, 0);
65  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
66  if (msg->enabledGrid[iGrid] == true) {
67  fftComputes[iGrid]->init(msg->dataGrid[iGrid], msg->dataSizes[iGrid], NULL, 0, Perm_X_Y_Z, pmeGrid, 3, 0, 0, 0);
68  }
69  }
70 }
71 
72 void PmePencilXYZ::forwardFFT() {
73  if (fftComputes[0] == NULL)
74  NAMD_bug("PmePencilXYZ::forwardFFT, fftCompute not initialized");
75  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
76  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->forward();
77  }
78 }
79 
80 void PmePencilXYZ::backwardFFT() {
81  if (fftComputes[0] == NULL)
82  NAMD_bug("PmePencilXYZ::backwardFFT, fftCompute not initialized");
83  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
84  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->backward();
85  }
86 }
87 
88 void PmePencilXYZ::forwardDone() {
89  if (pmeKSpaceComputes[0] == NULL)
90  NAMD_bug("PmePencilXYZ::forwardDone, pmeKSpaceCompute not initialized");
91  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
92  if (pmeKSpaceComputes[iGrid] != NULL && fftComputes[iGrid] != NULL)
93  pmeKSpaceComputes[iGrid]->solve(lattice, doEnergy, doVirial, fftComputes[iGrid]->getDataDst());
94  }
95 }
96 
98  NAMD_bug("PmePencilXYZ::backwardDone(), base class method called");
99 }
100 
101 void PmePencilXYZ::submitReductions(unsigned int iGrid) {
102 // fprintf(stderr, "PmePencilXYZ::submitReductions\n");
103  if (pmeKSpaceComputes[iGrid] == NULL)
104  NAMD_bug("PmePencilXYZ::submitReductions, pmeKSpaceCompute not initialized");
106  double virial[9];
107  double energy = pmeKSpaceComputes[iGrid]->getEnergy();
108  pmeKSpaceComputes[iGrid]->getVirial(virial);
109  if (simParams->alchOn) {
110  if (simParams->alchFepOn) {
111  double energy_F = energy;
112  double scale1 = 1.0; // energy scaling factor for λ_1
113  double scale2 = 1.0; // energy scaling factor for λ_2
114  switch (iGrid) {
115  case 0: {
116  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
117  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
118  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
119  const BigReal elecLambda2Up = simParams->getElecLambda(alchLambda2);
120  scale1 = elecLambdaUp;
121  scale2 = elecLambda2Up;
122  break;
123  }
124  case 1: {
125  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
126  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
127  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
128  const BigReal elecLambda2Down = simParams->getElecLambda(1 - alchLambda2);
129  scale1 = elecLambdaDown;
130  scale2 = elecLambda2Down;
131  break;
132  }
133  case 2: {
134  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
135  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
136  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
137  const BigReal elecLambda2Up = simParams->getElecLambda(alchLambda2);
138  scale1 = 1.0 - elecLambdaUp;
139  scale2 = 1.0 - elecLambda2Up;
140  break;
141  }
142  case 3: {
143  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
144  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
145  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
146  const BigReal elecLambda2Down = simParams->getElecLambda(1 - alchLambda2);
147  scale1 = 1.0 - elecLambdaDown;
148  scale2 = 1.0 - elecLambda2Down;
149  break;
150  }
151  case 4: {
152  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
153  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
154  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
155  const BigReal elecLambda2Up = simParams->getElecLambda(alchLambda2);
156  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
157  const BigReal elecLambda2Down = simParams->getElecLambda(1 - alchLambda2);
158  scale1 = -1.0 * (elecLambdaUp + elecLambdaDown - 1.0);
159  scale2 = -1.0 * (elecLambda2Up + elecLambda2Down - 1.0);
160  break;
161  }
162  }
163  energy *= scale1;
164  energy_F *= scale2;
165 // fprintf(stdout, "KSpace Grid %u ; E1 = %lf ; E2 = %lf ; scale1 = %lf ; scale2 = %lf\n", iGrid, energy, energy_F, scale1, scale2);
166  for (size_t i = 0; i < 9; ++i) {
167  virial[i] *= scale1;
168  }
169 #if NODEGROUP_FORCE_REGISTER
170  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW_F) += energy_F;
171 #endif
172  reduction->item(REDUCTION_ELECT_ENERGY_SLOW_F) += energy_F;
173  }
174  if (simParams->alchThermIntOn) {
175  double energy_TI_1 = 0.0;
176  double energy_TI_2 = 0.0;
177  double scale1 = 1.0;
178  switch (iGrid) {
179  case 0: {
180  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
181  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
182  scale1 = elecLambdaUp;
183  energy_TI_1 = energy;
184  break;
185  }
186  case 1: {
187  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
188  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
189  scale1 = elecLambdaDown;
190  energy_TI_2 = energy;
191  break;
192  }
193  case 2: {
194  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
195  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
196  scale1 = 1.0 - elecLambdaUp;
197  energy_TI_1 = -1.0 * energy;
198  break;
199  }
200  case 3: {
201  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
202  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
203  scale1 = 1.0 - elecLambdaDown;
204  energy_TI_2 = -1.0 * energy;
205  break;
206  }
207  case 4: {
208  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
209  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
210  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
211  scale1 = -1.0 * (elecLambdaUp + elecLambdaDown - 1.0);
212  energy_TI_1 = -1.0 * energy;
213  energy_TI_2 = -1.0 * energy;
214  break;
215  }
216  }
217  for (size_t i = 0; i < 9; ++i) {
218  virial[i] *= scale1;
219  }
220  energy *= scale1;
221 // fprintf(stdout, "Grid %u : energy_TI_1 = %lf ; energy_TI_2 = %lf\n", iGrid, energy_TI_1, energy_TI_2);
222 #if NODEGROUP_FORCE_REGISTER
223  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_1) += energy_TI_1;
224  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_2) += energy_TI_2;
225 #endif
226  reduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_1) += energy_TI_1;
227  reduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_2) += energy_TI_2;
228  }
229  }
230 #ifdef NODEGROUP_FORCE_REGISTER
231  // #if false
232  // XXX Expect a race condition, need atomic access to nodeReduction
233  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW) += energy;
234  nodeReduction->item(REDUCTION_VIRIAL_SLOW_XX) += virial[0];
235  nodeReduction->item(REDUCTION_VIRIAL_SLOW_XY) += virial[1];
236  nodeReduction->item(REDUCTION_VIRIAL_SLOW_XZ) += virial[2];
237  nodeReduction->item(REDUCTION_VIRIAL_SLOW_YX) += virial[3];
238  nodeReduction->item(REDUCTION_VIRIAL_SLOW_YY) += virial[4];
239  nodeReduction->item(REDUCTION_VIRIAL_SLOW_YZ) += virial[5];
240  nodeReduction->item(REDUCTION_VIRIAL_SLOW_ZX) += virial[6];
241  nodeReduction->item(REDUCTION_VIRIAL_SLOW_ZY) += virial[7];
242  nodeReduction->item(REDUCTION_VIRIAL_SLOW_ZZ) += virial[8];
243 #endif
244  reduction->item(REDUCTION_ELECT_ENERGY_SLOW) += energy;
245  reduction->item(REDUCTION_VIRIAL_SLOW_XX) += virial[0];
246  reduction->item(REDUCTION_VIRIAL_SLOW_XY) += virial[1];
247  reduction->item(REDUCTION_VIRIAL_SLOW_XZ) += virial[2];
248  reduction->item(REDUCTION_VIRIAL_SLOW_YX) += virial[3];
249  reduction->item(REDUCTION_VIRIAL_SLOW_YY) += virial[4];
250  reduction->item(REDUCTION_VIRIAL_SLOW_YZ) += virial[5];
251  reduction->item(REDUCTION_VIRIAL_SLOW_ZX) += virial[6];
252  reduction->item(REDUCTION_VIRIAL_SLOW_ZY) += virial[7];
253  reduction->item(REDUCTION_VIRIAL_SLOW_ZZ) += virial[8];
255  energyReady[iGrid] = 1;
256 // CmiLock(multipleGridLock);
257  bool ready_to_submit = true;
258  for (size_t i = 0; i < NUM_GRID_MAX; ++i) {
259  if (energyReady[i] == -1) continue;
260  if (energyReady[i] == 0) ready_to_submit = false;
261  }
262  if (ready_to_submit) {
263 // fprintf(stdout, "all energy ready\n");
264  reduction->submit();
265  for (size_t i = 0; i < NUM_GRID_MAX; ++i) {
266  if (energyReady[i] == -1) continue;
267  if (energyReady[i] == 1) energyReady[i] = 0;
268  }
269  }
270 // CmiUnlock(multipleGridLock);
271 }
272 
274  reduction->submit();
275 }
276 
277 //###########################################################################
278 //###########################################################################
279 //###########################################################################
280 
281 //
282 // Data flow for PmePencilXY & PmePencilZ
283 //
284 // dataSrc(XY) [xy] dataDst(XY)
285 //
286 // dataDst(XY) [transp] dataSrc(Z)
287 //---------------------------------
288 //
289 // dataSrc(Z) [z] dataDst(Z)
290 //
291 // dataDst(Z) [solve] dataDst(Z)
292 //
293 // dataDst(Z) [z] dataSrc(Z)
294 //
295 // dataSrc(Z) [transp] dataDst(XY)
296 //---------------------------------
297 //
298 // dataDst(XY) [xy] dataSrc(XY)
299 //
300 // dataSrc(XY) [force]
301 //
302 
304  __sdag_init();
305  setMigratable(false);
306  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
307  pmeTransposes[iGrid] = NULL;
308  fftComputes[iGrid] = NULL;
309  }
310 }
311 
312 PmePencilXY::PmePencilXY(CkMigrateMessage *m) {
313  NAMD_bug("PmePencilXY cannot be migrated");
314 //__sdag_init();
315  // setMigratable(false);
316  // fftCompute = NULL;
317  // pmeTranspose = NULL;
318 }
319 
321 // if (fftCompute != NULL) delete fftCompute;
322 // if (pmeTranspose != NULL) delete pmeTranspose;
323 // if (fftCompute2 != NULL) delete fftCompute2;
324 // if (pmeTranspose2 != NULL) delete pmeTranspose2;
325  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
326  if (pmeTransposes[iGrid] != NULL) delete pmeTransposes[iGrid];
327  if (fftComputes[iGrid] != NULL) delete fftComputes[iGrid];
328  }
329 }
330 
331 void PmePencilXY::initFFT(PmeStartMsg *msg) {
332  if (fftComputes[0] == NULL)
333  NAMD_bug("PmePencilXY::initFFT, fftCompute not initialized");
334 // fftCompute->init(msg->dataGrid[0], msg->dataSizes[0], NULL, 0, Perm_X_Y_Z, pmeGrid, 2, 0, thisIndex.z, 0);
335 // fftCompute2->init(msg->dataGrid[1], msg->dataSizes[1], NULL, 0, Perm_X_Y_Z, pmeGrid, 2, 0, thisIndex.z, 0);
336  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
337  if (msg->enabledGrid[iGrid] == true) {
338  fftComputes[iGrid]->init(msg->dataGrid[iGrid], msg->dataSizes[iGrid], NULL, 0, Perm_X_Y_Z, pmeGrid, 2, 0, thisIndex.z, 0);
339  }
340  }
341 }
342 
343 void PmePencilXY::forwardFFT() {
344  if (fftComputes[0] == NULL)
345  NAMD_bug("PmePencilXY::forwardFFT, fftCompute not initialized");
346  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
347  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->forward();
348  }
349 }
350 
351 void PmePencilXY::backwardFFT() {
352  if (fftComputes[0] == NULL)
353  NAMD_bug("PmePencilXY::backwardFFT, fftCompute not initialized");
354  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
355  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->backward();
356  }
357 }
358 
360  blockSizes.resize(pmeGrid.xBlocks);
361  for (int x=0;x < pmeGrid.xBlocks;x++) {
362  int i0, i1, j0, j1, k0, k1;
363  getBlockDim(pmeGrid, Perm_cX_Y_Z, x, 0, thisIndex.z,
364  i0, i1, j0, j1, k0, k1);
365  int size = (i1-i0+1)*(j1-j0+1)*(k1-k0+1);
366  blockSizes[x] = size;
367  }
368 }
369 
370 void PmePencilXY::forwardDone() {
371  NAMD_bug("PmePencilXY::forwardDone(), base class method called");
372 }
373 
374 void PmePencilXY::backwardDone() {
375  NAMD_bug("PmePencilXY::backwardDone(), base class method called");
376 }
377 
378 void PmePencilXY::recvDataFromZ(PmeBlockMsg *msg) {
379  NAMD_bug("PmePencilXY::recvDataFromZ(), base class method called");
380 }
381 
382 void PmePencilXY::start(const CkCallback &) {
383  NAMD_bug("PmePencilXY::start(), base class method called");
384 }
385 
386 //###########################################################################
387 //###########################################################################
388 //###########################################################################
389 
390 //
391 // Data flow for PmePencilX & PmePencilX & PmePencilZ
392 //
393 // dataSrc(X) [x] dataDst(X)
394 //
395 // dataDst(X) [transp] dataSrc(Y)
396 //---------------------------------
397 //
398 // dataSrc(Y) [y] dataDst(Y)
399 //
400 // dataDst(Y) [transp] dataSrc(Z)
401 //---------------------------------
402 //
403 // dataSrc(Z) [z] dataDst(Z)
404 //
405 // dataDst(Z) [solve] dataDst(Z)
406 //
407 // dataDst(Z) [z] dataSrc(Z)
408 //
409 // dataSrc(Z) [transp] dataDst(Y)
410 //---------------------------------
411 //
412 // dataDst(Y) [y] dataSrc(Y)
413 //
414 // dataSrc(Y) [transp] dataDst(X)
415 //---------------------------------
416 //
417 // dataDst(X) [x] dataSrc(X)
418 //
419 // dataSrc(X) [force]
420 //
421 
423  __sdag_init();
424  setMigratable(false);
425  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
426  pmeTransposes[iGrid] = NULL;
427  fftComputes[iGrid] = NULL;
428  }
429  numStrayAtoms = 0;
430 }
431 
432 PmePencilX::PmePencilX(CkMigrateMessage *m) {
433  NAMD_bug("PmePencilX cannot be migrated");
434 //__sdag_init();
435  // setMigratable(false);
436  // fftCompute = NULL;
437  // pmeTranspose = NULL;
438 }
439 
441  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
442  if (pmeTransposes[iGrid] != NULL) delete pmeTransposes[iGrid];
443  if (fftComputes[iGrid] != NULL) delete fftComputes[iGrid];
444  }
445 }
446 
447 void PmePencilX::initFFT(PmeStartMsg *msg) {
448  if (fftComputes[0] == NULL)
449  NAMD_bug("PmePencilX::initFFT, fftCompute not initialized");
450 // fftCompute->init(msg->dataGrid[0], msg->dataSizes[0], NULL, 0, Perm_X_Y_Z, pmeGrid, 1, thisIndex.y, thisIndex.z, 0);
451 // fftCompute2->init(msg->dataGrid[1], msg->dataSizes[1], NULL, 0, Perm_X_Y_Z, pmeGrid, 1, thisIndex.y, thisIndex.z, 0);
452  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
453  if (msg->enabledGrid[iGrid] == true) {
454  fftComputes[iGrid]->init(msg->dataGrid[iGrid], msg->dataSizes[iGrid], NULL, 0, Perm_X_Y_Z, pmeGrid, 1, thisIndex.y, thisIndex.z, 0);
455  }
456  }
457 }
458 
459 void PmePencilX::forwardFFT() {
460  if (fftComputes[0] == NULL)
461  NAMD_bug("PmePencilX::forwardFFT, fftCompute not initialized");
462  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
463  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->forward();
464  }
465 }
466 
467 void PmePencilX::backwardFFT() {
468  if (fftComputes[0] == NULL)
469  NAMD_bug("PmePencilX::backwardFFT, fftCompute not initialized");
470  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
471  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->backward();
472  }
473 }
474 
476  blockSizes.resize(pmeGrid.xBlocks);
477  for (int x=0;x < pmeGrid.xBlocks;x++) {
478  int i0, i1, j0, j1, k0, k1;
479  getBlockDim(pmeGrid, Perm_cX_Y_Z, x, thisIndex.y, thisIndex.z,
480  i0, i1, j0, j1, k0, k1);
481  int size = (i1-i0+1)*(j1-j0+1)*(k1-k0+1);
482  blockSizes[x] = size;
483  }
484 }
485 
486 void PmePencilX::forwardDone() {
487  NAMD_bug("PmePencilX::forwardDone(), base class method called");
488 }
489 
490 void PmePencilX::backwardDone() {
491  NAMD_bug("PmePencilX::backwardDone(), base class method called");
492 }
493 
494 void PmePencilX::recvDataFromY(PmeBlockMsg *msg) {
495  NAMD_bug("PmePencilX::recvDataFromY(), base class method called");
496 }
497 
498 void PmePencilX::start(const CkCallback &) {
499  NAMD_bug("PmePencilX::start(), base class method called");
500 }
501 
502 //###########################################################################
503 //###########################################################################
504 //###########################################################################
505 
507  __sdag_init();
508  setMigratable(false);
509  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
510  pmeTransposes[iGrid] = NULL;
511  fftComputes[iGrid] = NULL;
512  }
513  numStrayAtoms = 0;
514 }
515 
516 PmePencilY::PmePencilY(CkMigrateMessage *m) {
517  NAMD_bug("PmePencilY cannot be migrated");
518  // __sdag_init();
519  // setMigratable(false);
520  // fftCompute = NULL;
521  // pmeTranspose = NULL;
522 }
523 
525  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
526  if (pmeTransposes[iGrid] != NULL) delete pmeTransposes[iGrid];
527  if (fftComputes[iGrid] != NULL) delete fftComputes[iGrid];
528  }
529 }
530 
531 void PmePencilY::initFFT(PmeStartMsg *msg) {
532  if (fftComputes[0] == NULL)
533  NAMD_bug("PmePencilY::initFFT, fftCompute not initialized");
534 // fftCompute->init(msg->dataGrid[0], msg->dataSizes[0], NULL, 0, Perm_Y_Z_cX, pmeGrid, 1, thisIndex.z, thisIndex.x, 0);
535 // fftCompute2->init(msg->dataGrid[1], msg->dataSizes[1], NULL, 0, Perm_Y_Z_cX, pmeGrid, 1, thisIndex.z, thisIndex.x, 0);
536  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
537  if (msg->enabledGrid[iGrid] == true) {
538  fftComputes[iGrid]->init(msg->dataGrid[iGrid], msg->dataSizes[iGrid], NULL, 0, Perm_Y_Z_cX, pmeGrid, 1, thisIndex.z, thisIndex.x, 0);
539  }
540  }
541 }
542 
543 void PmePencilY::forwardFFT() {
544  if (fftComputes[0] == NULL)
545  NAMD_bug("PmePencilY::forwardFFT, fftCompute not initialized");
546  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
547  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->forward();
548  }
549 }
550 
551 void PmePencilY::backwardFFT() {
552  if (fftComputes[0] == NULL)
553  NAMD_bug("PmePencilY::backwardFFT, fftCompute not initialized");
554  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
555  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->backward();
556  }
557 }
558 
560  blockSizes.resize(pmeGrid.yBlocks);
561  for (int y=0;y < pmeGrid.yBlocks;y++) {
562  int i0, i1, j0, j1, k0, k1;
563  getBlockDim(pmeGrid, Perm_Y_Z_cX, y, thisIndex.z, thisIndex.x,
564  i0, i1, j0, j1, k0, k1);
565  int size = (i1-i0+1)*(j1-j0+1)*(k1-k0+1);
566  blockSizes[y] = size;
567  }
568 }
569 
570 void PmePencilY::forwardDone() {
571  NAMD_bug("PmePencilY::forwardDone(), base class method called");
572 }
573 
574 void PmePencilY::backwardDone() {
575  NAMD_bug("PmePencilY::backwardDone(), base class method called");
576 }
577 
578 void PmePencilY::recvDataFromX(PmeBlockMsg *msg) {
579  NAMD_bug("PmePencilY::recvDataFromX(), base class method called");
580 }
581 
582 void PmePencilY::recvDataFromZ(PmeBlockMsg *msg) {
583  NAMD_bug("PmePencilY::recvDataFromZ(), base class method called");
584 }
585 
586 void PmePencilY::start(const CkCallback &) {
587  NAMD_bug("PmePencilY::start(), base class method called");
588 }
589 
590 //###########################################################################
591 //###########################################################################
592 //###########################################################################
593 
595  __sdag_init();
596  setMigratable(false);
597  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
598  pmeTransposes[iGrid] = NULL;
599  fftComputes[iGrid] = NULL;
600  pmeKSpaceComputes[iGrid] = NULL;
601  }
603 #ifdef NODEGROUP_FORCE_REGISTER
604 // #if false
605  CProxy_PatchData cpdata(CkpvAccess(BOCclass_group).patchData);
606  PatchData *patchData = cpdata.ckLocalBranch();
607  nodeReduction = patchData->reduction;
608 #endif
609  doEnergy = false;
610  doVirial = false;
611  numStrayAtoms = 0;
612 }
613 
614 PmePencilZ::PmePencilZ(CkMigrateMessage *m) {
615  NAMD_bug("PmePencilZ cannot be migrated");
616  //__sdag_init();
617  // setMigratable(false);
618  // fftCompute = NULL;
619  // pmeTranspose = NULL;
620 }
621 
623 // if (fftCompute != NULL) delete fftCompute;
624 // if (pmeTranspose != NULL) delete pmeTranspose;
625 // if (pmeKSpaceCompute != NULL) delete pmeKSpaceCompute;
626 // if (fftCompute2 != NULL) delete fftCompute2;
627 // if (pmeTranspose2 != NULL) delete pmeTranspose2;
628 // if (pmeKSpaceCompute2 != NULL) delete pmeKSpaceCompute2;
629  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
630  if (pmeTransposes[iGrid] != NULL) delete pmeTransposes[iGrid];
631  if (fftComputes[iGrid] != NULL) delete fftComputes[iGrid];
632  if (pmeKSpaceComputes[iGrid] != NULL) delete pmeKSpaceComputes[iGrid];
633  }
634  delete reduction;
635 }
636 
637 void PmePencilZ::initFFT(PmeStartMsg *msg) {
638  if (fftComputes[0] == NULL)
639  NAMD_bug("PmePencilZ::initFFT, fftCompute not initialized");
640 // fftCompute->init(msg->dataGrid[0], msg->dataSizes[0], NULL, 0, Perm_Z_cX_Y, pmeGrid, 1, thisIndex.x, thisIndex.y, 0);
641 // fftCompute2->init(msg->dataGrid[1], msg->dataSizes[1], NULL, 0, Perm_Z_cX_Y, pmeGrid, 1, thisIndex.x, thisIndex.y, 0);
642  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
643  if (msg->enabledGrid[iGrid] == true) {
644  fftComputes[iGrid]->init(msg->dataGrid[iGrid], msg->dataSizes[iGrid], NULL, 0, Perm_Z_cX_Y, pmeGrid, 1, thisIndex.x, thisIndex.y, 0);
645  }
646  }
647 }
648 
649 void PmePencilZ::forwardFFT() {
650  if (fftComputes[0] == NULL)
651  NAMD_bug("PmePencilZ::forwardFFT, fftCompute not initialized");
652  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
653  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->forward();
654  }
655 }
656 
657 void PmePencilZ::backwardFFT() {
658  if (fftComputes[0] == NULL)
659  NAMD_bug("PmePencilZ::backwardFFT, fftCompute not initialized");
660  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
661  if (fftComputes[iGrid] != NULL) fftComputes[iGrid]->backward();
662  }
663 }
664 
666  blockSizes.resize(pmeGrid.zBlocks);
667  for (int z=0;z < pmeGrid.zBlocks;z++) {
668  int i0, i1, j0, j1, k0, k1;
669  getBlockDim(pmeGrid, Perm_Z_cX_Y, z, thisIndex.x, thisIndex.y,
670  i0, i1, j0, j1, k0, k1);
671  int size = (i1-i0+1)*(j1-j0+1)*(k1-k0+1);
672  blockSizes[z] = size;
673  }
674 }
675 
676 void PmePencilZ::forwardDone() {
677  if (pmeKSpaceComputes[0] == NULL)
678  NAMD_bug("PmePencilZ::forwardDone, pmeKSpaceCompute not initialized");
679 // pmeKSpaceCompute->solve(lattice, doEnergy, doVirial, fftCompute->getDataDst());
680 // pmeKSpaceCompute2->solve(lattice, doEnergy, doVirial, fftCompute2->getDataDst());
681  for (unsigned int iGrid = 0; iGrid < NUM_GRID_MAX; ++iGrid) {
682  if (pmeKSpaceComputes[iGrid] != NULL) {
683  pmeKSpaceComputes[iGrid]->solve(lattice, doEnergy, doVirial, fftComputes[iGrid]->getDataDst());
684  }
685  }
686 }
687 
688 void PmePencilZ::submitReductions(unsigned int iGrid) {
689  if (pmeKSpaceComputes[iGrid] == NULL)
690  NAMD_bug("PmePencilZ::submitReductions, pmeKSpaceCompute not initialized");
691 // fprintf(stderr, "PmePencilZ::submitReductions\n");
693  double virial[9];
694  double energy = pmeKSpaceComputes[iGrid]->getEnergy();
695  pmeKSpaceComputes[iGrid]->getVirial(virial);
696  if (simParams->alchOn) {
697  double energy_F = energy;
698  double scale1 = 1.0;
699  double scale2 = 1.0;
700  if (simParams->alchFepOn) {
701  switch (iGrid) {
702  case 0: {
703  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
704  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
705  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
706  const BigReal elecLambda2Up = simParams->getElecLambda(alchLambda2);
707  scale1 = elecLambdaUp;
708  scale2 = elecLambda2Up;
709  break;
710  }
711  case 1: {
712  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
713  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
714  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
715  const BigReal elecLambda2Down = simParams->getElecLambda(1 - alchLambda2);
716  scale1 = elecLambdaDown;
717  scale2 = elecLambda2Down;
718  break;
719  }
720  case 2: {
721  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
722  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
723  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
724  const BigReal elecLambda2Up = simParams->getElecLambda(alchLambda2);
725  scale1 = 1.0 - elecLambdaUp;
726  scale2 = 1.0 - elecLambda2Up;
727  break;
728  }
729  case 3: {
730  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
731  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
732  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
733  const BigReal elecLambda2Down = simParams->getElecLambda(1 - alchLambda2);
734  scale1 = 1.0 - elecLambdaDown;
735  scale2 = 1.0 - elecLambda2Down;
736  break;
737  }
738  case 4: {
739  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
740  const BigReal alchLambda2 = simParams->getCurrentLambda2(simulationStep);
741  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
742  const BigReal elecLambda2Up = simParams->getElecLambda(alchLambda2);
743  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
744  const BigReal elecLambda2Down = simParams->getElecLambda(1 - alchLambda2);
745  scale1 = -1.0 * (elecLambdaUp + elecLambdaDown - 1.0);
746  scale2 = -1.0 * (elecLambda2Up + elecLambda2Down - 1.0);
747  break;
748  }
749  }
750  energy *= scale1;
751  energy_F *= scale2;
752  for (size_t i = 0; i < 9; ++i) {
753  virial[i] *= scale1;
754  }
755 #if NODEGROUP_FORCE_REGISTER
756  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW_F) += energy_F;
757 #endif
758  reduction->item(REDUCTION_ELECT_ENERGY_SLOW_F) += energy_F;
759  }
760  if (simParams->alchThermIntOn) {
761  double energy_TI_1 = 0.0;
762  double energy_TI_2 = 0.0;
763  double scale1 = 1.0;
764  switch (iGrid) {
765  case 0: {
766  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
767  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
768  scale1 = elecLambdaUp;
769  energy_TI_1 = energy;
770  break;
771  }
772  case 1: {
773  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
774  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
775  scale1 = elecLambdaDown;
776  energy_TI_2 = energy;
777  break;
778  }
779  case 2: {
780  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
781  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
782  scale1 = 1.0 - elecLambdaUp;
783  energy_TI_1 = -1.0 * energy;
784  break;
785  }
786  case 3: {
787  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
788  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
789  scale1 = 1.0 - elecLambdaDown;
790  energy_TI_2 = -1.0 * energy;
791  break;
792  }
793  case 4: {
794  const BigReal alchLambda = simParams->getCurrentLambda(simulationStep);
795  const BigReal elecLambdaUp = simParams->getElecLambda(alchLambda);
796  const BigReal elecLambdaDown = simParams->getElecLambda(1 - alchLambda);
797  scale1 = -1.0 * (elecLambdaUp + elecLambdaDown - 1.0);
798  energy_TI_1 = -1.0 * energy;
799  energy_TI_2 = -1.0 * energy;
800  break;
801  }
802  }
803 // fprintf(stdout, "Grid %u : energy_TI_1 = %lf ; energy_TI_2 = %lf\n", iGrid, energy_TI_1, energy_TI_2);
804 #if NODEGROUP_FORCE_REGISTER
805  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_1) += energy_TI_1;
806  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_2) += energy_TI_2;
807 #endif
808  reduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_1) += energy_TI_1;
809  reduction->item(REDUCTION_ELECT_ENERGY_SLOW_TI_2) += energy_TI_2;
810  }
811  }
812 #ifdef NODEGROUP_FORCE_REGISTER
813 // #if false
814  // XXX Expect a race condition, need atomic access to nodeReduction
815  nodeReduction->item(REDUCTION_ELECT_ENERGY_SLOW) += energy;
816  nodeReduction->item(REDUCTION_VIRIAL_SLOW_XX) += virial[0];
817  nodeReduction->item(REDUCTION_VIRIAL_SLOW_XY) += virial[1];
818  nodeReduction->item(REDUCTION_VIRIAL_SLOW_XZ) += virial[2];
819  nodeReduction->item(REDUCTION_VIRIAL_SLOW_YX) += virial[3];
820  nodeReduction->item(REDUCTION_VIRIAL_SLOW_YY) += virial[4];
821  nodeReduction->item(REDUCTION_VIRIAL_SLOW_YZ) += virial[5];
822  nodeReduction->item(REDUCTION_VIRIAL_SLOW_ZX) += virial[6];
823  nodeReduction->item(REDUCTION_VIRIAL_SLOW_ZY) += virial[7];
824  nodeReduction->item(REDUCTION_VIRIAL_SLOW_ZZ) += virial[8];
825 #endif
826  reduction->item(REDUCTION_ELECT_ENERGY_SLOW) += energy;
827  reduction->item(REDUCTION_VIRIAL_SLOW_XX) += virial[0];
828  reduction->item(REDUCTION_VIRIAL_SLOW_XY) += virial[1];
829  reduction->item(REDUCTION_VIRIAL_SLOW_XZ) += virial[2];
830  reduction->item(REDUCTION_VIRIAL_SLOW_YX) += virial[3];
831  reduction->item(REDUCTION_VIRIAL_SLOW_YY) += virial[4];
832  reduction->item(REDUCTION_VIRIAL_SLOW_YZ) += virial[5];
833  reduction->item(REDUCTION_VIRIAL_SLOW_ZX) += virial[6];
834  reduction->item(REDUCTION_VIRIAL_SLOW_ZY) += virial[7];
835  reduction->item(REDUCTION_VIRIAL_SLOW_ZZ) += virial[8];
837  numStrayAtoms = 0;
838  energyReady[iGrid] = 1;
839  bool ready_to_submit = true;
840  for (size_t i = 0; i < NUM_GRID_MAX; ++i) {
841  if (energyReady[i] == -1) continue;
842  if (energyReady[i] == 0) ready_to_submit = false;
843  }
844  if (ready_to_submit) {
845  reduction->submit();
846  for (size_t i = 0; i < NUM_GRID_MAX; ++i) {
847  if (energyReady[i] == -1) continue;
848  if (energyReady[i] == 1) energyReady[i] = 0;
849  }
850  }
851 }
852 
853 void PmePencilZ::backwardDone() {
854  NAMD_bug("PmePencilZ::backwardDone(), base class method called");
855 }
856 
857 void PmePencilZ::recvDataFromY(PmeBlockMsg *msg) {
858  NAMD_bug("PmePencilY::recvDataFromY(), base class method called");
859 }
860 
861 void PmePencilZ::start(const CkCallback &) {
862  NAMD_bug("PmePencilZ::start(), base class method called");
863 }
864 
866  reduction->submit();
867 }
868 
869 #include "PmeSolver.def.h"
static Node * Object()
Definition: Node.h:86
virtual ~PmePencilXYZ()
Definition: PmeSolver.C:51
int zBlocks
Definition: PmeBase.h:25
std::array< PmeKSpaceCompute *, NUM_GRID_MAX > pmeKSpaceComputes
Definition: PmeSolver.h:270
Lattice lattice
Definition: PmeSolver.h:273
bool doEnergy
Definition: PmeSolver.h:266
std::vector< int > blockSizes
Definition: PmeSolver.h:272
virtual ~PmePencilXY()
Definition: PmeSolver.C:320
virtual void backwardDone()
Definition: PmeSolver.C:97
SimParameters * simParameters
Definition: Node.h:181
std::array< FFTCompute *, NUM_GRID_MAX > fftComputes
Definition: PmeSolver.h:182
void submitReductions(unsigned int iGrid)
Definition: PmeSolver.C:101
BigReal & item(int i)
Definition: ReductionMgr.h:313
int simulationStep
Definition: PmeSolver.h:267
std::array< PmeTranspose *, NUM_GRID_MAX > pmeTransposes
Definition: PmeSolver.h:183
SubmitReduction * willSubmit(int setID, int size=-1)
Definition: ReductionMgr.C:366
Lattice lattice
Definition: PmeSolver.h:155
PmePencilX_SDAG_CODE PmePencilX()
Definition: PmeSolver.C:422
static ReductionMgr * Object(void)
Definition: ReductionMgr.h:279
NodeReduction * reduction
Definition: PatchData.h:133
int numStrayAtoms
Definition: PmeSolver.h:156
const unsigned int NUM_GRID_MAX
Definition: PmeSolverUtil.h:9
std::vector< int > blockSizes
Definition: PmeSolver.h:240
PmeGrid pmeGrid
Definition: PmeSolver.h:179
bool doVirial
Definition: PmeSolver.h:266
std::vector< int > blockSizes
Definition: PmeSolver.h:212
std::array< PmeTranspose *, NUM_GRID_MAX > pmeTransposes
Definition: PmeSolver.h:211
virtual ~PmePencilY()
Definition: PmeSolver.C:524
std::array< FFTCompute *, NUM_GRID_MAX > fftComputes
Definition: PmeSolver.h:210
std::array< int, NUM_GRID_MAX > dataSizes
Definition: PmeSolver.h:106
std::array< float *, NUM_GRID_MAX > dataGrid
Definition: PmeSolver.h:105
int yBlocks
Definition: PmeBase.h:25
bool doVirial
Definition: PmeSolver.h:146
void initBlockSizes()
Definition: PmeSolver.C:475
std::array< int, NUM_GRID_MAX > energyReady
Definition: PmeSolver.h:153
int numStrayAtoms
Definition: PmeSolver.h:242
PmeGrid pmeGrid
Definition: PmeSolver.h:265
PmeGrid pmeGrid
Definition: PmeSolver.h:145
std::array< PmeKSpaceCompute *, NUM_GRID_MAX > pmeKSpaceComputes
Definition: PmeSolver.h:150
void NAMD_bug(const char *err_msg)
Definition: common.C:195
void initBlockSizes()
Definition: PmeSolver.C:559
PmePencilXY_SDAG_CODE PmePencilXY()
Definition: PmeSolver.C:303
std::vector< int > blockSizes
Definition: PmeSolver.h:184
PmePencilXYZ_SDAG_CODE PmePencilXYZ()
Definition: PmeSolver.C:22
bool doEnergy
Definition: PmeSolver.h:146
std::array< PmeTranspose *, NUM_GRID_MAX > pmeTransposes
Definition: PmeSolver.h:239
std::array< int, NUM_GRID_MAX > energyReady
Definition: PmeSolver.h:271
PmePencilZ_SDAG_CODE PmePencilZ()
Definition: PmeSolver.C:594
int simulationStep
Definition: PmeSolver.h:148
std::array< FFTCompute *, NUM_GRID_MAX > fftComputes
Definition: PmeSolver.h:149
int numStrayAtoms
Definition: PmeSolver.h:274
void skip()
Definition: PmeSolver.C:273
std::array< FFTCompute *, NUM_GRID_MAX > fftComputes
Definition: PmeSolver.h:238
virtual ~PmePencilX()
Definition: PmeSolver.C:440
#define simParams
Definition: Output.C:129
std::array< PmeTranspose *, NUM_GRID_MAX > pmeTransposes
Definition: PmeSolver.h:269
static void getBlockDim(const PmeGrid &pmeGrid, const int permutation, const int iblock, const int jblock, const int kblock, int &i0, int &i1, int &j0, int &j1, int &k0, int &k1)
Definition: PmeSolverUtil.h:89
void submitReductions(unsigned int iGrid)
Definition: PmeSolver.C:688
std::array< FFTCompute *, NUM_GRID_MAX > fftComputes
Definition: PmeSolver.h:268
void initBlockSizes()
Definition: PmeSolver.C:665
std::array< bool, NUM_GRID_MAX > enabledGrid
Definition: PmeSolver.h:107
void submit(void)
Definition: ReductionMgr.h:324
int xBlocks
Definition: PmeBase.h:25
void initBlockSizes()
Definition: PmeSolver.C:359
PmePencilY_SDAG_CODE PmePencilY()
Definition: PmeSolver.C:506
virtual ~PmePencilZ()
Definition: PmeSolver.C:622
PmeGrid pmeGrid
Definition: PmeSolver.h:235
PmeGrid pmeGrid
Definition: PmeSolver.h:207
int numStrayAtoms
Definition: PmeSolver.h:214
void skip()
Definition: PmeSolver.C:865
double BigReal
Definition: common.h:123