Unify init_gpu function in NBNXM
[alexxy/gromacs.git] / src / gromacs / nbnxm / sycl / nbnxm_sycl_data_mgmt.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 /*! \internal \file
37  *  \brief
38  *  Stubs of functions that must be defined by nbnxm sycl implementation.
39  *
40  *  \ingroup module_nbnxm
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/gpu_utils/pmalloc.h"
45 #include "gromacs/hardware/device_information.h"
46 #include "gromacs/mdtypes/interaction_const.h"
47 #include "gromacs/nbnxm/atomdata.h"
48 #include "gromacs/nbnxm/gpu_data_mgmt.h"
49 #include "gromacs/nbnxm/nbnxm_gpu.h"
50 #include "gromacs/nbnxm/nbnxm_gpu_data_mgmt.h"
51 #include "gromacs/pbcutil/ishift.h"
52 #include "gromacs/utility/exceptions.h"
53
54 #include "nbnxm_sycl_types.h"
55
56 namespace Nbnxm
57 {
58
59 void gpu_init_platform_specific(NbnxmGpu* /* nb */)
60 {
61     // Nothing specific in SYCL
62 }
63
64 void gpu_upload_shiftvec(NbnxmGpu* nb, const nbnxn_atomdata_t* nbatom)
65 {
66     NBAtomData*         adat        = nb->atdat;
67     const DeviceStream& localStream = *nb->deviceStreams[InteractionLocality::Local];
68
69     /* only if we have a dynamic box */
70     if (nbatom->bDynamicBox || !adat->shiftVecUploaded)
71     {
72         GMX_ASSERT(adat->shiftVec.elementSize() == sizeof(nbatom->shift_vec[0]),
73                    "Sizes of host- and device-side shift vectors should be the same.");
74         copyToDeviceBuffer(&adat->shiftVec,
75                            reinterpret_cast<const Float3*>(nbatom->shift_vec.data()),
76                            0,
77                            SHIFTS,
78                            localStream,
79                            GpuApiCallBehavior::Async,
80                            nullptr);
81         adat->shiftVecUploaded = true;
82     }
83 }
84
85 void gpu_free(NbnxmGpu* nb)
86 {
87     if (nb == nullptr)
88     {
89         return;
90     }
91
92     delete nb->timers;
93     sfree(nb->timings);
94
95     NBAtomData* atdat   = nb->atdat;
96     NBParamGpu* nbparam = nb->nbparam;
97
98     if (nbparam->elecType == ElecType::EwaldTab || nbparam->elecType == ElecType::EwaldTabTwin)
99     {
100         destroyParamLookupTable(&nbparam->coulomb_tab, nbparam->coulomb_tab_texobj);
101     }
102
103     if (!useLjCombRule(nb->nbparam->vdwType))
104     {
105         destroyParamLookupTable(&nbparam->nbfp, nbparam->nbfp_texobj);
106     }
107
108     if (nbparam->vdwType == VdwType::EwaldGeom || nbparam->vdwType == VdwType::EwaldLB)
109     {
110         destroyParamLookupTable(&nbparam->nbfp_comb, nbparam->nbfp_comb_texobj);
111     }
112
113     /* Free plist */
114     auto* plist = nb->plist[InteractionLocality::Local];
115     delete plist;
116     if (nb->bUseTwoStreams)
117     {
118         auto* plist_nl = nb->plist[InteractionLocality::NonLocal];
119         delete plist_nl;
120     }
121
122     /* Free nbst */
123     pfree(nb->nbst.eLJ);
124     nb->nbst.eLJ = nullptr;
125
126     pfree(nb->nbst.eElec);
127     nb->nbst.eElec = nullptr;
128
129     pfree(nb->nbst.fShift);
130     nb->nbst.fShift = nullptr;
131
132     delete atdat;
133     delete nbparam;
134     delete nb;
135 }
136
137 int gpu_min_ci_balanced(NbnxmGpu* nb)
138 {
139     // SYCL-TODO: Logic and magic values taken from OpenCL
140     static constexpr unsigned int balancedFactor = 50;
141     if (nb == nullptr)
142     {
143         return 0;
144     }
145     const cl::sycl::device device = nb->deviceContext_->deviceInfo().syclDevice;
146     const int numComputeUnits     = device.get_info<cl::sycl::info::device::max_compute_units>();
147     return balancedFactor * numComputeUnits;
148 }
149
150 } // namespace Nbnxm