Move initiation of local CPU force H2D transfer to producer
[alexxy/gromacs.git] / src / gromacs / nbnxm / cuda / nbnxm_cuda_data_mgmt.cu
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5  * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36 /*! \file
37  *  \brief Define CUDA implementation of nbnxn_gpu_data_mgmt.h
38  *
39  *  \author Szilard Pall <pall.szilard@gmail.com>
40  */
41 #include "gmxpre.h"
42
43 #include <assert.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47
48 // TODO We would like to move this down, but the way NbnxmGpu
49 //      is currently declared means this has to be before gpu_types.h
50 #include "nbnxm_cuda_types.h"
51
52 // TODO Remove this comment when the above order issue is resolved
53 #include "gromacs/gpu_utils/cudautils.cuh"
54 #include "gromacs/gpu_utils/device_context.h"
55 #include "gromacs/gpu_utils/device_stream_manager.h"
56 #include "gromacs/gpu_utils/gpu_utils.h"
57 #include "gromacs/gpu_utils/gpueventsynchronizer.cuh"
58 #include "gromacs/gpu_utils/pmalloc_cuda.h"
59 #include "gromacs/hardware/device_information.h"
60 #include "gromacs/hardware/device_management.h"
61 #include "gromacs/math/vectypes.h"
62 #include "gromacs/mdlib/force_flags.h"
63 #include "gromacs/mdtypes/interaction_const.h"
64 #include "gromacs/mdtypes/md_enums.h"
65 #include "gromacs/nbnxm/atomdata.h"
66 #include "gromacs/nbnxm/gpu_data_mgmt.h"
67 #include "gromacs/nbnxm/gridset.h"
68 #include "gromacs/nbnxm/nbnxm.h"
69 #include "gromacs/nbnxm/nbnxm_gpu.h"
70 #include "gromacs/nbnxm/nbnxm_gpu_data_mgmt.h"
71 #include "gromacs/nbnxm/pairlistsets.h"
72 #include "gromacs/pbcutil/ishift.h"
73 #include "gromacs/timing/gpu_timing.h"
74 #include "gromacs/utility/basedefinitions.h"
75 #include "gromacs/utility/cstringutil.h"
76 #include "gromacs/utility/fatalerror.h"
77 #include "gromacs/utility/real.h"
78 #include "gromacs/utility/smalloc.h"
79
80 #include "nbnxm_cuda.h"
81
82 namespace Nbnxm
83 {
84
85 /* This is a heuristically determined parameter for the Kepler
86  * and Maxwell architectures for the minimum size of ci lists by multiplying
87  * this constant with the # of multiprocessors on the current device.
88  * Since the maximum number of blocks per multiprocessor is 16, the ideal
89  * count for small systems is 32 or 48 blocks per multiprocessor. Because
90  * there is a bit of fluctuations in the generated block counts, we use
91  * a target of 44 instead of the ideal value of 48.
92  */
93 static unsigned int gpu_min_ci_balanced_factor = 44;
94
95 /* Fw. decl. */
96 static void nbnxn_cuda_clear_e_fshift(NbnxmGpu* nb);
97
98 /*! Initializes the atomdata structure first time, it only gets filled at
99     pair-search. */
100 static void init_atomdata_first(cu_atomdata_t* ad, int ntypes, const DeviceContext& deviceContext)
101 {
102     ad->ntypes = ntypes;
103     allocateDeviceBuffer(&ad->shift_vec, SHIFTS, deviceContext);
104     ad->bShiftVecUploaded = false;
105
106     allocateDeviceBuffer(&ad->fshift, SHIFTS, deviceContext);
107     allocateDeviceBuffer(&ad->e_lj, 1, deviceContext);
108     allocateDeviceBuffer(&ad->e_el, 1, deviceContext);
109
110     /* initialize to nullptr poiters to data that is not allocated here and will
111        need reallocation in nbnxn_cuda_init_atomdata */
112     ad->xq = nullptr;
113     ad->f  = nullptr;
114
115     /* size -1 indicates that the respective array hasn't been initialized yet */
116     ad->natoms = -1;
117     ad->nalloc = -1;
118 }
119
120 /*! Initializes the nonbonded parameter data structure. */
121 static void init_nbparam(NBParamGpu*                     nbp,
122                          const interaction_const_t*      ic,
123                          const PairlistParams&           listParams,
124                          const nbnxn_atomdata_t::Params& nbatParams,
125                          const DeviceContext&            deviceContext)
126 {
127     const int ntypes = nbatParams.numTypes;
128
129     set_cutoff_parameters(nbp, ic, listParams);
130
131     /* The kernel code supports LJ combination rules (geometric and LB) for
132      * all kernel types, but we only generate useful combination rule kernels.
133      * We currently only use LJ combination rule (geometric and LB) kernels
134      * for plain cut-off LJ. On Maxwell the force only kernels speed up 15%
135      * with PME and 20% with RF, the other kernels speed up about half as much.
136      * For LJ force-switch the geometric rule would give 7% speed-up, but this
137      * combination is rarely used. LJ force-switch with LB rule is more common,
138      * but gives only 1% speed-up.
139      */
140     nbp->vdwType  = nbnxmGpuPickVdwKernelType(ic, nbatParams.comb_rule);
141     nbp->elecType = nbnxmGpuPickElectrostaticsKernelType(ic);
142
143     /* generate table for PME */
144     nbp->coulomb_tab = nullptr;
145     if (nbp->elecType == ElecType::EwaldTab || nbp->elecType == ElecType::EwaldTabTwin)
146     {
147         GMX_RELEASE_ASSERT(ic->coulombEwaldTables, "Need valid Coulomb Ewald correction tables");
148         init_ewald_coulomb_force_table(*ic->coulombEwaldTables, nbp, deviceContext);
149     }
150
151     /* set up LJ parameter lookup table */
152     if (!useLjCombRule(nbp->vdwType))
153     {
154         initParamLookupTable(
155                 &nbp->nbfp, &nbp->nbfp_texobj, nbatParams.nbfp.data(), 2 * ntypes * ntypes, deviceContext);
156     }
157
158     /* set up LJ-PME parameter lookup table */
159     if (ic->vdwtype == evdwPME)
160     {
161         initParamLookupTable(
162                 &nbp->nbfp_comb, &nbp->nbfp_comb_texobj, nbatParams.nbfp_comb.data(), 2 * ntypes, deviceContext);
163     }
164 }
165
166 /*! Initializes simulation constant data. */
167 static void cuda_init_const(NbnxmGpu*                       nb,
168                             const interaction_const_t*      ic,
169                             const PairlistParams&           listParams,
170                             const nbnxn_atomdata_t::Params& nbatParams)
171 {
172     init_atomdata_first(nb->atdat, nbatParams.numTypes, *nb->deviceContext_);
173     init_nbparam(nb->nbparam, ic, listParams, nbatParams, *nb->deviceContext_);
174
175     /* clear energy and shift force outputs */
176     nbnxn_cuda_clear_e_fshift(nb);
177 }
178
179 NbnxmGpu* gpu_init(const gmx::DeviceStreamManager& deviceStreamManager,
180                    const interaction_const_t*      ic,
181                    const PairlistParams&           listParams,
182                    const nbnxn_atomdata_t*         nbat,
183                    bool                            bLocalAndNonlocal)
184 {
185     cudaError_t stat;
186
187     auto nb            = new NbnxmGpu();
188     nb->deviceContext_ = &deviceStreamManager.context();
189     snew(nb->atdat, 1);
190     snew(nb->nbparam, 1);
191     snew(nb->plist[InteractionLocality::Local], 1);
192     if (bLocalAndNonlocal)
193     {
194         snew(nb->plist[InteractionLocality::NonLocal], 1);
195     }
196
197     nb->bUseTwoStreams = bLocalAndNonlocal;
198
199     nb->timers = new cu_timers_t();
200     snew(nb->timings, 1);
201
202     /* init nbst */
203     pmalloc((void**)&nb->nbst.e_lj, sizeof(*nb->nbst.e_lj));
204     pmalloc((void**)&nb->nbst.e_el, sizeof(*nb->nbst.e_el));
205     pmalloc((void**)&nb->nbst.fshift, SHIFTS * sizeof(*nb->nbst.fshift));
206
207     init_plist(nb->plist[InteractionLocality::Local]);
208
209     /* local/non-local GPU streams */
210     GMX_RELEASE_ASSERT(deviceStreamManager.streamIsValid(gmx::DeviceStreamType::NonBondedLocal),
211                        "Local non-bonded stream should be initialized to use GPU for non-bonded.");
212     nb->deviceStreams[InteractionLocality::Local] =
213             &deviceStreamManager.stream(gmx::DeviceStreamType::NonBondedLocal);
214     if (nb->bUseTwoStreams)
215     {
216         init_plist(nb->plist[InteractionLocality::NonLocal]);
217
218         /* Note that the device we're running on does not have to support
219          * priorities, because we are querying the priority range which in this
220          * case will be a single value.
221          */
222         GMX_RELEASE_ASSERT(deviceStreamManager.streamIsValid(gmx::DeviceStreamType::NonBondedNonLocal),
223                            "Non-local non-bonded stream should be initialized to use GPU for "
224                            "non-bonded with domain decomposition.");
225         nb->deviceStreams[InteractionLocality::NonLocal] =
226                 &deviceStreamManager.stream(gmx::DeviceStreamType::NonBondedNonLocal);
227         ;
228     }
229
230     /* init events for sychronization (timing disabled for performance reasons!) */
231     stat = cudaEventCreateWithFlags(&nb->nonlocal_done, cudaEventDisableTiming);
232     CU_RET_ERR(stat, "cudaEventCreate on nonlocal_done failed");
233     stat = cudaEventCreateWithFlags(&nb->misc_ops_and_local_H2D_done, cudaEventDisableTiming);
234     CU_RET_ERR(stat, "cudaEventCreate on misc_ops_and_local_H2D_done failed");
235
236     nb->xNonLocalCopyD2HDone = new GpuEventSynchronizer();
237
238     /* WARNING: CUDA timings are incorrect with multiple streams.
239      *          This is the main reason why they are disabled by default.
240      */
241     // TODO: Consider turning on by default when we can detect nr of streams.
242     nb->bDoTime = (getenv("GMX_ENABLE_GPU_TIMING") != nullptr);
243
244     if (nb->bDoTime)
245     {
246         init_timings(nb->timings);
247     }
248
249     /* set the kernel type for the current GPU */
250     /* pick L1 cache configuration */
251     cuda_set_cacheconfig();
252
253     cuda_init_const(nb, ic, listParams, nbat->params());
254
255     nb->atomIndicesSize       = 0;
256     nb->atomIndicesSize_alloc = 0;
257     nb->ncxy_na               = 0;
258     nb->ncxy_na_alloc         = 0;
259     nb->ncxy_ind              = 0;
260     nb->ncxy_ind_alloc        = 0;
261
262     if (debug)
263     {
264         fprintf(debug, "Initialized CUDA data structures.\n");
265     }
266
267     return nb;
268 }
269
270 void gpu_upload_shiftvec(NbnxmGpu* nb, const nbnxn_atomdata_t* nbatom)
271 {
272     cu_atomdata_t*      adat        = nb->atdat;
273     const DeviceStream& localStream = *nb->deviceStreams[InteractionLocality::Local];
274
275     /* only if we have a dynamic box */
276     if (nbatom->bDynamicBox || !adat->bShiftVecUploaded)
277     {
278         static_assert(sizeof(adat->shift_vec[0]) == sizeof(nbatom->shift_vec[0]),
279                       "Sizes of host- and device-side shift vectors should be the same.");
280         copyToDeviceBuffer(&adat->shift_vec,
281                            reinterpret_cast<const float3*>(nbatom->shift_vec.data()),
282                            0,
283                            SHIFTS,
284                            localStream,
285                            GpuApiCallBehavior::Async,
286                            nullptr);
287         adat->bShiftVecUploaded = true;
288     }
289 }
290
291 /*! Clears the first natoms_clear elements of the GPU nonbonded force output array. */
292 static void nbnxn_cuda_clear_f(NbnxmGpu* nb, int natoms_clear)
293 {
294     cu_atomdata_t*      adat        = nb->atdat;
295     const DeviceStream& localStream = *nb->deviceStreams[InteractionLocality::Local];
296     clearDeviceBufferAsync(&adat->f, 0, natoms_clear, localStream);
297 }
298
299 /*! Clears nonbonded shift force output array and energy outputs on the GPU. */
300 static void nbnxn_cuda_clear_e_fshift(NbnxmGpu* nb)
301 {
302     cu_atomdata_t*      adat        = nb->atdat;
303     const DeviceStream& localStream = *nb->deviceStreams[InteractionLocality::Local];
304
305     clearDeviceBufferAsync(&adat->fshift, 0, SHIFTS, localStream);
306     clearDeviceBufferAsync(&adat->e_lj, 0, 1, localStream);
307     clearDeviceBufferAsync(&adat->e_el, 0, 1, localStream);
308 }
309
310 void gpu_clear_outputs(NbnxmGpu* nb, bool computeVirial)
311 {
312     nbnxn_cuda_clear_f(nb, nb->atdat->natoms);
313     /* clear shift force array and energies if the outputs were
314        used in the current step */
315     if (computeVirial)
316     {
317         nbnxn_cuda_clear_e_fshift(nb);
318     }
319 }
320
321 void gpu_init_atomdata(NbnxmGpu* nb, const nbnxn_atomdata_t* nbat)
322 {
323     int                  nalloc, natoms;
324     bool                 realloced;
325     bool                 bDoTime       = nb->bDoTime;
326     cu_timers_t*         timers        = nb->timers;
327     cu_atomdata_t*       d_atdat       = nb->atdat;
328     const DeviceContext& deviceContext = *nb->deviceContext_;
329     const DeviceStream&  localStream   = *nb->deviceStreams[InteractionLocality::Local];
330
331     natoms    = nbat->numAtoms();
332     realloced = false;
333
334     if (bDoTime)
335     {
336         /* time async copy */
337         timers->atdat.openTimingRegion(localStream);
338     }
339
340     /* need to reallocate if we have to copy more atoms than the amount of space
341        available and only allocate if we haven't initialized yet, i.e d_atdat->natoms == -1 */
342     if (natoms > d_atdat->nalloc)
343     {
344         nalloc = over_alloc_small(natoms);
345
346         /* free up first if the arrays have already been initialized */
347         if (d_atdat->nalloc != -1)
348         {
349             freeDeviceBuffer(&d_atdat->f);
350             freeDeviceBuffer(&d_atdat->xq);
351             freeDeviceBuffer(&d_atdat->atom_types);
352             freeDeviceBuffer(&d_atdat->lj_comb);
353         }
354
355         allocateDeviceBuffer(&d_atdat->f, nalloc, deviceContext);
356         allocateDeviceBuffer(&d_atdat->xq, nalloc, deviceContext);
357         if (useLjCombRule(nb->nbparam->vdwType))
358         {
359             allocateDeviceBuffer(&d_atdat->lj_comb, nalloc, deviceContext);
360         }
361         else
362         {
363             allocateDeviceBuffer(&d_atdat->atom_types, nalloc, deviceContext);
364         }
365
366         d_atdat->nalloc = nalloc;
367         realloced       = true;
368     }
369
370     d_atdat->natoms       = natoms;
371     d_atdat->natoms_local = nbat->natoms_local;
372
373     /* need to clear GPU f output if realloc happened */
374     if (realloced)
375     {
376         nbnxn_cuda_clear_f(nb, nalloc);
377     }
378
379     if (useLjCombRule(nb->nbparam->vdwType))
380     {
381         static_assert(sizeof(d_atdat->lj_comb[0]) == sizeof(float2),
382                       "Size of the LJ parameters element should be equal to the size of float2.");
383         copyToDeviceBuffer(&d_atdat->lj_comb,
384                            reinterpret_cast<const float2*>(nbat->params().lj_comb.data()),
385                            0,
386                            natoms,
387                            localStream,
388                            GpuApiCallBehavior::Async,
389                            nullptr);
390     }
391     else
392     {
393         static_assert(sizeof(d_atdat->atom_types[0]) == sizeof(nbat->params().type[0]),
394                       "Sizes of host- and device-side atom types should be the same.");
395         copyToDeviceBuffer(&d_atdat->atom_types,
396                            nbat->params().type.data(),
397                            0,
398                            natoms,
399                            localStream,
400                            GpuApiCallBehavior::Async,
401                            nullptr);
402     }
403
404     if (bDoTime)
405     {
406         timers->atdat.closeTimingRegion(localStream);
407     }
408 }
409
410 void gpu_free(NbnxmGpu* nb)
411 {
412     cudaError_t    stat;
413     cu_atomdata_t* atdat;
414     NBParamGpu*    nbparam;
415
416     if (nb == nullptr)
417     {
418         return;
419     }
420
421     atdat   = nb->atdat;
422     nbparam = nb->nbparam;
423
424     if ((!nbparam->coulomb_tab)
425         && (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin))
426     {
427         destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj);
428     }
429
430     stat = cudaEventDestroy(nb->nonlocal_done);
431     CU_RET_ERR(stat, "cudaEventDestroy failed on timers->nonlocal_done");
432     stat = cudaEventDestroy(nb->misc_ops_and_local_H2D_done);
433     CU_RET_ERR(stat, "cudaEventDestroy failed on timers->misc_ops_and_local_H2D_done");
434
435     delete nb->timers;
436
437     if (!useLjCombRule(nb->nbparam->vdwType))
438     {
439         destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj);
440     }
441
442     if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB)
443     {
444         destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj);
445     }
446
447     freeDeviceBuffer(&atdat->shift_vec);
448     freeDeviceBuffer(&atdat->fshift);
449
450     freeDeviceBuffer(&atdat->e_lj);
451     freeDeviceBuffer(&atdat->e_el);
452
453     freeDeviceBuffer(&atdat->f);
454     freeDeviceBuffer(&atdat->xq);
455     freeDeviceBuffer(&atdat->atom_types);
456     freeDeviceBuffer(&atdat->lj_comb);
457
458     /* Free plist */
459     auto* plist = nb->plist[InteractionLocality::Local];
460     freeDeviceBuffer(&plist->sci);
461     freeDeviceBuffer(&plist->cj4);
462     freeDeviceBuffer(&plist->imask);
463     freeDeviceBuffer(&plist->excl);
464     sfree(plist);
465     if (nb->bUseTwoStreams)
466     {
467         auto* plist_nl = nb->plist[InteractionLocality::NonLocal];
468         freeDeviceBuffer(&plist_nl->sci);
469         freeDeviceBuffer(&plist_nl->cj4);
470         freeDeviceBuffer(&plist_nl->imask);
471         freeDeviceBuffer(&plist_nl->excl);
472         sfree(plist_nl);
473     }
474
475     /* Free nbst */
476     pfree(nb->nbst.e_lj);
477     nb->nbst.e_lj = nullptr;
478
479     pfree(nb->nbst.e_el);
480     nb->nbst.e_el = nullptr;
481
482     pfree(nb->nbst.fshift);
483     nb->nbst.fshift = nullptr;
484
485     sfree(atdat);
486     sfree(nbparam);
487     sfree(nb->timings);
488     delete nb;
489
490     if (debug)
491     {
492         fprintf(debug, "Cleaned up CUDA data structures.\n");
493     }
494 }
495
496 int gpu_min_ci_balanced(NbnxmGpu* nb)
497 {
498     return nb != nullptr ? gpu_min_ci_balanced_factor * nb->deviceContext_->deviceInfo().prop.multiProcessorCount
499                          : 0;
500 }
501
502 void* gpu_get_xq(NbnxmGpu* nb)
503 {
504     assert(nb);
505
506     return static_cast<void*>(nb->atdat->xq);
507 }
508
509 DeviceBuffer<gmx::RVec> gpu_get_f(NbnxmGpu* nb)
510 {
511     assert(nb);
512
513     return reinterpret_cast<DeviceBuffer<gmx::RVec>>(nb->atdat->f);
514 }
515
516 DeviceBuffer<gmx::RVec> gpu_get_fshift(NbnxmGpu* nb)
517 {
518     assert(nb);
519
520     return reinterpret_cast<DeviceBuffer<gmx::RVec>>(nb->atdat->fshift);
521 }
522
523 /* Initialization for X buffer operations on GPU. */
524 /* TODO  Remove explicit pinning from host arrays from here and manage in a more natural way*/
525 void nbnxn_gpu_init_x_to_nbat_x(const Nbnxm::GridSet& gridSet, NbnxmGpu* gpu_nbv)
526 {
527     const DeviceStream& deviceStream  = *gpu_nbv->deviceStreams[InteractionLocality::Local];
528     bool                bDoTime       = gpu_nbv->bDoTime;
529     const int           maxNumColumns = gridSet.numColumnsMax();
530
531     reallocateDeviceBuffer(&gpu_nbv->cxy_na,
532                            maxNumColumns * gridSet.grids().size(),
533                            &gpu_nbv->ncxy_na,
534                            &gpu_nbv->ncxy_na_alloc,
535                            *gpu_nbv->deviceContext_);
536     reallocateDeviceBuffer(&gpu_nbv->cxy_ind,
537                            maxNumColumns * gridSet.grids().size(),
538                            &gpu_nbv->ncxy_ind,
539                            &gpu_nbv->ncxy_ind_alloc,
540                            *gpu_nbv->deviceContext_);
541
542     for (unsigned int g = 0; g < gridSet.grids().size(); g++)
543     {
544
545         const Nbnxm::Grid& grid = gridSet.grids()[g];
546
547         const int  numColumns      = grid.numColumns();
548         const int* atomIndices     = gridSet.atomIndices().data();
549         const int  atomIndicesSize = gridSet.atomIndices().size();
550         const int* cxy_na          = grid.cxy_na().data();
551         const int* cxy_ind         = grid.cxy_ind().data();
552
553         reallocateDeviceBuffer(&gpu_nbv->atomIndices,
554                                atomIndicesSize,
555                                &gpu_nbv->atomIndicesSize,
556                                &gpu_nbv->atomIndicesSize_alloc,
557                                *gpu_nbv->deviceContext_);
558
559         if (atomIndicesSize > 0)
560         {
561
562             if (bDoTime)
563             {
564                 gpu_nbv->timers->xf[AtomLocality::Local].nb_h2d.openTimingRegion(deviceStream);
565             }
566
567             copyToDeviceBuffer(&gpu_nbv->atomIndices,
568                                atomIndices,
569                                0,
570                                atomIndicesSize,
571                                deviceStream,
572                                GpuApiCallBehavior::Async,
573                                nullptr);
574
575             if (bDoTime)
576             {
577                 gpu_nbv->timers->xf[AtomLocality::Local].nb_h2d.closeTimingRegion(deviceStream);
578             }
579         }
580
581         if (numColumns > 0)
582         {
583             if (bDoTime)
584             {
585                 gpu_nbv->timers->xf[AtomLocality::Local].nb_h2d.openTimingRegion(deviceStream);
586             }
587
588             int* destPtr = &gpu_nbv->cxy_na[maxNumColumns * g];
589             copyToDeviceBuffer(
590                     &destPtr, cxy_na, 0, numColumns, deviceStream, GpuApiCallBehavior::Async, nullptr);
591
592             if (bDoTime)
593             {
594                 gpu_nbv->timers->xf[AtomLocality::Local].nb_h2d.closeTimingRegion(deviceStream);
595             }
596
597             if (bDoTime)
598             {
599                 gpu_nbv->timers->xf[AtomLocality::Local].nb_h2d.openTimingRegion(deviceStream);
600             }
601
602             destPtr = &gpu_nbv->cxy_ind[maxNumColumns * g];
603             copyToDeviceBuffer(
604                     &destPtr, cxy_ind, 0, numColumns, deviceStream, GpuApiCallBehavior::Async, nullptr);
605
606             if (bDoTime)
607             {
608                 gpu_nbv->timers->xf[AtomLocality::Local].nb_h2d.closeTimingRegion(deviceStream);
609             }
610         }
611     }
612
613     // The above data is transferred on the local stream but is a
614     // dependency of the nonlocal stream (specifically the nonlocal X
615     // buf ops kernel).  We therefore set a dependency to ensure
616     // that the nonlocal stream waits on the local stream here.
617     // This call records an event in the local stream:
618     nbnxnInsertNonlocalGpuDependency(gpu_nbv, Nbnxm::InteractionLocality::Local);
619     // ...and this call instructs the nonlocal stream to wait on that event:
620     nbnxnInsertNonlocalGpuDependency(gpu_nbv, Nbnxm::InteractionLocality::NonLocal);
621
622     return;
623 }
624
625 } // namespace Nbnxm