Make cl_nbparam into a struct
[alexxy/gromacs.git] / src / gromacs / nbnxm / nbnxm_gpu_data_mgmt.cpp
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 /*! \internal \file
37  *  \brief Define common implementation of nbnxm_gpu_data_mgmt.h
38  *
39  *  \author Anca Hamuraru <anca@streamcomputing.eu>
40  *  \author Dimitrios Karkoulis <dimitris.karkoulis@gmail.com>
41  *  \author Teemu Virolainen <teemu@streamcomputing.eu>
42  *  \author Szilárd Páll <pall.szilard@gmail.com>
43  *  \author Artem Zhmurov <zhmurov@gmail.com>
44  *
45  *  \ingroup module_nbnxm
46  */
47 #include "gmxpre.h"
48
49 #include "config.h"
50
51 #if GMX_GPU == GMX_GPU_CUDA
52 #    include "cuda/nbnxm_cuda_types.h"
53 #endif
54
55 #if GMX_GPU == GMX_GPU_OPENCL
56 #    include "opencl/nbnxm_ocl_types.h"
57 #endif
58
59 #include "nbnxm_gpu_data_mgmt.h"
60
61 #include "gromacs/timing/gpu_timing.h"
62
63 #include "nbnxm_gpu.h"
64
65 namespace Nbnxm
66 {
67
68 void init_ewald_coulomb_force_table(const EwaldCorrectionTables& tables,
69                                     NBParamGpu*                  nbp,
70                                     const DeviceContext&         deviceContext)
71 {
72     if (!nbp->coulomb_tab)
73     {
74         destroyParamLookupTable(&nbp->coulomb_tab, nbp->coulomb_tab_texobj);
75     }
76
77     nbp->coulomb_tab_scale = tables.scale;
78     initParamLookupTable(&nbp->coulomb_tab, &nbp->coulomb_tab_texobj, tables.tableF.data(),
79                          tables.tableF.size(), deviceContext);
80 }
81
82 int nbnxn_gpu_pick_ewald_kernel_type(const interaction_const_t& ic)
83 {
84     bool bTwinCut = (ic.rcoulomb != ic.rvdw);
85     bool bUseAnalyticalEwald, bForceAnalyticalEwald, bForceTabulatedEwald;
86     int  kernel_type;
87
88     /* Benchmarking/development environment variables to force the use of
89        analytical or tabulated Ewald kernel. */
90     bForceAnalyticalEwald = (getenv("GMX_GPU_NB_ANA_EWALD") != nullptr);
91     bForceTabulatedEwald  = (getenv("GMX_GPU_NB_TAB_EWALD") != nullptr);
92
93     if (bForceAnalyticalEwald && bForceTabulatedEwald)
94     {
95         gmx_incons(
96                 "Both analytical and tabulated Ewald GPU non-bonded kernels "
97                 "requested through environment variables.");
98     }
99
100     /* By default, use analytical Ewald
101      * TODO: tabulated does not work in OpenCL, it needs fixing, see init_nbparam() in nbnxn_ocl_data_mgmt.cpp
102      *
103      */
104     bUseAnalyticalEwald = true;
105     if (bForceAnalyticalEwald)
106     {
107         if (debug)
108         {
109             fprintf(debug, "Using analytical Ewald GPU kernels\n");
110         }
111     }
112     else if (bForceTabulatedEwald)
113     {
114         bUseAnalyticalEwald = false;
115
116         if (debug)
117         {
118             fprintf(debug, "Using tabulated Ewald GPU kernels\n");
119         }
120     }
121
122     /* Use twin cut-off kernels if requested by bTwinCut or the env. var.
123        forces it (use it for debugging/benchmarking only). */
124     if (!bTwinCut && (getenv("GMX_GPU_NB_EWALD_TWINCUT") == nullptr))
125     {
126         kernel_type = bUseAnalyticalEwald ? eelTypeEWALD_ANA : eelTypeEWALD_TAB;
127     }
128     else
129     {
130         kernel_type = bUseAnalyticalEwald ? eelTypeEWALD_ANA_TWIN : eelTypeEWALD_TAB_TWIN;
131     }
132
133     return kernel_type;
134 }
135
136 void set_cutoff_parameters(NBParamGpu* nbp, const interaction_const_t* ic, const PairlistParams& listParams)
137 {
138     nbp->ewald_beta        = ic->ewaldcoeff_q;
139     nbp->sh_ewald          = ic->sh_ewald;
140     nbp->epsfac            = ic->epsfac;
141     nbp->two_k_rf          = 2.0 * ic->k_rf;
142     nbp->c_rf              = ic->c_rf;
143     nbp->rvdw_sq           = ic->rvdw * ic->rvdw;
144     nbp->rcoulomb_sq       = ic->rcoulomb * ic->rcoulomb;
145     nbp->rlistOuter_sq     = listParams.rlistOuter * listParams.rlistOuter;
146     nbp->rlistInner_sq     = listParams.rlistInner * listParams.rlistInner;
147     nbp->useDynamicPruning = listParams.useDynamicPruning;
148
149     nbp->sh_lj_ewald   = ic->sh_lj_ewald;
150     nbp->ewaldcoeff_lj = ic->ewaldcoeff_lj;
151
152     nbp->rvdw_switch      = ic->rvdw_switch;
153     nbp->dispersion_shift = ic->dispersion_shift;
154     nbp->repulsion_shift  = ic->repulsion_shift;
155     nbp->vdw_switch       = ic->vdw_switch;
156 }
157
158 void init_plist(gpu_plist* pl)
159 {
160     /* initialize to nullptr pointers to data that is not allocated here and will
161        need reallocation in nbnxn_gpu_init_pairlist */
162     pl->sci   = nullptr;
163     pl->cj4   = nullptr;
164     pl->imask = nullptr;
165     pl->excl  = nullptr;
166
167     /* size -1 indicates that the respective array hasn't been initialized yet */
168     pl->na_c          = -1;
169     pl->nsci          = -1;
170     pl->sci_nalloc    = -1;
171     pl->ncj4          = -1;
172     pl->cj4_nalloc    = -1;
173     pl->nimask        = -1;
174     pl->imask_nalloc  = -1;
175     pl->nexcl         = -1;
176     pl->excl_nalloc   = -1;
177     pl->haveFreshList = false;
178 }
179
180 void init_timings(gmx_wallclock_gpu_nbnxn_t* t)
181 {
182     int i, j;
183
184     t->nb_h2d_t = 0.0;
185     t->nb_d2h_t = 0.0;
186     t->nb_c     = 0;
187     t->pl_h2d_t = 0.0;
188     t->pl_h2d_c = 0;
189     for (i = 0; i < 2; i++)
190     {
191         for (j = 0; j < 2; j++)
192         {
193             t->ktime[i][j].t = 0.0;
194             t->ktime[i][j].c = 0;
195         }
196     }
197     t->pruneTime.c        = 0;
198     t->pruneTime.t        = 0.0;
199     t->dynamicPruneTime.c = 0;
200     t->dynamicPruneTime.t = 0.0;
201 }
202
203 } // namespace Nbnxm