Set up build with hipSYCL
[alexxy/gromacs.git] / src / gromacs / mdlib / tests / leapfrogtestrunners.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 Leap-Frog tests runners.
37  *
38  * Declares test runner class for Leap-Frog algorithm. The test runners abstract
39  * class is used to unify the interfaces for CPU and GPU implementations of the
40  * Leap-Frog 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_LEAPFROGTESTRUNNERS_H
47 #define GMX_MDLIB_TESTS_LEAPFROGTESTRUNNERS_H
48
49 #include "config.h"
50
51 #include "gromacs/math/vec.h"
52
53 #include "testutils/test_device.h"
54
55 #include "leapfrogtestdata.h"
56
57 /*
58  * LeapFrog is available with CUDA and SYCL.
59  * However, the use of float3 in CPU code makes it hard to compile with hipSYCL until
60  * Issue #3312 is resolved.
61  */
62 #define HAVE_GPU_LEAPFROG (GMX_GPU_CUDA || (GMX_GPU_SYCL && !GMX_SYCL_HIPSYCL))
63
64 namespace gmx
65 {
66 namespace test
67 {
68
69 /* \brief LeapFrog integrator test runner interface.
70  *
71  * Wraps the actual implementation of LeapFrog algorithm into common interface.
72  */
73 class ILeapFrogTestRunner
74 {
75 public:
76     //! Virtual destructor
77     virtual ~ILeapFrogTestRunner() {}
78     /*! \brief The abstract function that runs the integrator for a given number of steps.
79      *
80      * Should be overriden.
81      *
82      * \param[in]     testData  Data needed for the integrator
83      * \param[in]     numSteps  Total number of steps to run integration for.
84      */
85     virtual void integrate(LeapFrogTestData* testData, int numSteps) = 0;
86
87     /*! \brief Get the human-friendly description of hardware used by the runner.
88      *
89      * \returns String with description of the hardware.
90      */
91     virtual std::string hardwareDescription() = 0;
92 };
93
94 // Runner for the CPU version of Leap-Frog.
95 class LeapFrogHostTestRunner : public ILeapFrogTestRunner
96 {
97 public:
98     //! Constructor.
99     LeapFrogHostTestRunner() {}
100     /*! \brief Integrate on the CPU for a given number of steps.
101      *
102      * Will update the test data with the integration result.
103      *
104      * \param[in]     testData  Data needed for the integrator
105      * \param[in]     numSteps  Total number of steps to run integration for.
106      */
107     void integrate(LeapFrogTestData* testData, int numSteps) override;
108     /*! \brief Get the hardware description
109      *
110      * \returns "CPU" string.
111      */
112     std::string hardwareDescription() override { return "CPU"; }
113 };
114
115 // Runner for the CPU version of Leap-Frog.
116 class LeapFrogDeviceTestRunner : public ILeapFrogTestRunner
117 {
118 public:
119     /*! \brief Constructor. Keeps a copy of the hardware context.
120      *
121      * \param[in] testDevice The device hardware context to be used by the runner.
122      */
123     LeapFrogDeviceTestRunner(const TestDevice& testDevice) : testDevice_(testDevice) {}
124     /*! \brief Integrate on the GPU for a given number of steps.
125      *
126      * Copies data from CPU to GPU, integrates the equation of motion
127      * for requested number of steps using Leap-Frog algorithm, copies
128      * the result back.
129      *
130      * \param[in]     testData  Data needed for the integrator
131      * \param[in]     numSteps  Total number of steps to run integration for.
132      */
133     void integrate(LeapFrogTestData* testData, int numSteps) override;
134     /*! \brief Get the hardware description
135      *
136      * \returns A string with GPU description.
137      */
138     std::string hardwareDescription() override { return testDevice_.description(); }
139
140 private:
141     //! Test device to be used in the runner.
142     const TestDevice& testDevice_;
143 };
144
145 } // namespace test
146 } // namespace gmx
147
148 #endif // GMX_MDLIB_TESTS_LEAPFROGTESTRUNNERS_H