2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
36 * \brief Tests for device buffer
38 * \author Artem Zhmurov <zhmurov@gmail.com>
40 * \ingroup module_gpu_utils
49 # include <gmock/gmock.h>
50 # include <gtest/gtest.h>
52 # include "gromacs/gpu_utils/device_context.h"
53 # include "gromacs/gpu_utils/device_stream.h"
54 # include "gromacs/gpu_utils/devicebuffer.h"
56 # include "testutils/test_hardware_environment.h"
57 # include "testutils/testasserts.h"
62 template<typename ValueType>
63 BasicVector<ValueType>& operator++(BasicVector<ValueType>& in)
71 template<typename ValueType>
72 BasicVector<ValueType>& operator++(BasicVector<ValueType>& in, int /* n */)
74 BasicVector<ValueType> temp = *in;
79 template<typename ValueType>
80 inline bool operator==(const BasicVector<ValueType>& lhs, const BasicVector<ValueType>& rhs)
82 return lhs[XX] == rhs[XX] && lhs[YY] == rhs[YY] && lhs[ZZ] == rhs[ZZ];
92 using testing::Pointwise;
94 //! Test fixture (needed for typed tests)
96 class DeviceBufferTest : public ::testing::Test
100 using TypeParamList = testing::Types<short, int, float, double, gmx::RVec>;
101 TYPED_TEST_CASE(DeviceBufferTest, TypeParamList);
103 TYPED_TEST(DeviceBufferTest, CanAllocateAndFreeDeviceBuffer)
105 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
107 const DeviceContext& deviceContext = testDevice->deviceContext();
108 setActiveDevice(testDevice->deviceInfo());
110 DeviceBuffer<TypeParam> buffer;
112 allocateDeviceBuffer(&buffer, numValues, deviceContext);
113 freeDeviceBuffer(&buffer);
117 TYPED_TEST(DeviceBufferTest, CanReallocateAndFreeDeviceBuffer)
119 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
121 const DeviceContext& deviceContext = testDevice->deviceContext();
122 setActiveDevice(testDevice->deviceInfo());
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, ¤tNumValues, ¤tMaxNumValues, deviceContext);
130 freeDeviceBuffer(&buffer);
134 //! Initial value to fill the buffer of the scalar type
136 const T c_initialValue = static_cast<T>(1);
138 //! Initial value to fill the buffer of the vector type
140 const gmx::RVec c_initialValue<gmx::RVec> = { 1, -2, 3 };
143 TYPED_TEST(DeviceBufferTest, CanCopyToAndFromDevice)
145 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
147 const DeviceContext& deviceContext = testDevice->deviceContext();
148 const DeviceStream& deviceStream = testDevice->deviceStream();
149 setActiveDevice(testDevice->deviceInfo());
151 DeviceBuffer<TypeParam> buffer;
153 allocateDeviceBuffer(&buffer, numValues, deviceContext);
154 std::vector<TypeParam> valuesIn(numValues);
155 std::vector<TypeParam> valuesOut(numValues);
157 std::iota(valuesIn.begin(), valuesIn.end(), c_initialValue<TypeParam>);
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);
168 TYPED_TEST(DeviceBufferTest, CanCopyToAndFromDeviceWithOffset)
170 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
172 const DeviceContext& deviceContext = testDevice->deviceContext();
173 const DeviceStream& deviceStream = testDevice->deviceStream();
174 setActiveDevice(testDevice->deviceInfo());
176 DeviceBuffer<TypeParam> buffer;
178 allocateDeviceBuffer(&buffer, 2 * numValues, deviceContext);
179 std::vector<TypeParam> valuesIn(numValues);
180 std::vector<TypeParam> valuesOut(2 * numValues);
182 std::iota(valuesIn.begin(), valuesIn.end(), c_initialValue<TypeParam>);
184 // Fill the buffer with two copies of valuesIn, one after the other.
186 &buffer, valuesIn.data(), 0, numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
188 &buffer, valuesIn.data(), numValues, numValues, deviceStream, GpuApiCallBehavior::Sync, nullptr);
189 // Do the same copying on the CPU, so we can test it works
191 valuesIn.insert(valuesIn.end(), valuesIn.begin(), valuesIn.end());
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.";
197 SCOPED_TRACE("Checking the copy respects the output range");
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.";