Pipeline GPU PME Spline/Spread with PP Comms
[alexxy/gromacs.git] / src / gromacs / ewald / pme_only.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
7  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
8  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9  * and including many others, as listed in the AUTHORS file in the
10  * top-level source directory and at http://www.gromacs.org.
11  *
12  * GROMACS is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 2.1
15  * of the License, or (at your option) any later version.
16  *
17  * GROMACS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with GROMACS; if not, see
24  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
26  *
27  * If you want to redistribute modifications to GROMACS, please
28  * consider that scientific software is very special. Version
29  * control is crucial - bugs must be traceable. We will be happy to
30  * consider code for inclusion in the official distribution, but
31  * derived work must not be called official GROMACS. Details are found
32  * in the README & COPYING files - if they are missing, get the
33  * official version at http://www.gromacs.org.
34  *
35  * To help us fund GROMACS development, we humbly ask that you cite
36  * the research papers on the package. Check out http://www.gromacs.org.
37  */
38 /* IMPORTANT FOR DEVELOPERS:
39  *
40  * Triclinic pme stuff isn't entirely trivial, and we've experienced
41  * some bugs during development (many of them due to me). To avoid
42  * this in the future, please check the following things if you make
43  * changes in this file:
44  *
45  * 1. You should obtain identical (at least to the PME precision)
46  *    energies, forces, and virial for
47  *    a rectangular box and a triclinic one where the z (or y) axis is
48  *    tilted a whole box side. For instance you could use these boxes:
49  *
50  *    rectangular       triclinic
51  *     2  0  0           2  0  0
52  *     0  2  0           0  2  0
53  *     0  0  6           2  2  6
54  *
55  * 2. You should check the energy conservation in a triclinic box.
56  *
57  * It might seem an overkill, but better safe than sorry.
58  * /Erik 001109
59  */
60
61 #include "gmxpre.h"
62
63 #include "pme_only.h"
64
65 #include "config.h"
66
67 #include <cassert>
68 #include <cmath>
69 #include <cstdio>
70 #include <cstdlib>
71 #include <cstring>
72
73 #include <memory>
74 #include <numeric>
75 #include <vector>
76
77 #include "gromacs/domdec/domdec.h"
78 #include "gromacs/ewald/pme.h"
79 #include "gromacs/ewald/pme_coordinate_receiver_gpu.h"
80 #include "gromacs/ewald/pme_force_sender_gpu.h"
81 #include "gromacs/fft/parallel_3dfft.h"
82 #include "gromacs/fileio/pdbio.h"
83 #include "gromacs/gmxlib/network.h"
84 #include "gromacs/gmxlib/nrnb.h"
85 #include "gromacs/gpu_utils/device_stream_manager.h"
86 #include "gromacs/gpu_utils/hostallocator.h"
87 #include "gromacs/math/gmxcomplex.h"
88 #include "gromacs/math/units.h"
89 #include "gromacs/math/vec.h"
90 #include "gromacs/mdtypes/commrec.h"
91 #include "gromacs/mdtypes/forceoutput.h"
92 #include "gromacs/mdtypes/inputrec.h"
93 #include "gromacs/mdtypes/simulation_workload.h"
94 #include "gromacs/mdtypes/state_propagator_data_gpu.h"
95 #include "gromacs/timing/cyclecounter.h"
96 #include "gromacs/timing/wallcycle.h"
97 #include "gromacs/utility/fatalerror.h"
98 #include "gromacs/utility/futil.h"
99 #include "gromacs/utility/gmxmpi.h"
100 #include "gromacs/utility/gmxomp.h"
101 #include "gromacs/utility/smalloc.h"
102
103 #include "pme_gpu_internal.h"
104 #include "pme_internal.h"
105 #include "pme_output.h"
106 #include "pme_pp_communication.h"
107
108 /*! \brief Master PP-PME communication data structure */
109 struct gmx_pme_pp
110 {
111     MPI_Comm             mpi_comm_mysim; /**< MPI communicator for this simulation */
112     std::vector<PpRanks> ppRanks;        /**< The PP partner ranks                 */
113     int                  peerRankId;     /**< The peer PP rank id                  */
114     //@{
115     /**< Vectors of A- and B-state parameters used to transfer vectors to PME ranks  */
116     gmx::PaddedHostVector<real> chargeA;
117     std::vector<real>           chargeB;
118     std::vector<real>           sqrt_c6A;
119     std::vector<real>           sqrt_c6B;
120     std::vector<real>           sigmaA;
121     std::vector<real>           sigmaB;
122     //@}
123     gmx::HostVector<gmx::RVec> x; /**< Vector of atom coordinates to transfer to PME ranks */
124     std::vector<gmx::RVec>     f; /**< Vector of atom forces received from PME ranks */
125     //@{
126     /**< Vectors of MPI objects used in non-blocking communication between multiple PP ranks per PME rank */
127     std::vector<MPI_Request> req;
128     std::vector<MPI_Status>  stat;
129     //@}
130
131     /*! \brief object for receiving coordinates using communications operating on GPU memory space */
132     std::unique_ptr<gmx::PmeCoordinateReceiverGpu> pmeCoordinateReceiverGpu;
133     /*! \brief object for sending PME force using communications operating on GPU memory space */
134     std::unique_ptr<gmx::PmeForceSenderGpu> pmeForceSenderGpu;
135
136     /*! \brief whether GPU direct communications are active for PME-PP transfers */
137     bool useGpuDirectComm = false;
138     /*! \brief whether GPU direct communications should send forces directly to remote GPU memory */
139     bool sendForcesDirectToPpGpu = false;
140 };
141
142 /*! \brief Initialize the PME-only side of the PME <-> PP communication */
143 static std::unique_ptr<gmx_pme_pp> gmx_pme_pp_init(const t_commrec* cr)
144 {
145     auto pme_pp = std::make_unique<gmx_pme_pp>();
146
147 #if GMX_MPI
148     int rank;
149
150     pme_pp->mpi_comm_mysim = cr->mpi_comm_mysim;
151     MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
152     auto ppRanks = get_pme_ddranks(cr, rank);
153     pme_pp->ppRanks.reserve(ppRanks.size());
154     for (const auto& ppRankId : ppRanks)
155     {
156         pme_pp->ppRanks.push_back({ ppRankId, 0 });
157     }
158     // The peer PP rank is the last one.
159     pme_pp->peerRankId = pme_pp->ppRanks.back().rankId;
160     pme_pp->req.resize(eCommType_NR * pme_pp->ppRanks.size());
161     pme_pp->stat.resize(eCommType_NR * pme_pp->ppRanks.size());
162 #else
163     GMX_UNUSED_VALUE(cr);
164 #endif
165
166     return pme_pp;
167 }
168
169 static void reset_pmeonly_counters(gmx_wallcycle*            wcycle,
170                                    gmx_walltime_accounting_t walltime_accounting,
171                                    t_nrnb*                   nrnb,
172                                    int64_t                   step,
173                                    bool                      useGpuForPme)
174 {
175     /* Reset all the counters related to performance over the run */
176     wallcycle_stop(wcycle, WallCycleCounter::Run);
177     wallcycle_reset_all(wcycle);
178     *nrnb = { 0 };
179     wallcycle_start(wcycle, WallCycleCounter::Run);
180     walltime_accounting_reset_time(walltime_accounting, step);
181
182     if (useGpuForPme)
183     {
184         resetGpuProfiler();
185     }
186 }
187
188 static gmx_pme_t* gmx_pmeonly_switch(std::vector<gmx_pme_t*>* pmedata,
189                                      const ivec               grid_size,
190                                      real                     ewaldcoeff_q,
191                                      real                     ewaldcoeff_lj,
192                                      const t_commrec*         cr,
193                                      const t_inputrec*        ir)
194 {
195     GMX_ASSERT(pmedata, "Bad PME tuning list pointer");
196     for (auto& pme : *pmedata)
197     {
198         GMX_ASSERT(pme, "Bad PME tuning list element pointer");
199         if (gmx_pme_grid_matches(*pme, grid_size))
200         {
201             /* Here we have found an existing PME data structure that suits us.
202              * However, in the GPU case, we have to reinitialize it - there's only one GPU structure.
203              * This should not cause actual GPU reallocations, at least (the allocated buffers are never shrunk).
204              * So, just some grid size updates in the GPU kernel parameters.
205              * TODO: this should be something like gmx_pme_update_split_params()
206              */
207             gmx_pme_reinit(&pme, cr, pme, ir, grid_size, ewaldcoeff_q, ewaldcoeff_lj);
208             return pme;
209         }
210     }
211
212     const auto& pme          = pmedata->back();
213     gmx_pme_t*  newStructure = nullptr;
214     // Copy last structure with new grid params
215     gmx_pme_reinit(&newStructure, cr, pme, ir, grid_size, ewaldcoeff_q, ewaldcoeff_lj);
216     pmedata->push_back(newStructure);
217     return newStructure;
218 }
219
220 /*! \brief Called by PME-only ranks to receive coefficients and coordinates
221  *
222  * Note that with GPU direct communication the transfer is only initiated, it is the responsibility
223  * of the caller to synchronize prior to launching spread.
224  *
225  * \param[in] pme                     PME data structure.
226  * \param[in,out] pme_pp              PME-PP communication structure.
227  * \param[out] natoms                 Number of received atoms.
228  * \param[out] box                    System box, if received.
229  * \param[out] maxshift_x             Maximum shift in X direction, if received.
230  * \param[out] maxshift_y             Maximum shift in Y direction, if received.
231  * \param[out] lambda_q               Free-energy lambda for electrostatics, if received.
232  * \param[out] lambda_lj              Free-energy lambda for Lennard-Jones, if received.
233  * \param[out] computeEnergyAndVirial Set to true if this is an energy/virial calculation
234  *                                    step, otherwise set to false.
235  * \param[out] step                   MD integration step number.
236  * \param[out] grid_size              PME grid size, if received.
237  * \param[out] ewaldcoeff_q           Ewald cut-off parameter for electrostatics, if received.
238  * \param[out] ewaldcoeff_lj          Ewald cut-off parameter for Lennard-Jones, if received.
239  * \param[in]  useGpuForPme           Flag on whether PME is on GPU.
240  * \param[in]  stateGpu               GPU state propagator object.
241  * \param[in]  runMode                PME run mode.
242  *
243  * \retval pmerecvqxX                 All parameters were set, chargeA and chargeB can be NULL.
244  * \retval pmerecvqxFINISH            No parameters were set.
245  * \retval pmerecvqxSWITCHGRID        Only grid_size and *ewaldcoeff were set.
246  * \retval pmerecvqxRESETCOUNTERS     *step was set.
247  */
248 static int gmx_pme_recv_coeffs_coords(struct gmx_pme_t*            pme,
249                                       gmx_pme_pp*                  pme_pp,
250                                       int*                         natoms,
251                                       matrix                       box,
252                                       int*                         maxshift_x,
253                                       int*                         maxshift_y,
254                                       real*                        lambda_q,
255                                       real*                        lambda_lj,
256                                       gmx_bool*                    computeEnergyAndVirial,
257                                       int64_t*                     step,
258                                       ivec*                        grid_size,
259                                       real*                        ewaldcoeff_q,
260                                       real*                        ewaldcoeff_lj,
261                                       bool                         useGpuForPme,
262                                       gmx::StatePropagatorDataGpu* stateGpu,
263                                       PmeRunMode gmx_unused        runMode)
264 {
265     int status = -1;
266     int nat    = 0;
267
268 #if GMX_MPI
269     int  messages       = 0;
270     bool atomSetChanged = false;
271
272     do
273     {
274         gmx_pme_comm_n_box_t cnb;
275         cnb.flags = 0;
276
277         /* Receive the send count, box and time step from the peer PP node */
278         MPI_Recv(&cnb, sizeof(cnb), MPI_BYTE, pme_pp->peerRankId, eCommType_CNB, pme_pp->mpi_comm_mysim, MPI_STATUS_IGNORE);
279
280         *step = cnb.step;
281
282         if (debug)
283         {
284             fprintf(debug,
285                     "PME only rank receiving:%s%s%s%s%s\n",
286                     (cnb.flags & PP_PME_CHARGE) ? " charges" : "",
287                     (cnb.flags & PP_PME_COORD) ? " coordinates" : "",
288                     (cnb.flags & PP_PME_FINISH) ? " finish" : "",
289                     (cnb.flags & PP_PME_SWITCHGRID) ? " switch grid" : "",
290                     (cnb.flags & PP_PME_RESETCOUNTERS) ? " reset counters" : "");
291         }
292
293         pme_pp->useGpuDirectComm = ((cnb.flags & PP_PME_GPUCOMMS) != 0);
294         GMX_ASSERT(!pme_pp->useGpuDirectComm || (pme_pp->pmeForceSenderGpu != nullptr),
295                    "The use of GPU direct communication for PME-PP is enabled, "
296                    "but the PME GPU force reciever object does not exist");
297         pme_pp->sendForcesDirectToPpGpu = ((cnb.flags & PP_PME_RECVFTOGPU) != 0);
298
299         if (cnb.flags & PP_PME_FINISH)
300         {
301             status = pmerecvqxFINISH;
302         }
303
304         if (cnb.flags & PP_PME_SWITCHGRID)
305         {
306             /* Special case, receive the new parameters and return */
307             copy_ivec(cnb.grid_size, *grid_size);
308             *ewaldcoeff_q  = cnb.ewaldcoeff_q;
309             *ewaldcoeff_lj = cnb.ewaldcoeff_lj;
310
311             status = pmerecvqxSWITCHGRID;
312         }
313
314         if (cnb.flags & PP_PME_RESETCOUNTERS)
315         {
316             /* Special case, receive the step (set above) and return */
317             status = pmerecvqxRESETCOUNTERS;
318         }
319
320         if (cnb.flags & (PP_PME_CHARGE | PP_PME_SQRTC6 | PP_PME_SIGMA))
321         {
322             atomSetChanged = true;
323
324             /* Receive the send counts from the other PP nodes */
325             for (auto& sender : pme_pp->ppRanks)
326             {
327                 if (sender.rankId == pme_pp->peerRankId)
328                 {
329                     sender.numAtoms = cnb.natoms;
330                 }
331                 else
332                 {
333                     MPI_Irecv(&sender.numAtoms,
334                               sizeof(sender.numAtoms),
335                               MPI_BYTE,
336                               sender.rankId,
337                               eCommType_CNB,
338                               pme_pp->mpi_comm_mysim,
339                               &pme_pp->req[messages++]);
340                 }
341             }
342             MPI_Waitall(messages, pme_pp->req.data(), pme_pp->stat.data());
343             messages = 0;
344
345             nat = 0;
346             for (const auto& sender : pme_pp->ppRanks)
347             {
348                 nat += sender.numAtoms;
349             }
350
351             if (cnb.flags & PP_PME_CHARGE)
352             {
353                 pme_pp->chargeA.resizeWithPadding(nat);
354             }
355             if (cnb.flags & PP_PME_CHARGEB)
356             {
357                 pme_pp->chargeB.resize(nat);
358             }
359             if (cnb.flags & PP_PME_SQRTC6)
360             {
361                 pme_pp->sqrt_c6A.resize(nat);
362             }
363             if (cnb.flags & PP_PME_SQRTC6B)
364             {
365                 pme_pp->sqrt_c6B.resize(nat);
366             }
367             if (cnb.flags & PP_PME_SIGMA)
368             {
369                 pme_pp->sigmaA.resize(nat);
370             }
371             if (cnb.flags & PP_PME_SIGMAB)
372             {
373                 pme_pp->sigmaB.resize(nat);
374             }
375             pme_pp->x.resize(nat);
376             pme_pp->f.resize(nat);
377
378             /* maxshift is sent when the charges are sent */
379             *maxshift_x = cnb.maxshift_x;
380             *maxshift_y = cnb.maxshift_y;
381
382             /* Receive the charges in place */
383             for (int q = 0; q < eCommType_NR; q++)
384             {
385                 real* bufferPtr;
386
387                 if (!(cnb.flags & (PP_PME_CHARGE << q)))
388                 {
389                     continue;
390                 }
391                 switch (q)
392                 {
393                     case eCommType_ChargeA: bufferPtr = pme_pp->chargeA.data(); break;
394                     case eCommType_ChargeB: bufferPtr = pme_pp->chargeB.data(); break;
395                     case eCommType_SQRTC6A: bufferPtr = pme_pp->sqrt_c6A.data(); break;
396                     case eCommType_SQRTC6B: bufferPtr = pme_pp->sqrt_c6B.data(); break;
397                     case eCommType_SigmaA: bufferPtr = pme_pp->sigmaA.data(); break;
398                     case eCommType_SigmaB: bufferPtr = pme_pp->sigmaB.data(); break;
399                     default: gmx_incons("Wrong eCommType");
400                 }
401                 nat = 0;
402                 for (const auto& sender : pme_pp->ppRanks)
403                 {
404                     if (sender.numAtoms > 0)
405                     {
406                         MPI_Irecv(bufferPtr + nat,
407                                   sender.numAtoms * sizeof(real),
408                                   MPI_BYTE,
409                                   sender.rankId,
410                                   q,
411                                   pme_pp->mpi_comm_mysim,
412                                   &pme_pp->req[messages++]);
413                         nat += sender.numAtoms;
414                         if (debug)
415                         {
416                             fprintf(debug,
417                                     "Received from PP rank %d: %d %s\n",
418                                     sender.rankId,
419                                     sender.numAtoms,
420                                     (q == eCommType_ChargeA || q == eCommType_ChargeB) ? "charges"
421                                                                                        : "params");
422                         }
423                     }
424                 }
425             }
426         }
427
428         if (cnb.flags & PP_PME_COORD)
429         {
430             if (atomSetChanged)
431             {
432                 gmx_pme_reinit_atoms(pme, nat, pme_pp->chargeA, pme_pp->chargeB);
433                 if (useGpuForPme)
434                 {
435                     stateGpu->reinit(nat, nat);
436                     pme_gpu_set_device_x(pme, stateGpu->getCoordinates());
437                 }
438                 if (pme_pp->useGpuDirectComm)
439                 {
440                     GMX_ASSERT(runMode == PmeRunMode::GPU,
441                                "GPU Direct PME-PP communication has been enabled, "
442                                "but PME run mode is not PmeRunMode::GPU\n");
443
444                     // This rank will have its data accessed directly by PP rank, so needs to send the remote addresses and re-set atom ranges associated with transfers.
445                     pme_pp->pmeCoordinateReceiverGpu->reinitCoordinateReceiver(stateGpu->getCoordinates());
446                     pme_pp->pmeForceSenderGpu->setForceSendBuffer(pme_gpu_get_device_f(pme));
447                 }
448             }
449
450
451             /* The box, FE flag and lambda are sent along with the coordinates
452              *  */
453             copy_mat(cnb.box, box);
454             *lambda_q               = cnb.lambda_q;
455             *lambda_lj              = cnb.lambda_lj;
456             *computeEnergyAndVirial = ((cnb.flags & PP_PME_ENER_VIR) != 0U);
457             *step                   = cnb.step;
458
459             /* Receive the coordinates in place */
460             nat = 0;
461             for (const auto& sender : pme_pp->ppRanks)
462             {
463                 if (sender.numAtoms > 0)
464                 {
465                     if (pme_pp->useGpuDirectComm)
466                     {
467                         if (GMX_THREAD_MPI)
468                         {
469                             pme_pp->pmeCoordinateReceiverGpu->receiveCoordinatesSynchronizerFromPpCudaDirect(
470                                     sender.rankId);
471                         }
472                         else
473                         {
474                             pme_pp->pmeCoordinateReceiverGpu->launchReceiveCoordinatesFromPpCudaMpi(
475                                     stateGpu->getCoordinates(), nat, sender.numAtoms * sizeof(rvec), sender.rankId);
476                         }
477                     }
478                     else
479                     {
480                         MPI_Irecv(pme_pp->x[nat],
481                                   sender.numAtoms * sizeof(rvec),
482                                   MPI_BYTE,
483                                   sender.rankId,
484                                   eCommType_COORD,
485                                   pme_pp->mpi_comm_mysim,
486                                   &pme_pp->req[messages++]);
487                     }
488                     nat += sender.numAtoms;
489                     if (debug)
490                     {
491                         fprintf(debug,
492                                 "Received from PP rank %d: %d "
493                                 "coordinates\n",
494                                 sender.rankId,
495                                 sender.numAtoms);
496                     }
497                 }
498             }
499
500             status = pmerecvqxX;
501         }
502
503         /* Wait for the coordinates and/or charges to arrive */
504         MPI_Waitall(messages, pme_pp->req.data(), pme_pp->stat.data());
505         messages = 0;
506     } while (status == -1);
507 #else
508     GMX_UNUSED_VALUE(pme);
509     GMX_UNUSED_VALUE(pme_pp);
510     GMX_UNUSED_VALUE(box);
511     GMX_UNUSED_VALUE(maxshift_x);
512     GMX_UNUSED_VALUE(maxshift_y);
513     GMX_UNUSED_VALUE(lambda_q);
514     GMX_UNUSED_VALUE(lambda_lj);
515     GMX_UNUSED_VALUE(computeEnergyAndVirial);
516     GMX_UNUSED_VALUE(step);
517     GMX_UNUSED_VALUE(grid_size);
518     GMX_UNUSED_VALUE(ewaldcoeff_q);
519     GMX_UNUSED_VALUE(ewaldcoeff_lj);
520     GMX_UNUSED_VALUE(useGpuForPme);
521     GMX_UNUSED_VALUE(stateGpu);
522
523     status = pmerecvqxX;
524 #endif
525
526     if (status == pmerecvqxX)
527     {
528         *natoms = nat;
529     }
530
531     return status;
532 }
533
534 /*! \brief Send the PME mesh force, virial and energy to the PP-only ranks. */
535 static void gmx_pme_send_force_vir_ener(const gmx_pme_t& pme,
536                                         gmx_pme_pp*      pme_pp,
537                                         const PmeOutput& output,
538                                         real             dvdlambda_q,
539                                         real             dvdlambda_lj,
540                                         float            cycles)
541 {
542 #if GMX_MPI
543     gmx_pme_comm_vir_ene_t cve;
544     int                    messages, ind_start, ind_end;
545     cve.cycles = cycles;
546
547     if (pme_pp->useGpuDirectComm)
548     {
549         GMX_ASSERT((pme_pp->pmeForceSenderGpu != nullptr),
550                    "The use of GPU direct communication for PME-PP is enabled, "
551                    "but the PME GPU force reciever object does not exist");
552     }
553
554     messages = 0;
555     ind_end  = 0;
556
557     /* Now the evaluated forces have to be transferred to the PP ranks */
558     if (pme_pp->useGpuDirectComm && GMX_THREAD_MPI)
559     {
560         int numPpRanks = static_cast<int>(pme_pp->ppRanks.size());
561 #    pragma omp parallel for num_threads(std::min(numPpRanks, pme.nthread)) schedule(static)
562         for (int i = 0; i < numPpRanks; i++)
563         {
564             auto& receiver = pme_pp->ppRanks[i];
565             pme_pp->pmeForceSenderGpu->sendFToPpCudaDirect(
566                     receiver.rankId, receiver.numAtoms, pme_pp->sendForcesDirectToPpGpu);
567         }
568     }
569     else
570     {
571         for (const auto& receiver : pme_pp->ppRanks)
572         {
573             ind_start = ind_end;
574             ind_end   = ind_start + receiver.numAtoms;
575             if (pme_pp->useGpuDirectComm)
576             {
577                 pme_pp->pmeForceSenderGpu->sendFToPpCudaMpi(pme_gpu_get_device_f(&pme),
578                                                             ind_start,
579                                                             receiver.numAtoms * sizeof(rvec),
580                                                             receiver.rankId,
581                                                             &pme_pp->req[messages]);
582             }
583             else
584             {
585                 void* sendbuf = const_cast<void*>(static_cast<const void*>(output.forces_[ind_start]));
586                 // Send using MPI
587                 MPI_Isend(sendbuf,
588                           receiver.numAtoms * sizeof(rvec),
589                           MPI_BYTE,
590                           receiver.rankId,
591                           0,
592                           pme_pp->mpi_comm_mysim,
593                           &pme_pp->req[messages]);
594             }
595             messages++;
596         }
597     }
598
599     /* send virial and energy to our last PP node */
600     copy_mat(output.coulombVirial_, cve.vir_q);
601     copy_mat(output.lennardJonesVirial_, cve.vir_lj);
602     cve.energy_q     = output.coulombEnergy_;
603     cve.energy_lj    = output.lennardJonesEnergy_;
604     cve.dvdlambda_q  = dvdlambda_q;
605     cve.dvdlambda_lj = dvdlambda_lj;
606     /* check for the signals to send back to a PP node */
607     cve.stop_cond = gmx_get_stop_condition();
608
609     cve.cycles = cycles;
610
611     if (debug)
612     {
613         fprintf(debug, "PME rank sending to PP rank %d: virial and energy\n", pme_pp->peerRankId);
614     }
615     MPI_Isend(&cve, sizeof(cve), MPI_BYTE, pme_pp->peerRankId, 1, pme_pp->mpi_comm_mysim, &pme_pp->req[messages++]);
616
617     /* Wait for the forces to arrive */
618     MPI_Waitall(messages, pme_pp->req.data(), pme_pp->stat.data());
619 #else
620     GMX_RELEASE_ASSERT(false, "Invalid call to gmx_pme_send_force_vir_ener");
621     GMX_UNUSED_VALUE(pme);
622     GMX_UNUSED_VALUE(pme_pp);
623     GMX_UNUSED_VALUE(output);
624     GMX_UNUSED_VALUE(dvdlambda_q);
625     GMX_UNUSED_VALUE(dvdlambda_lj);
626     GMX_UNUSED_VALUE(cycles);
627 #endif
628 }
629
630 int gmx_pmeonly(struct gmx_pme_t*               pme,
631                 const t_commrec*                cr,
632                 t_nrnb*                         mynrnb,
633                 gmx_wallcycle*                  wcycle,
634                 gmx_walltime_accounting_t       walltime_accounting,
635                 t_inputrec*                     ir,
636                 PmeRunMode                      runMode,
637                 bool                            useGpuPmePpCommunication,
638                 const gmx::DeviceStreamManager* deviceStreamManager)
639 {
640     int     ret;
641     int     natoms = 0;
642     matrix  box;
643     real    lambda_q   = 0;
644     real    lambda_lj  = 0;
645     int     maxshift_x = 0, maxshift_y = 0;
646     real    dvdlambda_q, dvdlambda_lj;
647     float   cycles;
648     int     count;
649     bool    computeEnergyAndVirial = false;
650     int64_t step;
651
652     /* This data will only use with PME tuning, i.e. switching PME grids */
653     std::vector<gmx_pme_t*> pmedata;
654     pmedata.push_back(pme);
655
656     auto pme_pp = gmx_pme_pp_init(cr);
657
658     std::unique_ptr<gmx::StatePropagatorDataGpu> stateGpu;
659     // TODO the variable below should be queried from the task assignment info
660     const bool useGpuForPme = (runMode == PmeRunMode::GPU) || (runMode == PmeRunMode::Mixed);
661     if (useGpuForPme)
662     {
663         GMX_RELEASE_ASSERT(
664                 deviceStreamManager != nullptr,
665                 "Device stream manager can not be nullptr when using GPU in PME-only rank.");
666         GMX_RELEASE_ASSERT(deviceStreamManager->streamIsValid(gmx::DeviceStreamType::Pme),
667                            "Device stream can not be nullptr when using GPU in PME-only rank");
668         changePinningPolicy(&pme_pp->chargeA, pme_get_pinning_policy());
669         changePinningPolicy(&pme_pp->x, pme_get_pinning_policy());
670         if (useGpuPmePpCommunication)
671         {
672             pme_pp->pmeCoordinateReceiverGpu = std::make_unique<gmx::PmeCoordinateReceiverGpu>(
673                     pme_pp->mpi_comm_mysim, deviceStreamManager->context(), pme_pp->ppRanks);
674             pme_pp->pmeForceSenderGpu =
675                     std::make_unique<gmx::PmeForceSenderGpu>(pme_gpu_get_f_ready_synchronizer(pme),
676                                                              pme_pp->mpi_comm_mysim,
677                                                              deviceStreamManager->context(),
678                                                              pme_pp->ppRanks);
679         }
680         // TODO: Special PME-only constructor is used here. There is no mechanism to prevent from using the other constructor here.
681         //       This should be made safer.
682         stateGpu = std::make_unique<gmx::StatePropagatorDataGpu>(
683                 &deviceStreamManager->stream(gmx::DeviceStreamType::Pme),
684                 deviceStreamManager->context(),
685                 GpuApiCallBehavior::Async,
686                 pme_gpu_get_block_size(pme),
687                 wcycle);
688     }
689
690     clear_nrnb(mynrnb);
691
692     count = 0;
693     do /****** this is a quasi-loop over time steps! */
694     {
695         /* The reason for having a loop here is PME grid tuning/switching */
696         do
697         {
698             /* Domain decomposition */
699             ivec newGridSize;
700             real ewaldcoeff_q = 0, ewaldcoeff_lj = 0;
701             ret = gmx_pme_recv_coeffs_coords(pme,
702                                              pme_pp.get(),
703                                              &natoms,
704                                              box,
705                                              &maxshift_x,
706                                              &maxshift_y,
707                                              &lambda_q,
708                                              &lambda_lj,
709                                              &computeEnergyAndVirial,
710                                              &step,
711                                              &newGridSize,
712                                              &ewaldcoeff_q,
713                                              &ewaldcoeff_lj,
714                                              useGpuForPme,
715                                              stateGpu.get(),
716                                              runMode);
717
718             if (ret == pmerecvqxSWITCHGRID)
719             {
720                 /* Switch the PME grid to newGridSize */
721                 pme = gmx_pmeonly_switch(&pmedata, newGridSize, ewaldcoeff_q, ewaldcoeff_lj, cr, ir);
722             }
723
724             if (ret == pmerecvqxRESETCOUNTERS)
725             {
726                 /* Reset the cycle and flop counters */
727                 reset_pmeonly_counters(wcycle, walltime_accounting, mynrnb, step, useGpuForPme);
728             }
729         } while (ret == pmerecvqxSWITCHGRID || ret == pmerecvqxRESETCOUNTERS);
730
731         if (ret == pmerecvqxFINISH)
732         {
733             /* We should stop: break out of the loop */
734             break;
735         }
736
737         if (count == 0)
738         {
739             wallcycle_start(wcycle, WallCycleCounter::Run);
740             walltime_accounting_start_time(walltime_accounting);
741         }
742
743         wallcycle_start(wcycle, WallCycleCounter::PmeMesh);
744
745         dvdlambda_q  = 0;
746         dvdlambda_lj = 0;
747
748         // TODO Make a struct of array refs onto these per-atom fields
749         // of pme_pp (maybe box, energy and virial, too; and likewise
750         // from mdatoms for the other call to gmx_pme_do), so we have
751         // fewer lines of code and less parameter passing.
752         gmx::StepWorkload stepWork;
753         stepWork.computeVirial = computeEnergyAndVirial;
754         stepWork.computeEnergy = computeEnergyAndVirial;
755         stepWork.computeForces = true;
756         PmeOutput output       = { {}, false, 0, { { 0 } }, 0, 0, { { 0 } }, 0 };
757         if (useGpuForPme)
758         {
759             stepWork.haveDynamicBox      = false;
760             stepWork.useGpuPmeFReduction = pme_pp->useGpuDirectComm;
761             // TODO this should be set properly by gmx_pme_recv_coeffs_coords,
762             // or maybe use inputrecDynamicBox(ir), at the very least - change this when this codepath is tested!
763             pme_gpu_prepare_computation(pme, box, wcycle, stepWork);
764             if (!pme_pp->useGpuDirectComm)
765             {
766                 stateGpu->copyCoordinatesToGpu(gmx::ArrayRef<gmx::RVec>(pme_pp->x),
767                                                gmx::AtomLocality::Local);
768             }
769             // On the separate PME rank we do not need a synchronizer as we schedule everything in a single stream
770             // TODO: with pme on GPU the receive should make a list of synchronizers and pass it here #3157
771             auto xReadyOnDevice = nullptr;
772
773             pme_gpu_launch_spread(pme,
774                                   xReadyOnDevice,
775                                   wcycle,
776                                   lambda_q,
777                                   pme_pp->useGpuDirectComm,
778                                   pme_pp->pmeCoordinateReceiverGpu.get());
779             pme_gpu_launch_complex_transforms(pme, wcycle, stepWork);
780             pme_gpu_launch_gather(pme, wcycle, lambda_q);
781             output = pme_gpu_wait_finish_task(pme, computeEnergyAndVirial, lambda_q, wcycle);
782             pme_gpu_reinit_computation(pme, wcycle);
783         }
784         else
785         {
786             GMX_ASSERT(pme_pp->x.size() == static_cast<size_t>(natoms),
787                        "The coordinate buffer should have size natoms");
788
789             gmx_pme_do(pme,
790                        pme_pp->x,
791                        pme_pp->f,
792                        pme_pp->chargeA,
793                        pme_pp->chargeB,
794                        pme_pp->sqrt_c6A,
795                        pme_pp->sqrt_c6B,
796                        pme_pp->sigmaA,
797                        pme_pp->sigmaB,
798                        box,
799                        cr,
800                        maxshift_x,
801                        maxshift_y,
802                        mynrnb,
803                        wcycle,
804                        output.coulombVirial_,
805                        output.lennardJonesVirial_,
806                        &output.coulombEnergy_,
807                        &output.lennardJonesEnergy_,
808                        lambda_q,
809                        lambda_lj,
810                        &dvdlambda_q,
811                        &dvdlambda_lj,
812                        stepWork);
813             output.forces_ = pme_pp->f;
814         }
815
816         cycles = wallcycle_stop(wcycle, WallCycleCounter::PmeMesh);
817         gmx_pme_send_force_vir_ener(*pme, pme_pp.get(), output, dvdlambda_q, dvdlambda_lj, cycles);
818
819         count++;
820     } /***** end of quasi-loop, we stop with the break above */
821     while (TRUE);
822
823     walltime_accounting_end_time(walltime_accounting);
824
825     return 0;
826 }