Use MessageStringCollector class to construct error messages
[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,2019,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 /*! \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 <algorithm>
49 #include <string>
50
51 #include "gromacs/listed_forces/gpubonded.h"
52 #include "gromacs/mdtypes/inputrec.h"
53 #include "gromacs/topology/topology.h"
54 #include "gromacs/utility/message_string_collector.h"
55
56 namespace gmx
57 {
58
59 //! Returns whether there are any interactions in ilists suitable for a GPU.
60 static bool someInteractionsCanRunOnGpu(const InteractionLists& ilists)
61 {
62     // Perturbation is not implemented in the GPU bonded
63     // kernels. If all the interactions were actually
64     // perturbed, then that will be detected later on each
65     // domain, and work will never run on the GPU. This is
66     // very unlikely to occur, and has little run-time cost,
67     // so we don't complicate the code by catering for it
68     // here.
69     return std::any_of(fTypesOnGpu.begin(), fTypesOnGpu.end(), [ilists](int fType) {
70         return !ilists[fType].iatoms.empty();
71     });
72 }
73
74 //! Returns whether there are any bonded interactions in the global topology suitable for a GPU.
75 static bool bondedInteractionsCanRunOnGpu(const gmx_mtop_t& mtop)
76 {
77     // Check the regular molecule types
78     for (const auto& moltype : mtop.moltype)
79     {
80         if (someInteractionsCanRunOnGpu(moltype.ilist))
81         {
82             return true;
83         }
84     }
85     // Check the inter-molecular interactions.
86     if (mtop.intermolecular_ilist)
87     {
88         if (someInteractionsCanRunOnGpu(*mtop.intermolecular_ilist))
89         {
90             return true;
91         }
92     }
93     return false;
94 }
95
96 bool buildSupportsGpuBondeds(std::string* error)
97 {
98     MessageStringCollector errorReasons;
99     // Before changing the prefix string, make sure that it is not searched for in regression tests.
100     errorReasons.startContext("Bonded interactions on GPU are not supported in:");
101     errorReasons.appendIf(GMX_DOUBLE, "Double precision build of GROMACS");
102     errorReasons.appendIf(GMX_GPU_OPENCL, "OpenCL build of GROMACS");
103     errorReasons.appendIf(GMX_GPU_SYCL, "SYCL build of GROMACS");
104     errorReasons.appendIf(!GMX_GPU, "CPU-only build of GROMACS");
105     errorReasons.finishContext();
106     if (error != nullptr)
107     {
108         *error = errorReasons.toString();
109     }
110     return errorReasons.isEmpty();
111 }
112
113 bool inputSupportsGpuBondeds(const t_inputrec& ir, const gmx_mtop_t& mtop, std::string* error)
114 {
115     MessageStringCollector errorReasons;
116     // Before changing the prefix string, make sure that it is not searched for in regression tests.
117     errorReasons.startContext("Bonded interactions can not be computed on a GPU:");
118
119     errorReasons.appendIf(!bondedInteractionsCanRunOnGpu(mtop),
120                           "None of the bonded types are implemented on the GPU.");
121     errorReasons.appendIf(
122             !EI_DYNAMICS(ir.eI),
123             "Cannot compute bonded interactions on a GPU, because GPU implementation requires "
124             "a dynamical integrator (md, sd, etc).");
125     errorReasons.appendIf(EI_MIMIC(ir.eI), "MiMiC");
126     errorReasons.appendIf(ir.useMts, "Cannot run with multiple time stepping");
127     errorReasons.appendIf((ir.opts.ngener > 1), "Cannot run with multiple energy groups");
128     errorReasons.finishContext();
129     if (error != nullptr)
130     {
131         *error = errorReasons.toString();
132     }
133     return errorReasons.isEmpty();
134 }
135
136 #if !GMX_GPU_CUDA
137
138 class GpuBonded::Impl
139 {
140 };
141
142 GpuBonded::GpuBonded(const gmx_ffparams_t& /* ffparams */,
143                      const float /* electrostaticsScaleFactor */,
144                      const DeviceContext& /* deviceContext */,
145                      const DeviceStream& /* deviceStream */,
146                      gmx_wallcycle* /* wcycle */) :
147     impl_(nullptr)
148 {
149 }
150
151 GpuBonded::~GpuBonded() = default;
152
153 void GpuBonded::updateInteractionListsAndDeviceBuffers(ArrayRef<const int> /* nbnxnAtomOrder */,
154                                                        const InteractionDefinitions& /* idef */,
155                                                        void* /* xqDevice */,
156                                                        DeviceBuffer<RVec> /* forceDevice */,
157                                                        DeviceBuffer<RVec> /* fshiftDevice */)
158 {
159 }
160
161 void GpuBonded::setPbc(PbcType /* pbcType */, const matrix /* box */, bool /* canMoleculeSpanPbc */)
162 {
163 }
164
165 bool GpuBonded::haveInteractions() const
166 {
167     return !impl_;
168 }
169
170 void GpuBonded::launchKernel(const gmx::StepWorkload& /* stepWork */) {}
171
172 void GpuBonded::setPbcAndlaunchKernel(PbcType /* pbcType */,
173                                       const matrix /* box */,
174                                       bool /* canMoleculeSpanPbc */,
175                                       const gmx::StepWorkload& /* stepWork */)
176 {
177 }
178
179 void GpuBonded::launchEnergyTransfer() {}
180
181 void GpuBonded::waitAccumulateEnergyTerms(gmx_enerdata_t* /* enerd */) {}
182
183 void GpuBonded::clearEnergies() {}
184
185 #endif // !GMX_GPU_CUDA
186
187 } // namespace gmx