f890cbd78d18e2f277bb4c5eb00268fc7e99fbb5
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / settletestrunners.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 SETTLE tests runners.
37  *
38  * Declares test runner class for SETTLE algorithm. The test runners abstract
39  * class is used to unify the interfaces for CPU and GPU implementations of the
40  * SETTLE algorithm. This allows to run the same test on the same data using
41  * different implementations of the parent class, that inherit its interfaces.
42  *
43  * \author Artem Zhmurov <zhmurov@gmail.com>
44  * \ingroup module_mdlib
45  */
46 #ifndef GMX_MDLIB_TESTS_SETTLETESTRUNNERS_H
47 #define GMX_MDLIB_TESTS_SETTLETESTRUNNERS_H
48
49 #include <gtest/gtest.h>
50
51 #include "testutils/test_device.h"
52
53 #include "settletestdata.h"
54
55 /*
56  * GPU version of SETTLE is only available with CUDA.
57  */
58 #define GPU_SETTLE_SUPPORTED (GMX_GPU_CUDA)
59
60 struct t_pbc;
61
62 namespace gmx
63 {
64 namespace test
65 {
66
67 /* \brief SETTLE test runner interface.
68  *
69  * Wraps the actual implementation of SETTLE into common interface.
70  */
71 class ISettleTestRunner
72 {
73 public:
74     //! Virtual destructor.
75     virtual ~ISettleTestRunner() {}
76
77     /*! \brief Apply SETTLE using CPU version of the algorithm
78      *
79      * Initializes SETTLE object, applies algorithm, destroys the object. The coordinates, velocities
80      * and virial are updated in the testData object.
81      *
82      * \param[in,out] testData          An object, containing all the data structures needed by SETTLE.
83      * \param[in]     pbc               Periodic boundary setup.
84      * \param[in]     updateVelocities  If the velocities should be updated.
85      * \param[in]     calcVirial        If the virial should be computed.
86      * \param[in]     testDescription   Brief description that will be printed in case of test failure.
87      */
88     virtual void applySettle(SettleTestData*    testData,
89                              t_pbc              pbc,
90                              bool               updateVelocities,
91                              bool               calcVirial,
92                              const std::string& testDescription) = 0;
93     /*! \brief Get the hardware description
94      *
95      * \returns A string, describing hardware used by the runner.
96      */
97     virtual std::string hardwareDescription() = 0;
98 };
99
100 // Runner for the CPU implementation of SETTLE.
101 class SettleHostTestRunner : public ISettleTestRunner
102 {
103 public:
104     //! Default constructor.
105     SettleHostTestRunner() {}
106     /*! \brief Apply SETTLE using CPU version of the algorithm
107      *
108      * Initializes SETTLE object, applies algorithm, destroys the object. The coordinates, velocities
109      * and virial are updated in the testData object.
110      *
111      * \param[in,out] testData          An object, containing all the data structures needed by SETTLE.
112      * \param[in]     pbc               Periodic boundary setup.
113      * \param[in]     updateVelocities  If the velocities should be updated.
114      * \param[in]     calcVirial        If the virial should be computed.
115      * \param[in]     testDescription   Brief description that will be printed in case of test failure.
116      */
117     void applySettle(SettleTestData*    testData,
118                      t_pbc              pbc,
119                      bool               updateVelocities,
120                      bool               calcVirial,
121                      const std::string& testDescription) override;
122     /*! \brief Get the hardware description
123      *
124      * \returns "CPU" string.
125      */
126     std::string hardwareDescription() override { return "CPU"; }
127 };
128
129 // Runner for the GPU implementation of SETTLE.
130 class SettleDeviceTestRunner : public ISettleTestRunner
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     SettleDeviceTestRunner(const TestDevice& testDevice) : testDevice_(testDevice) {}
138     /*! \brief Apply SETTLE using GPU version of the algorithm
139      *
140      * Initializes SETTLE object, copied data to the GPU, applies algorithm, copies the data back,
141      * destroys the object. The coordinates, velocities and virial are updated in the testData object.
142      *
143      * \param[in,out] testData          An object, containing all the data structures needed by SETTLE.
144      * \param[in]     pbc               Periodic boundary setup.
145      * \param[in]     updateVelocities  If the velocities should be updated.
146      * \param[in]     calcVirial        If the virial should be computed.
147      * \param[in]     testDescription   Brief description that will be printed in case of test failure.
148      */
149     void applySettle(SettleTestData*    testData,
150                      t_pbc              pbc,
151                      bool               updateVelocities,
152                      bool               calcVirial,
153                      const std::string& testDescription) override;
154     /*! \brief Get the hardware description
155      *
156      * \returns A string with GPU description.
157      */
158     std::string hardwareDescription() override { return testDevice_.description(); }
159
160 private:
161     //! Test test device to be used in the runner.
162     const TestDevice& testDevice_;
163 };
164
165 } // namespace test
166 } // namespace gmx
167
168 #endif // GMX_MDLIB_TESTS_SETTLETESTRUNNERS_H