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.
73 #include "gmx_fatal.h"
80 #include "gromacs/fft/parallel_3dfft.h"
81 #include "gromacs/fileio/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/macros.h"
91 #if defined GMX_HAVE_SIMD_MACROS
92 /* Turn on arbitrary width SIMD intrinsics for PME solve */
93 #define PME_SIMD_SOLVE
96 #define PME_GRID_QA 0 /* Gridindex for A-state for Q */
97 #define PME_GRID_C6A 2 /* Gridindex for A-state for LJ */
98 #define DO_Q 2 /* Electrostatic grids have index q<2 */
99 #define DO_Q_AND_LJ 4 /* non-LB LJ grids have index 2 <= q < 4 */
100 #define DO_Q_AND_LJ_LB 9 /* With LB rules we need a total of 2+7 grids */
102 /* Pascal triangle coefficients scaled with (1/2)^6 for LJ-PME with LB-rules */
103 const real lb_scale_factor[] = {
104 1.0/64, 6.0/64, 15.0/64, 20.0/64,
105 15.0/64, 6.0/64, 1.0/64
108 /* Pascal triangle coefficients used in solve_pme_lj_yzx, only need to do 4 calculations due to symmetry */
109 const real lb_scale_factor_symm[] = { 2.0/64, 12.0/64, 30.0/64, 20.0/64 };
111 /* Include the 4-wide SIMD macro file */
112 #include "gromacs/simd/four_wide_macros.h"
113 /* Check if we have 4-wide SIMD macro support */
114 #ifdef GMX_HAVE_SIMD4_MACROS
115 /* Do PME spread and gather with 4-wide SIMD.
116 * NOTE: SIMD is only used with PME order 4 and 5 (which are the most common).
118 #define PME_SIMD4_SPREAD_GATHER
120 #ifdef GMX_SIMD4_HAVE_UNALIGNED
121 /* With PME-order=4 on x86, unaligned load+store is slightly faster
122 * than doubling all SIMD operations when using aligned load+store.
124 #define PME_SIMD4_UNALIGNED
129 /* #define PRT_FORCE */
130 /* conditions for on the fly time-measurement */
131 /* #define TAKETIME (step > 1 && timesteps < 10) */
132 #define TAKETIME FALSE
134 /* #define PME_TIME_THREADS */
137 #define mpi_type MPI_DOUBLE
139 #define mpi_type MPI_FLOAT
142 #ifdef PME_SIMD4_SPREAD_GATHER
143 #define SIMD4_ALIGNMENT (GMX_SIMD4_WIDTH*sizeof(real))
145 /* We can use any alignment, apart from 0, so we use 4 reals */
146 #define SIMD4_ALIGNMENT (4*sizeof(real))
149 /* GMX_CACHE_SEP should be a multiple of the SIMD and SIMD4 register size
150 * to preserve alignment.
152 #define GMX_CACHE_SEP 64
154 /* We only define a maximum to be able to use local arrays without allocation.
155 * An order larger than 12 should never be needed, even for test cases.
156 * If needed it can be changed here.
158 #define PME_ORDER_MAX 12
160 /* Internal datastructures */
166 int recv_size; /* Receive buffer width, used with OpenMP */
177 int *send_id, *recv_id;
178 int send_size; /* Send buffer width, used with OpenMP */
179 pme_grid_comm_t *comm_data;
185 int *n; /* Cumulative counts of the number of particles per thread */
186 int nalloc; /* Allocation size of i */
187 int *i; /* Particle indices ordered on thread index (n) */
201 int dimind; /* The index of the dimension, 0=x, 1=y */
208 int *node_dest; /* The nodes to send x and q to with DD */
209 int *node_src; /* The nodes to receive x and q from with DD */
210 int *buf_index; /* Index for commnode into the buffers */
217 int *count; /* The number of atoms to send to each node */
219 int *rcount; /* The number of atoms to receive */
226 gmx_bool bSpread; /* These coordinates are used for spreading */
229 rvec *fractx; /* Fractional coordinate relative to
230 * the lower cell boundary
233 int *thread_idx; /* Which thread should spread which charge */
234 thread_plist_t *thread_plist;
235 splinedata_t *spline;
242 ivec ci; /* The spatial location of this grid */
243 ivec n; /* The used size of *grid, including order-1 */
244 ivec offset; /* The grid offset from the full node grid */
245 int order; /* PME spreading order */
246 ivec s; /* The allocated size of *grid, s >= n */
247 real *grid; /* The grid local thread, size n */
251 pmegrid_t grid; /* The full node grid (non thread-local) */
252 int nthread; /* The number of threads operating on this grid */
253 ivec nc; /* The local spatial decomposition over the threads */
254 pmegrid_t *grid_th; /* Array of grids for each thread */
255 real *grid_all; /* Allocated array for the grids in *grid_th */
256 int **g2t; /* The grid to thread index */
257 ivec nthread_comm; /* The number of threads to communicate with */
261 #ifdef PME_SIMD4_SPREAD_GATHER
262 /* Masks for 4-wide SIMD aligned spreading and gathering */
263 gmx_simd4_bool_t mask_S0[6], mask_S1[6];
265 int dummy; /* C89 requires that struct has at least one member */
270 /* work data for solve_pme */
289 typedef struct gmx_pme {
290 int ndecompdim; /* The number of decomposition dimensions */
291 int nodeid; /* Our nodeid in mpi->mpi_comm */
294 int nnodes; /* The number of nodes doing PME */
299 MPI_Comm mpi_comm_d[2]; /* Indexed on dimension, 0=x, 1=y */
301 MPI_Datatype rvec_mpi; /* the pme vector's MPI type */
304 gmx_bool bUseThreads; /* Does any of the PME ranks have nthread>1 ? */
305 int nthread; /* The number of threads doing PME on our rank */
307 gmx_bool bPPnode; /* Node also does particle-particle forces */
308 gmx_bool bFEP; /* Compute Free energy contribution */
311 int nkx, nky, nkz; /* Grid dimensions */
312 gmx_bool bP3M; /* Do P3M: optimize the influence function */
316 int ngrids; /* number of grids we maintain for pmegrid, (c)fftgrid and pfft_setups*/
318 pmegrids_t pmegrid[DO_Q_AND_LJ_LB]; /* Grids on which we do spreading/interpolation,
319 * includes overlap Grid indices are ordered as
321 * 0: Coloumb PME, state A
322 * 1: Coloumb PME, state B
324 * This can probably be done in a better way
325 * but this simple hack works for now
327 /* The PME charge spreading grid sizes/strides, includes pme_order-1 */
328 int pmegrid_nx, pmegrid_ny, pmegrid_nz;
329 /* pmegrid_nz might be larger than strictly necessary to ensure
330 * memory alignment, pmegrid_nz_base gives the real base size.
333 /* The local PME grid starting indices */
334 int pmegrid_start_ix, pmegrid_start_iy, pmegrid_start_iz;
336 /* Work data for spreading and gathering */
337 pme_spline_work_t *spline_work;
339 real **fftgrid; /* Grids for FFT. With 1D FFT decomposition this can be a pointer */
340 /* inside the interpolation grid, but separate for 2D PME decomp. */
341 int fftgrid_nx, fftgrid_ny, fftgrid_nz;
343 t_complex **cfftgrid; /* Grids for complex FFT data */
345 int cfftgrid_nx, cfftgrid_ny, cfftgrid_nz;
347 gmx_parallel_3dfft_t *pfft_setup;
349 int *nnx, *nny, *nnz;
350 real *fshx, *fshy, *fshz;
352 pme_atomcomm_t atc[2]; /* Indexed on decomposition index */
355 /* Buffers to store data for local atoms for L-B combination rule
356 * calculations in LJ-PME. lb_buf1 stores either the coefficients
357 * for spreading/gathering (in serial), or the C6 parameters for
358 * local atoms (in parallel). lb_buf2 is only used in parallel,
359 * and stores the sigma values for local atoms. */
360 real *lb_buf1, *lb_buf2;
361 int lb_buf_nalloc; /* Allocation size for the above buffers. */
363 pme_overlap_t overlap[2]; /* Indexed on dimension, 0=x, 1=y */
365 pme_atomcomm_t atc_energy; /* Only for gmx_pme_calc_energy */
367 rvec *bufv; /* Communication buffer */
368 real *bufr; /* Communication buffer */
369 int buf_nalloc; /* The communication buffer size */
371 /* thread local work data for solve_pme */
374 /* Work data for PME_redist */
375 gmx_bool redist_init;
383 int redist_buf_nalloc;
385 /* Work data for sum_qgrid */
386 real * sum_qgrid_tmp;
387 real * sum_qgrid_dd_tmp;
390 static void calc_interpolation_idx(gmx_pme_t pme, pme_atomcomm_t *atc,
391 int start, int end, int thread)
394 int *idxptr, tix, tiy, tiz;
395 real *xptr, *fptr, tx, ty, tz;
396 real rxx, ryx, ryy, rzx, rzy, rzz;
398 int start_ix, start_iy, start_iz;
399 int *g2tx, *g2ty, *g2tz;
401 int *thread_idx = NULL;
402 thread_plist_t *tpl = NULL;
410 start_ix = pme->pmegrid_start_ix;
411 start_iy = pme->pmegrid_start_iy;
412 start_iz = pme->pmegrid_start_iz;
414 rxx = pme->recipbox[XX][XX];
415 ryx = pme->recipbox[YY][XX];
416 ryy = pme->recipbox[YY][YY];
417 rzx = pme->recipbox[ZZ][XX];
418 rzy = pme->recipbox[ZZ][YY];
419 rzz = pme->recipbox[ZZ][ZZ];
421 g2tx = pme->pmegrid[PME_GRID_QA].g2t[XX];
422 g2ty = pme->pmegrid[PME_GRID_QA].g2t[YY];
423 g2tz = pme->pmegrid[PME_GRID_QA].g2t[ZZ];
425 bThreads = (atc->nthread > 1);
428 thread_idx = atc->thread_idx;
430 tpl = &atc->thread_plist[thread];
432 for (i = 0; i < atc->nthread; i++)
438 for (i = start; i < end; i++)
441 idxptr = atc->idx[i];
442 fptr = atc->fractx[i];
444 /* Fractional coordinates along box vectors, add 2.0 to make 100% sure we are positive for triclinic boxes */
445 tx = nx * ( xptr[XX] * rxx + xptr[YY] * ryx + xptr[ZZ] * rzx + 2.0 );
446 ty = ny * ( xptr[YY] * ryy + xptr[ZZ] * rzy + 2.0 );
447 tz = nz * ( xptr[ZZ] * rzz + 2.0 );
453 /* Because decomposition only occurs in x and y,
454 * we never have a fraction correction in z.
456 fptr[XX] = tx - tix + pme->fshx[tix];
457 fptr[YY] = ty - tiy + pme->fshy[tiy];
460 idxptr[XX] = pme->nnx[tix];
461 idxptr[YY] = pme->nny[tiy];
462 idxptr[ZZ] = pme->nnz[tiz];
465 range_check(idxptr[XX], 0, pme->pmegrid_nx);
466 range_check(idxptr[YY], 0, pme->pmegrid_ny);
467 range_check(idxptr[ZZ], 0, pme->pmegrid_nz);
472 thread_i = g2tx[idxptr[XX]] + g2ty[idxptr[YY]] + g2tz[idxptr[ZZ]];
473 thread_idx[i] = thread_i;
480 /* Make a list of particle indices sorted on thread */
482 /* Get the cumulative count */
483 for (i = 1; i < atc->nthread; i++)
485 tpl_n[i] += tpl_n[i-1];
487 /* The current implementation distributes particles equally
488 * over the threads, so we could actually allocate for that
489 * in pme_realloc_atomcomm_things.
491 if (tpl_n[atc->nthread-1] > tpl->nalloc)
493 tpl->nalloc = over_alloc_large(tpl_n[atc->nthread-1]);
494 srenew(tpl->i, tpl->nalloc);
496 /* Set tpl_n to the cumulative start */
497 for (i = atc->nthread-1; i >= 1; i--)
499 tpl_n[i] = tpl_n[i-1];
503 /* Fill our thread local array with indices sorted on thread */
504 for (i = start; i < end; i++)
506 tpl->i[tpl_n[atc->thread_idx[i]]++] = i;
508 /* Now tpl_n contains the cummulative count again */
512 static void make_thread_local_ind(pme_atomcomm_t *atc,
513 int thread, splinedata_t *spline)
515 int n, t, i, start, end;
518 /* Combine the indices made by each thread into one index */
522 for (t = 0; t < atc->nthread; t++)
524 tpl = &atc->thread_plist[t];
525 /* Copy our part (start - end) from the list of thread t */
528 start = tpl->n[thread-1];
530 end = tpl->n[thread];
531 for (i = start; i < end; i++)
533 spline->ind[n++] = tpl->i[i];
541 static void pme_calc_pidx(int start, int end,
542 matrix recipbox, rvec x[],
543 pme_atomcomm_t *atc, int *count)
548 real rxx, ryx, rzx, ryy, rzy;
551 /* Calculate PME task index (pidx) for each grid index.
552 * Here we always assign equally sized slabs to each node
553 * for load balancing reasons (the PME grid spacing is not used).
559 /* Reset the count */
560 for (i = 0; i < nslab; i++)
565 if (atc->dimind == 0)
567 rxx = recipbox[XX][XX];
568 ryx = recipbox[YY][XX];
569 rzx = recipbox[ZZ][XX];
570 /* Calculate the node index in x-dimension */
571 for (i = start; i < end; i++)
574 /* Fractional coordinates along box vectors */
575 s = nslab*(xptr[XX]*rxx + xptr[YY]*ryx + xptr[ZZ]*rzx);
576 si = (int)(s + 2*nslab) % nslab;
583 ryy = recipbox[YY][YY];
584 rzy = recipbox[ZZ][YY];
585 /* Calculate the node index in y-dimension */
586 for (i = start; i < end; i++)
589 /* Fractional coordinates along box vectors */
590 s = nslab*(xptr[YY]*ryy + xptr[ZZ]*rzy);
591 si = (int)(s + 2*nslab) % nslab;
598 static void pme_calc_pidx_wrapper(int natoms, matrix recipbox, rvec x[],
601 int nthread, thread, slab;
603 nthread = atc->nthread;
605 #pragma omp parallel for num_threads(nthread) schedule(static)
606 for (thread = 0; thread < nthread; thread++)
608 pme_calc_pidx(natoms* thread /nthread,
609 natoms*(thread+1)/nthread,
610 recipbox, x, atc, atc->count_thread[thread]);
612 /* Non-parallel reduction, since nslab is small */
614 for (thread = 1; thread < nthread; thread++)
616 for (slab = 0; slab < atc->nslab; slab++)
618 atc->count_thread[0][slab] += atc->count_thread[thread][slab];
623 static void realloc_splinevec(splinevec th, real **ptr_z, int nalloc)
625 const int padding = 4;
628 srenew(th[XX], nalloc);
629 srenew(th[YY], nalloc);
630 /* In z we add padding, this is only required for the aligned SIMD code */
631 sfree_aligned(*ptr_z);
632 snew_aligned(*ptr_z, nalloc+2*padding, SIMD4_ALIGNMENT);
633 th[ZZ] = *ptr_z + padding;
635 for (i = 0; i < padding; i++)
638 (*ptr_z)[padding+nalloc+i] = 0;
642 static void pme_realloc_splinedata(splinedata_t *spline, pme_atomcomm_t *atc)
646 srenew(spline->ind, atc->nalloc);
647 /* Initialize the index to identity so it works without threads */
648 for (i = 0; i < atc->nalloc; i++)
653 realloc_splinevec(spline->theta, &spline->ptr_theta_z,
654 atc->pme_order*atc->nalloc);
655 realloc_splinevec(spline->dtheta, &spline->ptr_dtheta_z,
656 atc->pme_order*atc->nalloc);
659 static void pme_realloc_atomcomm_things(pme_atomcomm_t *atc)
661 int nalloc_old, i, j, nalloc_tpl;
663 /* We have to avoid a NULL pointer for atc->x to avoid
664 * possible fatal errors in MPI routines.
666 if (atc->n > atc->nalloc || atc->nalloc == 0)
668 nalloc_old = atc->nalloc;
669 atc->nalloc = over_alloc_dd(max(atc->n, 1));
673 srenew(atc->x, atc->nalloc);
674 srenew(atc->q, atc->nalloc);
675 srenew(atc->f, atc->nalloc);
676 for (i = nalloc_old; i < atc->nalloc; i++)
678 clear_rvec(atc->f[i]);
683 srenew(atc->fractx, atc->nalloc);
684 srenew(atc->idx, atc->nalloc);
686 if (atc->nthread > 1)
688 srenew(atc->thread_idx, atc->nalloc);
691 for (i = 0; i < atc->nthread; i++)
693 pme_realloc_splinedata(&atc->spline[i], atc);
699 static void pmeredist_pd(gmx_pme_t pme, gmx_bool gmx_unused forw,
700 int n, gmx_bool gmx_unused bXF, rvec gmx_unused *x_f,
701 real gmx_unused *charge, pme_atomcomm_t *atc)
702 /* Redistribute particle data for PME calculation */
703 /* domain decomposition by x coordinate */
708 if (FALSE == pme->redist_init)
710 snew(pme->scounts, atc->nslab);
711 snew(pme->rcounts, atc->nslab);
712 snew(pme->sdispls, atc->nslab);
713 snew(pme->rdispls, atc->nslab);
714 snew(pme->sidx, atc->nslab);
715 pme->redist_init = TRUE;
717 if (n > pme->redist_buf_nalloc)
719 pme->redist_buf_nalloc = over_alloc_dd(n);
720 srenew(pme->redist_buf, pme->redist_buf_nalloc*DIM);
728 /* forward, redistribution from pp to pme */
730 /* Calculate send counts and exchange them with other nodes */
731 for (i = 0; (i < atc->nslab); i++)
735 for (i = 0; (i < n); i++)
737 pme->scounts[pme->idxa[i]]++;
739 MPI_Alltoall( pme->scounts, 1, MPI_INT, pme->rcounts, 1, MPI_INT, atc->mpi_comm);
741 /* Calculate send and receive displacements and index into send
746 for (i = 1; i < atc->nslab; i++)
748 pme->sdispls[i] = pme->sdispls[i-1]+pme->scounts[i-1];
749 pme->rdispls[i] = pme->rdispls[i-1]+pme->rcounts[i-1];
750 pme->sidx[i] = pme->sdispls[i];
752 /* Total # of particles to be received */
753 atc->n = pme->rdispls[atc->nslab-1] + pme->rcounts[atc->nslab-1];
755 pme_realloc_atomcomm_things(atc);
757 /* Copy particle coordinates into send buffer and exchange*/
758 for (i = 0; (i < n); i++)
760 ii = DIM*pme->sidx[pme->idxa[i]];
761 pme->sidx[pme->idxa[i]]++;
762 pme->redist_buf[ii+XX] = x_f[i][XX];
763 pme->redist_buf[ii+YY] = x_f[i][YY];
764 pme->redist_buf[ii+ZZ] = x_f[i][ZZ];
766 MPI_Alltoallv(pme->redist_buf, pme->scounts, pme->sdispls,
767 pme->rvec_mpi, atc->x, pme->rcounts, pme->rdispls,
768 pme->rvec_mpi, atc->mpi_comm);
772 /* Copy charge into send buffer and exchange*/
773 for (i = 0; i < atc->nslab; i++)
775 pme->sidx[i] = pme->sdispls[i];
777 for (i = 0; (i < n); i++)
779 ii = pme->sidx[pme->idxa[i]];
780 pme->sidx[pme->idxa[i]]++;
781 pme->redist_buf[ii] = charge[i];
783 MPI_Alltoallv(pme->redist_buf, pme->scounts, pme->sdispls, mpi_type,
784 atc->q, pme->rcounts, pme->rdispls, mpi_type,
787 else /* backward, redistribution from pme to pp */
789 MPI_Alltoallv(atc->f, pme->rcounts, pme->rdispls, pme->rvec_mpi,
790 pme->redist_buf, pme->scounts, pme->sdispls,
791 pme->rvec_mpi, atc->mpi_comm);
793 /* Copy data from receive buffer */
794 for (i = 0; i < atc->nslab; i++)
796 pme->sidx[i] = pme->sdispls[i];
798 for (i = 0; (i < n); i++)
800 ii = DIM*pme->sidx[pme->idxa[i]];
801 x_f[i][XX] += pme->redist_buf[ii+XX];
802 x_f[i][YY] += pme->redist_buf[ii+YY];
803 x_f[i][ZZ] += pme->redist_buf[ii+ZZ];
804 pme->sidx[pme->idxa[i]]++;
810 static void pme_dd_sendrecv(pme_atomcomm_t gmx_unused *atc,
811 gmx_bool gmx_unused bBackward, int gmx_unused shift,
812 void gmx_unused *buf_s, int gmx_unused nbyte_s,
813 void gmx_unused *buf_r, int gmx_unused nbyte_r)
819 if (bBackward == FALSE)
821 dest = atc->node_dest[shift];
822 src = atc->node_src[shift];
826 dest = atc->node_src[shift];
827 src = atc->node_dest[shift];
830 if (nbyte_s > 0 && nbyte_r > 0)
832 MPI_Sendrecv(buf_s, nbyte_s, MPI_BYTE,
834 buf_r, nbyte_r, MPI_BYTE,
836 atc->mpi_comm, &stat);
838 else if (nbyte_s > 0)
840 MPI_Send(buf_s, nbyte_s, MPI_BYTE,
844 else if (nbyte_r > 0)
846 MPI_Recv(buf_r, nbyte_r, MPI_BYTE,
848 atc->mpi_comm, &stat);
853 static void dd_pmeredist_x_q(gmx_pme_t pme,
854 int n, gmx_bool bX, rvec *x, real *charge,
857 int *commnode, *buf_index;
858 int nnodes_comm, i, nsend, local_pos, buf_pos, node, scount, rcount;
860 commnode = atc->node_dest;
861 buf_index = atc->buf_index;
863 nnodes_comm = min(2*atc->maxshift, atc->nslab-1);
866 for (i = 0; i < nnodes_comm; i++)
868 buf_index[commnode[i]] = nsend;
869 nsend += atc->count[commnode[i]];
873 if (atc->count[atc->nodeid] + nsend != n)
875 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"
876 "This usually means that your system is not well equilibrated.",
877 n - (atc->count[atc->nodeid] + nsend),
878 pme->nodeid, 'x'+atc->dimind);
881 if (nsend > pme->buf_nalloc)
883 pme->buf_nalloc = over_alloc_dd(nsend);
884 srenew(pme->bufv, pme->buf_nalloc);
885 srenew(pme->bufr, pme->buf_nalloc);
888 atc->n = atc->count[atc->nodeid];
889 for (i = 0; i < nnodes_comm; i++)
891 scount = atc->count[commnode[i]];
892 /* Communicate the count */
895 fprintf(debug, "dimind %d PME node %d send to node %d: %d\n",
896 atc->dimind, atc->nodeid, commnode[i], scount);
898 pme_dd_sendrecv(atc, FALSE, i,
899 &scount, sizeof(int),
900 &atc->rcount[i], sizeof(int));
901 atc->n += atc->rcount[i];
904 pme_realloc_atomcomm_things(atc);
908 for (i = 0; i < n; i++)
911 if (node == atc->nodeid)
913 /* Copy direct to the receive buffer */
916 copy_rvec(x[i], atc->x[local_pos]);
918 atc->q[local_pos] = charge[i];
923 /* Copy to the send buffer */
926 copy_rvec(x[i], pme->bufv[buf_index[node]]);
928 pme->bufr[buf_index[node]] = charge[i];
934 for (i = 0; i < nnodes_comm; i++)
936 scount = atc->count[commnode[i]];
937 rcount = atc->rcount[i];
938 if (scount > 0 || rcount > 0)
942 /* Communicate the coordinates */
943 pme_dd_sendrecv(atc, FALSE, i,
944 pme->bufv[buf_pos], scount*sizeof(rvec),
945 atc->x[local_pos], rcount*sizeof(rvec));
947 /* Communicate the charges */
948 pme_dd_sendrecv(atc, FALSE, i,
949 pme->bufr+buf_pos, scount*sizeof(real),
950 atc->q+local_pos, rcount*sizeof(real));
952 local_pos += atc->rcount[i];
957 static void dd_pmeredist_f(gmx_pme_t pme, pme_atomcomm_t *atc,
961 int *commnode, *buf_index;
962 int nnodes_comm, local_pos, buf_pos, i, scount, rcount, node;
964 commnode = atc->node_dest;
965 buf_index = atc->buf_index;
967 nnodes_comm = min(2*atc->maxshift, atc->nslab-1);
969 local_pos = atc->count[atc->nodeid];
971 for (i = 0; i < nnodes_comm; i++)
973 scount = atc->rcount[i];
974 rcount = atc->count[commnode[i]];
975 if (scount > 0 || rcount > 0)
977 /* Communicate the forces */
978 pme_dd_sendrecv(atc, TRUE, i,
979 atc->f[local_pos], scount*sizeof(rvec),
980 pme->bufv[buf_pos], rcount*sizeof(rvec));
983 buf_index[commnode[i]] = buf_pos;
990 for (i = 0; i < n; i++)
993 if (node == atc->nodeid)
995 /* Add from the local force array */
996 rvec_inc(f[i], atc->f[local_pos]);
1001 /* Add from the receive buffer */
1002 rvec_inc(f[i], pme->bufv[buf_index[node]]);
1009 for (i = 0; i < n; i++)
1012 if (node == atc->nodeid)
1014 /* Copy from the local force array */
1015 copy_rvec(atc->f[local_pos], f[i]);
1020 /* Copy from the receive buffer */
1021 copy_rvec(pme->bufv[buf_index[node]], f[i]);
1029 static void gmx_sum_qgrid_dd(gmx_pme_t pme, real *grid, int direction)
1031 pme_overlap_t *overlap;
1032 int send_index0, send_nindex;
1033 int recv_index0, recv_nindex;
1035 int i, j, k, ix, iy, iz, icnt;
1036 int ipulse, send_id, recv_id, datasize;
1038 real *sendptr, *recvptr;
1040 /* Start with minor-rank communication. This is a bit of a pain since it is not contiguous */
1041 overlap = &pme->overlap[1];
1043 for (ipulse = 0; ipulse < overlap->noverlap_nodes; ipulse++)
1045 /* Since we have already (un)wrapped the overlap in the z-dimension,
1046 * we only have to communicate 0 to nkz (not pmegrid_nz).
1048 if (direction == GMX_SUM_QGRID_FORWARD)
1050 send_id = overlap->send_id[ipulse];
1051 recv_id = overlap->recv_id[ipulse];
1052 send_index0 = overlap->comm_data[ipulse].send_index0;
1053 send_nindex = overlap->comm_data[ipulse].send_nindex;
1054 recv_index0 = overlap->comm_data[ipulse].recv_index0;
1055 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
1059 send_id = overlap->recv_id[ipulse];
1060 recv_id = overlap->send_id[ipulse];
1061 send_index0 = overlap->comm_data[ipulse].recv_index0;
1062 send_nindex = overlap->comm_data[ipulse].recv_nindex;
1063 recv_index0 = overlap->comm_data[ipulse].send_index0;
1064 recv_nindex = overlap->comm_data[ipulse].send_nindex;
1067 /* Copy data to contiguous send buffer */
1070 fprintf(debug, "PME send node %d %d -> %d grid start %d Communicating %d to %d\n",
1071 pme->nodeid, overlap->nodeid, send_id,
1072 pme->pmegrid_start_iy,
1073 send_index0-pme->pmegrid_start_iy,
1074 send_index0-pme->pmegrid_start_iy+send_nindex);
1077 for (i = 0; i < pme->pmegrid_nx; i++)
1080 for (j = 0; j < send_nindex; j++)
1082 iy = j + send_index0 - pme->pmegrid_start_iy;
1083 for (k = 0; k < pme->nkz; k++)
1086 overlap->sendbuf[icnt++] = grid[ix*(pme->pmegrid_ny*pme->pmegrid_nz)+iy*(pme->pmegrid_nz)+iz];
1091 datasize = pme->pmegrid_nx * pme->nkz;
1093 MPI_Sendrecv(overlap->sendbuf, send_nindex*datasize, GMX_MPI_REAL,
1095 overlap->recvbuf, recv_nindex*datasize, GMX_MPI_REAL,
1097 overlap->mpi_comm, &stat);
1099 /* Get data from contiguous recv buffer */
1102 fprintf(debug, "PME recv node %d %d <- %d grid start %d Communicating %d to %d\n",
1103 pme->nodeid, overlap->nodeid, recv_id,
1104 pme->pmegrid_start_iy,
1105 recv_index0-pme->pmegrid_start_iy,
1106 recv_index0-pme->pmegrid_start_iy+recv_nindex);
1109 for (i = 0; i < pme->pmegrid_nx; i++)
1112 for (j = 0; j < recv_nindex; j++)
1114 iy = j + recv_index0 - pme->pmegrid_start_iy;
1115 for (k = 0; k < pme->nkz; k++)
1118 if (direction == GMX_SUM_QGRID_FORWARD)
1120 grid[ix*(pme->pmegrid_ny*pme->pmegrid_nz)+iy*(pme->pmegrid_nz)+iz] += overlap->recvbuf[icnt++];
1124 grid[ix*(pme->pmegrid_ny*pme->pmegrid_nz)+iy*(pme->pmegrid_nz)+iz] = overlap->recvbuf[icnt++];
1131 /* Major dimension is easier, no copying required,
1132 * but we might have to sum to separate array.
1133 * Since we don't copy, we have to communicate up to pmegrid_nz,
1134 * not nkz as for the minor direction.
1136 overlap = &pme->overlap[0];
1138 for (ipulse = 0; ipulse < overlap->noverlap_nodes; ipulse++)
1140 if (direction == GMX_SUM_QGRID_FORWARD)
1142 send_id = overlap->send_id[ipulse];
1143 recv_id = overlap->recv_id[ipulse];
1144 send_index0 = overlap->comm_data[ipulse].send_index0;
1145 send_nindex = overlap->comm_data[ipulse].send_nindex;
1146 recv_index0 = overlap->comm_data[ipulse].recv_index0;
1147 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
1148 recvptr = overlap->recvbuf;
1152 send_id = overlap->recv_id[ipulse];
1153 recv_id = overlap->send_id[ipulse];
1154 send_index0 = overlap->comm_data[ipulse].recv_index0;
1155 send_nindex = overlap->comm_data[ipulse].recv_nindex;
1156 recv_index0 = overlap->comm_data[ipulse].send_index0;
1157 recv_nindex = overlap->comm_data[ipulse].send_nindex;
1158 recvptr = grid + (recv_index0-pme->pmegrid_start_ix)*(pme->pmegrid_ny*pme->pmegrid_nz);
1161 sendptr = grid + (send_index0-pme->pmegrid_start_ix)*(pme->pmegrid_ny*pme->pmegrid_nz);
1162 datasize = pme->pmegrid_ny * pme->pmegrid_nz;
1166 fprintf(debug, "PME send node %d %d -> %d grid start %d Communicating %d to %d\n",
1167 pme->nodeid, overlap->nodeid, send_id,
1168 pme->pmegrid_start_ix,
1169 send_index0-pme->pmegrid_start_ix,
1170 send_index0-pme->pmegrid_start_ix+send_nindex);
1171 fprintf(debug, "PME recv node %d %d <- %d grid start %d Communicating %d to %d\n",
1172 pme->nodeid, overlap->nodeid, recv_id,
1173 pme->pmegrid_start_ix,
1174 recv_index0-pme->pmegrid_start_ix,
1175 recv_index0-pme->pmegrid_start_ix+recv_nindex);
1178 MPI_Sendrecv(sendptr, send_nindex*datasize, GMX_MPI_REAL,
1180 recvptr, recv_nindex*datasize, GMX_MPI_REAL,
1182 overlap->mpi_comm, &stat);
1184 /* ADD data from contiguous recv buffer */
1185 if (direction == GMX_SUM_QGRID_FORWARD)
1187 p = grid + (recv_index0-pme->pmegrid_start_ix)*(pme->pmegrid_ny*pme->pmegrid_nz);
1188 for (i = 0; i < recv_nindex*datasize; i++)
1190 p[i] += overlap->recvbuf[i];
1198 static int copy_pmegrid_to_fftgrid(gmx_pme_t pme, real *pmegrid, real *fftgrid, int grid_index)
1200 ivec local_fft_ndata, local_fft_offset, local_fft_size;
1201 ivec local_pme_size;
1205 /* Dimensions should be identical for A/B grid, so we just use A here */
1206 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
1211 local_pme_size[0] = pme->pmegrid_nx;
1212 local_pme_size[1] = pme->pmegrid_ny;
1213 local_pme_size[2] = pme->pmegrid_nz;
1215 /* The fftgrid is always 'justified' to the lower-left corner of the PME grid,
1216 the offset is identical, and the PME grid always has more data (due to overlap)
1221 char fn[STRLEN], format[STRLEN];
1223 sprintf(fn, "pmegrid%d.pdb", pme->nodeid);
1224 fp = gmx_ffopen(fn, "w");
1225 sprintf(fn, "pmegrid%d.txt", pme->nodeid);
1226 fp2 = gmx_ffopen(fn, "w");
1227 sprintf(format, "%s%s\n", pdbformat, "%6.2f%6.2f");
1230 for (ix = 0; ix < local_fft_ndata[XX]; ix++)
1232 for (iy = 0; iy < local_fft_ndata[YY]; iy++)
1234 for (iz = 0; iz < local_fft_ndata[ZZ]; iz++)
1236 pmeidx = ix*(local_pme_size[YY]*local_pme_size[ZZ])+iy*(local_pme_size[ZZ])+iz;
1237 fftidx = ix*(local_fft_size[YY]*local_fft_size[ZZ])+iy*(local_fft_size[ZZ])+iz;
1238 fftgrid[fftidx] = pmegrid[pmeidx];
1240 val = 100*pmegrid[pmeidx];
1241 if (pmegrid[pmeidx] != 0)
1243 fprintf(fp, format, "ATOM", pmeidx, "CA", "GLY", ' ', pmeidx, ' ',
1244 5.0*ix, 5.0*iy, 5.0*iz, 1.0, val);
1246 if (pmegrid[pmeidx] != 0)
1248 fprintf(fp2, "%-12s %5d %5d %5d %12.5e\n",
1250 pme->pmegrid_start_ix + ix,
1251 pme->pmegrid_start_iy + iy,
1252 pme->pmegrid_start_iz + iz,
1268 static gmx_cycles_t omp_cyc_start()
1270 return gmx_cycles_read();
1273 static gmx_cycles_t omp_cyc_end(gmx_cycles_t c)
1275 return gmx_cycles_read() - c;
1279 static int copy_fftgrid_to_pmegrid(gmx_pme_t pme, const real *fftgrid, real *pmegrid, int grid_index,
1280 int nthread, int thread)
1282 ivec local_fft_ndata, local_fft_offset, local_fft_size;
1283 ivec local_pme_size;
1284 int ixy0, ixy1, ixy, ix, iy, iz;
1286 #ifdef PME_TIME_THREADS
1288 static double cs1 = 0;
1292 #ifdef PME_TIME_THREADS
1293 c1 = omp_cyc_start();
1295 /* Dimensions should be identical for A/B grid, so we just use A here */
1296 gmx_parallel_3dfft_real_limits(pme->pfft_setup[grid_index],
1301 local_pme_size[0] = pme->pmegrid_nx;
1302 local_pme_size[1] = pme->pmegrid_ny;
1303 local_pme_size[2] = pme->pmegrid_nz;
1305 /* The fftgrid is always 'justified' to the lower-left corner of the PME grid,
1306 the offset is identical, and the PME grid always has more data (due to overlap)
1308 ixy0 = ((thread )*local_fft_ndata[XX]*local_fft_ndata[YY])/nthread;
1309 ixy1 = ((thread+1)*local_fft_ndata[XX]*local_fft_ndata[YY])/nthread;
1311 for (ixy = ixy0; ixy < ixy1; ixy++)
1313 ix = ixy/local_fft_ndata[YY];
1314 iy = ixy - ix*local_fft_ndata[YY];
1316 pmeidx = (ix*local_pme_size[YY] + iy)*local_pme_size[ZZ];
1317 fftidx = (ix*local_fft_size[YY] + iy)*local_fft_size[ZZ];
1318 for (iz = 0; iz < local_fft_ndata[ZZ]; iz++)
1320 pmegrid[pmeidx+iz] = fftgrid[fftidx+iz];
1324 #ifdef PME_TIME_THREADS
1325 c1 = omp_cyc_end(c1);
1330 printf("copy %.2f\n", cs1*1e-9);
1338 static void wrap_periodic_pmegrid(gmx_pme_t pme, real *pmegrid)
1340 int nx, ny, nz, pnx, pny, pnz, ny_x, overlap, ix, iy, iz;
1346 pnx = pme->pmegrid_nx;
1347 pny = pme->pmegrid_ny;
1348 pnz = pme->pmegrid_nz;
1350 overlap = pme->pme_order - 1;
1352 /* Add periodic overlap in z */
1353 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1355 for (iy = 0; iy < pme->pmegrid_ny; iy++)
1357 for (iz = 0; iz < overlap; iz++)
1359 pmegrid[(ix*pny+iy)*pnz+iz] +=
1360 pmegrid[(ix*pny+iy)*pnz+nz+iz];
1365 if (pme->nnodes_minor == 1)
1367 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1369 for (iy = 0; iy < overlap; iy++)
1371 for (iz = 0; iz < nz; iz++)
1373 pmegrid[(ix*pny+iy)*pnz+iz] +=
1374 pmegrid[(ix*pny+ny+iy)*pnz+iz];
1380 if (pme->nnodes_major == 1)
1382 ny_x = (pme->nnodes_minor == 1 ? ny : pme->pmegrid_ny);
1384 for (ix = 0; ix < overlap; ix++)
1386 for (iy = 0; iy < ny_x; iy++)
1388 for (iz = 0; iz < nz; iz++)
1390 pmegrid[(ix*pny+iy)*pnz+iz] +=
1391 pmegrid[((nx+ix)*pny+iy)*pnz+iz];
1399 static void unwrap_periodic_pmegrid(gmx_pme_t pme, real *pmegrid)
1401 int nx, ny, nz, pnx, pny, pnz, ny_x, overlap, ix;
1407 pnx = pme->pmegrid_nx;
1408 pny = pme->pmegrid_ny;
1409 pnz = pme->pmegrid_nz;
1411 overlap = pme->pme_order - 1;
1413 if (pme->nnodes_major == 1)
1415 ny_x = (pme->nnodes_minor == 1 ? ny : pme->pmegrid_ny);
1417 for (ix = 0; ix < overlap; ix++)
1421 for (iy = 0; iy < ny_x; iy++)
1423 for (iz = 0; iz < nz; iz++)
1425 pmegrid[((nx+ix)*pny+iy)*pnz+iz] =
1426 pmegrid[(ix*pny+iy)*pnz+iz];
1432 if (pme->nnodes_minor == 1)
1434 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
1435 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1439 for (iy = 0; iy < overlap; iy++)
1441 for (iz = 0; iz < nz; iz++)
1443 pmegrid[(ix*pny+ny+iy)*pnz+iz] =
1444 pmegrid[(ix*pny+iy)*pnz+iz];
1450 /* Copy periodic overlap in z */
1451 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
1452 for (ix = 0; ix < pme->pmegrid_nx; ix++)
1456 for (iy = 0; iy < pme->pmegrid_ny; iy++)
1458 for (iz = 0; iz < overlap; iz++)
1460 pmegrid[(ix*pny+iy)*pnz+nz+iz] =
1461 pmegrid[(ix*pny+iy)*pnz+iz];
1468 /* This has to be a macro to enable full compiler optimization with xlC (and probably others too) */
1469 #define DO_BSPLINE(order) \
1470 for (ithx = 0; (ithx < order); ithx++) \
1472 index_x = (i0+ithx)*pny*pnz; \
1473 valx = qn*thx[ithx]; \
1475 for (ithy = 0; (ithy < order); ithy++) \
1477 valxy = valx*thy[ithy]; \
1478 index_xy = index_x+(j0+ithy)*pnz; \
1480 for (ithz = 0; (ithz < order); ithz++) \
1482 index_xyz = index_xy+(k0+ithz); \
1483 grid[index_xyz] += valxy*thz[ithz]; \
1489 static void spread_q_bsplines_thread(pmegrid_t *pmegrid,
1490 pme_atomcomm_t *atc,
1491 splinedata_t *spline,
1492 pme_spline_work_t gmx_unused *work)
1495 /* spread charges from home atoms to local grid */
1498 int b, i, nn, n, ithx, ithy, ithz, i0, j0, k0;
1500 int order, norder, index_x, index_xy, index_xyz;
1501 real valx, valxy, qn;
1502 real *thx, *thy, *thz;
1503 int localsize, bndsize;
1504 int pnx, pny, pnz, ndatatot;
1505 int offx, offy, offz;
1507 #if defined PME_SIMD4_SPREAD_GATHER && !defined PME_SIMD4_UNALIGNED
1508 real thz_buffer[12], *thz_aligned;
1510 thz_aligned = gmx_simd4_align_real(thz_buffer);
1513 pnx = pmegrid->s[XX];
1514 pny = pmegrid->s[YY];
1515 pnz = pmegrid->s[ZZ];
1517 offx = pmegrid->offset[XX];
1518 offy = pmegrid->offset[YY];
1519 offz = pmegrid->offset[ZZ];
1521 ndatatot = pnx*pny*pnz;
1522 grid = pmegrid->grid;
1523 for (i = 0; i < ndatatot; i++)
1528 order = pmegrid->order;
1530 for (nn = 0; nn < spline->n; nn++)
1532 n = spline->ind[nn];
1537 idxptr = atc->idx[n];
1540 i0 = idxptr[XX] - offx;
1541 j0 = idxptr[YY] - offy;
1542 k0 = idxptr[ZZ] - offz;
1544 thx = spline->theta[XX] + norder;
1545 thy = spline->theta[YY] + norder;
1546 thz = spline->theta[ZZ] + norder;
1551 #ifdef PME_SIMD4_SPREAD_GATHER
1552 #ifdef PME_SIMD4_UNALIGNED
1553 #define PME_SPREAD_SIMD4_ORDER4
1555 #define PME_SPREAD_SIMD4_ALIGNED
1558 #include "pme_simd4.h"
1564 #ifdef PME_SIMD4_SPREAD_GATHER
1565 #define PME_SPREAD_SIMD4_ALIGNED
1567 #include "pme_simd4.h"
1580 static void set_grid_alignment(int gmx_unused *pmegrid_nz, int gmx_unused pme_order)
1582 #ifdef PME_SIMD4_SPREAD_GATHER
1584 #ifndef PME_SIMD4_UNALIGNED
1589 /* Round nz up to a multiple of 4 to ensure alignment */
1590 *pmegrid_nz = ((*pmegrid_nz + 3) & ~3);
1595 static void set_gridsize_alignment(int gmx_unused *gridsize, int gmx_unused pme_order)
1597 #ifdef PME_SIMD4_SPREAD_GATHER
1598 #ifndef PME_SIMD4_UNALIGNED
1601 /* Add extra elements to ensured aligned operations do not go
1602 * beyond the allocated grid size.
1603 * Note that for pme_order=5, the pme grid z-size alignment
1604 * ensures that we will not go beyond the grid size.
1612 static void pmegrid_init(pmegrid_t *grid,
1613 int cx, int cy, int cz,
1614 int x0, int y0, int z0,
1615 int x1, int y1, int z1,
1616 gmx_bool set_alignment,
1625 grid->offset[XX] = x0;
1626 grid->offset[YY] = y0;
1627 grid->offset[ZZ] = z0;
1628 grid->n[XX] = x1 - x0 + pme_order - 1;
1629 grid->n[YY] = y1 - y0 + pme_order - 1;
1630 grid->n[ZZ] = z1 - z0 + pme_order - 1;
1631 copy_ivec(grid->n, grid->s);
1634 set_grid_alignment(&nz, pme_order);
1639 else if (nz != grid->s[ZZ])
1641 gmx_incons("pmegrid_init call with an unaligned z size");
1644 grid->order = pme_order;
1647 gridsize = grid->s[XX]*grid->s[YY]*grid->s[ZZ];
1648 set_gridsize_alignment(&gridsize, pme_order);
1649 snew_aligned(grid->grid, gridsize, SIMD4_ALIGNMENT);
1657 static int div_round_up(int enumerator, int denominator)
1659 return (enumerator + denominator - 1)/denominator;
1662 static void make_subgrid_division(const ivec n, int ovl, int nthread,
1665 int gsize_opt, gsize;
1670 for (nsx = 1; nsx <= nthread; nsx++)
1672 if (nthread % nsx == 0)
1674 for (nsy = 1; nsy <= nthread; nsy++)
1676 if (nsx*nsy <= nthread && nthread % (nsx*nsy) == 0)
1678 nsz = nthread/(nsx*nsy);
1680 /* Determine the number of grid points per thread */
1682 (div_round_up(n[XX], nsx) + ovl)*
1683 (div_round_up(n[YY], nsy) + ovl)*
1684 (div_round_up(n[ZZ], nsz) + ovl);
1686 /* Minimize the number of grids points per thread
1687 * and, secondarily, the number of cuts in minor dimensions.
1689 if (gsize_opt == -1 ||
1690 gsize < gsize_opt ||
1691 (gsize == gsize_opt &&
1692 (nsz < nsub[ZZ] || (nsz == nsub[ZZ] && nsy < nsub[YY]))))
1704 env = getenv("GMX_PME_THREAD_DIVISION");
1707 sscanf(env, "%d %d %d", &nsub[XX], &nsub[YY], &nsub[ZZ]);
1710 if (nsub[XX]*nsub[YY]*nsub[ZZ] != nthread)
1712 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);
1716 static void pmegrids_init(pmegrids_t *grids,
1717 int nx, int ny, int nz, int nz_base,
1719 gmx_bool bUseThreads,
1724 ivec n, n_base, g0, g1;
1725 int t, x, y, z, d, i, tfac;
1726 int max_comm_lines = -1;
1728 n[XX] = nx - (pme_order - 1);
1729 n[YY] = ny - (pme_order - 1);
1730 n[ZZ] = nz - (pme_order - 1);
1732 copy_ivec(n, n_base);
1733 n_base[ZZ] = nz_base;
1735 pmegrid_init(&grids->grid, 0, 0, 0, 0, 0, 0, n[XX], n[YY], n[ZZ], FALSE, pme_order,
1738 grids->nthread = nthread;
1740 make_subgrid_division(n_base, pme_order-1, grids->nthread, grids->nc);
1747 for (d = 0; d < DIM; d++)
1749 nst[d] = div_round_up(n[d], grids->nc[d]) + pme_order - 1;
1751 set_grid_alignment(&nst[ZZ], pme_order);
1755 fprintf(debug, "pmegrid thread local division: %d x %d x %d\n",
1756 grids->nc[XX], grids->nc[YY], grids->nc[ZZ]);
1757 fprintf(debug, "pmegrid %d %d %d max thread pmegrid %d %d %d\n",
1759 nst[XX], nst[YY], nst[ZZ]);
1762 snew(grids->grid_th, grids->nthread);
1764 gridsize = nst[XX]*nst[YY]*nst[ZZ];
1765 set_gridsize_alignment(&gridsize, pme_order);
1766 snew_aligned(grids->grid_all,
1767 grids->nthread*gridsize+(grids->nthread+1)*GMX_CACHE_SEP,
1770 for (x = 0; x < grids->nc[XX]; x++)
1772 for (y = 0; y < grids->nc[YY]; y++)
1774 for (z = 0; z < grids->nc[ZZ]; z++)
1776 pmegrid_init(&grids->grid_th[t],
1778 (n[XX]*(x ))/grids->nc[XX],
1779 (n[YY]*(y ))/grids->nc[YY],
1780 (n[ZZ]*(z ))/grids->nc[ZZ],
1781 (n[XX]*(x+1))/grids->nc[XX],
1782 (n[YY]*(y+1))/grids->nc[YY],
1783 (n[ZZ]*(z+1))/grids->nc[ZZ],
1786 grids->grid_all+GMX_CACHE_SEP+t*(gridsize+GMX_CACHE_SEP));
1794 grids->grid_th = NULL;
1797 snew(grids->g2t, DIM);
1799 for (d = DIM-1; d >= 0; d--)
1801 snew(grids->g2t[d], n[d]);
1803 for (i = 0; i < n[d]; i++)
1805 /* The second check should match the parameters
1806 * of the pmegrid_init call above.
1808 while (t + 1 < grids->nc[d] && i >= (n[d]*(t+1))/grids->nc[d])
1812 grids->g2t[d][i] = t*tfac;
1815 tfac *= grids->nc[d];
1819 case XX: max_comm_lines = overlap_x; break;
1820 case YY: max_comm_lines = overlap_y; break;
1821 case ZZ: max_comm_lines = pme_order - 1; break;
1823 grids->nthread_comm[d] = 0;
1824 while ((n[d]*grids->nthread_comm[d])/grids->nc[d] < max_comm_lines &&
1825 grids->nthread_comm[d] < grids->nc[d])
1827 grids->nthread_comm[d]++;
1831 fprintf(debug, "pmegrid thread grid communication range in %c: %d\n",
1832 'x'+d, grids->nthread_comm[d]);
1834 /* It should be possible to make grids->nthread_comm[d]==grids->nc[d]
1835 * work, but this is not a problematic restriction.
1837 if (grids->nc[d] > 1 && grids->nthread_comm[d] > grids->nc[d])
1839 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);
1845 static void pmegrids_destroy(pmegrids_t *grids)
1849 if (grids->grid.grid != NULL)
1851 sfree(grids->grid.grid);
1853 if (grids->nthread > 0)
1855 for (t = 0; t < grids->nthread; t++)
1857 sfree(grids->grid_th[t].grid);
1859 sfree(grids->grid_th);
1865 static void realloc_work(pme_work_t *work, int nkx)
1869 if (nkx > work->nalloc)
1872 srenew(work->mhx, work->nalloc);
1873 srenew(work->mhy, work->nalloc);
1874 srenew(work->mhz, work->nalloc);
1875 srenew(work->m2, work->nalloc);
1876 /* Allocate an aligned pointer for SIMD operations, including extra
1877 * elements at the end for padding.
1879 #ifdef PME_SIMD_SOLVE
1880 simd_width = GMX_SIMD_REAL_WIDTH;
1882 /* We can use any alignment, apart from 0, so we use 4 */
1885 sfree_aligned(work->denom);
1886 sfree_aligned(work->tmp1);
1887 sfree_aligned(work->tmp2);
1888 sfree_aligned(work->eterm);
1889 snew_aligned(work->denom, work->nalloc+simd_width, simd_width*sizeof(real));
1890 snew_aligned(work->tmp1, work->nalloc+simd_width, simd_width*sizeof(real));
1891 snew_aligned(work->tmp2, work->nalloc+simd_width, simd_width*sizeof(real));
1892 snew_aligned(work->eterm, work->nalloc+simd_width, simd_width*sizeof(real));
1893 srenew(work->m2inv, work->nalloc);
1898 static void free_work(pme_work_t *work)
1904 sfree_aligned(work->denom);
1905 sfree_aligned(work->tmp1);
1906 sfree_aligned(work->tmp2);
1907 sfree_aligned(work->eterm);
1912 #if defined PME_SIMD_SOLVE && defined GMX_SIMD_HAVE_EXP
1913 /* Calculate exponentials through SIMD */
1914 inline static void calc_exponentials_q(int gmx_unused start, int end, real f, real *d_aligned, real *r_aligned, real *e_aligned)
1917 const gmx_simd_real_t two = gmx_simd_set1_r(2.0);
1918 gmx_simd_real_t f_simd;
1920 gmx_simd_real_t tmp_d1, d_inv, tmp_r, tmp_e;
1922 f_simd = gmx_simd_set1_r(f);
1923 /* We only need to calculate from start. But since start is 0 or 1
1924 * and we want to use aligned loads/stores, we always start from 0.
1926 for (kx = 0; kx < end; kx += GMX_SIMD_REAL_WIDTH)
1928 tmp_d1 = gmx_simd_load_r(d_aligned+kx);
1929 d_inv = gmx_simd_inv_r(tmp_d1);
1930 tmp_r = gmx_simd_load_r(r_aligned+kx);
1931 tmp_r = gmx_simd_exp_r(tmp_r);
1932 tmp_e = gmx_simd_mul_r(f_simd, d_inv);
1933 tmp_e = gmx_simd_mul_r(tmp_e, tmp_r);
1934 gmx_simd_store_r(e_aligned+kx, tmp_e);
1939 inline static void calc_exponentials_q(int start, int end, real f, real *d, real *r, real *e)
1942 for (kx = start; kx < end; kx++)
1946 for (kx = start; kx < end; kx++)
1950 for (kx = start; kx < end; kx++)
1952 e[kx] = f*r[kx]*d[kx];
1957 #if defined PME_SIMD_SOLVE && defined GMX_SIMD_HAVE_ERFC
1958 /* Calculate exponentials through SIMD */
1959 inline static void calc_exponentials_lj(int gmx_unused start, int end, real *r_aligned, real *factor_aligned, real *d_aligned)
1961 gmx_simd_real_t tmp_r, tmp_d, tmp_fac, d_inv, tmp_mk;
1962 const gmx_simd_real_t sqr_PI = gmx_simd_sqrt_r(gmx_simd_set1_r(M_PI));
1964 for (kx = 0; kx < end; kx += GMX_SIMD_REAL_WIDTH)
1966 /* We only need to calculate from start. But since start is 0 or 1
1967 * and we want to use aligned loads/stores, we always start from 0.
1969 tmp_d = gmx_simd_load_r(d_aligned+kx);
1970 d_inv = gmx_simd_inv_r(tmp_d);
1971 gmx_simd_store_r(d_aligned+kx, d_inv);
1972 tmp_r = gmx_simd_load_r(r_aligned+kx);
1973 tmp_r = gmx_simd_exp_r(tmp_r);
1974 gmx_simd_store_r(r_aligned+kx, tmp_r);
1975 tmp_mk = gmx_simd_load_r(factor_aligned+kx);
1976 tmp_fac = gmx_simd_mul_r(sqr_PI, gmx_simd_mul_r(tmp_mk, gmx_simd_erfc_r(tmp_mk)));
1977 gmx_simd_store_r(factor_aligned+kx, tmp_fac);
1981 inline static void calc_exponentials_lj(int start, int end, real *r, real *tmp2, real *d)
1985 for (kx = start; kx < end; kx++)
1990 for (kx = start; kx < end; kx++)
1995 for (kx = start; kx < end; kx++)
1998 tmp2[kx] = sqrt(M_PI)*mk*gmx_erfc(mk);
2003 static int solve_pme_yzx(gmx_pme_t pme, t_complex *grid,
2004 real ewaldcoeff, real vol,
2006 int nthread, int thread)
2008 /* do recip sum over local cells in grid */
2009 /* y major, z middle, x minor or continuous */
2011 int kx, ky, kz, maxkx, maxky, maxkz;
2012 int nx, ny, nz, iyz0, iyz1, iyz, iy, iz, kxstart, kxend;
2014 real factor = M_PI*M_PI/(ewaldcoeff*ewaldcoeff);
2015 real ets2, struct2, vfactor, ets2vf;
2016 real d1, d2, energy = 0;
2018 real virxx = 0, virxy = 0, virxz = 0, viryy = 0, viryz = 0, virzz = 0;
2019 real rxx, ryx, ryy, rzx, rzy, rzz;
2021 real *mhx, *mhy, *mhz, *m2, *denom, *tmp1, *eterm, *m2inv;
2022 real mhxk, mhyk, mhzk, m2k;
2025 ivec local_ndata, local_offset, local_size;
2028 elfac = ONE_4PI_EPS0/pme->epsilon_r;
2034 /* Dimensions should be identical for A/B grid, so we just use A here */
2035 gmx_parallel_3dfft_complex_limits(pme->pfft_setup[PME_GRID_QA],
2041 rxx = pme->recipbox[XX][XX];
2042 ryx = pme->recipbox[YY][XX];
2043 ryy = pme->recipbox[YY][YY];
2044 rzx = pme->recipbox[ZZ][XX];
2045 rzy = pme->recipbox[ZZ][YY];
2046 rzz = pme->recipbox[ZZ][ZZ];
2052 work = &pme->work[thread];
2057 denom = work->denom;
2059 eterm = work->eterm;
2060 m2inv = work->m2inv;
2062 iyz0 = local_ndata[YY]*local_ndata[ZZ]* thread /nthread;
2063 iyz1 = local_ndata[YY]*local_ndata[ZZ]*(thread+1)/nthread;
2065 for (iyz = iyz0; iyz < iyz1; iyz++)
2067 iy = iyz/local_ndata[ZZ];
2068 iz = iyz - iy*local_ndata[ZZ];
2070 ky = iy + local_offset[YY];
2081 by = M_PI*vol*pme->bsp_mod[YY][ky];
2083 kz = iz + local_offset[ZZ];
2087 bz = pme->bsp_mod[ZZ][kz];
2089 /* 0.5 correction for corner points */
2091 if (kz == 0 || kz == (nz+1)/2)
2096 p0 = grid + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2098 /* We should skip the k-space point (0,0,0) */
2099 /* Note that since here x is the minor index, local_offset[XX]=0 */
2100 if (local_offset[XX] > 0 || ky > 0 || kz > 0)
2102 kxstart = local_offset[XX];
2106 kxstart = local_offset[XX] + 1;
2109 kxend = local_offset[XX] + local_ndata[XX];
2113 /* More expensive inner loop, especially because of the storage
2114 * of the mh elements in array's.
2115 * Because x is the minor grid index, all mh elements
2116 * depend on kx for triclinic unit cells.
2119 /* Two explicit loops to avoid a conditional inside the loop */
2120 for (kx = kxstart; kx < maxkx; kx++)
2125 mhyk = mx * ryx + my * ryy;
2126 mhzk = mx * rzx + my * rzy + mz * rzz;
2127 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2132 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2133 tmp1[kx] = -factor*m2k;
2136 for (kx = maxkx; kx < kxend; kx++)
2141 mhyk = mx * ryx + my * ryy;
2142 mhzk = mx * rzx + my * rzy + mz * rzz;
2143 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2148 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2149 tmp1[kx] = -factor*m2k;
2152 for (kx = kxstart; kx < kxend; kx++)
2154 m2inv[kx] = 1.0/m2[kx];
2157 calc_exponentials_q(kxstart, kxend, elfac, denom, tmp1, eterm);
2159 for (kx = kxstart; kx < kxend; kx++, p0++)
2164 p0->re = d1*eterm[kx];
2165 p0->im = d2*eterm[kx];
2167 struct2 = 2.0*(d1*d1+d2*d2);
2169 tmp1[kx] = eterm[kx]*struct2;
2172 for (kx = kxstart; kx < kxend; kx++)
2174 ets2 = corner_fac*tmp1[kx];
2175 vfactor = (factor*m2[kx] + 1.0)*2.0*m2inv[kx];
2178 ets2vf = ets2*vfactor;
2179 virxx += ets2vf*mhx[kx]*mhx[kx] - ets2;
2180 virxy += ets2vf*mhx[kx]*mhy[kx];
2181 virxz += ets2vf*mhx[kx]*mhz[kx];
2182 viryy += ets2vf*mhy[kx]*mhy[kx] - ets2;
2183 viryz += ets2vf*mhy[kx]*mhz[kx];
2184 virzz += ets2vf*mhz[kx]*mhz[kx] - ets2;
2189 /* We don't need to calculate the energy and the virial.
2190 * In this case the triclinic overhead is small.
2193 /* Two explicit loops to avoid a conditional inside the loop */
2195 for (kx = kxstart; kx < maxkx; kx++)
2200 mhyk = mx * ryx + my * ryy;
2201 mhzk = mx * rzx + my * rzy + mz * rzz;
2202 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2203 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2204 tmp1[kx] = -factor*m2k;
2207 for (kx = maxkx; kx < kxend; kx++)
2212 mhyk = mx * ryx + my * ryy;
2213 mhzk = mx * rzx + my * rzy + mz * rzz;
2214 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2215 denom[kx] = m2k*bz*by*pme->bsp_mod[XX][kx];
2216 tmp1[kx] = -factor*m2k;
2219 calc_exponentials_q(kxstart, kxend, elfac, denom, tmp1, eterm);
2221 for (kx = kxstart; kx < kxend; kx++, p0++)
2226 p0->re = d1*eterm[kx];
2227 p0->im = d2*eterm[kx];
2234 /* Update virial with local values.
2235 * The virial is symmetric by definition.
2236 * this virial seems ok for isotropic scaling, but I'm
2237 * experiencing problems on semiisotropic membranes.
2238 * IS THAT COMMENT STILL VALID??? (DvdS, 2001/02/07).
2240 work->vir_q[XX][XX] = 0.25*virxx;
2241 work->vir_q[YY][YY] = 0.25*viryy;
2242 work->vir_q[ZZ][ZZ] = 0.25*virzz;
2243 work->vir_q[XX][YY] = work->vir_q[YY][XX] = 0.25*virxy;
2244 work->vir_q[XX][ZZ] = work->vir_q[ZZ][XX] = 0.25*virxz;
2245 work->vir_q[YY][ZZ] = work->vir_q[ZZ][YY] = 0.25*viryz;
2247 /* This energy should be corrected for a charged system */
2248 work->energy_q = 0.5*energy;
2251 /* Return the loop count */
2252 return local_ndata[YY]*local_ndata[XX];
2255 static int solve_pme_lj_yzx(gmx_pme_t pme, t_complex **grid, gmx_bool bLB,
2256 real ewaldcoeff, real vol,
2257 gmx_bool bEnerVir, int nthread, int thread)
2259 /* do recip sum over local cells in grid */
2260 /* y major, z middle, x minor or continuous */
2262 int kx, ky, kz, maxkx, maxky, maxkz;
2263 int nx, ny, nz, iy, iyz0, iyz1, iyz, iz, kxstart, kxend;
2265 real factor = M_PI*M_PI/(ewaldcoeff*ewaldcoeff);
2267 real eterm, vterm, d1, d2, energy = 0;
2269 real virxx = 0, virxy = 0, virxz = 0, viryy = 0, viryz = 0, virzz = 0;
2270 real rxx, ryx, ryy, rzx, rzy, rzz;
2271 real *mhx, *mhy, *mhz, *m2, *denom, *tmp1, *tmp2;
2272 real mhxk, mhyk, mhzk, m2k;
2277 ivec local_ndata, local_offset, local_size;
2282 /* Dimensions should be identical for A/B grid, so we just use A here */
2283 gmx_parallel_3dfft_complex_limits(pme->pfft_setup[PME_GRID_C6A],
2288 rxx = pme->recipbox[XX][XX];
2289 ryx = pme->recipbox[YY][XX];
2290 ryy = pme->recipbox[YY][YY];
2291 rzx = pme->recipbox[ZZ][XX];
2292 rzy = pme->recipbox[ZZ][YY];
2293 rzz = pme->recipbox[ZZ][ZZ];
2299 work = &pme->work[thread];
2304 denom = work->denom;
2308 iyz0 = local_ndata[YY]*local_ndata[ZZ]* thread /nthread;
2309 iyz1 = local_ndata[YY]*local_ndata[ZZ]*(thread+1)/nthread;
2311 for (iyz = iyz0; iyz < iyz1; iyz++)
2313 iy = iyz/local_ndata[ZZ];
2314 iz = iyz - iy*local_ndata[ZZ];
2316 ky = iy + local_offset[YY];
2327 by = 3.0*vol*pme->bsp_mod[YY][ky]
2328 / (M_PI*sqrt(M_PI)*ewaldcoeff*ewaldcoeff*ewaldcoeff);
2330 kz = iz + local_offset[ZZ];
2334 bz = pme->bsp_mod[ZZ][kz];
2336 /* 0.5 correction for corner points */
2338 if (kz == 0 || kz == (nz+1)/2)
2343 kxstart = local_offset[XX];
2344 kxend = local_offset[XX] + local_ndata[XX];
2347 /* More expensive inner loop, especially because of the
2348 * storage of the mh elements in array's. Because x is the
2349 * minor grid index, all mh elements depend on kx for
2350 * triclinic unit cells.
2353 /* Two explicit loops to avoid a conditional inside the loop */
2354 for (kx = kxstart; kx < maxkx; kx++)
2359 mhyk = mx * ryx + my * ryy;
2360 mhzk = mx * rzx + my * rzy + mz * rzz;
2361 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2366 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2367 tmp1[kx] = -factor*m2k;
2368 tmp2[kx] = sqrt(factor*m2k);
2371 for (kx = maxkx; kx < kxend; kx++)
2376 mhyk = mx * ryx + my * ryy;
2377 mhzk = mx * rzx + my * rzy + mz * rzz;
2378 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2383 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2384 tmp1[kx] = -factor*m2k;
2385 tmp2[kx] = sqrt(factor*m2k);
2388 calc_exponentials_lj(kxstart, kxend, tmp1, tmp2, denom);
2390 for (kx = kxstart; kx < kxend; kx++)
2392 m2k = factor*m2[kx];
2393 eterm = -((1.0 - 2.0*m2k)*tmp1[kx]
2394 + 2.0*m2k*tmp2[kx]);
2395 vterm = 3.0*(-tmp1[kx] + tmp2[kx]);
2396 tmp1[kx] = eterm*denom[kx];
2397 tmp2[kx] = vterm*denom[kx];
2405 p0 = grid[0] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2406 for (kx = kxstart; kx < kxend; kx++, p0++)
2416 struct2 = 2.0*(d1*d1+d2*d2);
2418 tmp1[kx] = eterm*struct2;
2419 tmp2[kx] = vterm*struct2;
2424 real *struct2 = denom;
2427 for (kx = kxstart; kx < kxend; kx++)
2431 /* Due to symmetry we only need to calculate 4 of the 7 terms */
2432 for (ig = 0; ig <= 3; ++ig)
2437 p0 = grid[ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2438 p1 = grid[6-ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2439 scale = 2.0*lb_scale_factor_symm[ig];
2440 for (kx = kxstart; kx < kxend; ++kx, ++p0, ++p1)
2442 struct2[kx] += scale*(p0->re*p1->re + p0->im*p1->im);
2446 for (ig = 0; ig <= 6; ++ig)
2450 p0 = grid[ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2451 for (kx = kxstart; kx < kxend; kx++, p0++)
2461 for (kx = kxstart; kx < kxend; kx++)
2466 tmp1[kx] = eterm*str2;
2467 tmp2[kx] = vterm*str2;
2471 for (kx = kxstart; kx < kxend; kx++)
2473 ets2 = corner_fac*tmp1[kx];
2474 vterm = 2.0*factor*tmp2[kx];
2476 ets2vf = corner_fac*vterm;
2477 virxx += ets2vf*mhx[kx]*mhx[kx] - ets2;
2478 virxy += ets2vf*mhx[kx]*mhy[kx];
2479 virxz += ets2vf*mhx[kx]*mhz[kx];
2480 viryy += ets2vf*mhy[kx]*mhy[kx] - ets2;
2481 viryz += ets2vf*mhy[kx]*mhz[kx];
2482 virzz += ets2vf*mhz[kx]*mhz[kx] - ets2;
2487 /* We don't need to calculate the energy and the virial.
2488 * In this case the triclinic overhead is small.
2491 /* Two explicit loops to avoid a conditional inside the loop */
2493 for (kx = kxstart; kx < maxkx; kx++)
2498 mhyk = mx * ryx + my * ryy;
2499 mhzk = mx * rzx + my * rzy + mz * rzz;
2500 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2502 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2503 tmp1[kx] = -factor*m2k;
2504 tmp2[kx] = sqrt(factor*m2k);
2507 for (kx = maxkx; kx < kxend; kx++)
2512 mhyk = mx * ryx + my * ryy;
2513 mhzk = mx * rzx + my * rzy + mz * rzz;
2514 m2k = mhxk*mhxk + mhyk*mhyk + mhzk*mhzk;
2516 denom[kx] = bz*by*pme->bsp_mod[XX][kx];
2517 tmp1[kx] = -factor*m2k;
2518 tmp2[kx] = sqrt(factor*m2k);
2521 calc_exponentials_lj(kxstart, kxend, tmp1, tmp2, denom);
2523 for (kx = kxstart; kx < kxend; kx++)
2525 m2k = factor*m2[kx];
2526 eterm = -((1.0 - 2.0*m2k)*tmp1[kx]
2527 + 2.0*m2k*tmp2[kx]);
2528 tmp1[kx] = eterm*denom[kx];
2530 gcount = (bLB ? 7 : 1);
2531 for (ig = 0; ig < gcount; ++ig)
2535 p0 = grid[ig] + iy*local_size[ZZ]*local_size[XX] + iz*local_size[XX];
2536 for (kx = kxstart; kx < kxend; kx++, p0++)
2551 work->vir_lj[XX][XX] = 0.25*virxx;
2552 work->vir_lj[YY][YY] = 0.25*viryy;
2553 work->vir_lj[ZZ][ZZ] = 0.25*virzz;
2554 work->vir_lj[XX][YY] = work->vir_lj[YY][XX] = 0.25*virxy;
2555 work->vir_lj[XX][ZZ] = work->vir_lj[ZZ][XX] = 0.25*virxz;
2556 work->vir_lj[YY][ZZ] = work->vir_lj[ZZ][YY] = 0.25*viryz;
2557 /* This energy should be corrected for a charged system */
2558 work->energy_lj = 0.5*energy;
2560 /* Return the loop count */
2561 return local_ndata[YY]*local_ndata[XX];
2564 static void get_pme_ener_vir_q(const gmx_pme_t pme, int nthread,
2565 real *mesh_energy, matrix vir)
2567 /* This function sums output over threads and should therefore
2568 * only be called after thread synchronization.
2572 *mesh_energy = pme->work[0].energy_q;
2573 copy_mat(pme->work[0].vir_q, vir);
2575 for (thread = 1; thread < nthread; thread++)
2577 *mesh_energy += pme->work[thread].energy_q;
2578 m_add(vir, pme->work[thread].vir_q, vir);
2582 static void get_pme_ener_vir_lj(const gmx_pme_t pme, int nthread,
2583 real *mesh_energy, matrix vir)
2585 /* This function sums output over threads and should therefore
2586 * only be called after thread synchronization.
2590 *mesh_energy = pme->work[0].energy_lj;
2591 copy_mat(pme->work[0].vir_lj, vir);
2593 for (thread = 1; thread < nthread; thread++)
2595 *mesh_energy += pme->work[thread].energy_lj;
2596 m_add(vir, pme->work[thread].vir_lj, vir);
2601 #define DO_FSPLINE(order) \
2602 for (ithx = 0; (ithx < order); ithx++) \
2604 index_x = (i0+ithx)*pny*pnz; \
2608 for (ithy = 0; (ithy < order); ithy++) \
2610 index_xy = index_x+(j0+ithy)*pnz; \
2615 for (ithz = 0; (ithz < order); ithz++) \
2617 gval = grid[index_xy+(k0+ithz)]; \
2618 fxy1 += thz[ithz]*gval; \
2619 fz1 += dthz[ithz]*gval; \
2628 static void gather_f_bsplines(gmx_pme_t pme, real *grid,
2629 gmx_bool bClearF, pme_atomcomm_t *atc,
2630 splinedata_t *spline,
2633 /* sum forces for local particles */
2634 int nn, n, ithx, ithy, ithz, i0, j0, k0;
2635 int index_x, index_xy;
2636 int nx, ny, nz, pnx, pny, pnz;
2638 real tx, ty, dx, dy, qn;
2639 real fx, fy, fz, gval;
2641 real *thx, *thy, *thz, *dthx, *dthy, *dthz;
2643 real rxx, ryx, ryy, rzx, rzy, rzz;
2646 pme_spline_work_t *work;
2648 #if defined PME_SIMD4_SPREAD_GATHER && !defined PME_SIMD4_UNALIGNED
2649 real thz_buffer[12], *thz_aligned;
2650 real dthz_buffer[12], *dthz_aligned;
2652 thz_aligned = gmx_simd4_align_real(thz_buffer);
2653 dthz_aligned = gmx_simd4_align_real(dthz_buffer);
2656 work = pme->spline_work;
2658 order = pme->pme_order;
2659 thx = spline->theta[XX];
2660 thy = spline->theta[YY];
2661 thz = spline->theta[ZZ];
2662 dthx = spline->dtheta[XX];
2663 dthy = spline->dtheta[YY];
2664 dthz = spline->dtheta[ZZ];
2668 pnx = pme->pmegrid_nx;
2669 pny = pme->pmegrid_ny;
2670 pnz = pme->pmegrid_nz;
2672 rxx = pme->recipbox[XX][XX];
2673 ryx = pme->recipbox[YY][XX];
2674 ryy = pme->recipbox[YY][YY];
2675 rzx = pme->recipbox[ZZ][XX];
2676 rzy = pme->recipbox[ZZ][YY];
2677 rzz = pme->recipbox[ZZ][ZZ];
2679 for (nn = 0; nn < spline->n; nn++)
2681 n = spline->ind[nn];
2682 qn = scale*atc->q[n];
2695 idxptr = atc->idx[n];
2702 /* Pointer arithmetic alert, next six statements */
2703 thx = spline->theta[XX] + norder;
2704 thy = spline->theta[YY] + norder;
2705 thz = spline->theta[ZZ] + norder;
2706 dthx = spline->dtheta[XX] + norder;
2707 dthy = spline->dtheta[YY] + norder;
2708 dthz = spline->dtheta[ZZ] + norder;
2713 #ifdef PME_SIMD4_SPREAD_GATHER
2714 #ifdef PME_SIMD4_UNALIGNED
2715 #define PME_GATHER_F_SIMD4_ORDER4
2717 #define PME_GATHER_F_SIMD4_ALIGNED
2720 #include "pme_simd4.h"
2726 #ifdef PME_SIMD4_SPREAD_GATHER
2727 #define PME_GATHER_F_SIMD4_ALIGNED
2729 #include "pme_simd4.h"
2739 atc->f[n][XX] += -qn*( fx*nx*rxx );
2740 atc->f[n][YY] += -qn*( fx*nx*ryx + fy*ny*ryy );
2741 atc->f[n][ZZ] += -qn*( fx*nx*rzx + fy*ny*rzy + fz*nz*rzz );
2744 /* Since the energy and not forces are interpolated
2745 * the net force might not be exactly zero.
2746 * This can be solved by also interpolating F, but
2747 * that comes at a cost.
2748 * A better hack is to remove the net force every
2749 * step, but that must be done at a higher level
2750 * since this routine doesn't see all atoms if running
2751 * in parallel. Don't know how important it is? EL 990726
2756 static real gather_energy_bsplines(gmx_pme_t pme, real *grid,
2757 pme_atomcomm_t *atc)
2759 splinedata_t *spline;
2760 int n, ithx, ithy, ithz, i0, j0, k0;
2761 int index_x, index_xy;
2763 real energy, pot, tx, ty, qn, gval;
2764 real *thx, *thy, *thz;
2768 spline = &atc->spline[0];
2770 order = pme->pme_order;
2773 for (n = 0; (n < atc->n); n++)
2779 idxptr = atc->idx[n];
2786 /* Pointer arithmetic alert, next three statements */
2787 thx = spline->theta[XX] + norder;
2788 thy = spline->theta[YY] + norder;
2789 thz = spline->theta[ZZ] + norder;
2792 for (ithx = 0; (ithx < order); ithx++)
2794 index_x = (i0+ithx)*pme->pmegrid_ny*pme->pmegrid_nz;
2797 for (ithy = 0; (ithy < order); ithy++)
2799 index_xy = index_x+(j0+ithy)*pme->pmegrid_nz;
2802 for (ithz = 0; (ithz < order); ithz++)
2804 gval = grid[index_xy+(k0+ithz)];
2805 pot += tx*ty*thz[ithz]*gval;
2818 /* Macro to force loop unrolling by fixing order.
2819 * This gives a significant performance gain.
2821 #define CALC_SPLINE(order) \
2825 real data[PME_ORDER_MAX]; \
2826 real ddata[PME_ORDER_MAX]; \
2828 for (j = 0; (j < DIM); j++) \
2832 /* dr is relative offset from lower cell limit */ \
2833 data[order-1] = 0; \
2837 for (k = 3; (k < order); k++) \
2839 div = 1.0/(k - 1.0); \
2840 data[k-1] = div*dr*data[k-2]; \
2841 for (l = 1; (l < (k-1)); l++) \
2843 data[k-l-1] = div*((dr+l)*data[k-l-2]+(k-l-dr)* \
2846 data[0] = div*(1-dr)*data[0]; \
2848 /* differentiate */ \
2849 ddata[0] = -data[0]; \
2850 for (k = 1; (k < order); k++) \
2852 ddata[k] = data[k-1] - data[k]; \
2855 div = 1.0/(order - 1); \
2856 data[order-1] = div*dr*data[order-2]; \
2857 for (l = 1; (l < (order-1)); l++) \
2859 data[order-l-1] = div*((dr+l)*data[order-l-2]+ \
2860 (order-l-dr)*data[order-l-1]); \
2862 data[0] = div*(1 - dr)*data[0]; \
2864 for (k = 0; k < order; k++) \
2866 theta[j][i*order+k] = data[k]; \
2867 dtheta[j][i*order+k] = ddata[k]; \
2872 void make_bsplines(splinevec theta, splinevec dtheta, int order,
2873 rvec fractx[], int nr, int ind[], real charge[],
2874 gmx_bool bDoSplines)
2876 /* construct splines for local atoms */
2880 for (i = 0; i < nr; i++)
2882 /* With free energy we do not use the charge check.
2883 * In most cases this will be more efficient than calling make_bsplines
2884 * twice, since usually more than half the particles have charges.
2887 if (bDoSplines || charge[ii] != 0.0)
2892 case 4: CALC_SPLINE(4); break;
2893 case 5: CALC_SPLINE(5); break;
2894 default: CALC_SPLINE(order); break;
2901 void make_dft_mod(real *mod, real *data, int ndata)
2906 for (i = 0; i < ndata; i++)
2909 for (j = 0; j < ndata; j++)
2911 arg = (2.0*M_PI*i*j)/ndata;
2912 sc += data[j]*cos(arg);
2913 ss += data[j]*sin(arg);
2915 mod[i] = sc*sc+ss*ss;
2917 for (i = 0; i < ndata; i++)
2921 mod[i] = (mod[i-1]+mod[i+1])*0.5;
2927 static void make_bspline_moduli(splinevec bsp_mod,
2928 int nx, int ny, int nz, int order)
2930 int nmax = max(nx, max(ny, nz));
2931 real *data, *ddata, *bsp_data;
2937 snew(bsp_data, nmax);
2943 for (k = 3; k < order; k++)
2947 for (l = 1; l < (k-1); l++)
2949 data[k-l-1] = div*(l*data[k-l-2]+(k-l)*data[k-l-1]);
2951 data[0] = div*data[0];
2954 ddata[0] = -data[0];
2955 for (k = 1; k < order; k++)
2957 ddata[k] = data[k-1]-data[k];
2959 div = 1.0/(order-1);
2961 for (l = 1; l < (order-1); l++)
2963 data[order-l-1] = div*(l*data[order-l-2]+(order-l)*data[order-l-1]);
2965 data[0] = div*data[0];
2967 for (i = 0; i < nmax; i++)
2971 for (i = 1; i <= order; i++)
2973 bsp_data[i] = data[i-1];
2976 make_dft_mod(bsp_mod[XX], bsp_data, nx);
2977 make_dft_mod(bsp_mod[YY], bsp_data, ny);
2978 make_dft_mod(bsp_mod[ZZ], bsp_data, nz);
2986 /* Return the P3M optimal influence function */
2987 static double do_p3m_influence(double z, int order)
2994 /* The formula and most constants can be found in:
2995 * Ballenegger et al., JCTC 8, 936 (2012)
3000 return 1.0 - 2.0*z2/3.0;
3003 return 1.0 - z2 + 2.0*z4/15.0;
3006 return 1.0 - 4.0*z2/3.0 + 2.0*z4/5.0 + 4.0*z2*z4/315.0;
3009 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;
3012 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;
3015 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;
3017 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;
3024 /* Calculate the P3M B-spline moduli for one dimension */
3025 static void make_p3m_bspline_moduli_dim(real *bsp_mod, int n, int order)
3027 double zarg, zai, sinzai, infl;
3032 gmx_fatal(FARGS, "The current P3M code only supports orders up to 8");
3039 for (i = -maxk; i < 0; i++)
3043 infl = do_p3m_influence(sinzai, order);
3044 bsp_mod[n+i] = infl*infl*pow(sinzai/zai, -2.0*order);
3047 for (i = 1; i < maxk; i++)
3051 infl = do_p3m_influence(sinzai, order);
3052 bsp_mod[i] = infl*infl*pow(sinzai/zai, -2.0*order);
3056 /* Calculate the P3M B-spline moduli */
3057 static void make_p3m_bspline_moduli(splinevec bsp_mod,
3058 int nx, int ny, int nz, int order)
3060 make_p3m_bspline_moduli_dim(bsp_mod[XX], nx, order);
3061 make_p3m_bspline_moduli_dim(bsp_mod[YY], ny, order);
3062 make_p3m_bspline_moduli_dim(bsp_mod[ZZ], nz, order);
3066 static void setup_coordinate_communication(pme_atomcomm_t *atc)
3074 for (i = 1; i <= nslab/2; i++)
3076 fw = (atc->nodeid + i) % nslab;
3077 bw = (atc->nodeid - i + nslab) % nslab;
3080 atc->node_dest[n] = fw;
3081 atc->node_src[n] = bw;
3086 atc->node_dest[n] = bw;
3087 atc->node_src[n] = fw;
3093 int gmx_pme_destroy(FILE *log, gmx_pme_t *pmedata)
3099 fprintf(log, "Destroying PME data structures.\n");
3102 sfree((*pmedata)->nnx);
3103 sfree((*pmedata)->nny);
3104 sfree((*pmedata)->nnz);
3106 for (i = 0; i < (*pmedata)->ngrids; ++i)
3108 pmegrids_destroy(&(*pmedata)->pmegrid[i]);
3109 sfree((*pmedata)->fftgrid[i]);
3110 sfree((*pmedata)->cfftgrid[i]);
3111 gmx_parallel_3dfft_destroy((*pmedata)->pfft_setup[i]);
3114 sfree((*pmedata)->lb_buf1);
3115 sfree((*pmedata)->lb_buf2);
3117 for (thread = 0; thread < (*pmedata)->nthread; thread++)
3119 free_work(&(*pmedata)->work[thread]);
3121 sfree((*pmedata)->work);
3129 static int mult_up(int n, int f)
3131 return ((n + f - 1)/f)*f;
3135 static double pme_load_imbalance(gmx_pme_t pme)
3140 nma = pme->nnodes_major;
3141 nmi = pme->nnodes_minor;
3143 n1 = mult_up(pme->nkx, nma)*mult_up(pme->nky, nmi)*pme->nkz;
3144 n2 = mult_up(pme->nkx, nma)*mult_up(pme->nkz, nmi)*pme->nky;
3145 n3 = mult_up(pme->nky, nma)*mult_up(pme->nkz, nmi)*pme->nkx;
3147 /* pme_solve is roughly double the cost of an fft */
3149 return (n1 + n2 + 3*n3)/(double)(6*pme->nkx*pme->nky*pme->nkz);
3152 static void init_atomcomm(gmx_pme_t pme, pme_atomcomm_t *atc,
3153 int dimind, gmx_bool bSpread)
3155 int nk, k, s, thread;
3157 atc->dimind = dimind;
3162 if (pme->nnodes > 1)
3164 atc->mpi_comm = pme->mpi_comm_d[dimind];
3165 MPI_Comm_size(atc->mpi_comm, &atc->nslab);
3166 MPI_Comm_rank(atc->mpi_comm, &atc->nodeid);
3170 fprintf(debug, "For PME atom communication in dimind %d: nslab %d rank %d\n", atc->dimind, atc->nslab, atc->nodeid);
3174 atc->bSpread = bSpread;
3175 atc->pme_order = pme->pme_order;
3179 /* These three allocations are not required for particle decomp. */
3180 snew(atc->node_dest, atc->nslab);
3181 snew(atc->node_src, atc->nslab);
3182 setup_coordinate_communication(atc);
3184 snew(atc->count_thread, pme->nthread);
3185 for (thread = 0; thread < pme->nthread; thread++)
3187 snew(atc->count_thread[thread], atc->nslab);
3189 atc->count = atc->count_thread[0];
3190 snew(atc->rcount, atc->nslab);
3191 snew(atc->buf_index, atc->nslab);
3194 atc->nthread = pme->nthread;
3195 if (atc->nthread > 1)
3197 snew(atc->thread_plist, atc->nthread);
3199 snew(atc->spline, atc->nthread);
3200 for (thread = 0; thread < atc->nthread; thread++)
3202 if (atc->nthread > 1)
3204 snew(atc->thread_plist[thread].n, atc->nthread+2*GMX_CACHE_SEP);
3205 atc->thread_plist[thread].n += GMX_CACHE_SEP;
3207 snew(atc->spline[thread].thread_one, pme->nthread);
3208 atc->spline[thread].thread_one[thread] = 1;
3213 init_overlap_comm(pme_overlap_t * ol,
3223 int lbnd, rbnd, maxlr, b, i;
3226 pme_grid_comm_t *pgc;
3228 int fft_start, fft_end, send_index1, recv_index1;
3232 ol->mpi_comm = comm;
3235 ol->nnodes = nnodes;
3236 ol->nodeid = nodeid;
3238 /* Linear translation of the PME grid won't affect reciprocal space
3239 * calculations, so to optimize we only interpolate "upwards",
3240 * which also means we only have to consider overlap in one direction.
3241 * I.e., particles on this node might also be spread to grid indices
3242 * that belong to higher nodes (modulo nnodes)
3245 snew(ol->s2g0, ol->nnodes+1);
3246 snew(ol->s2g1, ol->nnodes);
3249 fprintf(debug, "PME slab boundaries:");
3251 for (i = 0; i < nnodes; i++)
3253 /* s2g0 the local interpolation grid start.
3254 * s2g1 the local interpolation grid end.
3255 * Because grid overlap communication only goes forward,
3256 * the grid the slabs for fft's should be rounded down.
3258 ol->s2g0[i] = ( i *ndata + 0 )/nnodes;
3259 ol->s2g1[i] = ((i+1)*ndata + nnodes-1)/nnodes + norder - 1;
3263 fprintf(debug, " %3d %3d", ol->s2g0[i], ol->s2g1[i]);
3266 ol->s2g0[nnodes] = ndata;
3269 fprintf(debug, "\n");
3272 /* Determine with how many nodes we need to communicate the grid overlap */
3278 for (i = 0; i < nnodes; i++)
3280 if ((i+b < nnodes && ol->s2g1[i] > ol->s2g0[i+b]) ||
3281 (i+b >= nnodes && ol->s2g1[i] > ol->s2g0[i+b-nnodes] + ndata))
3287 while (bCont && b < nnodes);
3288 ol->noverlap_nodes = b - 1;
3290 snew(ol->send_id, ol->noverlap_nodes);
3291 snew(ol->recv_id, ol->noverlap_nodes);
3292 for (b = 0; b < ol->noverlap_nodes; b++)
3294 ol->send_id[b] = (ol->nodeid + (b + 1)) % ol->nnodes;
3295 ol->recv_id[b] = (ol->nodeid - (b + 1) + ol->nnodes) % ol->nnodes;
3297 snew(ol->comm_data, ol->noverlap_nodes);
3300 for (b = 0; b < ol->noverlap_nodes; b++)
3302 pgc = &ol->comm_data[b];
3304 fft_start = ol->s2g0[ol->send_id[b]];
3305 fft_end = ol->s2g0[ol->send_id[b]+1];
3306 if (ol->send_id[b] < nodeid)
3311 send_index1 = ol->s2g1[nodeid];
3312 send_index1 = min(send_index1, fft_end);
3313 pgc->send_index0 = fft_start;
3314 pgc->send_nindex = max(0, send_index1 - pgc->send_index0);
3315 ol->send_size += pgc->send_nindex;
3317 /* We always start receiving to the first index of our slab */
3318 fft_start = ol->s2g0[ol->nodeid];
3319 fft_end = ol->s2g0[ol->nodeid+1];
3320 recv_index1 = ol->s2g1[ol->recv_id[b]];
3321 if (ol->recv_id[b] > nodeid)
3323 recv_index1 -= ndata;
3325 recv_index1 = min(recv_index1, fft_end);
3326 pgc->recv_index0 = fft_start;
3327 pgc->recv_nindex = max(0, recv_index1 - pgc->recv_index0);
3331 /* Communicate the buffer sizes to receive */
3332 for (b = 0; b < ol->noverlap_nodes; b++)
3334 MPI_Sendrecv(&ol->send_size, 1, MPI_INT, ol->send_id[b], b,
3335 &ol->comm_data[b].recv_size, 1, MPI_INT, ol->recv_id[b], b,
3336 ol->mpi_comm, &stat);
3340 /* For non-divisible grid we need pme_order iso pme_order-1 */
3341 snew(ol->sendbuf, norder*commplainsize);
3342 snew(ol->recvbuf, norder*commplainsize);
3346 make_gridindex5_to_localindex(int n, int local_start, int local_range,
3347 int **global_to_local,
3348 real **fraction_shift)
3356 for (i = 0; (i < 5*n); i++)
3358 /* Determine the global to local grid index */
3359 gtl[i] = (i - local_start + n) % n;
3360 /* For coordinates that fall within the local grid the fraction
3361 * is correct, we don't need to shift it.
3364 if (local_range < n)
3366 /* Due to rounding issues i could be 1 beyond the lower or
3367 * upper boundary of the local grid. Correct the index for this.
3368 * If we shift the index, we need to shift the fraction by
3369 * the same amount in the other direction to not affect
3371 * Note that due to this shifting the weights at the end of
3372 * the spline might change, but that will only involve values
3373 * between zero and values close to the precision of a real,
3374 * which is anyhow the accuracy of the whole mesh calculation.
3376 /* With local_range=0 we should not change i=local_start */
3377 if (i % n != local_start)
3384 else if (gtl[i] == local_range)
3386 gtl[i] = local_range - 1;
3393 *global_to_local = gtl;
3394 *fraction_shift = fsh;
3397 static pme_spline_work_t *make_pme_spline_work(int gmx_unused order)
3399 pme_spline_work_t *work;
3401 #ifdef PME_SIMD4_SPREAD_GATHER
3402 real tmp[12], *tmp_aligned;
3403 gmx_simd4_real_t zero_S;
3404 gmx_simd4_real_t real_mask_S0, real_mask_S1;
3407 snew_aligned(work, 1, SIMD4_ALIGNMENT);
3409 tmp_aligned = gmx_simd4_align_real(tmp);
3411 zero_S = gmx_simd4_setzero_r();
3413 /* Generate bit masks to mask out the unused grid entries,
3414 * as we only operate on order of the 8 grid entries that are
3415 * load into 2 SIMD registers.
3417 for (of = 0; of < 8-(order-1); of++)
3419 for (i = 0; i < 8; i++)
3421 tmp_aligned[i] = (i >= of && i < of+order ? -1.0 : 1.0);
3423 real_mask_S0 = gmx_simd4_load_r(tmp_aligned);
3424 real_mask_S1 = gmx_simd4_load_r(tmp_aligned+4);
3425 work->mask_S0[of] = gmx_simd4_cmplt_r(real_mask_S0, zero_S);
3426 work->mask_S1[of] = gmx_simd4_cmplt_r(real_mask_S1, zero_S);
3435 void gmx_pme_check_restrictions(int pme_order,
3436 int nkx, int nky, int nkz,
3439 gmx_bool bUseThreads,
3441 gmx_bool *bValidSettings)
3443 if (pme_order > PME_ORDER_MAX)
3447 *bValidSettings = FALSE;
3450 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.",
3451 pme_order, PME_ORDER_MAX);
3454 if (nkx <= pme_order*(nnodes_major > 1 ? 2 : 1) ||
3455 nky <= pme_order*(nnodes_minor > 1 ? 2 : 1) ||
3460 *bValidSettings = FALSE;
3463 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",
3467 /* Check for a limitation of the (current) sum_fftgrid_dd code.
3468 * We only allow multiple communication pulses in dim 1, not in dim 0.
3470 if (bUseThreads && (nkx < nnodes_major*pme_order &&
3471 nkx != nnodes_major*(pme_order - 1)))
3475 *bValidSettings = FALSE;
3478 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.",
3479 nkx/(double)nnodes_major, pme_order);
3482 if (bValidSettings != NULL)
3484 *bValidSettings = TRUE;
3490 int gmx_pme_init(gmx_pme_t * pmedata,
3496 gmx_bool bFreeEnergy_q,
3497 gmx_bool bFreeEnergy_lj,
3498 gmx_bool bReproducible,
3501 gmx_pme_t pme = NULL;
3503 int use_threads, sum_use_threads, i;
3508 fprintf(debug, "Creating PME data structures.\n");
3512 pme->redist_init = FALSE;
3513 pme->sum_qgrid_tmp = NULL;
3514 pme->sum_qgrid_dd_tmp = NULL;
3515 pme->buf_nalloc = 0;
3516 pme->redist_buf_nalloc = 0;
3519 pme->bPPnode = TRUE;
3521 pme->nnodes_major = nnodes_major;
3522 pme->nnodes_minor = nnodes_minor;
3525 if (nnodes_major*nnodes_minor > 1)
3527 pme->mpi_comm = cr->mpi_comm_mygroup;
3529 MPI_Comm_rank(pme->mpi_comm, &pme->nodeid);
3530 MPI_Comm_size(pme->mpi_comm, &pme->nnodes);
3531 if (pme->nnodes != nnodes_major*nnodes_minor)
3533 gmx_incons("PME node count mismatch");
3538 pme->mpi_comm = MPI_COMM_NULL;
3542 if (pme->nnodes == 1)
3545 pme->mpi_comm_d[0] = MPI_COMM_NULL;
3546 pme->mpi_comm_d[1] = MPI_COMM_NULL;
3548 pme->ndecompdim = 0;
3549 pme->nodeid_major = 0;
3550 pme->nodeid_minor = 0;
3552 pme->mpi_comm_d[0] = pme->mpi_comm_d[1] = MPI_COMM_NULL;
3557 if (nnodes_minor == 1)
3560 pme->mpi_comm_d[0] = pme->mpi_comm;
3561 pme->mpi_comm_d[1] = MPI_COMM_NULL;
3563 pme->ndecompdim = 1;
3564 pme->nodeid_major = pme->nodeid;
3565 pme->nodeid_minor = 0;
3568 else if (nnodes_major == 1)
3571 pme->mpi_comm_d[0] = MPI_COMM_NULL;
3572 pme->mpi_comm_d[1] = pme->mpi_comm;
3574 pme->ndecompdim = 1;
3575 pme->nodeid_major = 0;
3576 pme->nodeid_minor = pme->nodeid;
3580 if (pme->nnodes % nnodes_major != 0)
3582 gmx_incons("For 2D PME decomposition, #PME nodes must be divisible by the number of nodes in the major dimension");
3584 pme->ndecompdim = 2;
3587 MPI_Comm_split(pme->mpi_comm, pme->nodeid % nnodes_minor,
3588 pme->nodeid, &pme->mpi_comm_d[0]); /* My communicator along major dimension */
3589 MPI_Comm_split(pme->mpi_comm, pme->nodeid/nnodes_minor,
3590 pme->nodeid, &pme->mpi_comm_d[1]); /* My communicator along minor dimension */
3592 MPI_Comm_rank(pme->mpi_comm_d[0], &pme->nodeid_major);
3593 MPI_Comm_size(pme->mpi_comm_d[0], &pme->nnodes_major);
3594 MPI_Comm_rank(pme->mpi_comm_d[1], &pme->nodeid_minor);
3595 MPI_Comm_size(pme->mpi_comm_d[1], &pme->nnodes_minor);
3598 pme->bPPnode = (cr->duty & DUTY_PP);
3601 pme->nthread = nthread;
3603 /* Check if any of the PME MPI ranks uses threads */
3604 use_threads = (pme->nthread > 1 ? 1 : 0);
3606 if (pme->nnodes > 1)
3608 MPI_Allreduce(&use_threads, &sum_use_threads, 1, MPI_INT,
3609 MPI_SUM, pme->mpi_comm);
3614 sum_use_threads = use_threads;
3616 pme->bUseThreads = (sum_use_threads > 0);
3618 if (ir->ePBC == epbcSCREW)
3620 gmx_fatal(FARGS, "pme does not (yet) work with pbc = screw");
3623 pme->bFEP_q = ((ir->efep != efepNO) && bFreeEnergy_q);
3624 pme->bFEP_lj = ((ir->efep != efepNO) && bFreeEnergy_lj);
3625 pme->bFEP = (pme->bFEP_q || pme->bFEP_lj);
3629 pme->bP3M = (ir->coulombtype == eelP3M_AD || getenv("GMX_PME_P3M") != NULL);
3630 pme->pme_order = ir->pme_order;
3631 pme->epsilon_r = ir->epsilon_r;
3633 /* If we violate restrictions, generate a fatal error here */
3634 gmx_pme_check_restrictions(pme->pme_order,
3635 pme->nkx, pme->nky, pme->nkz,
3642 if (pme->nnodes > 1)
3647 MPI_Type_contiguous(DIM, mpi_type, &(pme->rvec_mpi));
3648 MPI_Type_commit(&(pme->rvec_mpi));
3651 /* Note that the charge spreading and force gathering, which usually
3652 * takes about the same amount of time as FFT+solve_pme,
3653 * is always fully load balanced
3654 * (unless the charge distribution is inhomogeneous).
3657 imbal = pme_load_imbalance(pme);
3658 if (imbal >= 1.2 && pme->nodeid_major == 0 && pme->nodeid_minor == 0)
3662 "NOTE: The load imbalance in PME FFT and solve is %d%%.\n"
3663 " For optimal PME load balancing\n"
3664 " PME grid_x (%d) and grid_y (%d) should be divisible by #PME_nodes_x (%d)\n"
3665 " and PME grid_y (%d) and grid_z (%d) should be divisible by #PME_nodes_y (%d)\n"
3667 (int)((imbal-1)*100 + 0.5),
3668 pme->nkx, pme->nky, pme->nnodes_major,
3669 pme->nky, pme->nkz, pme->nnodes_minor);
3673 /* For non-divisible grid we need pme_order iso pme_order-1 */
3674 /* In sum_qgrid_dd x overlap is copied in place: take padding into account.
3675 * y is always copied through a buffer: we don't need padding in z,
3676 * but we do need the overlap in x because of the communication order.
3678 init_overlap_comm(&pme->overlap[0], pme->pme_order,
3682 pme->nnodes_major, pme->nodeid_major,
3684 (div_round_up(pme->nky, pme->nnodes_minor)+pme->pme_order)*(pme->nkz+pme->pme_order-1));
3686 /* Along overlap dim 1 we can send in multiple pulses in sum_fftgrid_dd.
3687 * We do this with an offset buffer of equal size, so we need to allocate
3688 * extra for the offset. That's what the (+1)*pme->nkz is for.
3690 init_overlap_comm(&pme->overlap[1], pme->pme_order,
3694 pme->nnodes_minor, pme->nodeid_minor,
3696 (div_round_up(pme->nkx, pme->nnodes_major)+pme->pme_order+1)*pme->nkz);
3698 /* Double-check for a limitation of the (current) sum_fftgrid_dd code.
3699 * Note that gmx_pme_check_restrictions checked for this already.
3701 if (pme->bUseThreads && pme->overlap[0].noverlap_nodes > 1)
3703 gmx_incons("More than one communication pulse required for grid overlap communication along the major dimension while using threads");
3706 snew(pme->bsp_mod[XX], pme->nkx);
3707 snew(pme->bsp_mod[YY], pme->nky);
3708 snew(pme->bsp_mod[ZZ], pme->nkz);
3710 /* The required size of the interpolation grid, including overlap.
3711 * The allocated size (pmegrid_n?) might be slightly larger.
3713 pme->pmegrid_nx = pme->overlap[0].s2g1[pme->nodeid_major] -
3714 pme->overlap[0].s2g0[pme->nodeid_major];
3715 pme->pmegrid_ny = pme->overlap[1].s2g1[pme->nodeid_minor] -
3716 pme->overlap[1].s2g0[pme->nodeid_minor];
3717 pme->pmegrid_nz_base = pme->nkz;
3718 pme->pmegrid_nz = pme->pmegrid_nz_base + pme->pme_order - 1;
3719 set_grid_alignment(&pme->pmegrid_nz, pme->pme_order);
3721 pme->pmegrid_start_ix = pme->overlap[0].s2g0[pme->nodeid_major];
3722 pme->pmegrid_start_iy = pme->overlap[1].s2g0[pme->nodeid_minor];
3723 pme->pmegrid_start_iz = 0;
3725 make_gridindex5_to_localindex(pme->nkx,
3726 pme->pmegrid_start_ix,
3727 pme->pmegrid_nx - (pme->pme_order-1),
3728 &pme->nnx, &pme->fshx);
3729 make_gridindex5_to_localindex(pme->nky,
3730 pme->pmegrid_start_iy,
3731 pme->pmegrid_ny - (pme->pme_order-1),
3732 &pme->nny, &pme->fshy);
3733 make_gridindex5_to_localindex(pme->nkz,
3734 pme->pmegrid_start_iz,
3735 pme->pmegrid_nz_base,
3736 &pme->nnz, &pme->fshz);
3738 pme->spline_work = make_pme_spline_work(pme->pme_order);
3740 ndata[0] = pme->nkx;
3741 ndata[1] = pme->nky;
3742 ndata[2] = pme->nkz;
3743 pme->ngrids = ((ir->ljpme_combination_rule == eljpmeLB) ? DO_Q_AND_LJ_LB : DO_Q_AND_LJ);
3744 snew(pme->fftgrid, pme->ngrids);
3745 snew(pme->cfftgrid, pme->ngrids);
3746 snew(pme->pfft_setup, pme->ngrids);
3748 for (i = 0; i < pme->ngrids; ++i)
3750 if (((ir->ljpme_combination_rule == eljpmeLB) && i >= 2) || i % 2 == 0 || bFreeEnergy_q || bFreeEnergy_lj)
3752 pmegrids_init(&pme->pmegrid[i],
3753 pme->pmegrid_nx, pme->pmegrid_ny, pme->pmegrid_nz,
3754 pme->pmegrid_nz_base,
3758 pme->overlap[0].s2g1[pme->nodeid_major]-pme->overlap[0].s2g0[pme->nodeid_major+1],
3759 pme->overlap[1].s2g1[pme->nodeid_minor]-pme->overlap[1].s2g0[pme->nodeid_minor+1]);
3760 /* This routine will allocate the grid data to fit the FFTs */
3761 gmx_parallel_3dfft_init(&pme->pfft_setup[i], ndata,
3762 &pme->fftgrid[i], &pme->cfftgrid[i],
3764 bReproducible, pme->nthread);
3771 /* Use plain SPME B-spline interpolation */
3772 make_bspline_moduli(pme->bsp_mod, pme->nkx, pme->nky, pme->nkz, pme->pme_order);
3776 /* Use the P3M grid-optimized influence function */
3777 make_p3m_bspline_moduli(pme->bsp_mod, pme->nkx, pme->nky, pme->nkz, pme->pme_order);
3780 /* Use atc[0] for spreading */
3781 init_atomcomm(pme, &pme->atc[0], nnodes_major > 1 ? 0 : 1, TRUE);
3782 if (pme->ndecompdim >= 2)
3784 init_atomcomm(pme, &pme->atc[1], 1, FALSE);
3787 if (pme->nnodes == 1)
3789 pme->atc[0].n = homenr;
3790 pme_realloc_atomcomm_things(&pme->atc[0]);
3793 pme->lb_buf1 = NULL;
3794 pme->lb_buf2 = NULL;
3795 pme->lb_buf_nalloc = 0;
3800 /* Use fft5d, order after FFT is y major, z, x minor */
3802 snew(pme->work, pme->nthread);
3803 for (thread = 0; thread < pme->nthread; thread++)
3805 realloc_work(&pme->work[thread], pme->nkx);
3814 static void reuse_pmegrids(const pmegrids_t *old, pmegrids_t *new)
3818 for (d = 0; d < DIM; d++)
3820 if (new->grid.n[d] > old->grid.n[d])
3826 sfree_aligned(new->grid.grid);
3827 new->grid.grid = old->grid.grid;
3829 if (new->grid_th != NULL && new->nthread == old->nthread)
3831 sfree_aligned(new->grid_all);
3832 for (t = 0; t < new->nthread; t++)
3834 new->grid_th[t].grid = old->grid_th[t].grid;
3839 int gmx_pme_reinit(gmx_pme_t * pmedata,
3842 const t_inputrec * ir,
3850 irc.nkx = grid_size[XX];
3851 irc.nky = grid_size[YY];
3852 irc.nkz = grid_size[ZZ];
3854 if (pme_src->nnodes == 1)
3856 homenr = pme_src->atc[0].n;
3863 ret = gmx_pme_init(pmedata, cr, pme_src->nnodes_major, pme_src->nnodes_minor,
3864 &irc, homenr, pme_src->bFEP_q, pme_src->bFEP_lj, FALSE, pme_src->nthread);
3868 /* We can easily reuse the allocated pme grids in pme_src */
3869 reuse_pmegrids(&pme_src->pmegrid[PME_GRID_QA], &(*pmedata)->pmegrid[PME_GRID_QA]);
3870 /* We would like to reuse the fft grids, but that's harder */
3877 static void copy_local_grid(gmx_pme_t pme,
3878 pmegrids_t *pmegrids, int thread, real *fftgrid)
3880 ivec local_fft_ndata, local_fft_offset, local_fft_size;
3884 int offx, offy, offz, x, y, z, i0, i0t;
3889 gmx_parallel_3dfft_real_limits(pme->pfft_setup[PME_GRID_QA],
3893 fft_my = local_fft_size[YY];
3894 fft_mz = local_fft_size[ZZ];
3896 pmegrid = &pmegrids->grid_th[thread];
3898 nsx = pmegrid->s[XX];
3899 nsy = pmegrid->s[YY];
3900 nsz = pmegrid->s[ZZ];
3902 for (d = 0; d < DIM; d++)
3904 nf[d] = min(pmegrid->n[d] - (pmegrid->order - 1),
3905 local_fft_ndata[d] - pmegrid->offset[d]);
3908 offx = pmegrid->offset[XX];
3909 offy = pmegrid->offset[YY];
3910 offz = pmegrid->offset[ZZ];
3912 /* Directly copy the non-overlapping parts of the local grids.
3913 * This also initializes the full grid.
3915 grid_th = pmegrid->grid;
3916 for (x = 0; x < nf[XX]; x++)
3918 for (y = 0; y < nf[YY]; y++)
3920 i0 = ((offx + x)*fft_my + (offy + y))*fft_mz + offz;
3921 i0t = (x*nsy + y)*nsz;
3922 for (z = 0; z < nf[ZZ]; z++)
3924 fftgrid[i0+z] = grid_th[i0t+z];
3931 reduce_threadgrid_overlap(gmx_pme_t pme,
3932 const pmegrids_t *pmegrids, int thread,
3933 real *fftgrid, real *commbuf_x, real *commbuf_y)
3935 ivec local_fft_ndata, local_fft_offset, local_fft_size;
3936 int fft_nx, fft_ny, fft_nz;
3941 int offx, offy, offz, x, y, z, i0, i0t;
3942 int sx, sy, sz, fx, fy, fz, tx1, ty1, tz1, ox, oy, oz;
3943 gmx_bool bClearBufX, bClearBufY, bClearBufXY, bClearBuf;
3944 gmx_bool bCommX, bCommY;
3947 const pmegrid_t *pmegrid, *pmegrid_g, *pmegrid_f;
3948 const real *grid_th;
3949 real *commbuf = NULL;
3951 gmx_parallel_3dfft_real_limits(pme->pfft_setup[PME_GRID_QA],
3955 fft_nx = local_fft_ndata[XX];
3956 fft_ny = local_fft_ndata[YY];
3957 fft_nz = local_fft_ndata[ZZ];
3959 fft_my = local_fft_size[YY];
3960 fft_mz = local_fft_size[ZZ];
3962 /* This routine is called when all thread have finished spreading.
3963 * Here each thread sums grid contributions calculated by other threads
3964 * to the thread local grid volume.
3965 * To minimize the number of grid copying operations,
3966 * this routines sums immediately from the pmegrid to the fftgrid.
3969 /* Determine which part of the full node grid we should operate on,
3970 * this is our thread local part of the full grid.
3972 pmegrid = &pmegrids->grid_th[thread];
3974 for (d = 0; d < DIM; d++)
3976 ne[d] = min(pmegrid->offset[d]+pmegrid->n[d]-(pmegrid->order-1),
3977 local_fft_ndata[d]);
3980 offx = pmegrid->offset[XX];
3981 offy = pmegrid->offset[YY];
3982 offz = pmegrid->offset[ZZ];
3989 /* Now loop over all the thread data blocks that contribute
3990 * to the grid region we (our thread) are operating on.
3992 /* Note that ffy_nx/y is equal to the number of grid points
3993 * between the first point of our node grid and the one of the next node.
3995 for (sx = 0; sx >= -pmegrids->nthread_comm[XX]; sx--)
3997 fx = pmegrid->ci[XX] + sx;
4002 fx += pmegrids->nc[XX];
4004 bCommX = (pme->nnodes_major > 1);
4006 pmegrid_g = &pmegrids->grid_th[fx*pmegrids->nc[YY]*pmegrids->nc[ZZ]];
4007 ox += pmegrid_g->offset[XX];
4010 tx1 = min(ox + pmegrid_g->n[XX], ne[XX]);
4014 tx1 = min(ox + pmegrid_g->n[XX], pme->pme_order);
4017 for (sy = 0; sy >= -pmegrids->nthread_comm[YY]; sy--)
4019 fy = pmegrid->ci[YY] + sy;
4024 fy += pmegrids->nc[YY];
4026 bCommY = (pme->nnodes_minor > 1);
4028 pmegrid_g = &pmegrids->grid_th[fy*pmegrids->nc[ZZ]];
4029 oy += pmegrid_g->offset[YY];
4032 ty1 = min(oy + pmegrid_g->n[YY], ne[YY]);
4036 ty1 = min(oy + pmegrid_g->n[YY], pme->pme_order);
4039 for (sz = 0; sz >= -pmegrids->nthread_comm[ZZ]; sz--)
4041 fz = pmegrid->ci[ZZ] + sz;
4045 fz += pmegrids->nc[ZZ];
4048 pmegrid_g = &pmegrids->grid_th[fz];
4049 oz += pmegrid_g->offset[ZZ];
4050 tz1 = min(oz + pmegrid_g->n[ZZ], ne[ZZ]);
4052 if (sx == 0 && sy == 0 && sz == 0)
4054 /* We have already added our local contribution
4055 * before calling this routine, so skip it here.
4060 thread_f = (fx*pmegrids->nc[YY] + fy)*pmegrids->nc[ZZ] + fz;
4062 pmegrid_f = &pmegrids->grid_th[thread_f];
4064 grid_th = pmegrid_f->grid;
4066 nsx = pmegrid_f->s[XX];
4067 nsy = pmegrid_f->s[YY];
4068 nsz = pmegrid_f->s[ZZ];
4070 #ifdef DEBUG_PME_REDUCE
4071 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",
4072 pme->nodeid, thread, thread_f,
4073 pme->pmegrid_start_ix,
4074 pme->pmegrid_start_iy,
4075 pme->pmegrid_start_iz,
4077 offx-ox, tx1-ox, offx, tx1,
4078 offy-oy, ty1-oy, offy, ty1,
4079 offz-oz, tz1-oz, offz, tz1);
4082 if (!(bCommX || bCommY))
4084 /* Copy from the thread local grid to the node grid */
4085 for (x = offx; x < tx1; x++)
4087 for (y = offy; y < ty1; y++)
4089 i0 = (x*fft_my + y)*fft_mz;
4090 i0t = ((x - ox)*nsy + (y - oy))*nsz - oz;
4091 for (z = offz; z < tz1; z++)
4093 fftgrid[i0+z] += grid_th[i0t+z];
4100 /* The order of this conditional decides
4101 * where the corner volume gets stored with x+y decomp.
4105 commbuf = commbuf_y;
4106 buf_my = ty1 - offy;
4109 /* We index commbuf modulo the local grid size */
4110 commbuf += buf_my*fft_nx*fft_nz;
4112 bClearBuf = bClearBufXY;
4113 bClearBufXY = FALSE;
4117 bClearBuf = bClearBufY;
4123 commbuf = commbuf_x;
4125 bClearBuf = bClearBufX;
4129 /* Copy to the communication buffer */
4130 for (x = offx; x < tx1; x++)
4132 for (y = offy; y < ty1; y++)
4134 i0 = (x*buf_my + y)*fft_nz;
4135 i0t = ((x - ox)*nsy + (y - oy))*nsz - oz;
4139 /* First access of commbuf, initialize it */
4140 for (z = offz; z < tz1; z++)
4142 commbuf[i0+z] = grid_th[i0t+z];
4147 for (z = offz; z < tz1; z++)
4149 commbuf[i0+z] += grid_th[i0t+z];
4161 static void sum_fftgrid_dd(gmx_pme_t pme, real *fftgrid)
4163 ivec local_fft_ndata, local_fft_offset, local_fft_size;
4164 pme_overlap_t *overlap;
4165 int send_index0, send_nindex;
4170 int send_size_y, recv_size_y;
4171 int ipulse, send_id, recv_id, datasize, gridsize, size_yx;
4172 real *sendptr, *recvptr;
4173 int x, y, z, indg, indb;
4175 /* Note that this routine is only used for forward communication.
4176 * Since the force gathering, unlike the charge spreading,
4177 * can be trivially parallelized over the particles,
4178 * the backwards process is much simpler and can use the "old"
4179 * communication setup.
4182 gmx_parallel_3dfft_real_limits(pme->pfft_setup[PME_GRID_QA],
4187 if (pme->nnodes_minor > 1)
4189 /* Major dimension */
4190 overlap = &pme->overlap[1];
4192 if (pme->nnodes_major > 1)
4194 size_yx = pme->overlap[0].comm_data[0].send_nindex;
4200 datasize = (local_fft_ndata[XX] + size_yx)*local_fft_ndata[ZZ];
4202 send_size_y = overlap->send_size;
4204 for (ipulse = 0; ipulse < overlap->noverlap_nodes; ipulse++)
4206 send_id = overlap->send_id[ipulse];
4207 recv_id = overlap->recv_id[ipulse];
4209 overlap->comm_data[ipulse].send_index0 -
4210 overlap->comm_data[0].send_index0;
4211 send_nindex = overlap->comm_data[ipulse].send_nindex;
4212 /* We don't use recv_index0, as we always receive starting at 0 */
4213 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
4214 recv_size_y = overlap->comm_data[ipulse].recv_size;
4216 sendptr = overlap->sendbuf + send_index0*local_fft_ndata[ZZ];
4217 recvptr = overlap->recvbuf;
4220 MPI_Sendrecv(sendptr, send_size_y*datasize, GMX_MPI_REAL,
4222 recvptr, recv_size_y*datasize, GMX_MPI_REAL,
4224 overlap->mpi_comm, &stat);
4227 for (x = 0; x < local_fft_ndata[XX]; x++)
4229 for (y = 0; y < recv_nindex; y++)
4231 indg = (x*local_fft_size[YY] + y)*local_fft_size[ZZ];
4232 indb = (x*recv_size_y + y)*local_fft_ndata[ZZ];
4233 for (z = 0; z < local_fft_ndata[ZZ]; z++)
4235 fftgrid[indg+z] += recvptr[indb+z];
4240 if (pme->nnodes_major > 1)
4242 /* Copy from the received buffer to the send buffer for dim 0 */
4243 sendptr = pme->overlap[0].sendbuf;
4244 for (x = 0; x < size_yx; x++)
4246 for (y = 0; y < recv_nindex; y++)
4248 indg = (x*local_fft_ndata[YY] + y)*local_fft_ndata[ZZ];
4249 indb = ((local_fft_ndata[XX] + x)*recv_size_y + y)*local_fft_ndata[ZZ];
4250 for (z = 0; z < local_fft_ndata[ZZ]; z++)
4252 sendptr[indg+z] += recvptr[indb+z];
4260 /* We only support a single pulse here.
4261 * This is not a severe limitation, as this code is only used
4262 * with OpenMP and with OpenMP the (PME) domains can be larger.
4264 if (pme->nnodes_major > 1)
4266 /* Major dimension */
4267 overlap = &pme->overlap[0];
4269 datasize = local_fft_ndata[YY]*local_fft_ndata[ZZ];
4270 gridsize = local_fft_size[YY] *local_fft_size[ZZ];
4274 send_id = overlap->send_id[ipulse];
4275 recv_id = overlap->recv_id[ipulse];
4276 send_nindex = overlap->comm_data[ipulse].send_nindex;
4277 /* We don't use recv_index0, as we always receive starting at 0 */
4278 recv_nindex = overlap->comm_data[ipulse].recv_nindex;
4280 sendptr = overlap->sendbuf;
4281 recvptr = overlap->recvbuf;
4285 fprintf(debug, "PME fftgrid comm %2d x %2d x %2d\n",
4286 send_nindex, local_fft_ndata[YY], local_fft_ndata[ZZ]);
4290 MPI_Sendrecv(sendptr, send_nindex*datasize, GMX_MPI_REAL,
4292 recvptr, recv_nindex*datasize, GMX_MPI_REAL,
4294 overlap->mpi_comm, &stat);
4297 for (x = 0; x < recv_nindex; x++)
4299 for (y = 0; y < local_fft_ndata[YY]; y++)
4301 indg = (x*local_fft_size[YY] + y)*local_fft_size[ZZ];
4302 indb = (x*local_fft_ndata[YY] + y)*local_fft_ndata[ZZ];
4303 for (z = 0; z < local_fft_ndata[ZZ]; z++)
4305 fftgrid[indg+z] += recvptr[indb+z];
4313 static void spread_on_grid(gmx_pme_t pme,
4314 pme_atomcomm_t *atc, pmegrids_t *grids,
4315 gmx_bool bCalcSplines, gmx_bool bSpread,
4316 real *fftgrid, gmx_bool bDoSplines )
4318 int nthread, thread;
4319 #ifdef PME_TIME_THREADS
4320 gmx_cycles_t c1, c2, c3, ct1a, ct1b, ct1c;
4321 static double cs1 = 0, cs2 = 0, cs3 = 0;
4322 static double cs1a[6] = {0, 0, 0, 0, 0, 0};
4326 nthread = pme->nthread;
4327 assert(nthread > 0);
4329 #ifdef PME_TIME_THREADS
4330 c1 = omp_cyc_start();
4334 #pragma omp parallel for num_threads(nthread) schedule(static)
4335 for (thread = 0; thread < nthread; thread++)
4339 start = atc->n* thread /nthread;
4340 end = atc->n*(thread+1)/nthread;
4342 /* Compute fftgrid index for all atoms,
4343 * with help of some extra variables.
4345 calc_interpolation_idx(pme, atc, start, end, thread);
4348 #ifdef PME_TIME_THREADS
4349 c1 = omp_cyc_end(c1);
4353 #ifdef PME_TIME_THREADS
4354 c2 = omp_cyc_start();
4356 #pragma omp parallel for num_threads(nthread) schedule(static)
4357 for (thread = 0; thread < nthread; thread++)
4359 splinedata_t *spline;
4360 pmegrid_t *grid = NULL;
4362 /* make local bsplines */
4363 if (grids == NULL || !pme->bUseThreads)
4365 spline = &atc->spline[0];
4371 grid = &grids->grid;
4376 spline = &atc->spline[thread];
4378 if (grids->nthread == 1)
4380 /* One thread, we operate on all charges */
4385 /* Get the indices our thread should operate on */
4386 make_thread_local_ind(atc, thread, spline);
4389 grid = &grids->grid_th[thread];
4394 make_bsplines(spline->theta, spline->dtheta, pme->pme_order,
4395 atc->fractx, spline->n, spline->ind, atc->q, bDoSplines);
4400 /* put local atoms on grid. */
4401 #ifdef PME_TIME_SPREAD
4402 ct1a = omp_cyc_start();
4404 spread_q_bsplines_thread(grid, atc, spline, pme->spline_work);
4406 if (pme->bUseThreads)
4408 copy_local_grid(pme, grids, thread, fftgrid);
4410 #ifdef PME_TIME_SPREAD
4411 ct1a = omp_cyc_end(ct1a);
4412 cs1a[thread] += (double)ct1a;
4416 #ifdef PME_TIME_THREADS
4417 c2 = omp_cyc_end(c2);
4421 if (bSpread && pme->bUseThreads)
4423 #ifdef PME_TIME_THREADS
4424 c3 = omp_cyc_start();
4426 #pragma omp parallel for num_threads(grids->nthread) schedule(static)
4427 for (thread = 0; thread < grids->nthread; thread++)
4429 reduce_threadgrid_overlap(pme, grids, thread,
4431 pme->overlap[0].sendbuf,
4432 pme->overlap[1].sendbuf);
4434 #ifdef PME_TIME_THREADS
4435 c3 = omp_cyc_end(c3);
4439 if (pme->nnodes > 1)
4441 /* Communicate the overlapping part of the fftgrid.
4442 * For this communication call we need to check pme->bUseThreads
4443 * to have all ranks communicate here, regardless of pme->nthread.
4445 sum_fftgrid_dd(pme, fftgrid);
4449 #ifdef PME_TIME_THREADS
4453 printf("idx %.2f spread %.2f red %.2f",
4454 cs1*1e-9, cs2*1e-9, cs3*1e-9);
4455 #ifdef PME_TIME_SPREAD
4456 for (thread = 0; thread < nthread; thread++)
4458 printf(" %.2f", cs1a[thread]*1e-9);
4467 static void dump_grid(FILE *fp,
4468 int sx, int sy, int sz, int nx, int ny, int nz,
4469 int my, int mz, const real *g)
4473 for (x = 0; x < nx; x++)
4475 for (y = 0; y < ny; y++)
4477 for (z = 0; z < nz; z++)
4479 fprintf(fp, "%2d %2d %2d %6.3f\n",
4480 sx+x, sy+y, sz+z, g[(x*my + y)*mz + z]);
4486 static void dump_local_fftgrid(gmx_pme_t pme, const real *fftgrid)
4488 ivec local_fft_ndata, local_fft_offset, local_fft_size;
4490 gmx_parallel_3dfft_real_limits(pme->pfft_setup[PME_GRID_QA],
4496 pme->pmegrid_start_ix,
4497 pme->pmegrid_start_iy,
4498 pme->pmegrid_start_iz,
4499 pme->pmegrid_nx-pme->pme_order+1,
4500 pme->pmegrid_ny-pme->pme_order+1,
4501 pme->pmegrid_nz-pme->pme_order+1,
4508 void gmx_pme_calc_energy(gmx_pme_t pme, int n, rvec *x, real *q, real *V)
4510 pme_atomcomm_t *atc;
4513 if (pme->nnodes > 1)
4515 gmx_incons("gmx_pme_calc_energy called in parallel");
4519 gmx_incons("gmx_pme_calc_energy with free energy");
4522 atc = &pme->atc_energy;
4524 if (atc->spline == NULL)
4526 snew(atc->spline, atc->nthread);
4529 atc->bSpread = TRUE;
4530 atc->pme_order = pme->pme_order;
4532 pme_realloc_atomcomm_things(atc);
4536 /* We only use the A-charges grid */
4537 grid = &pme->pmegrid[PME_GRID_QA];
4539 /* Only calculate the spline coefficients, don't actually spread */
4540 spread_on_grid(pme, atc, NULL, TRUE, FALSE, pme->fftgrid[PME_GRID_QA], FALSE);
4542 *V = gather_energy_bsplines(pme, grid->grid.grid, atc);
4546 static void reset_pmeonly_counters(gmx_wallcycle_t wcycle,
4547 gmx_walltime_accounting_t walltime_accounting,
4548 t_nrnb *nrnb, t_inputrec *ir,
4551 /* Reset all the counters related to performance over the run */
4552 wallcycle_stop(wcycle, ewcRUN);
4553 wallcycle_reset_all(wcycle);
4555 if (ir->nsteps >= 0)
4557 /* ir->nsteps is not used here, but we update it for consistency */
4558 ir->nsteps -= step - ir->init_step;
4560 ir->init_step = step;
4561 wallcycle_start(wcycle, ewcRUN);
4562 walltime_accounting_start(walltime_accounting);
4566 static void gmx_pmeonly_switch(int *npmedata, gmx_pme_t **pmedata,
4568 t_commrec *cr, t_inputrec *ir,
4572 gmx_pme_t pme = NULL;
4575 while (ind < *npmedata)
4577 pme = (*pmedata)[ind];
4578 if (pme->nkx == grid_size[XX] &&
4579 pme->nky == grid_size[YY] &&
4580 pme->nkz == grid_size[ZZ])
4591 srenew(*pmedata, *npmedata);
4593 /* Generate a new PME data structure, copying part of the old pointers */
4594 gmx_pme_reinit(&((*pmedata)[ind]), cr, pme, ir, grid_size);
4596 *pme_ret = (*pmedata)[ind];
4599 int gmx_pmeonly(gmx_pme_t pme,
4600 t_commrec *cr, t_nrnb *mynrnb,
4601 gmx_wallcycle_t wcycle,
4602 gmx_walltime_accounting_t walltime_accounting,
4603 real ewaldcoeff_q, real ewaldcoeff_lj,
4608 gmx_pme_pp_t pme_pp;
4612 rvec *x_pp = NULL, *f_pp = NULL;
4613 real *chargeA = NULL, *chargeB = NULL;
4614 real *c6A = NULL, *c6B = NULL;
4615 real *sigmaA = NULL, *sigmaB = NULL;
4618 int maxshift_x = 0, maxshift_y = 0;
4619 real energy_q, energy_lj, dvdlambda_q, dvdlambda_lj;
4620 matrix vir_q, vir_lj;
4625 gmx_int64_t step, step_rel;
4628 /* This data will only use with PME tuning, i.e. switching PME grids */
4630 snew(pmedata, npmedata);
4633 pme_pp = gmx_pme_pp_init(cr);
4638 do /****** this is a quasi-loop over time steps! */
4640 /* The reason for having a loop here is PME grid tuning/switching */
4643 /* Domain decomposition */
4644 ret = gmx_pme_recv_params_coords(pme_pp,
4650 &maxshift_x, &maxshift_y,
4651 &pme->bFEP_q, &pme->bFEP_lj,
4652 &lambda_q, &lambda_lj,
4656 grid_switch, &ewaldcoeff_q, &ewaldcoeff_lj);
4658 if (ret == pmerecvqxSWITCHGRID)
4660 /* Switch the PME grid to grid_switch */
4661 gmx_pmeonly_switch(&npmedata, &pmedata, grid_switch, cr, ir, &pme);
4664 if (ret == pmerecvqxRESETCOUNTERS)
4666 /* Reset the cycle and flop counters */
4667 reset_pmeonly_counters(wcycle, walltime_accounting, mynrnb, ir, step);
4670 while (ret == pmerecvqxSWITCHGRID || ret == pmerecvqxRESETCOUNTERS);
4672 if (ret == pmerecvqxFINISH)
4674 /* We should stop: break out of the loop */
4678 step_rel = step - ir->init_step;
4682 wallcycle_start(wcycle, ewcRUN);
4683 walltime_accounting_start(walltime_accounting);
4686 wallcycle_start(wcycle, ewcPMEMESH);
4693 gmx_pme_do(pme, 0, natoms, x_pp, f_pp,
4694 chargeA, chargeB, c6A, c6B, sigmaA, sigmaB, box,
4695 cr, maxshift_x, maxshift_y, mynrnb, wcycle,
4696 vir_q, ewaldcoeff_q, vir_lj, ewaldcoeff_lj,
4697 &energy_q, &energy_lj, lambda_q, lambda_lj, &dvdlambda_q, &dvdlambda_lj,
4698 pme_flags | GMX_PME_DO_ALL_F | (bEnerVir ? GMX_PME_CALC_ENER_VIR : 0));
4700 cycles = wallcycle_stop(wcycle, ewcPMEMESH);
4702 gmx_pme_send_force_vir_ener(pme_pp,
4703 f_pp, vir_q, energy_q, vir_lj, energy_lj,
4704 dvdlambda_q, dvdlambda_lj, cycles);
4707 } /***** end of quasi-loop, we stop with the break above */
4710 walltime_accounting_end(walltime_accounting);
4716 calc_initial_lb_coeffs(gmx_pme_t pme, real *local_c6, real *local_sigma)
4720 for (i = 0; i < pme->atc[0].n; ++i)
4724 sigma4 = local_sigma[i];
4725 sigma4 = sigma4*sigma4;
4726 sigma4 = sigma4*sigma4;
4727 pme->atc[0].q[i] = local_c6[i] / sigma4;
4732 calc_next_lb_coeffs(gmx_pme_t pme, real *local_sigma)
4736 for (i = 0; i < pme->atc[0].n; ++i)
4738 pme->atc[0].q[i] *= local_sigma[i];
4743 do_redist_x_q(gmx_pme_t pme, t_commrec *cr, int start, int homenr,
4744 gmx_bool bFirst, rvec x[], real *data)
4747 pme_atomcomm_t *atc;
4750 for (d = pme->ndecompdim - 1; d >= 0; d--)
4756 if (d == pme->ndecompdim - 1)
4764 n_d = pme->atc[d + 1].n;
4770 if (atc->npd > atc->pd_nalloc)
4772 atc->pd_nalloc = over_alloc_dd(atc->npd);
4773 srenew(atc->pd, atc->pd_nalloc);
4775 pme_calc_pidx_wrapper(n_d, pme->recipbox, x_d, atc);
4777 /* Redistribute x (only once) and qA/c6A or qB/c6B */
4778 if (DOMAINDECOMP(cr))
4780 dd_pmeredist_x_q(pme, n_d, bFirst, x_d, q_d, atc);
4784 pmeredist_pd(pme, TRUE, n_d, bFirst, x_d, q_d, atc);
4789 /* TODO: when adding free-energy support, sigmaB will no longer be
4791 int gmx_pme_do(gmx_pme_t pme,
4792 int start, int homenr,
4794 real *chargeA, real *chargeB,
4795 real *c6A, real *c6B,
4796 real *sigmaA, real gmx_unused *sigmaB,
4797 matrix box, t_commrec *cr,
4798 int maxshift_x, int maxshift_y,
4799 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
4800 matrix vir_q, real ewaldcoeff_q,
4801 matrix vir_lj, real ewaldcoeff_lj,
4802 real *energy_q, real *energy_lj,
4803 real lambda_q, real lambda_lj,
4804 real *dvdlambda_q, real *dvdlambda_lj,
4807 int q, d, i, j, ntot, npme, qmax;
4810 pme_atomcomm_t *atc = NULL;
4811 pmegrids_t *pmegrid = NULL;
4815 real *charge = NULL, *q_d;
4820 gmx_parallel_3dfft_t pfft_setup;
4822 t_complex * cfftgrid;
4824 gmx_bool bFirst, bDoSplines;
4825 const gmx_bool bCalcEnerVir = flags & GMX_PME_CALC_ENER_VIR;
4826 const gmx_bool bCalcF = flags & GMX_PME_CALC_F;
4828 assert(pme->nnodes > 0);
4829 assert(pme->nnodes == 1 || pme->ndecompdim > 0);
4831 if (pme->nnodes > 1)
4835 if (atc->npd > atc->pd_nalloc)
4837 atc->pd_nalloc = over_alloc_dd(atc->npd);
4838 srenew(atc->pd, atc->pd_nalloc);
4840 for (d = pme->ndecompdim-1; d >= 0; d--)
4843 atc->maxshift = (atc->dimind == 0 ? maxshift_x : maxshift_y);
4849 /* This could be necessary for TPI */
4850 pme->atc[0].n = homenr;
4851 if (DOMAINDECOMP(cr))
4853 pme_realloc_atomcomm_things(atc);
4859 m_inv_ur0(box, pme->recipbox);
4862 /* For simplicity, we construct the splines for all particles if
4863 * more than one PME calculations is needed. Some optimization
4864 * could be done by keeping track of which atoms have splines
4865 * constructed, and construct new splines on each pass for atoms
4866 * that don't yet have them.
4869 bDoSplines = pme->bFEP || ((flags & GMX_PME_DO_COULOMB) && (flags & GMX_PME_DO_LJ));
4871 /* We need a maximum of four separate PME calculations:
4872 * q=0: Coulomb PME with charges from state A
4873 * q=1: Coulomb PME with charges from state B
4874 * q=2: LJ PME with C6 from state A
4875 * q=3: LJ PME with C6 from state B
4876 * For Lorentz-Berthelot combination rules, a separate loop is used to
4877 * calculate all the terms
4880 /* If we are doing LJ-PME with LB, we only do Q here */
4881 qmax = (flags & GMX_PME_LJ_LB) ? DO_Q : DO_Q_AND_LJ;
4883 for (q = 0; q < qmax; ++q)
4885 /* Check if we should do calculations at this q
4886 * If q is odd we should be doing FEP
4887 * If q < 2 we should be doing electrostatic PME
4888 * If q >= 2 we should be doing LJ-PME
4890 if ((!pme->bFEP_q && q == 1)
4891 || (!pme->bFEP_lj && q == 3)
4892 || (!(flags & GMX_PME_DO_COULOMB) && q < 2)
4893 || (!(flags & GMX_PME_DO_LJ) && q >= 2))
4897 /* Unpack structure */
4898 pmegrid = &pme->pmegrid[q];
4899 fftgrid = pme->fftgrid[q];
4900 cfftgrid = pme->cfftgrid[q];
4901 pfft_setup = pme->pfft_setup[q];
4904 case 0: charge = chargeA + start; break;
4905 case 1: charge = chargeB + start; break;
4906 case 2: charge = c6A + start; break;
4907 case 3: charge = c6B + start; break;
4910 grid = pmegrid->grid.grid;
4914 fprintf(debug, "PME: nnodes = %d, nodeid = %d\n",
4915 cr->nnodes, cr->nodeid);
4916 fprintf(debug, "Grid = %p\n", (void*)grid);
4919 gmx_fatal(FARGS, "No grid!");
4924 if (pme->nnodes == 1)
4930 wallcycle_start(wcycle, ewcPME_REDISTXF);
4931 do_redist_x_q(pme, cr, start, homenr, bFirst, x, charge);
4934 wallcycle_stop(wcycle, ewcPME_REDISTXF);
4939 fprintf(debug, "Node= %6d, pme local particles=%6d\n",
4940 cr->nodeid, atc->n);
4943 if (flags & GMX_PME_SPREAD_Q)
4945 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
4947 /* Spread the charges on a grid */
4948 spread_on_grid(pme, &pme->atc[0], pmegrid, bFirst, TRUE, fftgrid, bDoSplines);
4952 inc_nrnb(nrnb, eNR_WEIGHTS, DIM*atc->n);
4954 inc_nrnb(nrnb, eNR_SPREADQBSP,
4955 pme->pme_order*pme->pme_order*pme->pme_order*atc->n);
4957 if (!pme->bUseThreads)
4959 wrap_periodic_pmegrid(pme, grid);
4961 /* sum contributions to local grid from other nodes */
4963 if (pme->nnodes > 1)
4965 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_QGRID_FORWARD);
4970 copy_pmegrid_to_fftgrid(pme, grid, fftgrid, q);
4973 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
4976 dump_local_fftgrid(pme,fftgrid);
4981 /* Here we start a large thread parallel region */
4982 #pragma omp parallel num_threads(pme->nthread) private(thread)
4984 thread = gmx_omp_get_thread_num();
4985 if (flags & GMX_PME_SOLVE)
4992 wallcycle_start(wcycle, ewcPME_FFT);
4994 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_REAL_TO_COMPLEX,
4998 wallcycle_stop(wcycle, ewcPME_FFT);
5002 /* solve in k-space for our local cells */
5005 wallcycle_start(wcycle, (q < DO_Q ? ewcPME_SOLVE : ewcLJPME));
5010 solve_pme_yzx(pme, cfftgrid, ewaldcoeff_q,
5011 box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
5013 pme->nthread, thread);
5018 solve_pme_lj_yzx(pme, &cfftgrid, FALSE, ewaldcoeff_lj,
5019 box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
5021 pme->nthread, thread);
5026 wallcycle_stop(wcycle, (q < DO_Q ? ewcPME_SOLVE : ewcLJPME));
5028 inc_nrnb(nrnb, eNR_SOLVEPME, loop_count);
5038 wallcycle_start(wcycle, ewcPME_FFT);
5040 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_COMPLEX_TO_REAL,
5044 wallcycle_stop(wcycle, ewcPME_FFT);
5048 if (pme->nodeid == 0)
5050 ntot = pme->nkx*pme->nky*pme->nkz;
5051 npme = ntot*log((real)ntot)/log(2.0);
5052 inc_nrnb(nrnb, eNR_FFT, 2*npme);
5055 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
5058 copy_fftgrid_to_pmegrid(pme, fftgrid, grid, q, pme->nthread, thread);
5061 /* End of thread parallel section.
5062 * With MPI we have to synchronize here before gmx_sum_qgrid_dd.
5067 /* distribute local grid to all nodes */
5069 if (pme->nnodes > 1)
5071 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_QGRID_BACKWARD);
5076 unwrap_periodic_pmegrid(pme, grid);
5078 /* interpolate forces for our local atoms */
5082 /* If we are running without parallelization,
5083 * atc->f is the actual force array, not a buffer,
5084 * therefore we should not clear it.
5086 lambda = q < DO_Q ? lambda_q : lambda_lj;
5087 bClearF = (bFirst && PAR(cr));
5088 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
5089 for (thread = 0; thread < pme->nthread; thread++)
5091 gather_f_bsplines(pme, grid, bClearF, atc,
5092 &atc->spline[thread],
5093 pme->bFEP ? (q % 2 == 0 ? 1.0-lambda : lambda) : 1.0);
5098 inc_nrnb(nrnb, eNR_GATHERFBSP,
5099 pme->pme_order*pme->pme_order*pme->pme_order*pme->atc[0].n);
5100 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
5105 /* This should only be called on the master thread
5106 * and after the threads have synchronized.
5110 get_pme_ener_vir_q(pme, pme->nthread, &energy_AB[q], vir_AB[q]);
5114 get_pme_ener_vir_lj(pme, pme->nthread, &energy_AB[q], vir_AB[q]);
5120 /* For Lorentz-Berthelot combination rules in LJ-PME, we need to calculate
5123 if (flags & GMX_PME_LJ_LB)
5125 real *local_c6 = NULL, *local_sigma = NULL;
5126 if (pme->nnodes == 1)
5128 if (pme->lb_buf1 == NULL)
5130 pme->lb_buf_nalloc = pme->atc[0].n;
5131 snew(pme->lb_buf1, pme->lb_buf_nalloc);
5133 pme->atc[0].q = pme->lb_buf1;
5135 local_sigma = sigmaA;
5141 wallcycle_start(wcycle, ewcPME_REDISTXF);
5143 do_redist_x_q(pme, cr, start, homenr, bFirst, x, c6A);
5144 if (pme->lb_buf_nalloc < atc->n)
5146 pme->lb_buf_nalloc = atc->nalloc;
5147 srenew(pme->lb_buf1, pme->lb_buf_nalloc);
5148 srenew(pme->lb_buf2, pme->lb_buf_nalloc);
5150 local_c6 = pme->lb_buf1;
5151 for (i = 0; i < atc->n; ++i)
5153 local_c6[i] = atc->q[i];
5157 do_redist_x_q(pme, cr, start, homenr, FALSE, x, sigmaA);
5158 local_sigma = pme->lb_buf2;
5159 for (i = 0; i < atc->n; ++i)
5161 local_sigma[i] = atc->q[i];
5165 wallcycle_stop(wcycle, ewcPME_REDISTXF);
5167 calc_initial_lb_coeffs(pme, local_c6, local_sigma);
5169 /*Seven terms in LJ-PME with LB, q < 2 reserved for electrostatics*/
5170 for (q = 2; q < 9; ++q)
5172 /* Unpack structure */
5173 pmegrid = &pme->pmegrid[q];
5174 fftgrid = pme->fftgrid[q];
5175 cfftgrid = pme->cfftgrid[q];
5176 pfft_setup = pme->pfft_setup[q];
5177 calc_next_lb_coeffs(pme, local_sigma);
5178 grid = pmegrid->grid.grid;
5181 if (flags & GMX_PME_SPREAD_Q)
5183 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
5184 /* Spread the charges on a grid */
5185 spread_on_grid(pme, &pme->atc[0], pmegrid, bFirst, TRUE, fftgrid, bDoSplines);
5189 inc_nrnb(nrnb, eNR_WEIGHTS, DIM*atc->n);
5191 inc_nrnb(nrnb, eNR_SPREADQBSP,
5192 pme->pme_order*pme->pme_order*pme->pme_order*atc->n);
5193 if (pme->nthread == 1)
5195 wrap_periodic_pmegrid(pme, grid);
5197 /* sum contributions to local grid from other nodes */
5199 if (pme->nnodes > 1)
5201 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_QGRID_FORWARD);
5205 copy_pmegrid_to_fftgrid(pme, grid, fftgrid, q);
5208 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
5211 /*Here we start a large thread parallel region*/
5212 #pragma omp parallel num_threads(pme->nthread) private(thread)
5214 thread = gmx_omp_get_thread_num();
5215 if (flags & GMX_PME_SOLVE)
5220 wallcycle_start(wcycle, ewcPME_FFT);
5223 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_REAL_TO_COMPLEX,
5227 wallcycle_stop(wcycle, ewcPME_FFT);
5234 if (flags & GMX_PME_SOLVE)
5237 /* solve in k-space for our local cells */
5238 #pragma omp parallel num_threads(pme->nthread) private(thread)
5240 thread = gmx_omp_get_thread_num();
5243 wallcycle_start(wcycle, ewcLJPME);
5247 solve_pme_lj_yzx(pme, &pme->cfftgrid[2], TRUE, ewaldcoeff_lj,
5248 box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
5250 pme->nthread, thread);
5253 wallcycle_stop(wcycle, ewcLJPME);
5255 inc_nrnb(nrnb, eNR_SOLVEPME, loop_count);
5262 /* This should only be called on the master thread and
5263 * after the threads have synchronized.
5265 get_pme_ener_vir_lj(pme, pme->nthread, &energy_AB[2], vir_AB[2]);
5270 bFirst = !(flags & GMX_PME_DO_COULOMB);
5271 calc_initial_lb_coeffs(pme, local_c6, local_sigma);
5272 for (q = 8; q >= 2; --q)
5274 /* Unpack structure */
5275 pmegrid = &pme->pmegrid[q];
5276 fftgrid = pme->fftgrid[q];
5277 cfftgrid = pme->cfftgrid[q];
5278 pfft_setup = pme->pfft_setup[q];
5279 grid = pmegrid->grid.grid;
5280 calc_next_lb_coeffs(pme, local_sigma);
5282 #pragma omp parallel num_threads(pme->nthread) private(thread)
5284 thread = gmx_omp_get_thread_num();
5289 wallcycle_start(wcycle, ewcPME_FFT);
5292 gmx_parallel_3dfft_execute(pfft_setup, GMX_FFT_COMPLEX_TO_REAL,
5296 wallcycle_stop(wcycle, ewcPME_FFT);
5300 if (pme->nodeid == 0)
5302 ntot = pme->nkx*pme->nky*pme->nkz;
5303 npme = ntot*log((real)ntot)/log(2.0);
5304 inc_nrnb(nrnb, eNR_FFT, 2*npme);
5306 wallcycle_start(wcycle, ewcPME_SPREADGATHER);
5309 copy_fftgrid_to_pmegrid(pme, fftgrid, grid, q, pme->nthread, thread);
5311 } /*#pragma omp parallel*/
5313 /* distribute local grid to all nodes */
5315 if (pme->nnodes > 1)
5317 gmx_sum_qgrid_dd(pme, grid, GMX_SUM_QGRID_BACKWARD);
5322 unwrap_periodic_pmegrid(pme, grid);
5324 /* interpolate forces for our local atoms */
5326 bClearF = (bFirst && PAR(cr));
5327 scale = pme->bFEP ? (q < 9 ? 1.0-lambda_lj : lambda_lj) : 1.0;
5328 scale *= lb_scale_factor[q-2];
5329 #pragma omp parallel for num_threads(pme->nthread) schedule(static)
5330 for (thread = 0; thread < pme->nthread; thread++)
5332 gather_f_bsplines(pme, grid, bClearF, &pme->atc[0],
5333 &pme->atc[0].spline[thread],
5338 inc_nrnb(nrnb, eNR_GATHERFBSP,
5339 pme->pme_order*pme->pme_order*pme->pme_order*pme->atc[0].n);
5340 wallcycle_stop(wcycle, ewcPME_SPREADGATHER);
5343 } /*for (q = 8; q >= 2; --q)*/
5345 } /*if (flags & GMX_PME_LJ_LB)*/
5347 if (bCalcF && pme->nnodes > 1)
5349 wallcycle_start(wcycle, ewcPME_REDISTXF);
5350 for (d = 0; d < pme->ndecompdim; d++)
5353 if (d == pme->ndecompdim - 1)
5360 n_d = pme->atc[d+1].n;
5361 f_d = pme->atc[d+1].f;
5363 if (DOMAINDECOMP(cr))
5365 dd_pmeredist_f(pme, atc, n_d, f_d,
5366 d == pme->ndecompdim-1 && pme->bPPnode);
5370 pmeredist_pd(pme, FALSE, n_d, TRUE, f_d, NULL, atc);
5374 wallcycle_stop(wcycle, ewcPME_REDISTXF);
5380 if (flags & GMX_PME_DO_COULOMB)
5384 *energy_q = energy_AB[0];
5385 m_add(vir_q, vir_AB[0], vir_q);
5389 *energy_q = (1.0-lambda_q)*energy_AB[0] + lambda_q*energy_AB[1];
5390 *dvdlambda_q += energy_AB[1] - energy_AB[0];
5391 for (i = 0; i < DIM; i++)
5393 for (j = 0; j < DIM; j++)
5395 vir_q[i][j] += (1.0-lambda_q)*vir_AB[0][i][j] +
5396 lambda_q*vir_AB[1][i][j];
5402 fprintf(debug, "Electrostatic PME mesh energy: %g\n", *energy_q);
5410 if (flags & GMX_PME_DO_LJ)
5414 *energy_lj = energy_AB[2];
5415 m_add(vir_lj, vir_AB[2], vir_lj);
5419 *energy_lj = (1.0-lambda_lj)*energy_AB[2] + lambda_lj*energy_AB[3];
5420 *dvdlambda_lj += energy_AB[3] - energy_AB[2];
5421 for (i = 0; i < DIM; i++)
5423 for (j = 0; j < DIM; j++)
5425 vir_lj[i][j] += (1.0-lambda_lj)*vir_AB[2][i][j] + lambda_lj*vir_AB[3][i][j];
5431 fprintf(debug, "Lennard-Jones PME mesh energy: %g\n", *energy_lj);