1edb0b8797ee2271298a68863e1faec3313212fc
[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,2021, 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/gpu_utils.h"
56 #include "gromacs/gpu_utils/gpueventsynchronizer.cuh"
57 #include "gromacs/gpu_utils/pmalloc.h"
58 #include "gromacs/hardware/device_information.h"
59 #include "gromacs/hardware/device_management.h"
60 #include "gromacs/math/vectypes.h"
61 #include "gromacs/mdlib/force_flags.h"
62 #include "gromacs/mdtypes/interaction_const.h"
63 #include "gromacs/mdtypes/md_enums.h"
64 #include "gromacs/nbnxm/atomdata.h"
65 #include "gromacs/nbnxm/gpu_data_mgmt.h"
66 #include "gromacs/nbnxm/gridset.h"
67 #include "gromacs/nbnxm/nbnxm.h"
68 #include "gromacs/nbnxm/nbnxm_gpu.h"
69 #include "gromacs/nbnxm/nbnxm_gpu_data_mgmt.h"
70 #include "gromacs/nbnxm/pairlistsets.h"
71 #include "gromacs/pbcutil/ishift.h"
72 #include "gromacs/timing/gpu_timing.h"
73 #include "gromacs/utility/basedefinitions.h"
74 #include "gromacs/utility/cstringutil.h"
75 #include "gromacs/utility/fatalerror.h"
76 #include "gromacs/utility/real.h"
77 #include "gromacs/utility/smalloc.h"
78
79 #include "nbnxm_cuda.h"
80
81 namespace Nbnxm
82 {
83
84 /* This is a heuristically determined parameter for the Kepler
85  * and Maxwell architectures for the minimum size of ci lists by multiplying
86  * this constant with the # of multiprocessors on the current device.
87  * Since the maximum number of blocks per multiprocessor is 16, the ideal
88  * count for small systems is 32 or 48 blocks per multiprocessor. Because
89  * there is a bit of fluctuations in the generated block counts, we use
90  * a target of 44 instead of the ideal value of 48.
91  */
92 static unsigned int gpu_min_ci_balanced_factor = 44;
93
94 void gpu_init_platform_specific(NbnxmGpu* /* nb */)
95 {
96     /* set the kernel type for the current GPU */
97     /* pick L1 cache configuration */
98     cuda_set_cacheconfig();
99 }
100
101 void gpu_free(NbnxmGpu* nb)
102 {
103     if (nb == nullptr)
104     {
105         return;
106     }
107
108     delete nb->timers;
109     sfree(nb->timings);
110
111     NBAtomData* atdat   = nb->atdat;
112     NBParamGpu* nbparam = nb->nbparam;
113
114     if (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin)
115     {
116         destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj);
117     }
118
119     if (!useLjCombRule(nb->nbparam->vdwType))
120     {
121         destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj);
122     }
123
124     if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB)
125     {
126         destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj);
127     }
128
129     freeDeviceBuffer(&atdat->shiftVec);
130     freeDeviceBuffer(&atdat->fShift);
131
132     freeDeviceBuffer(&atdat->eLJ);
133     freeDeviceBuffer(&atdat->eElec);
134
135     freeDeviceBuffer(&atdat->f);
136     freeDeviceBuffer(&atdat->xq);
137     if (useLjCombRule(nb->nbparam->vdwType))
138     {
139         freeDeviceBuffer(&atdat->ljComb);
140     }
141     else
142     {
143         freeDeviceBuffer(&atdat->atomTypes);
144     }
145
146     /* Free plist */
147     auto* plist = nb->plist[InteractionLocality::Local];
148     freeDeviceBuffer(&plist->sci);
149     freeDeviceBuffer(&plist->cj4);
150     freeDeviceBuffer(&plist->imask);
151     freeDeviceBuffer(&plist->excl);
152     delete plist;
153     if (nb->bUseTwoStreams)
154     {
155         auto* plist_nl = nb->plist[InteractionLocality::NonLocal];
156         freeDeviceBuffer(&plist_nl->sci);
157         freeDeviceBuffer(&plist_nl->cj4);
158         freeDeviceBuffer(&plist_nl->imask);
159         freeDeviceBuffer(&plist_nl->excl);
160         delete plist_nl;
161     }
162
163     /* Free nbst */
164     pfree(nb->nbst.eLJ);
165     nb->nbst.eLJ = nullptr;
166
167     pfree(nb->nbst.eElec);
168     nb->nbst.eElec = nullptr;
169
170     pfree(nb->nbst.fShift);
171     nb->nbst.fShift = nullptr;
172
173     delete atdat;
174     delete nbparam;
175     delete nb;
176
177     if (debug)
178     {
179         fprintf(debug, "Cleaned up CUDA data structures.\n");
180     }
181 }
182
183 int gpu_min_ci_balanced(NbnxmGpu* nb)
184 {
185     return nb != nullptr ? gpu_min_ci_balanced_factor * nb->deviceContext_->deviceInfo().prop.multiProcessorCount
186                          : 0;
187 }
188
189 void* gpu_get_xq(NbnxmGpu* nb)
190 {
191     assert(nb);
192
193     return static_cast<void*>(nb->atdat->xq);
194 }
195
196 DeviceBuffer<gmx::RVec> gpu_get_f(NbnxmGpu* nb)
197 {
198     assert(nb);
199
200     return reinterpret_cast<DeviceBuffer<gmx::RVec>>(nb->atdat->f);
201 }
202
203 DeviceBuffer<gmx::RVec> gpu_get_fshift(NbnxmGpu* nb)
204 {
205     assert(nb);
206
207     return reinterpret_cast<DeviceBuffer<gmx::RVec>>(nb->atdat->fShift);
208 }
209
210 } // namespace Nbnxm