Unify gpu_free(...) function in OpenCL, CUDA and SYCL versions of NBNXM
[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_platform_specific(NbnxmGpu* /* nb */)
102 {
103     // Nothing specific in CUDA
104 }
105
106 int gpu_min_ci_balanced(NbnxmGpu* nb)
107 {
108     return nb != nullptr ? gpu_min_ci_balanced_factor * nb->deviceContext_->deviceInfo().prop.multiProcessorCount
109                          : 0;
110 }
111
112 void* gpu_get_xq(NbnxmGpu* nb)
113 {
114     assert(nb);
115
116     return static_cast<void*>(nb->atdat->xq);
117 }
118
119 DeviceBuffer<gmx::RVec> gpu_get_f(NbnxmGpu* nb)
120 {
121     assert(nb);
122
123     return reinterpret_cast<DeviceBuffer<gmx::RVec>>(nb->atdat->f);
124 }
125
126 DeviceBuffer<gmx::RVec> gpu_get_fshift(NbnxmGpu* nb)
127 {
128     assert(nb);
129
130     return reinterpret_cast<DeviceBuffer<gmx::RVec>>(nb->atdat->fShift);
131 }
132
133 } // namespace Nbnxm