2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 /* IMPORTANT FOR DEVELOPERS:
39 * Triclinic pme stuff isn't entirely trivial, and we've experienced
40 * some bugs during development (many of them due to me). To avoid
41 * this in the future, please check the following things if you make
42 * changes in this file:
44 * 1. You should obtain identical (at least to the PME precision)
45 * energies, forces, and virial for
46 * a rectangular box and a triclinic one where the z (or y) axis is
47 * tilted a whole box side. For instance you could use these boxes:
49 * rectangular triclinic
54 * 2. You should check the energy conservation in a triclinic box.
56 * It might seem an overkill, but better safe than sorry.
71 #include "gromacs/utility/smalloc.h"
73 #include "gromacs/utility/fatalerror.h"
80 #include "gromacs/fft/parallel_3dfft.h"
81 #include "gromacs/utility/futil.h"
82 #include "gromacs/fileio/pdbio.h"
83 #include "gromacs/math/gmxcomplex.h"
84 #include "gromacs/timing/cyclecounter.h"
85 #include "gromacs/timing/wallcycle.h"
86 #include "gromacs/utility/gmxmpi.h"
87 #include "gromacs/utility/gmxomp.h"
89 /* Include the SIMD macro file and then check for support */
90 #include "gromacs/simd/simd.h"
91 #include "gromacs/simd/simd_math.h"
92 #ifdef GMX_SIMD_HAVE_REAL
93 /* Turn on arbitrary width SIMD intrinsics for PME solve */
94 # define PME_SIMD_SOLVE
97 #define PME_GRID_QA 0 /* Gridindex for A-state for Q */
98 #define PME_GRID_C6A 2 /* Gridindex for A-state for LJ */
99 #define DO_Q 2 /* Electrostatic grids have index q<2 */
100 #define DO_Q_AND_LJ 4 /* non-LB LJ grids have index 2 <= q < 4 */
101 #define DO_Q_AND_LJ_LB 9 /* With LB rules we need a total of 2+7 grids */
103 /* Pascal triangle coefficients scaled with (1/2)^6 for LJ-PME with LB-rules */
104 const real lb_scale_factor[] = {
105 1.0/64, 6.0/64, 15.0/64, 20.0/64,
106 15.0/64, 6.0/64, 1.0/64
109 /* Pascal triangle coefficients used in solve_pme_lj_yzx, only need to do 4 calculations due to symmetry */
110 const real lb_scale_factor_symm[] = { 2.0/64, 12.0/64, 30.0/64, 20.0/64 };
112 /* Check if we have 4-wide SIMD macro support */
113 #if (defined GMX_SIMD4_HAVE_REAL)
114 /* Do PME spread and gather with 4-wide SIMD.
115 * NOTE: SIMD is only used with PME order 4 and 5 (which are the most common).
117 # define PME_SIMD4_SPREAD_GATHER
119 # if (defined GMX_SIMD_HAVE_LOADU) && (defined GMX_SIMD_HAVE_STOREU)
120 /* With PME-order=4 on x86, unaligned load+store is slightly faster
121 * than doubling all SIMD operations when using aligned load+store.
123 # define PME_SIMD4_UNALIGNED
128 /* #define PRT_FORCE */
129 /* conditions for on the fly time-measurement */
130 /* #define TAKETIME (step > 1 && timesteps < 10) */
131 #define TAKETIME FALSE
133 /* #define PME_TIME_THREADS */
136 #define mpi_type MPI_DOUBLE
138 #define mpi_type MPI_FLOAT
141 #ifdef PME_SIMD4_SPREAD_GATHER
142 # define SIMD4_ALIGNMENT (GMX_SIMD4_WIDTH*sizeof(real))
144 /* We can use any alignment, apart from 0, so we use 4 reals */
145 # define SIMD4_ALIGNMENT (4*sizeof(real))
148 /* GMX_CACHE_SEP should be a multiple of the SIMD and SIMD4 register size
149 * to preserve alignment.
151 #define GMX_CACHE_SEP 64
153 /* We only define a maximum to be able to use local arrays without allocation.
154 * An order larger than 12 should never be needed, even for test cases.
155 * If needed it can be changed here.
157 #define PME_ORDER_MAX 12
159 /* Internal datastructures */
165 int recv_size; /* Receive buffer width, used with OpenMP */
176 int *send_id, *recv_id;
177 int send_size; /* Send buffer width, used with OpenMP */
178 pme_grid_comm_t *comm_data;
184 int *n; /* Cumulative counts of the number of particles per thread */
185 int nalloc; /* Allocation size of i */
186 int *i; /* Particle indices ordered on thread index (n) */
200 int dimind; /* The index of the dimension, 0=x, 1=y */
207 int *node_dest; /* The nodes to send x and q to with DD */
208 int *node_src; /* The nodes to receive x and q from with DD */
209 int *buf_index; /* Index for commnode into the buffers */
216 int *count; /* The number of atoms to send to each node */
218 int *rcount; /* The number of atoms to receive */
225 gmx_bool bSpread; /* These coordinates are used for spreading */
228 rvec *fractx; /* Fractional coordinate relative to
229 * the lower cell boundary
232 int *thread_idx; /* Which thread should spread which coefficient */
233 thread_plist_t *thread_plist;
234 splinedata_t *spline;
241 ivec ci; /* The spatial location of this grid */
242 ivec n; /* The used size of *grid, including order-1 */
243 ivec offset; /* The grid offset from the full node grid */
244 int order; /* PME spreading order */
245 ivec s; /* The allocated size of *grid, s >= n */
246 real *grid; /* The grid local thread, size n */
250 pmegrid_t grid; /* The full node grid (non thread-local) */
251 int nthread; /* The number of threads operating on this grid */
252 ivec nc; /* The local spatial decomposition over the threads */
253 pmegrid_t *grid_th; /* Array of grids for each thread */
254 real *grid_all; /* Allocated array for the grids in *grid_th */
255 int **g2t; /* The grid to thread index */
256 ivec nthread_comm; /* The number of threads to communicate with */
260 #ifdef PME_SIMD4_SPREAD_GATHER
261 /* Masks for 4-wide SIMD aligned spreading and gathering */
262 gmx_simd4_bool_t mask_S0[6], mask_S1[6];
264 int dummy; /* C89 requires that struct has at least one member */
269 /* work data for solve_pme */
288 typedef struct gmx_pme {
289 int ndecompdim; /* The number of decomposition dimensions */
290 int nodeid; /* Our nodeid in mpi->mpi_comm */
293 int nnodes; /* The number of nodes doing PME */
298 MPI_Comm mpi_comm_d[2]; /* Indexed on dimension, 0=x, 1=y */
300 MPI_Datatype rvec_mpi; /* the pme vector's MPI type */
303 gmx_bool bUseThreads; /* Does any of the PME ranks have nthread>1 ? */
304 int nthread; /* The number of threads doing PME on our rank */
306 gmx_bool bPPnode; /* Node also does particle-particle forces */
307 gmx_bool bFEP; /* Compute Free energy contribution */
310 int nkx, nky, nkz; /* Grid dimensions */
311 gmx_bool bP3M; /* Do P3M: optimize the influence function */
315 int ljpme_combination_rule; /* Type of combination rule in LJ-PME */
317 int ngrids; /* number of grids we maintain for pmegrid, (c)fftgrid and pfft_setups*/
319 pmegrids_t pmegrid[DO_Q_AND_LJ_LB]; /* Grids on which we do spreading/interpolation,
320 * includes overlap Grid indices are ordered as
322 * 0: Coloumb PME, state A
323 * 1: Coloumb PME, state B
325 * This can probably be done in a better way
326 * but this simple hack works for now
328 /* The PME coefficient spreading grid sizes/strides, includes pme_order-1 */
329 int pmegrid_nx, pmegrid_ny, pmegrid_nz;
330 /* pmegrid_nz might be larger than strictly necessary to ensure
331 * memory alignment, pmegrid_nz_base gives the real base size.
334 /* The local PME grid starting indices */
335 int pmegrid_start_ix, pmegrid_start_iy, pmegrid_start_iz;
337 /* Work data for spreading and gathering */
338 pme_spline_work_t *spline_work;
340 real **fftgrid; /* Grids for FFT. With 1D FFT decomposition this can be a pointer */
341 /* inside the interpolation grid, but separate for 2D PME decomp. */
342 int fftgrid_nx, fftgrid_ny, fftgrid_nz;
344 t_complex **cfftgrid; /* Grids for complex FFT data */
346 int cfftgrid_nx, cfftgrid_ny, cfftgrid_nz;
348 gmx_parallel_3dfft_t *pfft_setup;
350 int *nnx, *nny, *nnz;
351 real *fshx, *fshy, *fshz;
353 pme_atomcomm_t atc[2]; /* Indexed on decomposition index */
356 /* Buffers to store data for local atoms for L-B combination rule
357 * calculations in LJ-PME. lb_buf1 stores either the coefficients
358 * for spreading/gathering (in serial), or the C6 coefficient for
359 * local atoms (in parallel). lb_buf2 is only used in parallel,
360 * and stores the sigma values for local atoms. */
361 real *lb_buf1, *lb_buf2;
362 int lb_buf_nalloc; /* Allocation size for the above buffers. */
364 pme_overlap_t overlap[2]; /* Indexed on dimension, 0=x, 1=y */
366 pme_atomcomm_t atc_energy; /* Only for gmx_pme_calc_energy */
368 rvec *bufv; /* Communication buffer */
369 real *bufr; /* Communication buffer */
370 int buf_nalloc; /* The communication buffer size */
372 /* thread local work data for solve_pme */
375 /* Work data for sum_qgrid */
376 real * sum_qgrid_tmp;
377 real * sum_qgrid_dd_tmp;
380 static void calc_interpolation_idx(gmx_pme_t pme, pme_atomcomm_t *atc,
381 int start, int grid_index, int end, int thread)
384 int *idxptr, tix, tiy, tiz;
385 real *xptr, *fptr, tx, ty, tz;
386 real rxx, ryx, ryy, rzx, rzy, rzz;
388 int start_ix, start_iy, start_iz;
389 int *g2tx, *g2ty, *g2tz;
391 int *thread_idx = NULL;
392 thread_plist_t *tpl = NULL;
400 start_ix = pme->pmegrid_start_ix;
401 start_iy = pme->pmegrid_start_iy;
402 start_iz = pme->pmegrid_start_iz;
404 rxx = pme->recipbox[XX][XX];
405 ryx = pme->recipbox[YY][XX];
406 ryy = pme->recipbox[YY][YY];
407 rzx = pme->recipbox[ZZ][XX];
408 rzy = pme->recipbox[ZZ][YY];
409 rzz = pme->recipbox[ZZ][ZZ];
411 g2tx = pme->pmegrid[grid_index].g2t[XX];
412 g2ty = pme->pmegrid[grid_index].g2t[YY];
413 g2tz = pme->pmegrid[grid_index].g2t[ZZ];
415 bThreads = (atc->nthread > 1);
418 thread_idx = atc->thread_idx;
420 tpl = &atc->thread_plist[thread];
422 for (i = 0; i < atc->nthread; i++)
428 for (i = start; i < end; i++)
431 idxptr = atc->idx[i];
432 fptr = atc->fractx[i];
434 /* Fractional coordinates along box vectors, add 2.0 to make 100% sure we are positive for triclinic boxes */
435 tx = nx * ( xptr[XX] * rxx + xptr[YY] * ryx + xptr[ZZ] * rzx + 2.0 );
436 ty = ny * ( xptr[YY] * ryy + xptr[ZZ] * rzy + 2.0 );
437 tz = nz * ( xptr[ZZ] * rzz + 2.0 );
443 /* Because decomposition only occurs in x and y,
444 * we never have a fraction correction in z.
446 fptr[XX] = tx - tix + pme->fshx[tix];
447 fptr[YY] = ty - tiy + pme->fshy[tiy];
450 idxptr[XX] = pme->nnx[tix];
451 idxptr[YY] = pme->nny[tiy];
452 idxptr[ZZ] = pme->nnz[tiz];
455 range_check(idxptr[XX], 0, pme->pmegrid_nx);
456 range_check(idxptr[YY], 0, pme->pmegrid_ny);
457 range_check(idxptr[ZZ], 0, pme->pmegrid_nz);
462 thread_i = g2tx[idxptr[XX]] + g2ty[idxptr[YY]] + g2tz[idxptr[ZZ]];
463 thread_idx[i] = thread_i;
470 /* Make a list of particle indices sorted on thread */
472 /* Get the cumulative count */
473 for (i = 1; i < atc->nthread; i++)
475 tpl_n[i] += tpl_n[i-1];
477 /* The current implementation distributes particles equally
478 * over the threads, so we could actually allocate for that
479 * in pme_realloc_atomcomm_things.
481 if (tpl_n[atc->nthread-1] > tpl->nalloc)
483 tpl->nalloc = over_alloc_large(tpl_n[atc->nthread-1]);
484 srenew(tpl->i, tpl->nalloc);
486 /* Set tpl_n to the cumulative start */
487 for (i = atc->nthread-1; i >= 1; i--)
489 tpl_n[i] = tpl_n[i-1];
493 /* Fill our thread local array with indices sorted on thread */
494 for (i = start; i < end; i++)
496 tpl->i[tpl_n[atc->thread_idx[i]]++] = i;
498 /* Now tpl_n contains the cummulative count again */
502 static void make_thread_local_ind(pme_atomcomm_t *atc,
503 int thread, splinedata_t *spline)
505 int n, t, i, start, end;
508 /* Combine the indices made by each thread into one index */
512 for (t = 0; t < atc->nthread; t++)
514 tpl = &atc->thread_plist[t];
515 /* Copy our part (start - end) from the list of thread t */
518 start = tpl->n[thread-1];
520 end = tpl->n[thread];
521 for (i = start; i < end; i++)
523 spline->ind[n++] = tpl->i[i];
531 static void pme_calc_pidx(int start, int end,
532 matrix recipbox, rvec x[],
533 pme_atomcomm_t *atc, int *count)
538 real rxx, ryx, rzx, ryy, rzy;
541 /* Calculate PME task index (pidx) for each grid index.
542 * Here we always assign equally sized slabs to each node
543 * for load balancing reasons (the PME grid spacing is not used).
549 /* Reset the count */
550 for (i = 0; i < nslab; i++)
555 if (atc->dimind == 0)
557 rxx = recipbox[XX][XX];
558 ryx = recipbox[YY][XX];
559 rzx = recipbox[ZZ][XX];
560 /* Calculate the node index in x-dimension */
561 for (i = start; i < end; i++)
564 /* Fractional coordinates along box vectors */
565 s = nslab*(xptr[XX]*rxx + xptr[YY]*ryx + xptr[ZZ]*rzx);
566 si = (int)(s + 2*nslab) % nslab;
573 ryy = recipbox[YY][YY];
574 rzy = recipbox[ZZ][YY];
575 /* Calculate the node index in y-dimension */
576 for (i = start; i < end; i++)
579 /* Fractional coordinates along box vectors */
580 s = nslab*(xptr[YY]*ryy + xptr[ZZ]*rzy);
581 si = (int)(s + 2*nslab) % nslab;
588 static void pme_calc_pidx_wrapper(int natoms, matrix recipbox, rvec x[],
591 int nthread, thread, slab;
593 nthread = atc->nthread;
595 #pragma omp parallel for num_threads(nthread) schedule(static)
596 for (thread = 0; thread < nthread; thread++)
598 pme_calc_pidx(natoms* thread /nthread,
599 natoms*(thread+1)/nthread,
600 recipbox, x, atc, atc->count_thread[thread]);
602 /* Non-parallel reduction, since nslab is small */
604 for (thread = 1; thread < nthread; thread++)
606 for (slab = 0; slab < atc->nslab; slab++)
608 atc->count_thread[0][slab] += atc->count_thread[thread][slab];
613 static void realloc_splinevec(splinevec th, real **ptr_z, int nalloc)
615 const int padding = 4;
618 srenew(th[XX], nalloc);
619 srenew(th[YY], nalloc);
620 /* In z we add padding, this is only required for the aligned SIMD code */
621 sfree_aligned(*ptr_z);
622 snew_aligned(*ptr_z, nalloc+2*padding, SIMD4_ALIGNMENT);
623 th[ZZ] = *ptr_z + padding;
625 for (i = 0; i < padding; i++)
628 (*ptr_z)[padding+nalloc+i] = 0;
632 static void pme_realloc_splinedata(splinedata_t *spline, pme_atomcomm_t *atc)
636 srenew(spline->ind, atc->nalloc);
637 /* Initialize the index to identity so it works without threads */
638 for (i = 0; i < atc->nalloc; i++)
643 realloc_splinevec(spline->theta, &spline->ptr_theta_z,
644 atc->pme_order*atc->nalloc);
645 realloc_splinevec(spline->dtheta, &spline->ptr_dtheta_z,
646 atc->pme_order*atc->nalloc);
649 static void pme_realloc_atomcomm_things(pme_atomcomm_t *atc)
651 int nalloc_old, i, j, nalloc_tpl;
653 /* We have to avoid a NULL pointer for atc->x to avoid
654 * possible fatal errors in MPI routines.
656 if (atc->n > atc->nalloc || atc->nalloc == 0)
658 nalloc_old = atc->nalloc;
659 atc->nalloc = over_alloc_dd(max(atc->n, 1));
663 srenew(atc->x, atc->nalloc);
664 srenew(atc->coefficient, atc->nalloc);
665 srenew(atc->f, atc->nalloc);
666 for (i = nalloc_old; i < atc->nalloc; i++)
668 clear_rvec(atc->f[i]);
673 srenew(atc->fractx, atc->nalloc);
674 srenew(atc->idx, atc->nalloc);
676 if (atc->nthread > 1)
678 srenew(atc->thread_idx, atc->nalloc);
681 for (i = 0; i < atc->nthread; i++)
683 pme_realloc_splinedata(&atc->spline[i], atc);
689 static void pme_dd_sendrecv(pme_atomcomm_t gmx_unused *atc,
690 gmx_bool gmx_unused bBackward, int gmx_unused shift,
691 void gmx_unused *buf_s, int gmx_unused nbyte_s,
692 void gmx_unused *buf_r, int gmx_unused nbyte_r)
698 if (bBackward == FALSE)
700 dest = atc->node_dest[shift];
701 src = atc->node_src[shift];
705 dest = atc->node_src[shift];
706 src = atc->node_dest[shift];
709 if (nbyte_s > 0 && nbyte_r > 0)
711 MPI_Sendrecv(buf_s, nbyte_s, MPI_BYTE,
713 buf_r, nbyte_r, MPI_BYTE,
715 atc->mpi_comm, &stat);
717 else if (nbyte_s > 0)
719 MPI_Send(buf_s, nbyte_s, MPI_BYTE,
723 else if (nbyte_r > 0)
725 MPI_Recv(buf_r, nbyte_r, MPI_BYTE,
727 atc->mpi_comm, &stat);
732 static void dd_pmeredist_pos_coeffs(gmx_pme_t pme,
733 int n, gmx_bool bX, rvec *x, real *data,
736 int *commnode, *buf_index;
737 int nnodes_comm, i, nsend, local_pos, buf_pos, node, scount, rcount;
739 commnode = atc->node_dest;
740 buf_index = atc->buf_index;
742 nnodes_comm = min(2*atc->maxshift, atc->nslab-1);
745 for (i = 0; i < nnodes_comm; i++)
747 buf_index[commnode[i]] = nsend;
748 nsend += atc->count[commnode[i]];
752 if (atc->count[atc->nodeid] + nsend != n)
754 gmx_fatal(FARGS, "%d particles communicated to PME node %d are more than 2/3 times the cut-off out of the domain decomposition cell of their charge group in dimension %c.\n"
755 "This usually means that your system is not well equilibrated.",
756 n - (atc->count[atc->nodeid] + nsend),
757 pme->nodeid, 'x'+atc->dimind);
760 if (nsend > pme->buf_nalloc)
762 pme->buf_nalloc = over_alloc_dd(nsend);
763 srenew(pme->bufv, pme->buf_nalloc);
764 srenew(pme->bufr, pme->buf_nalloc);
767 atc->n = atc->count[atc->nodeid];
768 for (i = 0; i < nnodes_comm; i++)
770 scount = atc->count[commnode[i]];
771 /* Communicate the count */
774 fprintf(debug, "dimind %d PME node %d send to node %d: %d\n",
775 atc->dimind, atc->nodeid, commnode[i], scount);
777 pme_dd_sendrecv(atc, FALSE, i,
778 &scount, sizeof(int),
779 &atc->rcount[i], sizeof(int));
780 atc->n += atc->rcount[i];
783 pme_realloc_atomcomm_things(atc);
787 for (i = 0; i < n; i++)
790 if (node == atc->nodeid)
792 /* Copy direct to the receive buffer */
795 copy_rvec(x[i], atc->x[local_pos]);
797 atc->coefficient[local_pos] = data[i];
802 /* Copy to the send buffer */
805 copy_rvec(x[i], pme->bufv[buf_index[node]]);
807 pme->bufr[buf_index[node]] = data[i];
813 for (i = 0; i < nnodes_comm; i++)
815 scount = atc->count[commnode[i]];
816 rcount = atc->rcount[i];
817 if (scount > 0 || rcount > 0)
821 /* Communicate the coordinates */
822 pme_dd_sendrecv(atc, FALSE, i,
823 pme->bufv[buf_pos], scount*sizeof(rvec),
824 atc->x[local_pos], rcount*sizeof(rvec));
826 /* Communicate the coefficients */
827 pme_dd_sendrecv(atc, FALSE, i,
828 pme->bufr+buf_pos, scount*sizeof(real),
829 atc->coefficient+local_pos, rcount*sizeof(real));
831 local_pos += atc->rcount[i];
836 static void dd_pmeredist_f(gmx_pme_t pme, pme_atomcomm_t *atc,
840 int *commnode, *buf_index;
841 int nnodes_comm, local_pos, buf_pos, i, scount, rcount, node;
843 commnode = atc->node_dest;
844 buf_index = atc->buf_index;
846 nnodes_comm = min(2*atc->maxshift, atc->nslab-1);
848 local_pos = atc->count[atc->nodeid];
850 for (i = 0; i < nnodes_comm; i++)
852 scount = atc->rcount[i];
853 rcount = atc->count[commnode[i]];
854 if (scount > 0 || rcount > 0)
856 /* Communicate the forces */
857 pme_dd_sendrecv(atc, TRUE, i,
858 atc->f[local_pos], scount*sizeof(rvec),
859 pme->bufv[buf_pos], rcount*sizeof(rvec));
862 buf_index[commnode[i]] = buf_pos;
869 for (i = 0; i < n; i++)
872 if (node == atc->nodeid)
874 /* Add from the local force array */
875 rvec_inc(f[i], atc->f[local_pos]);
880 /* Add from the receive buffer */
881 rvec_inc(f[i], pme->bufv[buf_index[node]]);
888 for (i = 0; i < n; i++)
891 if (node == atc->nodeid)
893 /* Copy from the local force array */
894 copy_rvec(atc->f[local_pos], f[i]);
899 /* Copy from the receive buffer */
900 copy_rvec(pme->bufv[buf_index[node]], f[i]);
908 static void gmx_sum_qgrid_dd(gmx_pme_t pme, real *grid, int direction)
910 pme_overlap_t *overlap;
911 int send_index0, send_nindex;
912 int recv_index0, recv_nindex;
914 int i, j, k, ix, iy, iz, icnt;
915 int ipulse, send_id, recv_id, datasize;
917 real *sendptr, *recvptr;
919 /* Start with minor-rank communication. This is a bit of a pain since it is not contiguous */
920 overlap = &pme->overlap[1];
922 for (ipulse = 0; ipulse < overlap->noverlap_nodes; ipulse++)
924 /* Since we have already (un)wrapped the overlap in the z-dimension,
925 * we only have to communicate 0 to nkz (not pmegrid_nz).
927 if (direction == GMX_SUM_GRID_FORWARD)
929 send_id = overlap->send_id[ipulse];
930 recv_id = overlap->recv_id[ipulse];
931 send_index0 = overlap->comm_data[ipulse].send_index0;
932 send_nindex = overlap->comm_data[ipulse].send_nindex;
933 recv_index0 = overlap->comm_data[ipulse].recv_index0;
934 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
938 send_id = overlap->recv_id[ipulse];
939 recv_id = overlap->send_id[ipulse];
940 send_index0 = overlap->comm_data[ipulse].recv_index0;
941 send_nindex = overlap->comm_data[ipulse].recv_nindex;
942 recv_index0 = overlap->comm_data[ipulse].send_index0;
943 recv_nindex = overlap->comm_data[ipulse].send_nindex;
946 /* Copy data to contiguous send buffer */
949 fprintf(debug, "PME send node %d %d -> %d grid start %d Communicating %d to %d\n",
950 pme->nodeid, overlap->nodeid, send_id,
951 pme->pmegrid_start_iy,
952 send_index0-pme->pmegrid_start_iy,
953 send_index0-pme->pmegrid_start_iy+send_nindex);
956 for (i = 0; i < pme->pmegrid_nx; i++)
959 for (j = 0; j < send_nindex; j++)
961 iy = j + send_index0 - pme->pmegrid_start_iy;
962 for (k = 0; k < pme->nkz; k++)
965 overlap->sendbuf[icnt++] = grid[ix*(pme->pmegrid_ny*pme->pmegrid_nz)+iy*(pme->pmegrid_nz)+iz];
970 datasize = pme->pmegrid_nx * pme->nkz;
972 MPI_Sendrecv(overlap->sendbuf, send_nindex*datasize, GMX_MPI_REAL,
974 overlap->recvbuf, recv_nindex*datasize, GMX_MPI_REAL,
976 overlap->mpi_comm, &stat);
978 /* Get data from contiguous recv buffer */
981 fprintf(debug, "PME recv node %d %d <- %d grid start %d Communicating %d to %d\n",
982 pme->nodeid, overlap->nodeid, recv_id,
983 pme->pmegrid_start_iy,
984 recv_index0-pme->pmegrid_start_iy,
985 recv_index0-pme->pmegrid_start_iy+recv_nindex);
988 for (i = 0; i < pme->pmegrid_nx; i++)
991 for (j = 0; j < recv_nindex; j++)
993 iy = j + recv_index0 - pme->pmegrid_start_iy;
994 for (k = 0; k < pme->nkz; k++)
997 if (direction == GMX_SUM_GRID_FORWARD)
999 grid[ix*(pme->pmegrid_ny*pme->pmegrid_nz)+iy*(pme->pmegrid_nz)+iz] += overlap->recvbuf[icnt++];
1003 grid[ix*(pme->pmegrid_ny*pme->pmegrid_nz)+iy*(pme->pmegrid_nz)+iz] = overlap->recvbuf[icnt++];
1010 /* Major dimension is easier, no copying required,
1011 * but we might have to sum to separate array.
1012 * Since we don't copy, we have to communicate up to pmegrid_nz,
1013 * not nkz as for the minor direction.
1015 overlap = &pme->overlap[0];
1017 for (ipulse = 0; ipulse < overlap->noverlap_nodes; ipulse++)
1019 if (direction == GMX_SUM_GRID_FORWARD)
1021 send_id = overlap->send_id[ipulse];
1022 recv_id = overlap->recv_id[ipulse];
1023 send_index0 = overlap->comm_data[ipulse].send_index0;
1024 send_nindex = overlap->comm_data[ipulse].send_nindex;
1025 recv_index0 = overlap->comm_data[ipulse].recv_index0;
1026 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
1027 recvptr = overlap->recvbuf;
1031 send_id = overlap->recv_id[ipulse];
1032 recv_id = overlap->send_id[ipulse];
1033 send_index0 = overlap->comm_data[ipulse].recv_index0;
1034 send_nindex = overlap->comm_data[ipulse].recv_nindex;
1035 recv_index0 = overlap->comm_data[ipulse].send_index0;
1036 recv_nindex = overlap->comm_data[ipulse].send_nindex;
1037 recvptr = grid + (recv_index0-pme->pmegrid_start_ix)*(pme->pmegrid_ny*pme->pmegrid_nz);
1040 sendptr = grid + (send_index0-pme->pmegrid_start_ix)*(pme->pmegrid_ny*pme->pmegrid_nz);
1041 datasize = pme->pmegrid_ny * pme->pmegrid_nz;
1045 fprintf(debug, "PME send node %d %d -> %d grid start %d Communicating %d to %d\n",
1046 pme->nodeid, overlap->nodeid, send_id,
1047 pme->pmegrid_start_ix,
1048 send_index0-pme->pmegrid_start_ix,
1049 send_index0-pme->pmegrid_start_ix+send_nindex);
1050 fprintf(debug, "PME recv node %d %d <- %d grid start %d Communicating %d to %d\n",
1051 pme->nodeid, overlap->nodeid, recv_id,
1052 pme->pmegrid_start_ix,
1053 recv_index0-pme->pmegrid_start_ix,
1054 recv_index0-pme->pmegrid_start_ix+recv_nindex);
1057 MPI_Sendrecv(sendptr, send_nindex*datasize, GMX_MPI_REAL,
1059 recvptr, recv_nindex*datasize, GMX_MPI_REAL,
1061 overlap->mpi_comm, &stat);
1063 /* ADD data from contiguous recv buffer */
1064 if (direction == GMX_SUM_GRID_FORWARD)
1066 p = grid + (recv_index0-pme->pmegrid_start_ix)*(pme->pmegrid_ny*pme->pmegrid_nz);
1067 for (i = 0; i < recv_nindex*datasize; i++)
1069 p[i] += overlap->recvbuf[i];
1077 static int copy_pmegrid_to_fftgrid(gmx_pme_t pme, real *pmegrid, real *fftgrid, int grid_index)
1079 ivec local_fft_ndata, local_fft_offset, local_fft_size;
1080 ivec local_pme_size;
1084 /* Dimensions should be identical for A/B grid, so we just use A here */
1085 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
1090 local_pme_size[0] = pme->pmegrid_nx;
1091 local_pme_size[1] = pme->pmegrid_ny;
1092 local_pme_size[2] = pme->pmegrid_nz;
1094 /* The fftgrid is always 'justified' to the lower-left corner of the PME grid,
1095 the offset is identical, and the PME grid always has more data (due to overlap)
1100 char fn[STRLEN], format[STRLEN];
1102 sprintf(fn, "pmegrid%d.pdb", pme->nodeid);
1103 fp = gmx_ffopen(fn, "w");
1104 sprintf(fn, "pmegrid%d.txt", pme->nodeid);
1105 fp2 = gmx_ffopen(fn, "w");
1106 sprintf(format, "%s%s\n", pdbformat, "%6.2f%6.2f");
1109 for (ix = 0; ix < local_fft_ndata[XX]; ix++)
1111 for (iy = 0; iy < local_fft_ndata[YY]; iy++)
1113 for (iz = 0; iz < local_fft_ndata[ZZ]; iz++)
1115 pmeidx = ix*(local_pme_size[YY]*local_pme_size[ZZ])+iy*(local_pme_size[ZZ])+iz;
1116 fftidx = ix*(local_fft_size[YY]*local_fft_size[ZZ])+iy*(local_fft_size[ZZ])+iz;
1117 fftgrid[fftidx] = pmegrid[pmeidx];
1119 val = 100*pmegrid[pmeidx];
1120 if (pmegrid[pmeidx] != 0)
1122 fprintf(fp, format, "ATOM", pmeidx, "CA", "GLY", ' ', pmeidx, ' ',
1123 5.0*ix, 5.0*iy, 5.0*iz, 1.0, val);
1125 if (pmegrid[pmeidx] != 0)
1127 fprintf(fp2, "%-12s %5d %5d %5d %12.5e\n",
1129 pme->pmegrid_start_ix + ix,
1130 pme->pmegrid_start_iy + iy,
1131 pme->pmegrid_start_iz + iz,
1147 static gmx_cycles_t omp_cyc_start()
1149 return gmx_cycles_read();
1152 static gmx_cycles_t omp_cyc_end(gmx_cycles_t c)
1154 return gmx_cycles_read() - c;
1158 static int copy_fftgrid_to_pmegrid(gmx_pme_t pme, const real *fftgrid, real *pmegrid, int grid_index,
1159 int nthread, int thread)
1161 ivec local_fft_ndata, local_fft_offset, local_fft_size;
1162 ivec local_pme_size;
1163 int ixy0, ixy1, ixy, ix, iy, iz;
1165 #ifdef PME_TIME_THREADS
1167 static double cs1 = 0;
1171 #ifdef PME_TIME_THREADS
1172 c1 = omp_cyc_start();
1174 /* Dimensions should be identical for A/B grid, so we just use A here */
1175 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
1180 local_pme_size[0] = pme->pmegrid_nx;
1181 local_pme_size[1] = pme->pmegrid_ny;
1182 local_pme_size[2] = pme->pmegrid_nz;
1184 /* The fftgrid is always 'justified' to the lower-left corner of the PME grid,
1185 the offset is identical, and the PME grid always has more data (due to overlap)
1187 ixy0 = ((thread )*local_fft_ndata[XX]*local_fft_ndata[YY])/nthread;
1188 ixy1 = ((thread+1)*local_fft_ndata[XX]*local_fft_ndata[YY])/nthread;
1190 for (ixy = ixy0; ixy < ixy1; ixy++)
1192 ix = ixy/local_fft_ndata[YY];
1193 iy = ixy - ix*local_fft_ndata[YY];
1195 pmeidx = (ix*local_pme_size[YY] + iy)*local_pme_size[ZZ];
1196 fftidx = (ix*local_fft_size[YY] + iy)*local_fft_size[ZZ];
1197 for (iz = 0; iz < local_fft_ndata[ZZ]; iz++)
1199 pmegrid[pmeidx+iz] = fftgrid[fftidx+iz];
1203 #ifdef PME_TIME_THREADS
1204 c1 = omp_cyc_end(c1);
1209 printf("copy %.2f\n", cs1*1e-9);
1217 static void wrap_periodic_pmegrid(gmx_pme_t pme, real *pmegrid)
1219 int nx, ny, nz, pnx, pny, pnz, ny_x, overlap, ix, iy, iz;
1225 pnx = pme->pmegrid_nx;
1226 pny = pme->pmegrid_ny;
1227 pnz = pme->pmegrid_nz;
1229 overlap = pme->pme_order - 1;
1231 /* Add periodic overlap in z */
1232 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1234 for (iy = 0; iy < pme->pmegrid_ny; iy++)
1236 for (iz = 0; iz < overlap; iz++)
1238 pmegrid[(ix*pny+iy)*pnz+iz] +=
1239 pmegrid[(ix*pny+iy)*pnz+nz+iz];
1244 if (pme->nnodes_minor == 1)
1246 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1248 for (iy = 0; iy < overlap; iy++)
1250 for (iz = 0; iz < nz; iz++)
1252 pmegrid[(ix*pny+iy)*pnz+iz] +=
1253 pmegrid[(ix*pny+ny+iy)*pnz+iz];
1259 if (pme->nnodes_major == 1)
1261 ny_x = (pme->nnodes_minor == 1 ? ny : pme->pmegrid_ny);
1263 for (ix = 0; ix < overlap; ix++)
1265 for (iy = 0; iy < ny_x; iy++)
1267 for (iz = 0; iz < nz; iz++)
1269 pmegrid[(ix*pny+iy)*pnz+iz] +=
1270 pmegrid[((nx+ix)*pny+iy)*pnz+iz];
1278 static void unwrap_periodic_pmegrid(gmx_pme_t pme, real *pmegrid)
1280 int nx, ny, nz, pnx, pny, pnz, ny_x, overlap, ix;
1286 pnx = pme->pmegrid_nx;
1287 pny = pme->pmegrid_ny;
1288 pnz = pme->pmegrid_nz;
1290 overlap = pme->pme_order - 1;
1292 if (pme->nnodes_major == 1)
1294 ny_x = (pme->nnodes_minor == 1 ? ny : pme->pmegrid_ny);
1296 for (ix = 0; ix < overlap; ix++)
1300 for (iy = 0; iy < ny_x; iy++)
1302 for (iz = 0; iz < nz; iz++)
1304 pmegrid[((nx+ix)*pny+iy)*pnz+iz] =
1305 pmegrid[(ix*pny+iy)*pnz+iz];
1311 if (pme->nnodes_minor == 1)
1313 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
1314 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1318 for (iy = 0; iy < overlap; iy++)
1320 for (iz = 0; iz < nz; iz++)
1322 pmegrid[(ix*pny+ny+iy)*pnz+iz] =
1323 pmegrid[(ix*pny+iy)*pnz+iz];
1329 /* Copy periodic overlap in z */
1330 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
1331 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1335 for (iy = 0; iy < pme->pmegrid_ny; iy++)
1337 for (iz = 0; iz < overlap; iz++)
1339 pmegrid[(ix*pny+iy)*pnz+nz+iz] =
1340 pmegrid[(ix*pny+iy)*pnz+iz];
1347 /* This has to be a macro to enable full compiler optimization with xlC (and probably others too) */
1348 #define DO_BSPLINE(order) \
1349 for (ithx = 0; (ithx < order); ithx++) \
1351 index_x = (i0+ithx)*pny*pnz; \
1352 valx = coefficient*thx[ithx]; \
1354 for (ithy = 0; (ithy < order); ithy++) \
1356 valxy = valx*thy[ithy]; \
1357 index_xy = index_x+(j0+ithy)*pnz; \
1359 for (ithz = 0; (ithz < order); ithz++) \
1361 index_xyz = index_xy+(k0+ithz); \
1362 grid[index_xyz] += valxy*thz[ithz]; \
1368 static void spread_coefficients_bsplines_thread(pmegrid_t *pmegrid,
1369 pme_atomcomm_t *atc,
1370 splinedata_t *spline,
1371 pme_spline_work_t gmx_unused *work)
1374 /* spread coefficients from home atoms to local grid */
1377 int b, i, nn, n, ithx, ithy, ithz, i0, j0, k0;
1379 int order, norder, index_x, index_xy, index_xyz;
1380 real valx, valxy, coefficient;
1381 real *thx, *thy, *thz;
1382 int localsize, bndsize;
1383 int pnx, pny, pnz, ndatatot;
1384 int offx, offy, offz;
1386 #if defined PME_SIMD4_SPREAD_GATHER && !defined PME_SIMD4_UNALIGNED
1387 real thz_buffer[GMX_SIMD4_WIDTH*3], *thz_aligned;
1389 thz_aligned = gmx_simd4_align_r(thz_buffer);
1392 pnx = pmegrid->s[XX];
1393 pny = pmegrid->s[YY];
1394 pnz = pmegrid->s[ZZ];
1396 offx = pmegrid->offset[XX];
1397 offy = pmegrid->offset[YY];
1398 offz = pmegrid->offset[ZZ];
1400 ndatatot = pnx*pny*pnz;
1401 grid = pmegrid->grid;
1402 for (i = 0; i < ndatatot; i++)
1407 order = pmegrid->order;
1409 for (nn = 0; nn < spline->n; nn++)
1411 n = spline->ind[nn];
1412 coefficient = atc->coefficient[n];
1414 if (coefficient != 0)
1416 idxptr = atc->idx[n];
1419 i0 = idxptr[XX] - offx;
1420 j0 = idxptr[YY] - offy;
1421 k0 = idxptr[ZZ] - offz;
1423 thx = spline->theta[XX] + norder;
1424 thy = spline->theta[YY] + norder;
1425 thz = spline->theta[ZZ] + norder;
1430 #ifdef PME_SIMD4_SPREAD_GATHER
1431 #ifdef PME_SIMD4_UNALIGNED
1432 #define PME_SPREAD_SIMD4_ORDER4
1434 #define PME_SPREAD_SIMD4_ALIGNED
1437 #include "pme_simd4.h"
1443 #ifdef PME_SIMD4_SPREAD_GATHER
1444 #define PME_SPREAD_SIMD4_ALIGNED
1446 #include "pme_simd4.h"
1459 static void set_grid_alignment(int gmx_unused *pmegrid_nz, int gmx_unused pme_order)
1461 #ifdef PME_SIMD4_SPREAD_GATHER
1463 #ifndef PME_SIMD4_UNALIGNED
1468 /* Round nz up to a multiple of 4 to ensure alignment */
1469 *pmegrid_nz = ((*pmegrid_nz + 3) & ~3);
1474 static void set_gridsize_alignment(int gmx_unused *gridsize, int gmx_unused pme_order)
1476 #ifdef PME_SIMD4_SPREAD_GATHER
1477 #ifndef PME_SIMD4_UNALIGNED
1480 /* Add extra elements to ensured aligned operations do not go
1481 * beyond the allocated grid size.
1482 * Note that for pme_order=5, the pme grid z-size alignment
1483 * ensures that we will not go beyond the grid size.
1491 static void pmegrid_init(pmegrid_t *grid,
1492 int cx, int cy, int cz,
1493 int x0, int y0, int z0,
1494 int x1, int y1, int z1,
1495 gmx_bool set_alignment,
1504 grid->offset[XX] = x0;
1505 grid->offset[YY] = y0;
1506 grid->offset[ZZ] = z0;
1507 grid->n[XX] = x1 - x0 + pme_order - 1;
1508 grid->n[YY] = y1 - y0 + pme_order - 1;
1509 grid->n[ZZ] = z1 - z0 + pme_order - 1;
1510 copy_ivec(grid->n, grid->s);
1513 set_grid_alignment(&nz, pme_order);
1518 else if (nz != grid->s[ZZ])
1520 gmx_incons("pmegrid_init call with an unaligned z size");
1523 grid->order = pme_order;
1526 gridsize = grid->s[XX]*grid->s[YY]*grid->s[ZZ];
1527 set_gridsize_alignment(&gridsize, pme_order);
1528 snew_aligned(grid->grid, gridsize, SIMD4_ALIGNMENT);
1536 static int div_round_up(int enumerator, int denominator)
1538 return (enumerator + denominator - 1)/denominator;
1541 static void make_subgrid_division(const ivec n, int ovl, int nthread,
1544 int gsize_opt, gsize;
1549 for (nsx = 1; nsx <= nthread; nsx++)
1551 if (nthread % nsx == 0)
1553 for (nsy = 1; nsy <= nthread; nsy++)
1555 if (nsx*nsy <= nthread && nthread % (nsx*nsy) == 0)
1557 nsz = nthread/(nsx*nsy);
1559 /* Determine the number of grid points per thread */
1561 (div_round_up(n[XX], nsx) + ovl)*
1562 (div_round_up(n[YY], nsy) + ovl)*
1563 (div_round_up(n[ZZ], nsz) + ovl);
1565 /* Minimize the number of grids points per thread
1566 * and, secondarily, the number of cuts in minor dimensions.
1568 if (gsize_opt == -1 ||
1569 gsize < gsize_opt ||
1570 (gsize == gsize_opt &&
1571 (nsz < nsub[ZZ] || (nsz == nsub[ZZ] && nsy < nsub[YY]))))
1583 env = getenv("GMX_PME_THREAD_DIVISION");
1586 sscanf(env, "%d %d %d", &nsub[XX], &nsub[YY], &nsub[ZZ]);
1589 if (nsub[XX]*nsub[YY]*nsub[ZZ] != nthread)
1591 gmx_fatal(FARGS, "PME grid thread division (%d x %d x %d) does not match the total number of threads (%d)", nsub[XX], nsub[YY], nsub[ZZ], nthread);
1595 static void pmegrids_init(pmegrids_t *grids,
1596 int nx, int ny, int nz, int nz_base,
1598 gmx_bool bUseThreads,
1603 ivec n, n_base, g0, g1;
1604 int t, x, y, z, d, i, tfac;
1605 int max_comm_lines = -1;
1607 n[XX] = nx - (pme_order - 1);
1608 n[YY] = ny - (pme_order - 1);
1609 n[ZZ] = nz - (pme_order - 1);
1611 copy_ivec(n, n_base);
1612 n_base[ZZ] = nz_base;
1614 pmegrid_init(&grids->grid, 0, 0, 0, 0, 0, 0, n[XX], n[YY], n[ZZ], FALSE, pme_order,
1617 grids->nthread = nthread;
1619 make_subgrid_division(n_base, pme_order-1, grids->nthread, grids->nc);
1626 for (d = 0; d < DIM; d++)
1628 nst[d] = div_round_up(n[d], grids->nc[d]) + pme_order - 1;
1630 set_grid_alignment(&nst[ZZ], pme_order);
1634 fprintf(debug, "pmegrid thread local division: %d x %d x %d\n",
1635 grids->nc[XX], grids->nc[YY], grids->nc[ZZ]);
1636 fprintf(debug, "pmegrid %d %d %d max thread pmegrid %d %d %d\n",
1638 nst[XX], nst[YY], nst[ZZ]);
1641 snew(grids->grid_th, grids->nthread);
1643 gridsize = nst[XX]*nst[YY]*nst[ZZ];
1644 set_gridsize_alignment(&gridsize, pme_order);
1645 snew_aligned(grids->grid_all,
1646 grids->nthread*gridsize+(grids->nthread+1)*GMX_CACHE_SEP,
1649 for (x = 0; x < grids->nc[XX]; x++)
1651 for (y = 0; y < grids->nc[YY]; y++)
1653 for (z = 0; z < grids->nc[ZZ]; z++)
1655 pmegrid_init(&grids->grid_th[t],
1657 (n[XX]*(x ))/grids->nc[XX],
1658 (n[YY]*(y ))/grids->nc[YY],
1659 (n[ZZ]*(z ))/grids->nc[ZZ],
1660 (n[XX]*(x+1))/grids->nc[XX],
1661 (n[YY]*(y+1))/grids->nc[YY],
1662 (n[ZZ]*(z+1))/grids->nc[ZZ],
1665 grids->grid_all+GMX_CACHE_SEP+t*(gridsize+GMX_CACHE_SEP));
1673 grids->grid_th = NULL;
1676 snew(grids->g2t, DIM);
1678 for (d = DIM-1; d >= 0; d--)
1680 snew(grids->g2t[d], n[d]);
1682 for (i = 0; i < n[d]; i++)
1684 /* The second check should match the parameters
1685 * of the pmegrid_init call above.
1687 while (t + 1 < grids->nc[d] && i >= (n[d]*(t+1))/grids->nc[d])
1691 grids->g2t[d][i] = t*tfac;
1694 tfac *= grids->nc[d];
1698 case XX: max_comm_lines = overlap_x; break;
1699 case YY: max_comm_lines = overlap_y; break;
1700 case ZZ: max_comm_lines = pme_order - 1; break;
1702 grids->nthread_comm[d] = 0;
1703 while ((n[d]*grids->nthread_comm[d])/grids->nc[d] < max_comm_lines &&
1704 grids->nthread_comm[d] < grids->nc[d])
1706 grids->nthread_comm[d]++;
1710 fprintf(debug, "pmegrid thread grid communication range in %c: %d\n",
1711 'x'+d, grids->nthread_comm[d]);
1713 /* It should be possible to make grids->nthread_comm[d]==grids->nc[d]
1714 * work, but this is not a problematic restriction.
1716 if (grids->nc[d] > 1 && grids->nthread_comm[d] > grids->nc[d])
1718 gmx_fatal(FARGS, "Too many threads for PME (%d) compared to the number of grid lines, reduce the number of threads doing PME", grids->nthread);
1724 static void pmegrids_destroy(pmegrids_t *grids)
1728 if (grids->grid.grid != NULL)
1730 sfree(grids->grid.grid);
1732 if (grids->nthread > 0)
1734 for (t = 0; t < grids->nthread; t++)
1736 sfree(grids->grid_th[t].grid);
1738 sfree(grids->grid_th);
1744 static void realloc_work(pme_work_t *work, int nkx)
1748 if (nkx > work->nalloc)
1751 srenew(work->mhx, work->nalloc);
1752 srenew(work->mhy, work->nalloc);
1753 srenew(work->mhz, work->nalloc);
1754 srenew(work->m2, work->nalloc);
1755 /* Allocate an aligned pointer for SIMD operations, including extra
1756 * elements at the end for padding.
1758 #ifdef PME_SIMD_SOLVE
1759 simd_width = GMX_SIMD_REAL_WIDTH;
1761 /* We can use any alignment, apart from 0, so we use 4 */
1764 sfree_aligned(work->denom);
1765 sfree_aligned(work->tmp1);
1766 sfree_aligned(work->tmp2);
1767 sfree_aligned(work->eterm);
1768 snew_aligned(work->denom, work->nalloc+simd_width, simd_width*sizeof(real));
1769 snew_aligned(work->tmp1, work->nalloc+simd_width, simd_width*sizeof(real));
1770 snew_aligned(work->tmp2, work->nalloc+simd_width, simd_width*sizeof(real));
1771 snew_aligned(work->eterm, work->nalloc+simd_width, simd_width*sizeof(real));
1772 srenew(work->m2inv, work->nalloc);
1777 static void free_work(pme_work_t *work)
1783 sfree_aligned(work->denom);
1784 sfree_aligned(work->tmp1);
1785 sfree_aligned(work->tmp2);
1786 sfree_aligned(work->eterm);
1791 #if defined PME_SIMD_SOLVE
1792 /* Calculate exponentials through SIMD */
1793 gmx_inline static void calc_exponentials_q(int gmx_unused start, int end, real f, real *d_aligned, real *r_aligned, real *e_aligned)
1796 const gmx_simd_real_t two = gmx_simd_set1_r(2.0);
1797 gmx_simd_real_t f_simd;
1799 gmx_simd_real_t tmp_d1, d_inv, tmp_r, tmp_e;
1801 f_simd = gmx_simd_set1_r(f);
1802 /* We only need to calculate from start. But since start is 0 or 1
1803 * and we want to use aligned loads/stores, we always start from 0.
1805 for (kx = 0; kx < end; kx += GMX_SIMD_REAL_WIDTH)
1807 tmp_d1 = gmx_simd_load_r(d_aligned+kx);
1808 d_inv = gmx_simd_inv_r(tmp_d1);
1809 tmp_r = gmx_simd_load_r(r_aligned+kx);
1810 tmp_r = gmx_simd_exp_r(tmp_r);
1811 tmp_e = gmx_simd_mul_r(f_simd, d_inv);
1812 tmp_e = gmx_simd_mul_r(tmp_e, tmp_r);
1813 gmx_simd_store_r(e_aligned+kx, tmp_e);
1818 gmx_inline static void calc_exponentials_q(int start, int end, real f, real *d, real *r, real *e)
1821 for (kx = start; kx < end; kx++)
1825 for (kx = start; kx < end; kx++)
1829 for (kx = start; kx < end; kx++)
1831 e[kx] = f*r[kx]*d[kx];
1836 #if defined PME_SIMD_SOLVE
1837 /* Calculate exponentials through SIMD */
1838 gmx_inline static void calc_exponentials_lj(int gmx_unused start, int end, real *r_aligned, real *factor_aligned, real *d_aligned)
1840 gmx_simd_real_t tmp_r, tmp_d, tmp_fac, d_inv, tmp_mk;
1841 const gmx_simd_real_t sqr_PI = gmx_simd_sqrt_r(gmx_simd_set1_r(M_PI));
1843 for (kx = 0; kx < end; kx += GMX_SIMD_REAL_WIDTH)
1845 /* We only need to calculate from start. But since start is 0 or 1
1846 * and we want to use aligned loads/stores, we always start from 0.
1848 tmp_d = gmx_simd_load_r(d_aligned+kx);
1849 d_inv = gmx_simd_inv_r(tmp_d);
1850 gmx_simd_store_r(d_aligned+kx, d_inv);
1851 tmp_r = gmx_simd_load_r(r_aligned+kx);
1852 tmp_r = gmx_simd_exp_r(tmp_r);
1853 gmx_simd_store_r(r_aligned+kx, tmp_r);
1854 tmp_mk = gmx_simd_load_r(factor_aligned+kx);
1855 tmp_fac = gmx_simd_mul_r(sqr_PI, gmx_simd_mul_r(tmp_mk, gmx_simd_erfc_r(tmp_mk)));
1856 gmx_simd_store_r(factor_aligned+kx, tmp_fac);
1860 gmx_inline static void calc_exponentials_lj(int start, int end, real *r, real *tmp2, real *d)
1864 for (kx = start; kx < end; kx++)
1869 for (kx = start; kx < end; kx++)
1874 for (kx = start; kx < end; kx++)
1877 tmp2[kx] = sqrt(M_PI)*mk*gmx_erfc(mk);
1882 static int solve_pme_yzx(gmx_pme_t pme, t_complex *grid,
1883 real ewaldcoeff, real vol,
1885 int nthread, int thread)
1887 /* do recip sum over local cells in grid */
1888 /* y major, z middle, x minor or continuous */
1890 int kx, ky, kz, maxkx, maxky, maxkz;
1891 int nx, ny, nz, iyz0, iyz1, iyz, iy, iz, kxstart, kxend;
1893 real factor = M_PI*M_PI/(ewaldcoeff*ewaldcoeff);
1894 real ets2, struct2, vfactor, ets2vf;
1895 real d1, d2, energy = 0;
1897 real virxx = 0, virxy = 0, virxz = 0, viryy = 0, viryz = 0, virzz = 0;
1898 real rxx, ryx, ryy, rzx, rzy, rzz;
1900 real *mhx, *mhy, *mhz, *m2, *denom, *tmp1, *eterm, *m2inv;
1901 real mhxk, mhyk, mhzk, m2k;
1904 ivec local_ndata, local_offset, local_size;
1907 elfac = ONE_4PI_EPS0/pme->epsilon_r;
1913 /* Dimensions should be identical for A/B grid, so we just use A here */
1914 gmx_parallel_3dfft_complex_limits(pme->pfft_setup[PME_GRID_QA],
1920 rxx = pme->recipbox[XX][XX];
1921 ryx = pme->recipbox[YY][XX];
1922 ryy = pme->recipbox[YY][YY];
1923 rzx = pme->recipbox[ZZ][XX];
1924 rzy = pme->recipbox[ZZ][YY];
1925 rzz = pme->recipbox[ZZ][ZZ];
1931 work = &pme->work[thread];
1936 denom = work->denom;
1938 eterm = work->eterm;
1939 m2inv = work->m2inv;
1941 iyz0 = local_ndata[YY]*local_ndata[ZZ]* thread /nthread;
1942 iyz1 = local_ndata[YY]*local_ndata[ZZ]*(thread+1)/nthread;
1944 for (iyz = iyz0; iyz < iyz1; iyz++)
1946 iy = iyz/local_ndata[ZZ];
1947 iz = iyz - iy*local_ndata[ZZ];
1949 ky = iy + local_offset[YY];
1960 by = M_PI*vol*pme->bsp_mod[YY][ky];
1962 kz = iz + local_offset[ZZ];
1966 bz = pme->bsp_mod[ZZ][kz];
1968 /* 0.5 correction for corner points */
1970 if (kz == 0 || kz == (nz+1)/2)
1975 p0 = grid + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
1977 /* We should skip the k-space point (0,0,0) */
1978 /* Note that since here x is the minor index, local_offset[XX]=0 */
1979 if (local_offset[XX] > 0 || ky > 0 || kz > 0)
1981 kxstart = local_offset[XX];
1985 kxstart = local_offset[XX] + 1;
1988 kxend = local_offset[XX] + local_ndata[XX];
1992 /* More expensive inner loop, especially because of the storage
1993 * of the mh elements in array's.
1994 * Because x is the minor grid index, all mh elements
1995 * depend on kx for triclinic unit cells.
1998 /* Two explicit loops to avoid a conditional inside the loop */
1999 for (kx = kxstart; kx < maxkx; kx++)
2004 mhyk = mx * ryx + my * ryy;
2005 mhzk = mx * rzx + my * rzy + mz * rzz;
2006 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2011 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2012 tmp1[kx] = -factor*m2k;
2015 for (kx = maxkx; kx < kxend; kx++)
2020 mhyk = mx * ryx + my * ryy;
2021 mhzk = mx * rzx + my * rzy + mz * rzz;
2022 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2027 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2028 tmp1[kx] = -factor*m2k;
2031 for (kx = kxstart; kx < kxend; kx++)
2033 m2inv[kx] = 1.0/m2[kx];
2036 calc_exponentials_q(kxstart, kxend, elfac, denom, tmp1, eterm);
2038 for (kx = kxstart; kx < kxend; kx++, p0++)
2043 p0->re = d1*eterm[kx];
2044 p0->im = d2*eterm[kx];
2046 struct2 = 2.0*(d1*d1+d2*d2);
2048 tmp1[kx] = eterm[kx]*struct2;
2051 for (kx = kxstart; kx < kxend; kx++)
2053 ets2 = corner_fac*tmp1[kx];
2054 vfactor = (factor*m2[kx] + 1.0)*2.0*m2inv[kx];
2057 ets2vf = ets2*vfactor;
2058 virxx += ets2vf*mhx[kx]*mhx[kx] - ets2;
2059 virxy += ets2vf*mhx[kx]*mhy[kx];
2060 virxz += ets2vf*mhx[kx]*mhz[kx];
2061 viryy += ets2vf*mhy[kx]*mhy[kx] - ets2;
2062 viryz += ets2vf*mhy[kx]*mhz[kx];
2063 virzz += ets2vf*mhz[kx]*mhz[kx] - ets2;
2068 /* We don't need to calculate the energy and the virial.
2069 * In this case the triclinic overhead is small.
2072 /* Two explicit loops to avoid a conditional inside the loop */
2074 for (kx = kxstart; kx < maxkx; kx++)
2079 mhyk = mx * ryx + my * ryy;
2080 mhzk = mx * rzx + my * rzy + mz * rzz;
2081 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2082 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2083 tmp1[kx] = -factor*m2k;
2086 for (kx = maxkx; kx < kxend; kx++)
2091 mhyk = mx * ryx + my * ryy;
2092 mhzk = mx * rzx + my * rzy + mz * rzz;
2093 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2094 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2095 tmp1[kx] = -factor*m2k;
2098 calc_exponentials_q(kxstart, kxend, elfac, denom, tmp1, eterm);
2100 for (kx = kxstart; kx < kxend; kx++, p0++)
2105 p0->re = d1*eterm[kx];
2106 p0->im = d2*eterm[kx];
2113 /* Update virial with local values.
2114 * The virial is symmetric by definition.
2115 * this virial seems ok for isotropic scaling, but I'm
2116 * experiencing problems on semiisotropic membranes.
2117 * IS THAT COMMENT STILL VALID??? (DvdS, 2001/02/07).
2119 work->vir_q[XX][XX] = 0.25*virxx;
2120 work->vir_q[YY][YY] = 0.25*viryy;
2121 work->vir_q[ZZ][ZZ] = 0.25*virzz;
2122 work->vir_q[XX][YY] = work->vir_q[YY][XX] = 0.25*virxy;
2123 work->vir_q[XX][ZZ] = work->vir_q[ZZ][XX] = 0.25*virxz;
2124 work->vir_q[YY][ZZ] = work->vir_q[ZZ][YY] = 0.25*viryz;
2126 /* This energy should be corrected for a charged system */
2127 work->energy_q = 0.5*energy;
2130 /* Return the loop count */
2131 return local_ndata[YY]*local_ndata[XX];
2134 static int solve_pme_lj_yzx(gmx_pme_t pme, t_complex **grid, gmx_bool bLB,
2135 real ewaldcoeff, real vol,
2136 gmx_bool bEnerVir, int nthread, int thread)
2138 /* do recip sum over local cells in grid */
2139 /* y major, z middle, x minor or continuous */
2141 int kx, ky, kz, maxkx, maxky, maxkz;
2142 int nx, ny, nz, iy, iyz0, iyz1, iyz, iz, kxstart, kxend;
2144 real factor = M_PI*M_PI/(ewaldcoeff*ewaldcoeff);
2146 real eterm, vterm, d1, d2, energy = 0;
2148 real virxx = 0, virxy = 0, virxz = 0, viryy = 0, viryz = 0, virzz = 0;
2149 real rxx, ryx, ryy, rzx, rzy, rzz;
2150 real *mhx, *mhy, *mhz, *m2, *denom, *tmp1, *tmp2;
2151 real mhxk, mhyk, mhzk, m2k;
2156 ivec local_ndata, local_offset, local_size;
2161 /* Dimensions should be identical for A/B grid, so we just use A here */
2162 gmx_parallel_3dfft_complex_limits(pme->pfft_setup[PME_GRID_C6A],
2167 rxx = pme->recipbox[XX][XX];
2168 ryx = pme->recipbox[YY][XX];
2169 ryy = pme->recipbox[YY][YY];
2170 rzx = pme->recipbox[ZZ][XX];
2171 rzy = pme->recipbox[ZZ][YY];
2172 rzz = pme->recipbox[ZZ][ZZ];
2178 work = &pme->work[thread];
2183 denom = work->denom;
2187 iyz0 = local_ndata[YY]*local_ndata[ZZ]* thread /nthread;
2188 iyz1 = local_ndata[YY]*local_ndata[ZZ]*(thread+1)/nthread;
2190 for (iyz = iyz0; iyz < iyz1; iyz++)
2192 iy = iyz/local_ndata[ZZ];
2193 iz = iyz - iy*local_ndata[ZZ];
2195 ky = iy + local_offset[YY];
2206 by = 3.0*vol*pme->bsp_mod[YY][ky]
2207 / (M_PI*sqrt(M_PI)*ewaldcoeff*ewaldcoeff*ewaldcoeff);
2209 kz = iz + local_offset[ZZ];
2213 bz = pme->bsp_mod[ZZ][kz];
2215 /* 0.5 correction for corner points */
2217 if (kz == 0 || kz == (nz+1)/2)
2222 kxstart = local_offset[XX];
2223 kxend = local_offset[XX] + local_ndata[XX];
2226 /* More expensive inner loop, especially because of the
2227 * storage of the mh elements in array's. Because x is the
2228 * minor grid index, all mh elements depend on kx for
2229 * triclinic unit cells.
2232 /* Two explicit loops to avoid a conditional inside the loop */
2233 for (kx = kxstart; kx < maxkx; kx++)
2238 mhyk = mx * ryx + my * ryy;
2239 mhzk = mx * rzx + my * rzy + mz * rzz;
2240 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2245 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2246 tmp1[kx] = -factor*m2k;
2247 tmp2[kx] = sqrt(factor*m2k);
2250 for (kx = maxkx; kx < kxend; kx++)
2255 mhyk = mx * ryx + my * ryy;
2256 mhzk = mx * rzx + my * rzy + mz * rzz;
2257 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2262 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2263 tmp1[kx] = -factor*m2k;
2264 tmp2[kx] = sqrt(factor*m2k);
2267 calc_exponentials_lj(kxstart, kxend, tmp1, tmp2, denom);
2269 for (kx = kxstart; kx < kxend; kx++)
2271 m2k = factor*m2[kx];
2272 eterm = -((1.0 - 2.0*m2k)*tmp1[kx]
2273 + 2.0*m2k*tmp2[kx]);
2274 vterm = 3.0*(-tmp1[kx] + tmp2[kx]);
2275 tmp1[kx] = eterm*denom[kx];
2276 tmp2[kx] = vterm*denom[kx];
2284 p0 = grid[0] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2285 for (kx = kxstart; kx < kxend; kx++, p0++)
2295 struct2 = 2.0*(d1*d1+d2*d2);
2297 tmp1[kx] = eterm*struct2;
2298 tmp2[kx] = vterm*struct2;
2303 real *struct2 = denom;
2306 for (kx = kxstart; kx < kxend; kx++)
2310 /* Due to symmetry we only need to calculate 4 of the 7 terms */
2311 for (ig = 0; ig <= 3; ++ig)
2316 p0 = grid[ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2317 p1 = grid[6-ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2318 scale = 2.0*lb_scale_factor_symm[ig];
2319 for (kx = kxstart; kx < kxend; ++kx, ++p0, ++p1)
2321 struct2[kx] += scale*(p0->re*p1->re + p0->im*p1->im);
2325 for (ig = 0; ig <= 6; ++ig)
2329 p0 = grid[ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2330 for (kx = kxstart; kx < kxend; kx++, p0++)
2340 for (kx = kxstart; kx < kxend; kx++)
2345 tmp1[kx] = eterm*str2;
2346 tmp2[kx] = vterm*str2;
2350 for (kx = kxstart; kx < kxend; kx++)
2352 ets2 = corner_fac*tmp1[kx];
2353 vterm = 2.0*factor*tmp2[kx];
2355 ets2vf = corner_fac*vterm;
2356 virxx += ets2vf*mhx[kx]*mhx[kx] - ets2;
2357 virxy += ets2vf*mhx[kx]*mhy[kx];
2358 virxz += ets2vf*mhx[kx]*mhz[kx];
2359 viryy += ets2vf*mhy[kx]*mhy[kx] - ets2;
2360 viryz += ets2vf*mhy[kx]*mhz[kx];
2361 virzz += ets2vf*mhz[kx]*mhz[kx] - ets2;
2366 /* We don't need to calculate the energy and the virial.
2367 * In this case the triclinic overhead is small.
2370 /* Two explicit loops to avoid a conditional inside the loop */
2372 for (kx = kxstart; kx < maxkx; kx++)
2377 mhyk = mx * ryx + my * ryy;
2378 mhzk = mx * rzx + my * rzy + mz * rzz;
2379 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2381 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2382 tmp1[kx] = -factor*m2k;
2383 tmp2[kx] = sqrt(factor*m2k);
2386 for (kx = maxkx; kx < kxend; kx++)
2391 mhyk = mx * ryx + my * ryy;
2392 mhzk = mx * rzx + my * rzy + mz * rzz;
2393 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2395 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2396 tmp1[kx] = -factor*m2k;
2397 tmp2[kx] = sqrt(factor*m2k);
2400 calc_exponentials_lj(kxstart, kxend, tmp1, tmp2, denom);
2402 for (kx = kxstart; kx < kxend; kx++)
2404 m2k = factor*m2[kx];
2405 eterm = -((1.0 - 2.0*m2k)*tmp1[kx]
2406 + 2.0*m2k*tmp2[kx]);
2407 tmp1[kx] = eterm*denom[kx];
2409 gcount = (bLB ? 7 : 1);
2410 for (ig = 0; ig < gcount; ++ig)
2414 p0 = grid[ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2415 for (kx = kxstart; kx < kxend; kx++, p0++)
2430 work->vir_lj[XX][XX] = 0.25*virxx;
2431 work->vir_lj[YY][YY] = 0.25*viryy;
2432 work->vir_lj[ZZ][ZZ] = 0.25*virzz;
2433 work->vir_lj[XX][YY] = work->vir_lj[YY][XX] = 0.25*virxy;
2434 work->vir_lj[XX][ZZ] = work->vir_lj[ZZ][XX] = 0.25*virxz;
2435 work->vir_lj[YY][ZZ] = work->vir_lj[ZZ][YY] = 0.25*viryz;
2437 /* This energy should be corrected for a charged system */
2438 work->energy_lj = 0.5*energy;
2440 /* Return the loop count */
2441 return local_ndata[YY]*local_ndata[XX];
2444 static void get_pme_ener_vir_q(const gmx_pme_t pme, int nthread,
2445 real *mesh_energy, matrix vir)
2447 /* This function sums output over threads and should therefore
2448 * only be called after thread synchronization.
2452 *mesh_energy = pme->work[0].energy_q;
2453 copy_mat(pme->work[0].vir_q, vir);
2455 for (thread = 1; thread < nthread; thread++)
2457 *mesh_energy += pme->work[thread].energy_q;
2458 m_add(vir, pme->work[thread].vir_q, vir);
2462 static void get_pme_ener_vir_lj(const gmx_pme_t pme, int nthread,
2463 real *mesh_energy, matrix vir)
2465 /* This function sums output over threads and should therefore
2466 * only be called after thread synchronization.
2470 *mesh_energy = pme->work[0].energy_lj;
2471 copy_mat(pme->work[0].vir_lj, vir);
2473 for (thread = 1; thread < nthread; thread++)
2475 *mesh_energy += pme->work[thread].energy_lj;
2476 m_add(vir, pme->work[thread].vir_lj, vir);
2481 #define DO_FSPLINE(order) \
2482 for (ithx = 0; (ithx < order); ithx++) \
2484 index_x = (i0+ithx)*pny*pnz; \
2488 for (ithy = 0; (ithy < order); ithy++) \
2490 index_xy = index_x+(j0+ithy)*pnz; \
2495 for (ithz = 0; (ithz < order); ithz++) \
2497 gval = grid[index_xy+(k0+ithz)]; \
2498 fxy1 += thz[ithz]*gval; \
2499 fz1 += dthz[ithz]*gval; \
2508 static void gather_f_bsplines(gmx_pme_t pme, real *grid,
2509 gmx_bool bClearF, pme_atomcomm_t *atc,
2510 splinedata_t *spline,
2513 /* sum forces for local particles */
2514 int nn, n, ithx, ithy, ithz, i0, j0, k0;
2515 int index_x, index_xy;
2516 int nx, ny, nz, pnx, pny, pnz;
2518 real tx, ty, dx, dy, coefficient;
2519 real fx, fy, fz, gval;
2521 real *thx, *thy, *thz, *dthx, *dthy, *dthz;
2523 real rxx, ryx, ryy, rzx, rzy, rzz;
2526 pme_spline_work_t *work;
2528 #if defined PME_SIMD4_SPREAD_GATHER && !defined PME_SIMD4_UNALIGNED
2529 real thz_buffer[GMX_SIMD4_WIDTH*3], *thz_aligned;
2530 real dthz_buffer[GMX_SIMD4_WIDTH*3], *dthz_aligned;
2532 thz_aligned = gmx_simd4_align_r(thz_buffer);
2533 dthz_aligned = gmx_simd4_align_r(dthz_buffer);
2536 work = pme->spline_work;
2538 order = pme->pme_order;
2539 thx = spline->theta[XX];
2540 thy = spline->theta[YY];
2541 thz = spline->theta[ZZ];
2542 dthx = spline->dtheta[XX];
2543 dthy = spline->dtheta[YY];
2544 dthz = spline->dtheta[ZZ];
2548 pnx = pme->pmegrid_nx;
2549 pny = pme->pmegrid_ny;
2550 pnz = pme->pmegrid_nz;
2552 rxx = pme->recipbox[XX][XX];
2553 ryx = pme->recipbox[YY][XX];
2554 ryy = pme->recipbox[YY][YY];
2555 rzx = pme->recipbox[ZZ][XX];
2556 rzy = pme->recipbox[ZZ][YY];
2557 rzz = pme->recipbox[ZZ][ZZ];
2559 for (nn = 0; nn < spline->n; nn++)
2561 n = spline->ind[nn];
2562 coefficient = scale*atc->coefficient[n];
2570 if (coefficient != 0)
2575 idxptr = atc->idx[n];
2582 /* Pointer arithmetic alert, next six statements */
2583 thx = spline->theta[XX] + norder;
2584 thy = spline->theta[YY] + norder;
2585 thz = spline->theta[ZZ] + norder;
2586 dthx = spline->dtheta[XX] + norder;
2587 dthy = spline->dtheta[YY] + norder;
2588 dthz = spline->dtheta[ZZ] + norder;
2593 #ifdef PME_SIMD4_SPREAD_GATHER
2594 #ifdef PME_SIMD4_UNALIGNED
2595 #define PME_GATHER_F_SIMD4_ORDER4
2597 #define PME_GATHER_F_SIMD4_ALIGNED
2600 #include "pme_simd4.h"
2606 #ifdef PME_SIMD4_SPREAD_GATHER
2607 #define PME_GATHER_F_SIMD4_ALIGNED
2609 #include "pme_simd4.h"
2619 atc->f[n][XX] += -coefficient*( fx*nx*rxx );
2620 atc->f[n][YY] += -coefficient*( fx*nx*ryx + fy*ny*ryy );
2621 atc->f[n][ZZ] += -coefficient*( fx*nx*rzx + fy*ny*rzy + fz*nz*rzz );
2624 /* Since the energy and not forces are interpolated
2625 * the net force might not be exactly zero.
2626 * This can be solved by also interpolating F, but
2627 * that comes at a cost.
2628 * A better hack is to remove the net force every
2629 * step, but that must be done at a higher level
2630 * since this routine doesn't see all atoms if running
2631 * in parallel. Don't know how important it is? EL 990726
2636 static real gather_energy_bsplines(gmx_pme_t pme, real *grid,
2637 pme_atomcomm_t *atc)
2639 splinedata_t *spline;
2640 int n, ithx, ithy, ithz, i0, j0, k0;
2641 int index_x, index_xy;
2643 real energy, pot, tx, ty, coefficient, gval;
2644 real *thx, *thy, *thz;
2648 spline = &atc->spline[0];
2650 order = pme->pme_order;
2653 for (n = 0; (n < atc->n); n++)
2655 coefficient = atc->coefficient[n];
2657 if (coefficient != 0)
2659 idxptr = atc->idx[n];
2666 /* Pointer arithmetic alert, next three statements */
2667 thx = spline->theta[XX] + norder;
2668 thy = spline->theta[YY] + norder;
2669 thz = spline->theta[ZZ] + norder;
2672 for (ithx = 0; (ithx < order); ithx++)
2674 index_x = (i0+ithx)*pme->pmegrid_ny*pme->pmegrid_nz;
2677 for (ithy = 0; (ithy < order); ithy++)
2679 index_xy = index_x+(j0+ithy)*pme->pmegrid_nz;
2682 for (ithz = 0; (ithz < order); ithz++)
2684 gval = grid[index_xy+(k0+ithz)];
2685 pot += tx*ty*thz[ithz]*gval;
2691 energy += pot*coefficient;
2698 /* Macro to force loop unrolling by fixing order.
2699 * This gives a significant performance gain.
2701 #define CALC_SPLINE(order) \
2705 real data[PME_ORDER_MAX]; \
2706 real ddata[PME_ORDER_MAX]; \
2708 for (j = 0; (j < DIM); j++) \
2712 /* dr is relative offset from lower cell limit */ \
2713 data[order-1] = 0; \
2717 for (k = 3; (k < order); k++) \
2719 div = 1.0/(k - 1.0); \
2720 data[k-1] = div*dr*data[k-2]; \
2721 for (l = 1; (l < (k-1)); l++) \
2723 data[k-l-1] = div*((dr+l)*data[k-l-2]+(k-l-dr)* \
2726 data[0] = div*(1-dr)*data[0]; \
2728 /* differentiate */ \
2729 ddata[0] = -data[0]; \
2730 for (k = 1; (k < order); k++) \
2732 ddata[k] = data[k-1] - data[k]; \
2735 div = 1.0/(order - 1); \
2736 data[order-1] = div*dr*data[order-2]; \
2737 for (l = 1; (l < (order-1)); l++) \
2739 data[order-l-1] = div*((dr+l)*data[order-l-2]+ \
2740 (order-l-dr)*data[order-l-1]); \
2742 data[0] = div*(1 - dr)*data[0]; \
2744 for (k = 0; k < order; k++) \
2746 theta[j][i*order+k] = data[k]; \
2747 dtheta[j][i*order+k] = ddata[k]; \
2752 void make_bsplines(splinevec theta, splinevec dtheta, int order,
2753 rvec fractx[], int nr, int ind[], real coefficient[],
2754 gmx_bool bDoSplines)
2756 /* construct splines for local atoms */
2760 for (i = 0; i < nr; i++)
2762 /* With free energy we do not use the coefficient check.
2763 * In most cases this will be more efficient than calling make_bsplines
2764 * twice, since usually more than half the particles have non-zero coefficients.
2767 if (bDoSplines || coefficient[ii] != 0.0)
2772 case 4: CALC_SPLINE(4); break;
2773 case 5: CALC_SPLINE(5); break;
2774 default: CALC_SPLINE(order); break;
2781 void make_dft_mod(real *mod, real *data, int ndata)
2786 for (i = 0; i < ndata; i++)
2789 for (j = 0; j < ndata; j++)
2791 arg = (2.0*M_PI*i*j)/ndata;
2792 sc += data[j]*cos(arg);
2793 ss += data[j]*sin(arg);
2795 mod[i] = sc*sc+ss*ss;
2797 for (i = 0; i < ndata; i++)
2801 mod[i] = (mod[i-1]+mod[i+1])*0.5;
2807 static void make_bspline_moduli(splinevec bsp_mod,
2808 int nx, int ny, int nz, int order)
2810 int nmax = max(nx, max(ny, nz));
2811 real *data, *ddata, *bsp_data;
2817 snew(bsp_data, nmax);
2823 for (k = 3; k < order; k++)
2827 for (l = 1; l < (k-1); l++)
2829 data[k-l-1] = div*(l*data[k-l-2]+(k-l)*data[k-l-1]);
2831 data[0] = div*data[0];
2834 ddata[0] = -data[0];
2835 for (k = 1; k < order; k++)
2837 ddata[k] = data[k-1]-data[k];
2839 div = 1.0/(order-1);
2841 for (l = 1; l < (order-1); l++)
2843 data[order-l-1] = div*(l*data[order-l-2]+(order-l)*data[order-l-1]);
2845 data[0] = div*data[0];
2847 for (i = 0; i < nmax; i++)
2851 for (i = 1; i <= order; i++)
2853 bsp_data[i] = data[i-1];
2856 make_dft_mod(bsp_mod[XX], bsp_data, nx);
2857 make_dft_mod(bsp_mod[YY], bsp_data, ny);
2858 make_dft_mod(bsp_mod[ZZ], bsp_data, nz);
2866 /* Return the P3M optimal influence function */
2867 static double do_p3m_influence(double z, int order)
2874 /* The formula and most constants can be found in:
2875 * Ballenegger et al., JCTC 8, 936 (2012)
2880 return 1.0 - 2.0*z2/3.0;
2883 return 1.0 - z2 + 2.0*z4/15.0;
2886 return 1.0 - 4.0*z2/3.0 + 2.0*z4/5.0 + 4.0*z2*z4/315.0;
2889 return 1.0 - 5.0*z2/3.0 + 7.0*z4/9.0 - 17.0*z2*z4/189.0 + 2.0*z4*z4/2835.0;
2892 return 1.0 - 2.0*z2 + 19.0*z4/15.0 - 256.0*z2*z4/945.0 + 62.0*z4*z4/4725.0 + 4.0*z2*z4*z4/155925.0;
2895 return 1.0 - 7.0*z2/3.0 + 28.0*z4/15.0 - 16.0*z2*z4/27.0 + 26.0*z4*z4/405.0 - 2.0*z2*z4*z4/1485.0 + 4.0*z4*z4*z4/6081075.0;
2897 return 1.0 - 8.0*z2/3.0 + 116.0*z4/45.0 - 344.0*z2*z4/315.0 + 914.0*z4*z4/4725.0 - 248.0*z4*z4*z2/22275.0 + 21844.0*z4*z4*z4/212837625.0 - 8.0*z4*z4*z4*z2/638512875.0;
2904 /* Calculate the P3M B-spline moduli for one dimension */
2905 static void make_p3m_bspline_moduli_dim(real *bsp_mod, int n, int order)
2907 double zarg, zai, sinzai, infl;
2912 gmx_fatal(FARGS, "The current P3M code only supports orders up to 8");
2919 for (i = -maxk; i < 0; i++)
2923 infl = do_p3m_influence(sinzai, order);
2924 bsp_mod[n+i] = infl*infl*pow(sinzai/zai, -2.0*order);
2927 for (i = 1; i < maxk; i++)
2931 infl = do_p3m_influence(sinzai, order);
2932 bsp_mod[i] = infl*infl*pow(sinzai/zai, -2.0*order);
2936 /* Calculate the P3M B-spline moduli */
2937 static void make_p3m_bspline_moduli(splinevec bsp_mod,
2938 int nx, int ny, int nz, int order)
2940 make_p3m_bspline_moduli_dim(bsp_mod[XX], nx, order);
2941 make_p3m_bspline_moduli_dim(bsp_mod[YY], ny, order);
2942 make_p3m_bspline_moduli_dim(bsp_mod[ZZ], nz, order);
2946 static void setup_coordinate_communication(pme_atomcomm_t *atc)
2954 for (i = 1; i <= nslab/2; i++)
2956 fw = (atc->nodeid + i) % nslab;
2957 bw = (atc->nodeid - i + nslab) % nslab;
2960 atc->node_dest[n] = fw;
2961 atc->node_src[n] = bw;
2966 atc->node_dest[n] = bw;
2967 atc->node_src[n] = fw;
2973 int gmx_pme_destroy(FILE *log, gmx_pme_t *pmedata)
2979 fprintf(log, "Destroying PME data structures.\n");
2982 sfree((*pmedata)->nnx);
2983 sfree((*pmedata)->nny);
2984 sfree((*pmedata)->nnz);
2986 for (i = 0; i < (*pmedata)->ngrids; ++i)
2988 pmegrids_destroy(&(*pmedata)->pmegrid[i]);
2989 sfree((*pmedata)->fftgrid[i]);
2990 sfree((*pmedata)->cfftgrid[i]);
2991 gmx_parallel_3dfft_destroy((*pmedata)->pfft_setup[i]);
2994 sfree((*pmedata)->lb_buf1);
2995 sfree((*pmedata)->lb_buf2);
2997 for (thread = 0; thread < (*pmedata)->nthread; thread++)
2999 free_work(&(*pmedata)->work[thread]);
3001 sfree((*pmedata)->work);
3009 static int mult_up(int n, int f)
3011 return ((n + f - 1)/f)*f;
3015 static double pme_load_imbalance(gmx_pme_t pme)
3020 nma = pme->nnodes_major;
3021 nmi = pme->nnodes_minor;
3023 n1 = mult_up(pme->nkx, nma)*mult_up(pme->nky, nmi)*pme->nkz;
3024 n2 = mult_up(pme->nkx, nma)*mult_up(pme->nkz, nmi)*pme->nky;
3025 n3 = mult_up(pme->nky, nma)*mult_up(pme->nkz, nmi)*pme->nkx;
3027 /* pme_solve is roughly double the cost of an fft */
3029 return (n1 + n2 + 3*n3)/(double)(6*pme->nkx*pme->nky*pme->nkz);
3032 static void init_atomcomm(gmx_pme_t pme, pme_atomcomm_t *atc,
3033 int dimind, gmx_bool bSpread)
3035 int nk, k, s, thread;
3037 atc->dimind = dimind;
3042 if (pme->nnodes > 1)
3044 atc->mpi_comm = pme->mpi_comm_d[dimind];
3045 MPI_Comm_size(atc->mpi_comm, &atc->nslab);
3046 MPI_Comm_rank(atc->mpi_comm, &atc->nodeid);
3050 fprintf(debug, "For PME atom communication in dimind %d: nslab %d rank %d\n", atc->dimind, atc->nslab, atc->nodeid);
3054 atc->bSpread = bSpread;
3055 atc->pme_order = pme->pme_order;
3059 snew(atc->node_dest, atc->nslab);
3060 snew(atc->node_src, atc->nslab);
3061 setup_coordinate_communication(atc);
3063 snew(atc->count_thread, pme->nthread);
3064 for (thread = 0; thread < pme->nthread; thread++)
3066 snew(atc->count_thread[thread], atc->nslab);
3068 atc->count = atc->count_thread[0];
3069 snew(atc->rcount, atc->nslab);
3070 snew(atc->buf_index, atc->nslab);
3073 atc->nthread = pme->nthread;
3074 if (atc->nthread > 1)
3076 snew(atc->thread_plist, atc->nthread);
3078 snew(atc->spline, atc->nthread);
3079 for (thread = 0; thread < atc->nthread; thread++)
3081 if (atc->nthread > 1)
3083 snew(atc->thread_plist[thread].n, atc->nthread+2*GMX_CACHE_SEP);
3084 atc->thread_plist[thread].n += GMX_CACHE_SEP;
3086 snew(atc->spline[thread].thread_one, pme->nthread);
3087 atc->spline[thread].thread_one[thread] = 1;
3092 init_overlap_comm(pme_overlap_t * ol,
3102 int lbnd, rbnd, maxlr, b, i;
3105 pme_grid_comm_t *pgc;
3107 int fft_start, fft_end, send_index1, recv_index1;
3111 ol->mpi_comm = comm;
3114 ol->nnodes = nnodes;
3115 ol->nodeid = nodeid;
3117 /* Linear translation of the PME grid won't affect reciprocal space
3118 * calculations, so to optimize we only interpolate "upwards",
3119 * which also means we only have to consider overlap in one direction.
3120 * I.e., particles on this node might also be spread to grid indices
3121 * that belong to higher nodes (modulo nnodes)
3124 snew(ol->s2g0, ol->nnodes+1);
3125 snew(ol->s2g1, ol->nnodes);
3128 fprintf(debug, "PME slab boundaries:");
3130 for (i = 0; i < nnodes; i++)
3132 /* s2g0 the local interpolation grid start.
3133 * s2g1 the local interpolation grid end.
3134 * Because grid overlap communication only goes forward,
3135 * the grid the slabs for fft's should be rounded down.
3137 ol->s2g0[i] = ( i *ndata + 0 )/nnodes;
3138 ol->s2g1[i] = ((i+1)*ndata + nnodes-1)/nnodes + norder - 1;
3142 fprintf(debug, " %3d %3d", ol->s2g0[i], ol->s2g1[i]);
3145 ol->s2g0[nnodes] = ndata;
3148 fprintf(debug, "\n");
3151 /* Determine with how many nodes we need to communicate the grid overlap */
3157 for (i = 0; i < nnodes; i++)
3159 if ((i+b < nnodes && ol->s2g1[i] > ol->s2g0[i+b]) ||
3160 (i+b >= nnodes && ol->s2g1[i] > ol->s2g0[i+b-nnodes] + ndata))
3166 while (bCont && b < nnodes);
3167 ol->noverlap_nodes = b - 1;
3169 snew(ol->send_id, ol->noverlap_nodes);
3170 snew(ol->recv_id, ol->noverlap_nodes);
3171 for (b = 0; b < ol->noverlap_nodes; b++)
3173 ol->send_id[b] = (ol->nodeid + (b + 1)) % ol->nnodes;
3174 ol->recv_id[b] = (ol->nodeid - (b + 1) + ol->nnodes) % ol->nnodes;
3176 snew(ol->comm_data, ol->noverlap_nodes);
3179 for (b = 0; b < ol->noverlap_nodes; b++)
3181 pgc = &ol->comm_data[b];
3183 fft_start = ol->s2g0[ol->send_id[b]];
3184 fft_end = ol->s2g0[ol->send_id[b]+1];
3185 if (ol->send_id[b] < nodeid)
3190 send_index1 = ol->s2g1[nodeid];
3191 send_index1 = min(send_index1, fft_end);
3192 pgc->send_index0 = fft_start;
3193 pgc->send_nindex = max(0, send_index1 - pgc->send_index0);
3194 ol->send_size += pgc->send_nindex;
3196 /* We always start receiving to the first index of our slab */
3197 fft_start = ol->s2g0[ol->nodeid];
3198 fft_end = ol->s2g0[ol->nodeid+1];
3199 recv_index1 = ol->s2g1[ol->recv_id[b]];
3200 if (ol->recv_id[b] > nodeid)
3202 recv_index1 -= ndata;
3204 recv_index1 = min(recv_index1, fft_end);
3205 pgc->recv_index0 = fft_start;
3206 pgc->recv_nindex = max(0, recv_index1 - pgc->recv_index0);
3210 /* Communicate the buffer sizes to receive */
3211 for (b = 0; b < ol->noverlap_nodes; b++)
3213 MPI_Sendrecv(&ol->send_size, 1, MPI_INT, ol->send_id[b], b,
3214 &ol->comm_data[b].recv_size, 1, MPI_INT, ol->recv_id[b], b,
3215 ol->mpi_comm, &stat);
3219 /* For non-divisible grid we need pme_order iso pme_order-1 */
3220 snew(ol->sendbuf, norder*commplainsize);
3221 snew(ol->recvbuf, norder*commplainsize);
3225 make_gridindex5_to_localindex(int n, int local_start, int local_range,
3226 int **global_to_local,
3227 real **fraction_shift)
3235 for (i = 0; (i < 5*n); i++)
3237 /* Determine the global to local grid index */
3238 gtl[i] = (i - local_start + n) % n;
3239 /* For coordinates that fall within the local grid the fraction
3240 * is correct, we don't need to shift it.
3243 if (local_range < n)
3245 /* Due to rounding issues i could be 1 beyond the lower or
3246 * upper boundary of the local grid. Correct the index for this.
3247 * If we shift the index, we need to shift the fraction by
3248 * the same amount in the other direction to not affect
3250 * Note that due to this shifting the weights at the end of
3251 * the spline might change, but that will only involve values
3252 * between zero and values close to the precision of a real,
3253 * which is anyhow the accuracy of the whole mesh calculation.
3255 /* With local_range=0 we should not change i=local_start */
3256 if (i % n != local_start)
3263 else if (gtl[i] == local_range)
3265 gtl[i] = local_range - 1;
3272 *global_to_local = gtl;
3273 *fraction_shift = fsh;
3276 static pme_spline_work_t *make_pme_spline_work(int gmx_unused order)
3278 pme_spline_work_t *work;
3280 #ifdef PME_SIMD4_SPREAD_GATHER
3281 real tmp[GMX_SIMD4_WIDTH*3], *tmp_aligned;
3282 gmx_simd4_real_t zero_S;
3283 gmx_simd4_real_t real_mask_S0, real_mask_S1;
3286 snew_aligned(work, 1, SIMD4_ALIGNMENT);
3288 tmp_aligned = gmx_simd4_align_r(tmp);
3290 zero_S = gmx_simd4_setzero_r();
3292 /* Generate bit masks to mask out the unused grid entries,
3293 * as we only operate on order of the 8 grid entries that are
3294 * load into 2 SIMD registers.
3296 for (of = 0; of < 2*GMX_SIMD4_WIDTH-(order-1); of++)
3298 for (i = 0; i < 2*GMX_SIMD4_WIDTH; i++)
3300 tmp_aligned[i] = (i >= of && i < of+order ? -1.0 : 1.0);
3302 real_mask_S0 = gmx_simd4_load_r(tmp_aligned);
3303 real_mask_S1 = gmx_simd4_load_r(tmp_aligned+GMX_SIMD4_WIDTH);
3304 work->mask_S0[of] = gmx_simd4_cmplt_r(real_mask_S0, zero_S);
3305 work->mask_S1[of] = gmx_simd4_cmplt_r(real_mask_S1, zero_S);
3314 void gmx_pme_check_restrictions(int pme_order,
3315 int nkx, int nky, int nkz,
3318 gmx_bool bUseThreads,
3320 gmx_bool *bValidSettings)
3322 if (pme_order > PME_ORDER_MAX)
3326 *bValidSettings = FALSE;
3329 gmx_fatal(FARGS, "pme_order (%d) is larger than the maximum allowed value (%d). Modify and recompile the code if you really need such a high order.",
3330 pme_order, PME_ORDER_MAX);
3333 if (nkx <= pme_order*(nnodes_major > 1 ? 2 : 1) ||
3334 nky <= pme_order*(nnodes_minor > 1 ? 2 : 1) ||
3339 *bValidSettings = FALSE;
3342 gmx_fatal(FARGS, "The PME grid sizes need to be larger than pme_order (%d) and for dimensions with domain decomposition larger than 2*pme_order",
3346 /* Check for a limitation of the (current) sum_fftgrid_dd code.
3347 * We only allow multiple communication pulses in dim 1, not in dim 0.
3349 if (bUseThreads && (nkx < nnodes_major*pme_order &&
3350 nkx != nnodes_major*(pme_order - 1)))
3354 *bValidSettings = FALSE;
3357 gmx_fatal(FARGS, "The number of PME grid lines per node along x is %g. But when using OpenMP threads, the number of grid lines per node along x should be >= pme_order (%d) or = pmeorder-1. To resolve this issue, use less nodes along x (and possibly more along y and/or z) by specifying -dd manually.",
3358 nkx/(double)nnodes_major, pme_order);
3361 if (bValidSettings != NULL)
3363 *bValidSettings = TRUE;
3369 int gmx_pme_init(gmx_pme_t * pmedata,
3375 gmx_bool bFreeEnergy_q,
3376 gmx_bool bFreeEnergy_lj,
3377 gmx_bool bReproducible,
3380 gmx_pme_t pme = NULL;
3382 int use_threads, sum_use_threads, i;
3387 fprintf(debug, "Creating PME data structures.\n");
3391 pme->sum_qgrid_tmp = NULL;
3392 pme->sum_qgrid_dd_tmp = NULL;
3393 pme->buf_nalloc = 0;
3396 pme->bPPnode = TRUE;
3398 pme->nnodes_major = nnodes_major;
3399 pme->nnodes_minor = nnodes_minor;
3402 if (nnodes_major*nnodes_minor > 1)
3404 pme->mpi_comm = cr->mpi_comm_mygroup;
3406 MPI_Comm_rank(pme->mpi_comm, &pme->nodeid);
3407 MPI_Comm_size(pme->mpi_comm, &pme->nnodes);
3408 if (pme->nnodes != nnodes_major*nnodes_minor)
3410 gmx_incons("PME node count mismatch");
3415 pme->mpi_comm = MPI_COMM_NULL;
3419 if (pme->nnodes == 1)
3422 pme->mpi_comm_d[0] = MPI_COMM_NULL;
3423 pme->mpi_comm_d[1] = MPI_COMM_NULL;
3425 pme->ndecompdim = 0;
3426 pme->nodeid_major = 0;
3427 pme->nodeid_minor = 0;
3429 pme->mpi_comm_d[0] = pme->mpi_comm_d[1] = MPI_COMM_NULL;
3434 if (nnodes_minor == 1)
3437 pme->mpi_comm_d[0] = pme->mpi_comm;
3438 pme->mpi_comm_d[1] = MPI_COMM_NULL;
3440 pme->ndecompdim = 1;
3441 pme->nodeid_major = pme->nodeid;
3442 pme->nodeid_minor = 0;
3445 else if (nnodes_major == 1)
3448 pme->mpi_comm_d[0] = MPI_COMM_NULL;
3449 pme->mpi_comm_d[1] = pme->mpi_comm;
3451 pme->ndecompdim = 1;
3452 pme->nodeid_major = 0;
3453 pme->nodeid_minor = pme->nodeid;
3457 if (pme->nnodes % nnodes_major != 0)
3459 gmx_incons("For 2D PME decomposition, #PME nodes must be divisible by the number of nodes in the major dimension");
3461 pme->ndecompdim = 2;
3464 MPI_Comm_split(pme->mpi_comm, pme->nodeid % nnodes_minor,
3465 pme->nodeid, &pme->mpi_comm_d[0]); /* My communicator along major dimension */
3466 MPI_Comm_split(pme->mpi_comm, pme->nodeid/nnodes_minor,
3467 pme->nodeid, &pme->mpi_comm_d[1]); /* My communicator along minor dimension */
3469 MPI_Comm_rank(pme->mpi_comm_d[0], &pme->nodeid_major);
3470 MPI_Comm_size(pme->mpi_comm_d[0], &pme->nnodes_major);
3471 MPI_Comm_rank(pme->mpi_comm_d[1], &pme->nodeid_minor);
3472 MPI_Comm_size(pme->mpi_comm_d[1], &pme->nnodes_minor);
3475 pme->bPPnode = (cr->duty & DUTY_PP);
3478 pme->nthread = nthread;
3480 /* Check if any of the PME MPI ranks uses threads */
3481 use_threads = (pme->nthread > 1 ? 1 : 0);
3483 if (pme->nnodes > 1)
3485 MPI_Allreduce(&use_threads, &sum_use_threads, 1, MPI_INT,
3486 MPI_SUM, pme->mpi_comm);
3491 sum_use_threads = use_threads;
3493 pme->bUseThreads = (sum_use_threads > 0);
3495 if (ir->ePBC == epbcSCREW)
3497 gmx_fatal(FARGS, "pme does not (yet) work with pbc = screw");
3500 pme->bFEP_q = ((ir->efep != efepNO) && bFreeEnergy_q);
3501 pme->bFEP_lj = ((ir->efep != efepNO) && bFreeEnergy_lj);
3502 pme->bFEP = (pme->bFEP_q || pme->bFEP_lj);
3506 pme->bP3M = (ir->coulombtype == eelP3M_AD || getenv("GMX_PME_P3M") != NULL);
3507 pme->pme_order = ir->pme_order;
3509 /* Always constant electrostatics coefficients */
3510 pme->epsilon_r = ir->epsilon_r;
3512 /* Always constant LJ coefficients */
3513 pme->ljpme_combination_rule = ir->ljpme_combination_rule;
3515 /* If we violate restrictions, generate a fatal error here */
3516 gmx_pme_check_restrictions(pme->pme_order,
3517 pme->nkx, pme->nky, pme->nkz,
3524 if (pme->nnodes > 1)
3529 MPI_Type_contiguous(DIM, mpi_type, &(pme->rvec_mpi));
3530 MPI_Type_commit(&(pme->rvec_mpi));
3533 /* Note that the coefficient spreading and force gathering, which usually
3534 * takes about the same amount of time as FFT+solve_pme,
3535 * is always fully load balanced
3536 * (unless the coefficient distribution is inhomogeneous).
3539 imbal = pme_load_imbalance(pme);
3540 if (imbal >= 1.2 && pme->nodeid_major == 0 && pme->nodeid_minor == 0)
3544 "NOTE: The load imbalance in PME FFT and solve is %d%%.\n"
3545 " For optimal PME load balancing\n"
3546 " PME grid_x (%d) and grid_y (%d) should be divisible by #PME_nodes_x (%d)\n"
3547 " and PME grid_y (%d) and grid_z (%d) should be divisible by #PME_nodes_y (%d)\n"
3549 (int)((imbal-1)*100 + 0.5),
3550 pme->nkx, pme->nky, pme->nnodes_major,
3551 pme->nky, pme->nkz, pme->nnodes_minor);
3555 /* For non-divisible grid we need pme_order iso pme_order-1 */
3556 /* In sum_qgrid_dd x overlap is copied in place: take padding into account.
3557 * y is always copied through a buffer: we don't need padding in z,
3558 * but we do need the overlap in x because of the communication order.
3560 init_overlap_comm(&pme->overlap[0], pme->pme_order,
3564 pme->nnodes_major, pme->nodeid_major,
3566 (div_round_up(pme->nky, pme->nnodes_minor)+pme->pme_order)*(pme->nkz+pme->pme_order-1));
3568 /* Along overlap dim 1 we can send in multiple pulses in sum_fftgrid_dd.
3569 * We do this with an offset buffer of equal size, so we need to allocate
3570 * extra for the offset. That's what the (+1)*pme->nkz is for.
3572 init_overlap_comm(&pme->overlap[1], pme->pme_order,
3576 pme->nnodes_minor, pme->nodeid_minor,
3578 (div_round_up(pme->nkx, pme->nnodes_major)+pme->pme_order+1)*pme->nkz);
3580 /* Double-check for a limitation of the (current) sum_fftgrid_dd code.
3581 * Note that gmx_pme_check_restrictions checked for this already.
3583 if (pme->bUseThreads && pme->overlap[0].noverlap_nodes > 1)
3585 gmx_incons("More than one communication pulse required for grid overlap communication along the major dimension while using threads");
3588 snew(pme->bsp_mod[XX], pme->nkx);
3589 snew(pme->bsp_mod[YY], pme->nky);
3590 snew(pme->bsp_mod[ZZ], pme->nkz);
3592 /* The required size of the interpolation grid, including overlap.
3593 * The allocated size (pmegrid_n?) might be slightly larger.
3595 pme->pmegrid_nx = pme->overlap[0].s2g1[pme->nodeid_major] -
3596 pme->overlap[0].s2g0[pme->nodeid_major];
3597 pme->pmegrid_ny = pme->overlap[1].s2g1[pme->nodeid_minor] -
3598 pme->overlap[1].s2g0[pme->nodeid_minor];
3599 pme->pmegrid_nz_base = pme->nkz;
3600 pme->pmegrid_nz = pme->pmegrid_nz_base + pme->pme_order - 1;
3601 set_grid_alignment(&pme->pmegrid_nz, pme->pme_order);
3603 pme->pmegrid_start_ix = pme->overlap[0].s2g0[pme->nodeid_major];
3604 pme->pmegrid_start_iy = pme->overlap[1].s2g0[pme->nodeid_minor];
3605 pme->pmegrid_start_iz = 0;
3607 make_gridindex5_to_localindex(pme->nkx,
3608 pme->pmegrid_start_ix,
3609 pme->pmegrid_nx - (pme->pme_order-1),
3610 &pme->nnx, &pme->fshx);
3611 make_gridindex5_to_localindex(pme->nky,
3612 pme->pmegrid_start_iy,
3613 pme->pmegrid_ny - (pme->pme_order-1),
3614 &pme->nny, &pme->fshy);
3615 make_gridindex5_to_localindex(pme->nkz,
3616 pme->pmegrid_start_iz,
3617 pme->pmegrid_nz_base,
3618 &pme->nnz, &pme->fshz);
3620 pme->spline_work = make_pme_spline_work(pme->pme_order);
3622 ndata[0] = pme->nkx;
3623 ndata[1] = pme->nky;
3624 ndata[2] = pme->nkz;
3625 /* It doesn't matter if we allocate too many grids here,
3626 * we only allocate and use the ones we need.
3628 if (EVDW_PME(ir->vdwtype))
3630 pme->ngrids = ((ir->ljpme_combination_rule == eljpmeLB) ? DO_Q_AND_LJ_LB : DO_Q_AND_LJ);
3636 snew(pme->fftgrid, pme->ngrids);
3637 snew(pme->cfftgrid, pme->ngrids);
3638 snew(pme->pfft_setup, pme->ngrids);
3640 for (i = 0; i < pme->ngrids; ++i)
3642 if ((i < DO_Q && EEL_PME(ir->coulombtype) && (i == 0 ||
3644 (i >= DO_Q && EVDW_PME(ir->vdwtype) && (i == 2 ||
3646 ir->ljpme_combination_rule == eljpmeLB)))
3648 pmegrids_init(&pme->pmegrid[i],
3649 pme->pmegrid_nx, pme->pmegrid_ny, pme->pmegrid_nz,
3650 pme->pmegrid_nz_base,
3654 pme->overlap[0].s2g1[pme->nodeid_major]-pme->overlap[0].s2g0[pme->nodeid_major+1],
3655 pme->overlap[1].s2g1[pme->nodeid_minor]-pme->overlap[1].s2g0[pme->nodeid_minor+1]);
3656 /* This routine will allocate the grid data to fit the FFTs */
3657 gmx_parallel_3dfft_init(&pme->pfft_setup[i], ndata,
3658 &pme->fftgrid[i], &pme->cfftgrid[i],
3660 bReproducible, pme->nthread);
3667 /* Use plain SPME B-spline interpolation */
3668 make_bspline_moduli(pme->bsp_mod, pme->nkx, pme->nky, pme->nkz, pme->pme_order);
3672 /* Use the P3M grid-optimized influence function */
3673 make_p3m_bspline_moduli(pme->bsp_mod, pme->nkx, pme->nky, pme->nkz, pme->pme_order);
3676 /* Use atc[0] for spreading */
3677 init_atomcomm(pme, &pme->atc[0], nnodes_major > 1 ? 0 : 1, TRUE);
3678 if (pme->ndecompdim >= 2)
3680 init_atomcomm(pme, &pme->atc[1], 1, FALSE);
3683 if (pme->nnodes == 1)
3685 pme->atc[0].n = homenr;
3686 pme_realloc_atomcomm_things(&pme->atc[0]);
3689 pme->lb_buf1 = NULL;
3690 pme->lb_buf2 = NULL;
3691 pme->lb_buf_nalloc = 0;
3696 /* Use fft5d, order after FFT is y major, z, x minor */
3698 snew(pme->work, pme->nthread);
3699 for (thread = 0; thread < pme->nthread; thread++)
3701 realloc_work(&pme->work[thread], pme->nkx);
3710 static void reuse_pmegrids(const pmegrids_t *old, pmegrids_t *new)
3714 for (d = 0; d < DIM; d++)
3716 if (new->grid.n[d] > old->grid.n[d])
3722 sfree_aligned(new->grid.grid);
3723 new->grid.grid = old->grid.grid;
3725 if (new->grid_th != NULL && new->nthread == old->nthread)
3727 sfree_aligned(new->grid_all);
3728 for (t = 0; t < new->nthread; t++)
3730 new->grid_th[t].grid = old->grid_th[t].grid;
3735 int gmx_pme_reinit(gmx_pme_t * pmedata,
3738 const t_inputrec * ir,
3746 irc.nkx = grid_size[XX];
3747 irc.nky = grid_size[YY];
3748 irc.nkz = grid_size[ZZ];
3750 if (pme_src->nnodes == 1)
3752 homenr = pme_src->atc[0].n;
3759 ret = gmx_pme_init(pmedata, cr, pme_src->nnodes_major, pme_src->nnodes_minor,
3760 &irc, homenr, pme_src->bFEP_q, pme_src->bFEP_lj, FALSE, pme_src->nthread);
3764 /* We can easily reuse the allocated pme grids in pme_src */
3765 reuse_pmegrids(&pme_src->pmegrid[PME_GRID_QA], &(*pmedata)->pmegrid[PME_GRID_QA]);
3766 /* We would like to reuse the fft grids, but that's harder */
3773 static void copy_local_grid(gmx_pme_t pme, pmegrids_t *pmegrids,
3774 int grid_index, int thread, real *fftgrid)
3776 ivec local_fft_ndata, local_fft_offset, local_fft_size;
3780 int offx, offy, offz, x, y, z, i0, i0t;
3785 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
3789 fft_my = local_fft_size[YY];
3790 fft_mz = local_fft_size[ZZ];
3792 pmegrid = &pmegrids->grid_th[thread];
3794 nsx = pmegrid->s[XX];
3795 nsy = pmegrid->s[YY];
3796 nsz = pmegrid->s[ZZ];
3798 for (d = 0; d < DIM; d++)
3800 nf[d] = min(pmegrid->n[d] - (pmegrid->order - 1),
3801 local_fft_ndata[d] - pmegrid->offset[d]);
3804 offx = pmegrid->offset[XX];
3805 offy = pmegrid->offset[YY];
3806 offz = pmegrid->offset[ZZ];
3808 /* Directly copy the non-overlapping parts of the local grids.
3809 * This also initializes the full grid.
3811 grid_th = pmegrid->grid;
3812 for (x = 0; x < nf[XX]; x++)
3814 for (y = 0; y < nf[YY]; y++)
3816 i0 = ((offx + x)*fft_my + (offy + y))*fft_mz + offz;
3817 i0t = (x*nsy + y)*nsz;
3818 for (z = 0; z < nf[ZZ]; z++)
3820 fftgrid[i0+z] = grid_th[i0t+z];
3827 reduce_threadgrid_overlap(gmx_pme_t pme,
3828 const pmegrids_t *pmegrids, int thread,
3829 real *fftgrid, real *commbuf_x, real *commbuf_y,
3832 ivec local_fft_ndata, local_fft_offset, local_fft_size;
3833 int fft_nx, fft_ny, fft_nz;
3838 int offx, offy, offz, x, y, z, i0, i0t;
3839 int sx, sy, sz, fx, fy, fz, tx1, ty1, tz1, ox, oy, oz;
3840 gmx_bool bClearBufX, bClearBufY, bClearBufXY, bClearBuf;
3841 gmx_bool bCommX, bCommY;
3844 const pmegrid_t *pmegrid, *pmegrid_g, *pmegrid_f;
3845 const real *grid_th;
3846 real *commbuf = NULL;
3848 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
3852 fft_nx = local_fft_ndata[XX];
3853 fft_ny = local_fft_ndata[YY];
3854 fft_nz = local_fft_ndata[ZZ];
3856 fft_my = local_fft_size[YY];
3857 fft_mz = local_fft_size[ZZ];
3859 /* This routine is called when all thread have finished spreading.
3860 * Here each thread sums grid contributions calculated by other threads
3861 * to the thread local grid volume.
3862 * To minimize the number of grid copying operations,
3863 * this routines sums immediately from the pmegrid to the fftgrid.
3866 /* Determine which part of the full node grid we should operate on,
3867 * this is our thread local part of the full grid.
3869 pmegrid = &pmegrids->grid_th[thread];
3871 for (d = 0; d < DIM; d++)
3873 ne[d] = min(pmegrid->offset[d]+pmegrid->n[d]-(pmegrid->order-1),
3874 local_fft_ndata[d]);
3877 offx = pmegrid->offset[XX];
3878 offy = pmegrid->offset[YY];
3879 offz = pmegrid->offset[ZZ];
3886 /* Now loop over all the thread data blocks that contribute
3887 * to the grid region we (our thread) are operating on.
3889 /* Note that ffy_nx/y is equal to the number of grid points
3890 * between the first point of our node grid and the one of the next node.
3892 for (sx = 0; sx >= -pmegrids->nthread_comm[XX]; sx--)
3894 fx = pmegrid->ci[XX] + sx;
3899 fx += pmegrids->nc[XX];
3901 bCommX = (pme->nnodes_major > 1);
3903 pmegrid_g = &pmegrids->grid_th[fx*pmegrids->nc[YY]*pmegrids->nc[ZZ]];
3904 ox += pmegrid_g->offset[XX];
3907 tx1 = min(ox + pmegrid_g->n[XX], ne[XX]);
3911 tx1 = min(ox + pmegrid_g->n[XX], pme->pme_order);
3914 for (sy = 0; sy >= -pmegrids->nthread_comm[YY]; sy--)
3916 fy = pmegrid->ci[YY] + sy;
3921 fy += pmegrids->nc[YY];
3923 bCommY = (pme->nnodes_minor > 1);
3925 pmegrid_g = &pmegrids->grid_th[fy*pmegrids->nc[ZZ]];
3926 oy += pmegrid_g->offset[YY];
3929 ty1 = min(oy + pmegrid_g->n[YY], ne[YY]);
3933 ty1 = min(oy + pmegrid_g->n[YY], pme->pme_order);
3936 for (sz = 0; sz >= -pmegrids->nthread_comm[ZZ]; sz--)
3938 fz = pmegrid->ci[ZZ] + sz;
3942 fz += pmegrids->nc[ZZ];
3945 pmegrid_g = &pmegrids->grid_th[fz];
3946 oz += pmegrid_g->offset[ZZ];
3947 tz1 = min(oz + pmegrid_g->n[ZZ], ne[ZZ]);
3949 if (sx == 0 && sy == 0 && sz == 0)
3951 /* We have already added our local contribution
3952 * before calling this routine, so skip it here.
3957 thread_f = (fx*pmegrids->nc[YY] + fy)*pmegrids->nc[ZZ] + fz;
3959 pmegrid_f = &pmegrids->grid_th[thread_f];
3961 grid_th = pmegrid_f->grid;
3963 nsx = pmegrid_f->s[XX];
3964 nsy = pmegrid_f->s[YY];
3965 nsz = pmegrid_f->s[ZZ];
3967 #ifdef DEBUG_PME_REDUCE
3968 printf("n%d t%d add %d %2d %2d %2d %2d %2d %2d %2d-%2d %2d-%2d, %2d-%2d %2d-%2d, %2d-%2d %2d-%2d\n",
3969 pme->nodeid, thread, thread_f,
3970 pme->pmegrid_start_ix,
3971 pme->pmegrid_start_iy,
3972 pme->pmegrid_start_iz,
3974 offx-ox, tx1-ox, offx, tx1,
3975 offy-oy, ty1-oy, offy, ty1,
3976 offz-oz, tz1-oz, offz, tz1);
3979 if (!(bCommX || bCommY))
3981 /* Copy from the thread local grid to the node grid */
3982 for (x = offx; x < tx1; x++)
3984 for (y = offy; y < ty1; y++)
3986 i0 = (x*fft_my + y)*fft_mz;
3987 i0t = ((x - ox)*nsy + (y - oy))*nsz - oz;
3988 for (z = offz; z < tz1; z++)
3990 fftgrid[i0+z] += grid_th[i0t+z];
3997 /* The order of this conditional decides
3998 * where the corner volume gets stored with x+y decomp.
4002 commbuf = commbuf_y;
4003 buf_my = ty1 - offy;
4006 /* We index commbuf modulo the local grid size */
4007 commbuf += buf_my*fft_nx*fft_nz;
4009 bClearBuf = bClearBufXY;
4010 bClearBufXY = FALSE;
4014 bClearBuf = bClearBufY;
4020 commbuf = commbuf_x;
4022 bClearBuf = bClearBufX;
4026 /* Copy to the communication buffer */
4027 for (x = offx; x < tx1; x++)
4029 for (y = offy; y < ty1; y++)
4031 i0 = (x*buf_my + y)*fft_nz;
4032 i0t = ((x - ox)*nsy + (y - oy))*nsz - oz;
4036 /* First access of commbuf, initialize it */
4037 for (z = offz; z < tz1; z++)
4039 commbuf[i0+z] = grid_th[i0t+z];
4044 for (z = offz; z < tz1; z++)
4046 commbuf[i0+z] += grid_th[i0t+z];
4058 static void sum_fftgrid_dd(gmx_pme_t pme, real *fftgrid, int grid_index)
4060 ivec local_fft_ndata, local_fft_offset, local_fft_size;
4061 pme_overlap_t *overlap;
4062 int send_index0, send_nindex;
4067 int send_size_y, recv_size_y;
4068 int ipulse, send_id, recv_id, datasize, gridsize, size_yx;
4069 real *sendptr, *recvptr;
4070 int x, y, z, indg, indb;
4072 /* Note that this routine is only used for forward communication.
4073 * Since the force gathering, unlike the coefficient spreading,
4074 * can be trivially parallelized over the particles,
4075 * the backwards process is much simpler and can use the "old"
4076 * communication setup.
4079 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
4084 if (pme->nnodes_minor > 1)
4086 /* Major dimension */
4087 overlap = &pme->overlap[1];
4089 if (pme->nnodes_major > 1)
4091 size_yx = pme->overlap[0].comm_data[0].send_nindex;
4097 datasize = (local_fft_ndata[XX] + size_yx)*local_fft_ndata[ZZ];
4099 send_size_y = overlap->send_size;
4101 for (ipulse = 0; ipulse < overlap->noverlap_nodes; ipulse++)
4103 send_id = overlap->send_id[ipulse];
4104 recv_id = overlap->recv_id[ipulse];
4106 overlap->comm_data[ipulse].send_index0 -
4107 overlap->comm_data[0].send_index0;
4108 send_nindex = overlap->comm_data[ipulse].send_nindex;
4109 /* We don't use recv_index0, as we always receive starting at 0 */
4110 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
4111 recv_size_y = overlap->comm_data[ipulse].recv_size;
4113 sendptr = overlap->sendbuf + send_index0*local_fft_ndata[ZZ];
4114 recvptr = overlap->recvbuf;
4117 MPI_Sendrecv(sendptr, send_size_y*datasize, GMX_MPI_REAL,
4119 recvptr, recv_size_y*datasize, GMX_MPI_REAL,
4121 overlap->mpi_comm, &stat);
4124 for (x = 0; x < local_fft_ndata[XX]; x++)
4126 for (y = 0; y < recv_nindex; y++)
4128 indg = (x*local_fft_size[YY] + y)*local_fft_size[ZZ];
4129 indb = (x*recv_size_y + y)*local_fft_ndata[ZZ];
4130 for (z = 0; z < local_fft_ndata[ZZ]; z++)
4132 fftgrid[indg+z] += recvptr[indb+z];
4137 if (pme->nnodes_major > 1)
4139 /* Copy from the received buffer to the send buffer for dim 0 */
4140 sendptr = pme->overlap[0].sendbuf;
4141 for (x = 0; x < size_yx; x++)
4143 for (y = 0; y < recv_nindex; y++)
4145 indg = (x*local_fft_ndata[YY] + y)*local_fft_ndata[ZZ];
4146 indb = ((local_fft_ndata[XX] + x)*recv_size_y + y)*local_fft_ndata[ZZ];
4147 for (z = 0; z < local_fft_ndata[ZZ]; z++)
4149 sendptr[indg+z] += recvptr[indb+z];
4157 /* We only support a single pulse here.
4158 * This is not a severe limitation, as this code is only used
4159 * with OpenMP and with OpenMP the (PME) domains can be larger.
4161 if (pme->nnodes_major > 1)
4163 /* Major dimension */
4164 overlap = &pme->overlap[0];
4166 datasize = local_fft_ndata[YY]*local_fft_ndata[ZZ];
4167 gridsize = local_fft_size[YY] *local_fft_size[ZZ];
4171 send_id = overlap->send_id[ipulse];
4172 recv_id = overlap->recv_id[ipulse];
4173 send_nindex = overlap->comm_data[ipulse].send_nindex;
4174 /* We don't use recv_index0, as we always receive starting at 0 */
4175 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
4177 sendptr = overlap->sendbuf;
4178 recvptr = overlap->recvbuf;
4182 fprintf(debug, "PME fftgrid comm %2d x %2d x %2d\n",
4183 send_nindex, local_fft_ndata[YY], local_fft_ndata[ZZ]);
4187 MPI_Sendrecv(sendptr, send_nindex*datasize, GMX_MPI_REAL,
4189 recvptr, recv_nindex*datasize, GMX_MPI_REAL,
4191 overlap->mpi_comm, &stat);
4194 for (x = 0; x < recv_nindex; x++)
4196 for (y = 0; y < local_fft_ndata[YY]; y++)
4198 indg = (x*local_fft_size[YY] + y)*local_fft_size[ZZ];
4199 indb = (x*local_fft_ndata[YY] + y)*local_fft_ndata[ZZ];
4200 for (z = 0; z < local_fft_ndata[ZZ]; z++)
4202 fftgrid[indg+z] += recvptr[indb+z];
4210 static void spread_on_grid(gmx_pme_t pme,
4211 pme_atomcomm_t *atc, pmegrids_t *grids,
4212 gmx_bool bCalcSplines, gmx_bool bSpread,
4213 real *fftgrid, gmx_bool bDoSplines, int grid_index)
4215 int nthread, thread;
4216 #ifdef PME_TIME_THREADS
4217 gmx_cycles_t c1, c2, c3, ct1a, ct1b, ct1c;
4218 static double cs1 = 0, cs2 = 0, cs3 = 0;
4219 static double cs1a[6] = {0, 0, 0, 0, 0, 0};
4223 nthread = pme->nthread;
4224 assert(nthread > 0);
4226 #ifdef PME_TIME_THREADS
4227 c1 = omp_cyc_start();
4231 #pragma omp parallel for num_threads(nthread) schedule(static)
4232 for (thread = 0; thread < nthread; thread++)
4236 start = atc->n* thread /nthread;
4237 end = atc->n*(thread+1)/nthread;
4239 /* Compute fftgrid index for all atoms,
4240 * with help of some extra variables.
4242 calc_interpolation_idx(pme, atc, start, grid_index, end, thread);
4245 #ifdef PME_TIME_THREADS
4246 c1 = omp_cyc_end(c1);
4250 #ifdef PME_TIME_THREADS
4251 c2 = omp_cyc_start();
4253 #pragma omp parallel for num_threads(nthread) schedule(static)
4254 for (thread = 0; thread < nthread; thread++)
4256 splinedata_t *spline;
4257 pmegrid_t *grid = NULL;
4259 /* make local bsplines */
4260 if (grids == NULL || !pme->bUseThreads)
4262 spline = &atc->spline[0];
4268 grid = &grids->grid;
4273 spline = &atc->spline[thread];
4275 if (grids->nthread == 1)
4277 /* One thread, we operate on all coefficients */
4282 /* Get the indices our thread should operate on */
4283 make_thread_local_ind(atc, thread, spline);
4286 grid = &grids->grid_th[thread];
4291 make_bsplines(spline->theta, spline->dtheta, pme->pme_order,
4292 atc->fractx, spline->n, spline->ind, atc->coefficient, bDoSplines);
4297 /* put local atoms on grid. */
4298 #ifdef PME_TIME_SPREAD
4299 ct1a = omp_cyc_start();
4301 spread_coefficients_bsplines_thread(grid, atc, spline, pme->spline_work);
4303 if (pme->bUseThreads)
4305 copy_local_grid(pme, grids, grid_index, thread, fftgrid);
4307 #ifdef PME_TIME_SPREAD
4308 ct1a = omp_cyc_end(ct1a);
4309 cs1a[thread] += (double)ct1a;
4313 #ifdef PME_TIME_THREADS
4314 c2 = omp_cyc_end(c2);
4318 if (bSpread && pme->bUseThreads)
4320 #ifdef PME_TIME_THREADS
4321 c3 = omp_cyc_start();
4323 #pragma omp parallel for num_threads(grids->nthread) schedule(static)
4324 for (thread = 0; thread < grids->nthread; thread++)
4326 reduce_threadgrid_overlap(pme, grids, thread,
4328 pme->overlap[0].sendbuf,
4329 pme->overlap[1].sendbuf,
4332 #ifdef PME_TIME_THREADS
4333 c3 = omp_cyc_end(c3);
4337 if (pme->nnodes > 1)
4339 /* Communicate the overlapping part of the fftgrid.
4340 * For this communication call we need to check pme->bUseThreads
4341 * to have all ranks communicate here, regardless of pme->nthread.
4343 sum_fftgrid_dd(pme, fftgrid, grid_index);
4347 #ifdef PME_TIME_THREADS
4351 printf("idx %.2f spread %.2f red %.2f",
4352 cs1*1e-9, cs2*1e-9, cs3*1e-9);
4353 #ifdef PME_TIME_SPREAD
4354 for (thread = 0; thread < nthread; thread++)
4356 printf(" %.2f", cs1a[thread]*1e-9);
4365 static void dump_grid(FILE *fp,
4366 int sx, int sy, int sz, int nx, int ny, int nz,
4367 int my, int mz, const real *g)
4371 for (x = 0; x < nx; x++)
4373 for (y = 0; y < ny; y++)
4375 for (z = 0; z < nz; z++)
4377 fprintf(fp, "%2d %2d %2d %6.3f\n",
4378 sx+x, sy+y, sz+z, g[(x*my + y)*mz + z]);
4384 static void dump_local_fftgrid(gmx_pme_t pme, const real *fftgrid)
4386 ivec local_fft_ndata, local_fft_offset, local_fft_size;
4388 gmx_parallel_3dfft_real_limits(pme->pfft_setup[PME_GRID_QA],
4394 pme->pmegrid_start_ix,
4395 pme->pmegrid_start_iy,
4396 pme->pmegrid_start_iz,
4397 pme->pmegrid_nx-pme->pme_order+1,
4398 pme->pmegrid_ny-pme->pme_order+1,
4399 pme->pmegrid_nz-pme->pme_order+1,
4406 void gmx_pme_calc_energy(gmx_pme_t pme, int n, rvec *x, real *q, real *V)
4408 pme_atomcomm_t *atc;
4411 if (pme->nnodes > 1)
4413 gmx_incons("gmx_pme_calc_energy called in parallel");
4415 if (pme->bFEP_q > 1)
4417 gmx_incons("gmx_pme_calc_energy with free energy");
4420 atc = &pme->atc_energy;
4422 if (atc->spline == NULL)
4424 snew(atc->spline, atc->nthread);
4427 atc->bSpread = TRUE;
4428 atc->pme_order = pme->pme_order;
4430 pme_realloc_atomcomm_things(atc);
4432 atc->coefficient = q;
4434 /* We only use the A-charges grid */
4435 grid = &pme->pmegrid[PME_GRID_QA];
4437 /* Only calculate the spline coefficients, don't actually spread */
4438 spread_on_grid(pme, atc, NULL, TRUE, FALSE, pme->fftgrid[PME_GRID_QA], FALSE, PME_GRID_QA);
4440 *V = gather_energy_bsplines(pme, grid->grid.grid, atc);
4444 static void reset_pmeonly_counters(gmx_wallcycle_t wcycle,
4445 gmx_walltime_accounting_t walltime_accounting,
4446 t_nrnb *nrnb, t_inputrec *ir,
4449 /* Reset all the counters related to performance over the run */
4450 wallcycle_stop(wcycle, ewcRUN);
4451 wallcycle_reset_all(wcycle);
4453 if (ir->nsteps >= 0)
4455 /* ir->nsteps is not used here, but we update it for consistency */
4456 ir->nsteps -= step - ir->init_step;
4458 ir->init_step = step;
4459 wallcycle_start(wcycle, ewcRUN);
4460 walltime_accounting_start(walltime_accounting);
4464 static void gmx_pmeonly_switch(int *npmedata, gmx_pme_t **pmedata,
4466 t_commrec *cr, t_inputrec *ir,
4470 gmx_pme_t pme = NULL;
4473 while (ind < *npmedata)
4475 pme = (*pmedata)[ind];
4476 if (pme->nkx == grid_size[XX] &&
4477 pme->nky == grid_size[YY] &&
4478 pme->nkz == grid_size[ZZ])
4489 srenew(*pmedata, *npmedata);
4491 /* Generate a new PME data structure, copying part of the old pointers */
4492 gmx_pme_reinit(&((*pmedata)[ind]), cr, pme, ir, grid_size);
4494 *pme_ret = (*pmedata)[ind];
4497 int gmx_pmeonly(gmx_pme_t pme,
4498 t_commrec *cr, t_nrnb *mynrnb,
4499 gmx_wallcycle_t wcycle,
4500 gmx_walltime_accounting_t walltime_accounting,
4501 real ewaldcoeff_q, real ewaldcoeff_lj,
4506 gmx_pme_pp_t pme_pp;
4510 rvec *x_pp = NULL, *f_pp = NULL;
4511 real *chargeA = NULL, *chargeB = NULL;
4512 real *c6A = NULL, *c6B = NULL;
4513 real *sigmaA = NULL, *sigmaB = NULL;
4516 int maxshift_x = 0, maxshift_y = 0;
4517 real energy_q, energy_lj, dvdlambda_q, dvdlambda_lj;
4518 matrix vir_q, vir_lj;
4523 gmx_int64_t step, step_rel;
4526 /* This data will only use with PME tuning, i.e. switching PME grids */
4528 snew(pmedata, npmedata);
4531 pme_pp = gmx_pme_pp_init(cr);
4536 do /****** this is a quasi-loop over time steps! */
4538 /* The reason for having a loop here is PME grid tuning/switching */
4541 /* Domain decomposition */
4542 ret = gmx_pme_recv_coeffs_coords(pme_pp,
4548 &maxshift_x, &maxshift_y,
4549 &pme->bFEP_q, &pme->bFEP_lj,
4550 &lambda_q, &lambda_lj,
4554 grid_switch, &ewaldcoeff_q, &ewaldcoeff_lj);
4556 if (ret == pmerecvqxSWITCHGRID)
4558 /* Switch the PME grid to grid_switch */
4559 gmx_pmeonly_switch(&npmedata, &pmedata, grid_switch, cr, ir, &pme);
4562 if (ret == pmerecvqxRESETCOUNTERS)
4564 /* Reset the cycle and flop counters */
4565 reset_pmeonly_counters(wcycle, walltime_accounting, mynrnb, ir, step);
4568 while (ret == pmerecvqxSWITCHGRID || ret == pmerecvqxRESETCOUNTERS);
4570 if (ret == pmerecvqxFINISH)
4572 /* We should stop: break out of the loop */
4576 step_rel = step - ir->init_step;
4580 wallcycle_start(wcycle, ewcRUN);
4581 walltime_accounting_start(walltime_accounting);
4584 wallcycle_start(wcycle, ewcPMEMESH);
4591 gmx_pme_do(pme, 0, natoms, x_pp, f_pp,
4592 chargeA, chargeB, c6A, c6B, sigmaA, sigmaB, box,
4593 cr, maxshift_x, maxshift_y, mynrnb, wcycle,
4594 vir_q, ewaldcoeff_q, vir_lj, ewaldcoeff_lj,
4595 &energy_q, &energy_lj, lambda_q, lambda_lj, &dvdlambda_q, &dvdlambda_lj,
4596 pme_flags | GMX_PME_DO_ALL_F | (bEnerVir ? GMX_PME_CALC_ENER_VIR : 0));
4598 cycles = wallcycle_stop(wcycle, ewcPMEMESH);
4600 gmx_pme_send_force_vir_ener(pme_pp,
4601 f_pp, vir_q, energy_q, vir_lj, energy_lj,
4602 dvdlambda_q, dvdlambda_lj, cycles);
4605 } /***** end of quasi-loop, we stop with the break above */
4608 walltime_accounting_end(walltime_accounting);
4614 calc_initial_lb_coeffs(gmx_pme_t pme, real *local_c6, real *local_sigma)
4618 for (i = 0; i < pme->atc[0].n; ++i)
4622 sigma4 = local_sigma[i];
4623 sigma4 = sigma4*sigma4;
4624 sigma4 = sigma4*sigma4;
4625 pme->atc[0].coefficient[i] = local_c6[i] / sigma4;
4630 calc_next_lb_coeffs(gmx_pme_t pme, real *local_sigma)
4634 for (i = 0; i < pme->atc[0].n; ++i)
4636 pme->atc[0].coefficient[i] *= local_sigma[i];
4641 do_redist_pos_coeffs(gmx_pme_t pme, t_commrec *cr, int start, int homenr,
4642 gmx_bool bFirst, rvec x[], real *data)
4645 pme_atomcomm_t *atc;
4648 for (d = pme->ndecompdim - 1; d >= 0; d--)
4654 if (d == pme->ndecompdim - 1)
4662 n_d = pme->atc[d + 1].n;
4664 param_d = atc->coefficient;
4668 if (atc->npd > atc->pd_nalloc)
4670 atc->pd_nalloc = over_alloc_dd(atc->npd);
4671 srenew(atc->pd, atc->pd_nalloc);
4673 pme_calc_pidx_wrapper(n_d, pme->recipbox, x_d, atc);
4675 /* Redistribute x (only once) and qA/c6A or qB/c6B */
4676 if (DOMAINDECOMP(cr))
4678 dd_pmeredist_pos_coeffs(pme, n_d, bFirst, x_d, param_d, atc);
4683 int gmx_pme_do(gmx_pme_t pme,
4684 int start, int homenr,
4686 real *chargeA, real *chargeB,
4687 real *c6A, real *c6B,
4688 real *sigmaA, real *sigmaB,
4689 matrix box, t_commrec *cr,
4690 int maxshift_x, int maxshift_y,
4691 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
4692 matrix vir_q, real ewaldcoeff_q,
4693 matrix vir_lj, real ewaldcoeff_lj,
4694 real *energy_q, real *energy_lj,
4695 real lambda_q, real lambda_lj,
4696 real *dvdlambda_q, real *dvdlambda_lj,
4699 int d, i, j, k, ntot, npme, grid_index, max_grid_index;
4702 pme_atomcomm_t *atc = NULL;
4703 pmegrids_t *pmegrid = NULL;
4707 real *coefficient = NULL;
4712 gmx_parallel_3dfft_t pfft_setup;
4714 t_complex * cfftgrid;
4716 gmx_bool bFirst, bDoSplines;
4718 int fep_states_lj = pme->bFEP_lj ? 2 : 1;
4719 const gmx_bool bCalcEnerVir = flags & GMX_PME_CALC_ENER_VIR;
4720 const gmx_bool bCalcF = flags & GMX_PME_CALC_F;
4722 assert(pme->nnodes > 0);
4723 assert(pme->nnodes == 1 || pme->ndecompdim > 0);
4725 if (pme->nnodes > 1)
4729 if (atc->npd > atc->pd_nalloc)
4731 atc->pd_nalloc = over_alloc_dd(atc->npd);
4732 srenew(atc->pd, atc->pd_nalloc);
4734 for (d = pme->ndecompdim-1; d >= 0; d--)
4737 atc->maxshift = (atc->dimind == 0 ? maxshift_x : maxshift_y);
4743 /* This could be necessary for TPI */
4744 pme->atc[0].n = homenr;
4745 if (DOMAINDECOMP(cr))
4747 pme_realloc_atomcomm_things(atc);
4753 m_inv_ur0(box, pme->recipbox);
4756 /* For simplicity, we construct the splines for all particles if
4757 * more than one PME calculations is needed. Some optimization
4758 * could be done by keeping track of which atoms have splines
4759 * constructed, and construct new splines on each pass for atoms
4760 * that don't yet have them.
4763 bDoSplines = pme->bFEP || ((flags & GMX_PME_DO_COULOMB) && (flags & GMX_PME_DO_LJ));
4765 /* We need a maximum of four separate PME calculations:
4766 * grid_index=0: Coulomb PME with charges from state A
4767 * grid_index=1: Coulomb PME with charges from state B
4768 * grid_index=2: LJ PME with C6 from state A
4769 * grid_index=3: LJ PME with C6 from state B
4770 * For Lorentz-Berthelot combination rules, a separate loop is used to
4771 * calculate all the terms
4774 /* If we are doing LJ-PME with LB, we only do Q here */
4775 max_grid_index = (pme->ljpme_combination_rule == eljpmeLB) ? DO_Q : DO_Q_AND_LJ;
4777 for (grid_index = 0; grid_index < max_grid_index; ++grid_index)
4779 /* Check if we should do calculations at this grid_index
4780 * If grid_index is odd we should be doing FEP
4781 * If grid_index < 2 we should be doing electrostatic PME
4782 * If grid_index >= 2 we should be doing LJ-PME
4784 if ((grid_index < DO_Q && (!(flags & GMX_PME_DO_COULOMB) ||
4785 (grid_index == 1 && !pme->bFEP_q))) ||
4786 (grid_index >= DO_Q && (!(flags & GMX_PME_DO_LJ) ||
4787 (grid_index == 3 && !pme->bFEP_lj))))
4791 /* Unpack structure */
4792 pmegrid = &pme->pmegrid[grid_index];
4793 fftgrid = pme->fftgrid[grid_index];
4794 cfftgrid = pme->cfftgrid[grid_index];
4795 pfft_setup = pme->pfft_setup[grid_index];
4798 case 0: coefficient = chargeA + start; break;
4799 case 1: coefficient = chargeB + start; break;
4800 case 2: coefficient = c6A + start; break;
4801 case 3: coefficient = c6B + start; break;
4804 grid = pmegrid->grid.grid;
4808 fprintf(debug, "PME: nnodes = %d, nodeid = %d\n",
4809 cr->nnodes, cr->nodeid);
4810 fprintf(debug, "Grid = %p\n", (void*)grid);
4813 gmx_fatal(FARGS, "No grid!");
4818 if (pme->nnodes == 1)
4820 atc->coefficient = coefficient;
4824 wallcycle_start(wcycle, ewcPME_REDISTXF);
4825 do_redist_pos_coeffs(pme, cr, start, homenr, bFirst, x, coefficient);
4828 wallcycle_stop(wcycle, ewcPME_REDISTXF);
4833 fprintf(debug, "Node= %6d, pme local particles=%6d\n",
4834 cr->nodeid, atc->n);
4837 if (flags & GMX_PME_SPREAD)
4839 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
4841 /* Spread the coefficients on a grid */
4842 spread_on_grid(pme, &pme->atc[0], pmegrid, bFirst, TRUE, fftgrid, bDoSplines, grid_index);
4846 inc_nrnb(nrnb, eNR_WEIGHTS, DIM*atc->n);
4848 inc_nrnb(nrnb, eNR_SPREADBSP,
4849 pme->pme_order*pme->pme_order*pme->pme_order*atc->n);
4851 if (!pme->bUseThreads)
4853 wrap_periodic_pmegrid(pme, grid);
4855 /* sum contributions to local grid from other nodes */
4857 if (pme->nnodes > 1)
4859 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_GRID_FORWARD);
4864 copy_pmegrid_to_fftgrid(pme, grid, fftgrid, grid_index);
4867 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
4870 dump_local_fftgrid(pme,fftgrid);
4875 /* Here we start a large thread parallel region */
4876 #pragma omp parallel num_threads(pme->nthread) private(thread)
4878 thread = gmx_omp_get_thread_num();
4879 if (flags & GMX_PME_SOLVE)
4886 wallcycle_start(wcycle, ewcPME_FFT);
4888 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_REAL_TO_COMPLEX,
4892 wallcycle_stop(wcycle, ewcPME_FFT);
4896 /* solve in k-space for our local cells */
4899 wallcycle_start(wcycle, (grid_index < DO_Q ? ewcPME_SOLVE : ewcLJPME));
4901 if (grid_index < DO_Q)
4904 solve_pme_yzx(pme, cfftgrid, ewaldcoeff_q,
4905 box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
4907 pme->nthread, thread);
4912 solve_pme_lj_yzx(pme, &cfftgrid, FALSE, ewaldcoeff_lj,
4913 box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
4915 pme->nthread, thread);
4920 wallcycle_stop(wcycle, (grid_index < DO_Q ? ewcPME_SOLVE : ewcLJPME));
4922 inc_nrnb(nrnb, eNR_SOLVEPME, loop_count);
4932 wallcycle_start(wcycle, ewcPME_FFT);
4934 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_COMPLEX_TO_REAL,
4938 wallcycle_stop(wcycle, ewcPME_FFT);
4942 if (pme->nodeid == 0)
4944 ntot = pme->nkx*pme->nky*pme->nkz;
4945 npme = ntot*log((real)ntot)/log(2.0);
4946 inc_nrnb(nrnb, eNR_FFT, 2*npme);
4949 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
4952 copy_fftgrid_to_pmegrid(pme, fftgrid, grid, grid_index, pme->nthread, thread);
4955 /* End of thread parallel section.
4956 * With MPI we have to synchronize here before gmx_sum_qgrid_dd.
4961 /* distribute local grid to all nodes */
4963 if (pme->nnodes > 1)
4965 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_GRID_BACKWARD);
4970 unwrap_periodic_pmegrid(pme, grid);
4972 /* interpolate forces for our local atoms */
4976 /* If we are running without parallelization,
4977 * atc->f is the actual force array, not a buffer,
4978 * therefore we should not clear it.
4980 lambda = grid_index < DO_Q ? lambda_q : lambda_lj;
4981 bClearF = (bFirst && PAR(cr));
4982 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
4983 for (thread = 0; thread < pme->nthread; thread++)
4985 gather_f_bsplines(pme, grid, bClearF, atc,
4986 &atc->spline[thread],
4987 pme->bFEP ? (grid_index % 2 == 0 ? 1.0-lambda : lambda) : 1.0);
4992 inc_nrnb(nrnb, eNR_GATHERFBSP,
4993 pme->pme_order*pme->pme_order*pme->pme_order*pme->atc[0].n);
4994 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
4999 /* This should only be called on the master thread
5000 * and after the threads have synchronized.
5004 get_pme_ener_vir_q(pme, pme->nthread, &energy_AB[grid_index], vir_AB[grid_index]);
5008 get_pme_ener_vir_lj(pme, pme->nthread, &energy_AB[grid_index], vir_AB[grid_index]);
5012 } /* of grid_index-loop */
5014 /* For Lorentz-Berthelot combination rules in LJ-PME, we need to calculate
5017 if ((flags & GMX_PME_DO_LJ) && pme->ljpme_combination_rule == eljpmeLB)
5019 /* Loop over A- and B-state if we are doing FEP */
5020 for (fep_state = 0; fep_state < fep_states_lj; ++fep_state)
5022 real *local_c6 = NULL, *local_sigma = NULL, *RedistC6 = NULL, *RedistSigma = NULL;
5023 if (pme->nnodes == 1)
5025 if (pme->lb_buf1 == NULL)
5027 pme->lb_buf_nalloc = pme->atc[0].n;
5028 snew(pme->lb_buf1, pme->lb_buf_nalloc);
5030 pme->atc[0].coefficient = pme->lb_buf1;
5035 local_sigma = sigmaA;
5039 local_sigma = sigmaB;
5042 gmx_incons("Trying to access wrong FEP-state in LJ-PME routine");
5052 RedistSigma = sigmaA;
5056 RedistSigma = sigmaB;
5059 gmx_incons("Trying to access wrong FEP-state in LJ-PME routine");
5061 wallcycle_start(wcycle, ewcPME_REDISTXF);
5063 do_redist_pos_coeffs(pme, cr, start, homenr, bFirst, x, RedistC6);
5064 if (pme->lb_buf_nalloc < atc->n)
5066 pme->lb_buf_nalloc = atc->nalloc;
5067 srenew(pme->lb_buf1, pme->lb_buf_nalloc);
5068 srenew(pme->lb_buf2, pme->lb_buf_nalloc);
5070 local_c6 = pme->lb_buf1;
5071 for (i = 0; i < atc->n; ++i)
5073 local_c6[i] = atc->coefficient[i];
5077 do_redist_pos_coeffs(pme, cr, start, homenr, FALSE, x, RedistSigma);
5078 local_sigma = pme->lb_buf2;
5079 for (i = 0; i < atc->n; ++i)
5081 local_sigma[i] = atc->coefficient[i];
5085 wallcycle_stop(wcycle, ewcPME_REDISTXF);
5087 calc_initial_lb_coeffs(pme, local_c6, local_sigma);
5089 /*Seven terms in LJ-PME with LB, grid_index < 2 reserved for electrostatics*/
5090 for (grid_index = 2; grid_index < 9; ++grid_index)
5092 /* Unpack structure */
5093 pmegrid = &pme->pmegrid[grid_index];
5094 fftgrid = pme->fftgrid[grid_index];
5095 cfftgrid = pme->cfftgrid[grid_index];
5096 pfft_setup = pme->pfft_setup[grid_index];
5097 calc_next_lb_coeffs(pme, local_sigma);
5098 grid = pmegrid->grid.grid;
5101 if (flags & GMX_PME_SPREAD)
5103 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
5104 /* Spread the c6 on a grid */
5105 spread_on_grid(pme, &pme->atc[0], pmegrid, bFirst, TRUE, fftgrid, bDoSplines, grid_index);
5109 inc_nrnb(nrnb, eNR_WEIGHTS, DIM*atc->n);
5112 inc_nrnb(nrnb, eNR_SPREADBSP,
5113 pme->pme_order*pme->pme_order*pme->pme_order*atc->n);
5114 if (pme->nthread == 1)
5116 wrap_periodic_pmegrid(pme, grid);
5117 /* sum contributions to local grid from other nodes */
5119 if (pme->nnodes > 1)
5121 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_GRID_FORWARD);
5125 copy_pmegrid_to_fftgrid(pme, grid, fftgrid, grid_index);
5127 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
5129 /*Here we start a large thread parallel region*/
5130 #pragma omp parallel num_threads(pme->nthread) private(thread)
5132 thread = gmx_omp_get_thread_num();
5133 if (flags & GMX_PME_SOLVE)
5138 wallcycle_start(wcycle, ewcPME_FFT);
5141 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_REAL_TO_COMPLEX,
5145 wallcycle_stop(wcycle, ewcPME_FFT);
5152 if (flags & GMX_PME_SOLVE)
5155 /* solve in k-space for our local cells */
5156 #pragma omp parallel num_threads(pme->nthread) private(thread)
5158 thread = gmx_omp_get_thread_num();
5161 wallcycle_start(wcycle, ewcLJPME);
5165 solve_pme_lj_yzx(pme, &pme->cfftgrid[2], TRUE, ewaldcoeff_lj,
5166 box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
5168 pme->nthread, thread);
5171 wallcycle_stop(wcycle, ewcLJPME);
5173 inc_nrnb(nrnb, eNR_SOLVEPME, loop_count);
5180 /* This should only be called on the master thread and
5181 * after the threads have synchronized.
5183 get_pme_ener_vir_lj(pme, pme->nthread, &energy_AB[2+fep_state], vir_AB[2+fep_state]);
5188 bFirst = !(flags & GMX_PME_DO_COULOMB);
5189 calc_initial_lb_coeffs(pme, local_c6, local_sigma);
5190 for (grid_index = 8; grid_index >= 2; --grid_index)
5192 /* Unpack structure */
5193 pmegrid = &pme->pmegrid[grid_index];
5194 fftgrid = pme->fftgrid[grid_index];
5195 cfftgrid = pme->cfftgrid[grid_index];
5196 pfft_setup = pme->pfft_setup[grid_index];
5197 grid = pmegrid->grid.grid;
5198 calc_next_lb_coeffs(pme, local_sigma);
5200 #pragma omp parallel num_threads(pme->nthread) private(thread)
5202 thread = gmx_omp_get_thread_num();
5207 wallcycle_start(wcycle, ewcPME_FFT);
5210 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_COMPLEX_TO_REAL,
5214 wallcycle_stop(wcycle, ewcPME_FFT);
5218 if (pme->nodeid == 0)
5220 ntot = pme->nkx*pme->nky*pme->nkz;
5221 npme = ntot*log((real)ntot)/log(2.0);
5222 inc_nrnb(nrnb, eNR_FFT, 2*npme);
5224 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
5227 copy_fftgrid_to_pmegrid(pme, fftgrid, grid, grid_index, pme->nthread, thread);
5229 } /*#pragma omp parallel*/
5231 /* distribute local grid to all nodes */
5233 if (pme->nnodes > 1)
5235 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_GRID_BACKWARD);
5240 unwrap_periodic_pmegrid(pme, grid);
5242 /* interpolate forces for our local atoms */
5244 bClearF = (bFirst && PAR(cr));
5245 scale = pme->bFEP ? (fep_state < 1 ? 1.0-lambda_lj : lambda_lj) : 1.0;
5246 scale *= lb_scale_factor[grid_index-2];
5247 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
5248 for (thread = 0; thread < pme->nthread; thread++)
5250 gather_f_bsplines(pme, grid, bClearF, &pme->atc[0],
5251 &pme->atc[0].spline[thread],
5256 inc_nrnb(nrnb, eNR_GATHERFBSP,
5257 pme->pme_order*pme->pme_order*pme->pme_order*pme->atc[0].n);
5258 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
5261 } /* for (grid_index = 8; grid_index >= 2; --grid_index) */
5263 } /* for (fep_state = 0; fep_state < fep_states_lj; ++fep_state) */
5264 } /* if ((flags & GMX_PME_DO_LJ) && pme->ljpme_combination_rule == eljpmeLB) */
5266 if (bCalcF && pme->nnodes > 1)
5268 wallcycle_start(wcycle, ewcPME_REDISTXF);
5269 for (d = 0; d < pme->ndecompdim; d++)
5272 if (d == pme->ndecompdim - 1)
5279 n_d = pme->atc[d+1].n;
5280 f_d = pme->atc[d+1].f;
5282 if (DOMAINDECOMP(cr))
5284 dd_pmeredist_f(pme, atc, n_d, f_d,
5285 d == pme->ndecompdim-1 && pme->bPPnode);
5289 wallcycle_stop(wcycle, ewcPME_REDISTXF);
5295 if (flags & GMX_PME_DO_COULOMB)
5299 *energy_q = energy_AB[0];
5300 m_add(vir_q, vir_AB[0], vir_q);
5304 *energy_q = (1.0-lambda_q)*energy_AB[0] + lambda_q*energy_AB[1];
5305 *dvdlambda_q += energy_AB[1] - energy_AB[0];
5306 for (i = 0; i < DIM; i++)
5308 for (j = 0; j < DIM; j++)
5310 vir_q[i][j] += (1.0-lambda_q)*vir_AB[0][i][j] +
5311 lambda_q*vir_AB[1][i][j];
5317 fprintf(debug, "Electrostatic PME mesh energy: %g\n", *energy_q);
5325 if (flags & GMX_PME_DO_LJ)
5329 *energy_lj = energy_AB[2];
5330 m_add(vir_lj, vir_AB[2], vir_lj);
5334 *energy_lj = (1.0-lambda_lj)*energy_AB[2] + lambda_lj*energy_AB[3];
5335 *dvdlambda_lj += energy_AB[3] - energy_AB[2];
5336 for (i = 0; i < DIM; i++)
5338 for (j = 0; j < DIM; j++)
5340 vir_lj[i][j] += (1.0-lambda_lj)*vir_AB[2][i][j] + lambda_lj*vir_AB[3][i][j];
5346 fprintf(debug, "Lennard-Jones PME mesh energy: %g\n", *energy_lj);