86cfa6bdd52244089f015f0249712071d55d3801
[alexxy/gromacs.git] / src / gromacs / gpu_utils / tests / device_buffer.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, 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 Tests for device buffer
37  *
38  * \author Artem Zhmurov <zhmurov@gmail.com>
39  *
40  * \ingroup module_gpu_utils
41  */
42 #include "gmxpre.h"
43
44 #include "config.h"
45
46 #if GMX_GPU
47 #    include <numeric>
48
49 #    include <gmock/gmock.h>
50 #    include <gtest/gtest.h>
51
52 #    include "gromacs/gpu_utils/device_context.h"
53 #    include "gromacs/gpu_utils/device_stream.h"
54 #    include "gromacs/gpu_utils/devicebuffer.h"
55
56 #    include "testutils/test_hardware_environment.h"
57 #    include "testutils/testasserts.h"
58
59 namespace gmx
60 {
61
62 template<typename ValueType>
63 BasicVector<ValueType>& operator++(BasicVector<ValueType>& in)
64 {
65     in[XX]++;
66     in[YY]++;
67     in[ZZ]++;
68     return in;
69 }
70
71 template<typename ValueType>
72 BasicVector<ValueType>& operator++(BasicVector<ValueType>& in, int /* n */)
73 {
74     BasicVector<ValueType> temp = *in;
75     ++*in;
76     return temp;
77 }
78
79 template<typename ValueType>
80 inline bool operator==(const BasicVector<ValueType>& lhs, const BasicVector<ValueType>& rhs)
81 {
82     return lhs[XX] == rhs[XX] && lhs[YY] == rhs[YY] && lhs[ZZ] == rhs[ZZ];
83 }
84
85 namespace test
86 {
87
88 namespace
89 {
90
91 using testing::Eq;
92 using testing::Pointwise;
93
94 //! Test fixture (needed for typed tests)
95 template<typename T>
96 class DeviceBufferTest : public ::testing::Test
97 {
98 };
99
100 using TypeParamList = testing::Types<short, int, float, double, gmx::RVec>;
101 TYPED_TEST_CASE(DeviceBufferTest, TypeParamList);
102
103 TYPED_TEST(DeviceBufferTest, CanAllocateAndFreeDeviceBuffer)
104 {
105     for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
106     {
107         const DeviceContext& deviceContext = testDevice->deviceContext();
108         setActiveDevice(testDevice->deviceInfo());
109
110         DeviceBuffer<TypeParam> buffer;
111         int                     numValues = 123;
112         allocateDeviceBuffer(&buffer, numValues, deviceContext);
113         freeDeviceBuffer(&buffer);
114     }
115 }
116
117 TYPED_TEST(DeviceBufferTest, CanReallocateAndFreeDeviceBuffer)
118 {
119     for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
120     {
121         const DeviceContext& deviceContext = testDevice->deviceContext();
122         setActiveDevice(testDevice->deviceInfo());
123
124         DeviceBuffer<TypeParam> buffer;
125         int                     currentNumValues    = 456;
126         int                     newNumValues        = 789;
127         int                     currentMaxNumValues = 0;
128         allocateDeviceBuffer(&buffer, currentNumValues, deviceContext);
129         reallocateDeviceBuffer(&buffer, newNumValues, &currentNumValues, &currentMaxNumValues, deviceContext);
130         freeDeviceBuffer(&buffer);
131     }
132 }
133
134 //! Initial value to fill the buffer of the scalar type
135 template<typename T>
136 const T c_initialValue = static_cast<T>(1);
137
138 //! Initial value to fill the buffer of the vector type
139 template<>
140 const gmx::RVec c_initialValue<gmx::RVec> = { 1, -2, 3 };
141
142
143 TYPED_TEST(DeviceBufferTest, CanCopyToAndFromDevice)
144 {
145     for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
146     {
147         const DeviceContext& deviceContext = testDevice->deviceContext();
148         const DeviceStream&  deviceStream  = testDevice->deviceStream();
149         setActiveDevice(testDevice->deviceInfo());
150
151         DeviceBuffer<TypeParam> buffer;
152         int                     numValues = 123;
153         allocateDeviceBuffer(&buffer, numValues, deviceContext);
154         std::vector<TypeParam> valuesIn(numValues);
155         std::vector<TypeParam> valuesOut(numValues);
156
157         std::iota(valuesIn.begin(), valuesIn.end(), c_initialValue<TypeParam>);
158
159         copyToDeviceBuffer(
160                 &buffer, valuesIn.data(), 0, numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
161         copyFromDeviceBuffer(
162                 valuesOut.data(), &buffer, 0, numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
163         EXPECT_THAT(valuesOut, Pointwise(Eq(), valuesIn)) << "Changed after H2D and D2H copy.";
164         freeDeviceBuffer(&buffer);
165     }
166 }
167
168 TYPED_TEST(DeviceBufferTest, CanCopyToAndFromDeviceWithOffset)
169 {
170     for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
171     {
172         const DeviceContext& deviceContext = testDevice->deviceContext();
173         const DeviceStream&  deviceStream  = testDevice->deviceStream();
174         setActiveDevice(testDevice->deviceInfo());
175
176         DeviceBuffer<TypeParam> buffer;
177         int                     numValues = 123;
178         allocateDeviceBuffer(&buffer, 2 * numValues, deviceContext);
179         std::vector<TypeParam> valuesIn(numValues);
180         std::vector<TypeParam> valuesOut(2 * numValues);
181
182         std::iota(valuesIn.begin(), valuesIn.end(), c_initialValue<TypeParam>);
183
184         // Fill the buffer with two copies of valuesIn, one after the other.
185         copyToDeviceBuffer(
186                 &buffer, valuesIn.data(), 0, numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
187         copyToDeviceBuffer(
188                 &buffer, valuesIn.data(), numValues, numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
189         // Do the same copying on the CPU, so we can test it works
190         // correctly.
191         valuesIn.insert(valuesIn.end(), valuesIn.begin(), valuesIn.end());
192
193         copyFromDeviceBuffer(
194                 valuesOut.data(), &buffer, 0, 2 * numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
195         EXPECT_THAT(valuesOut, Pointwise(Eq(), valuesIn)) << "Changed after H2D and D2H copy.";
196
197         SCOPED_TRACE("Checking the copy respects the output range");
198
199         // Remove the first element, and push another copy of the last
200         // element, so we can check that a copy of all of the data
201         // skipping the first element correctly over-writes exactly
202         // all but one of the old values.
203         valuesIn.erase(valuesIn.begin());
204         valuesIn.push_back(valuesIn.back());
205         copyFromDeviceBuffer(
206                 valuesOut.data(), &buffer, 1, 2 * numValues - 1, deviceStream, GpuApiCallBehavior::Sync, nullptr);
207         EXPECT_THAT(valuesOut, Pointwise(Eq(), valuesIn)) << "Changed after H2D and D2H copy.";
208     }
209 }
210
211
212 } // namespace
213 } // namespace test
214 } // namespace gmx
215
216 #endif // GMX_GPU