998ad7a5c8e22a1257475e2d03045dc524de825b
[alexxy/gromacs.git] / src / gromacs / listed-forces / gpubonded-impl.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018, 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 /*! \internal \file
36  *
37  * \brief Implements GPU bonded lists for non-GPU builds
38  *
39  * \author Mark Abraham <mark.j.abraham@gmail.com>
40  *
41  * \ingroup module_listed-forces
42  */
43
44 #include "gmxpre.h"
45
46 #include "config.h"
47
48 #include <string>
49
50 #include "gromacs/listed-forces/gpubonded.h"
51 #include "gromacs/mdtypes/inputrec.h"
52 #include "gromacs/topology/topology.h"
53 #include "gromacs/utility/stringutil.h"
54
55 namespace gmx
56 {
57
58 //! Returns whether there are any interactions in ilists suitable for a GPU.
59 static bool someInteractionsCanRunOnGpu(const InteractionLists &ilists)
60 {
61     for (int ftype : ftypesOnGpu)
62     {
63         if (!ilists[ftype].iatoms.empty())
64         {
65             // Perturbation is not implemented in the GPU bonded
66             // kernels. If all the interactions were actually
67             // perturbed, then that will be detected later on each
68             // domain, and work will never run on the GPU. This is
69             // very unlikely to occur, and has little run-time cost,
70             // so we don't complicate the code by catering for it
71             // here.
72             return true;
73         }
74     }
75     return false;
76 }
77
78 //! Returns whether there are any bonded interactions in the global topology suitable for a GPU.
79 static bool bondedInteractionsCanRunOnGpu(const gmx_mtop_t &mtop)
80 {
81     // Check the regular molecule types
82     for (const auto &moltype : mtop.moltype)
83     {
84         if (someInteractionsCanRunOnGpu(moltype.ilist))
85         {
86             return true;
87         }
88     }
89     // Check the inter-molecular interactions.
90     if (mtop.intermolecular_ilist)
91     {
92         if (someInteractionsCanRunOnGpu(*mtop.intermolecular_ilist))
93         {
94             return true;
95         }
96     }
97     return false;
98 }
99
100 /*! \brief Help build a descriptive message in \c error if there are
101  * \c errorReasons why bondeds on a GPU are not supported.
102  *
103  * \returns Whether the lack of errorReasons indicate there is support. */
104 static bool
105 addMessageIfNotSupported(ArrayRef <const std::string> errorReasons,
106                          std::string                 *error)
107 {
108     bool isSupported = errorReasons.empty();
109     if (!isSupported && error)
110     {
111         *error  = "Bonded interactions cannot run on GPUs: ";
112         *error += joinStrings(errorReasons, "; ") + ".";
113     }
114     return isSupported;
115 }
116
117 bool buildSupportsGpuBondeds(std::string *error)
118 {
119     std::vector<std::string> errorReasons;
120
121     if (GMX_DOUBLE)
122     {
123         errorReasons.emplace_back("not supported with double precision");
124     }
125     if (GMX_GPU == GMX_GPU_OPENCL)
126     {
127         errorReasons.emplace_back("not supported with OpenCL build of GROMACS");
128     }
129     else if (GMX_GPU == GMX_GPU_NONE)
130     {
131         errorReasons.emplace_back("not supported with CPU-only build of GROMACS");
132     }
133     return addMessageIfNotSupported(errorReasons, error);
134 }
135
136 bool inputSupportsGpuBondeds(const t_inputrec &ir,
137                              const gmx_mtop_t &mtop,
138                              std::string      *error)
139 {
140     std::vector<std::string> errorReasons;
141
142     if (!bondedInteractionsCanRunOnGpu(mtop))
143     {
144         errorReasons.emplace_back("No supported bonded interactions are present");
145     }
146     if (ir.cutoff_scheme == ecutsGROUP)
147     {
148         errorReasons.emplace_back("group cutoff scheme");
149     }
150     if (!EI_DYNAMICS(ir.eI))
151     {
152         errorReasons.emplace_back("not a dynamical integrator");
153     }
154     if (EI_MIMIC(ir.eI))
155     {
156         errorReasons.emplace_back("MiMiC");
157     }
158     if (ir.opts.ngener > 1)
159     {
160         errorReasons.emplace_back("Cannot run with multiple energy groups");
161     }
162     return addMessageIfNotSupported(errorReasons, error);
163 }
164
165 #if GMX_GPU != GMX_GPU_CUDA
166
167 class GpuBonded::Impl
168 {
169 };
170
171 GpuBonded::GpuBonded(const gmx_ffparams_t & /* ffparams */,
172                      void                 * /*streamPtr */)
173     : impl_(nullptr)
174 {
175 }
176
177 GpuBonded::~GpuBonded() = default;
178
179 void
180 GpuBonded::updateInteractionListsAndDeviceBuffers(ArrayRef<const int>   /* nbnxnAtomOrder */,
181                                                   const t_idef        & /* idef */,
182                                                   void                * /* xqDevice */,
183                                                   void                * /* forceDevice */,
184                                                   void                * /* fshiftDevice */)
185 {
186 }
187
188 bool
189 GpuBonded::haveInteractions() const
190 {
191     return false;
192 }
193
194 void
195 GpuBonded::launchKernels(const t_forcerec * /* fr */,
196                          int            /* forceFlags */,
197                          const matrix   /* box */)
198 {
199 }
200
201 void
202 GpuBonded::launchEnergyTransfer()
203 {
204 }
205
206 void
207 GpuBonded::accumulateEnergyTerms(gmx_enerdata_t * /* enerd */)
208 {
209 }
210
211 void
212 GpuBonded::clearEnergies()
213 {
214 }
215
216 #endif /* GMX_GPU != GMX_GPU_CUDA */
217
218 }      // namespace gmx