04a900075c438e8d4314bba4bda36fa045190bc4
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / constrtestrunners.h
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  * \brief SHAKE and LINCS tests runners.
37  *
38  * Declares test runner class for constraints. The test runner abstract class is used
39  * to unify the interfaces for different constraints methods, running on different
40  * hardware.  This allows to run the same test on the same data using different
41  * implementations of the parent class, that inherit its interfaces.
42  *
43  * \author Artem Zhmurov <zhmurov@gmail.com>
44  * \ingroup module_mdlib
45  */
46
47 #ifndef GMX_MDLIB_TESTS_CONSTRTESTRUNNERS_H
48 #define GMX_MDLIB_TESTS_CONSTRTESTRUNNERS_H
49
50 #include <gtest/gtest.h>
51
52 #include "testutils/test_device.h"
53
54 #include "constrtestdata.h"
55
56 /*
57  * GPU version of constraints is only available with CUDA.
58  */
59 #define GPU_CONSTRAINTS_SUPPORTED (GMX_GPU_CUDA)
60
61 struct t_pbc;
62
63 namespace gmx
64 {
65 namespace test
66 {
67
68 /* \brief Constraints test runner interface.
69  *
70  * Wraps the actual implementation of constraints algorithm into common interface.
71  */
72 class IConstraintsTestRunner
73 {
74 public:
75     //! Virtual destructor.
76     virtual ~IConstraintsTestRunner() {}
77     /*! \brief Abstract constraining function. Should be overriden.
78      *
79      * \param[in] testData             Test data structure.
80      * \param[in] pbc                  Periodic boundary data.
81      */
82     virtual void applyConstraints(ConstraintsTestData* testData, t_pbc pbc) = 0;
83
84     /*! \brief Get the name of the implementation.
85      *
86      * \return "<algorithm> on <device>", depending on the actual implementation used. E.g., "LINCS on #0: NVIDIA GeForce GTX 1660 SUPER".
87      */
88     virtual std::string name() = 0;
89 };
90
91 // Runner for the CPU implementation of SHAKE constraints algorithm.
92 class ShakeConstraintsRunner : public IConstraintsTestRunner
93 {
94 public:
95     //! Default constructor.
96     ShakeConstraintsRunner() {}
97     /*! \brief Apply SHAKE constraints to the test data.
98      *
99      * \param[in] testData             Test data structure.
100      * \param[in] pbc                  Periodic boundary data.
101      */
102     void applyConstraints(ConstraintsTestData* testData, t_pbc pbc) override;
103     /*! \brief Get the name of the implementation.
104      *
105      * \return "SHAKE" string;
106      */
107     std::string name() override { return "SHAKE on CPU"; }
108 };
109
110 // Runner for the CPU implementation of LINCS constraints algorithm.
111 class LincsConstraintsRunner : public IConstraintsTestRunner
112 {
113 public:
114     //! Default constructor.
115     LincsConstraintsRunner() {}
116     /*! \brief Apply LINCS constraints to the test data on the CPU.
117      *
118      * \param[in] testData             Test data structure.
119      * \param[in] pbc                  Periodic boundary data.
120      */
121     void applyConstraints(ConstraintsTestData* testData, t_pbc pbc) override;
122     /*! \brief Get the name of the implementation.
123      *
124      * \return "LINCS" string;
125      */
126     std::string name() override { return "LINCS on CPU"; }
127 };
128
129 // Runner for the GPU implementation of LINCS constraints algorithm.
130 class LincsDeviceConstraintsRunner : public IConstraintsTestRunner
131 {
132 public:
133     /*! \brief Constructor. Keeps a copy of the hardware context.
134      *
135      * \param[in] testDevice The device hardware context to be used by the runner.
136      */
137     LincsDeviceConstraintsRunner(const TestDevice& testDevice) : testDevice_(testDevice) {}
138     /*! \brief Apply LINCS constraints to the test data on the GPU.
139      *
140      * \param[in] testData             Test data structure.
141      * \param[in] pbc                  Periodic boundary data.
142      */
143     void applyConstraints(ConstraintsTestData* testData, t_pbc pbc) override;
144     /*! \brief Get the name of the implementation.
145      *
146      * \return "LINCS_GPU" string;
147      */
148     std::string name() override { return "LINCS on " + testDevice_.description(); }
149
150 private:
151     //! Test device to be used in the runner.
152     const TestDevice& testDevice_;
153 };
154
155 } // namespace test
156 } // namespace gmx
157
158 #endif // GMX_MDLIB_TESTS_CONSTRTESTRUNNERS_H