2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 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.
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"
55 # include "gromacs/gpu_utils/hostallocator.h"
57 # include "testutils/test_hardware_environment.h"
58 # include "testutils/testasserts.h"
63 template<typename ValueType>
64 BasicVector<ValueType>& operator++(BasicVector<ValueType>& in)
72 template<typename ValueType>
73 BasicVector<ValueType>& operator++(BasicVector<ValueType>& in, int /* n */)
75 BasicVector<ValueType> temp = *in;
80 template<typename ValueType>
81 inline bool operator==(const BasicVector<ValueType>& lhs, const BasicVector<ValueType>& rhs)
83 return lhs[XX] == rhs[XX] && lhs[YY] == rhs[YY] && lhs[ZZ] == rhs[ZZ];
93 using testing::Pointwise;
95 //! Test fixture (needed for typed tests)
97 class DeviceBufferTest : public ::testing::Test
101 using TypeParamList = testing::Types<short, int, float, double, gmx::RVec>;
102 TYPED_TEST_CASE(DeviceBufferTest, TypeParamList);
104 TYPED_TEST(DeviceBufferTest, CanAllocateAndFreeDeviceBuffer)
106 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
108 const DeviceContext& deviceContext = testDevice->deviceContext();
109 setActiveDevice(testDevice->deviceInfo());
111 DeviceBuffer<TypeParam> buffer;
113 allocateDeviceBuffer(&buffer, numValues, deviceContext);
114 freeDeviceBuffer(&buffer);
118 TYPED_TEST(DeviceBufferTest, CanReallocateAndFreeDeviceBuffer)
120 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
122 const DeviceContext& deviceContext = testDevice->deviceContext();
123 setActiveDevice(testDevice->deviceInfo());
125 DeviceBuffer<TypeParam> buffer;
126 int currentNumValues = 456;
127 int newNumValues = 789;
128 int currentMaxNumValues = 0;
129 allocateDeviceBuffer(&buffer, currentNumValues, deviceContext);
130 reallocateDeviceBuffer(&buffer, newNumValues, ¤tNumValues, ¤tMaxNumValues, deviceContext);
131 freeDeviceBuffer(&buffer);
135 //! Initial value to fill the buffer of the scalar type
137 const T c_initialValue = static_cast<T>(1);
139 //! Initial value to fill the buffer of the vector type
141 const gmx::RVec c_initialValue<gmx::RVec> = { 1, -2, 3 };
144 TYPED_TEST(DeviceBufferTest, CanCopyToAndFromDevice)
146 for (auto transferKind : { GpuApiCallBehavior::Sync, GpuApiCallBehavior::Async })
148 PinningPolicy pinningPolicy = (transferKind == GpuApiCallBehavior::Async)
149 ? PinningPolicy::PinnedIfSupported
150 : PinningPolicy::CannotBePinned;
151 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
153 const DeviceContext& deviceContext = testDevice->deviceContext();
154 const DeviceStream& deviceStream = testDevice->deviceStream();
155 setActiveDevice(testDevice->deviceInfo());
157 DeviceBuffer<TypeParam> buffer;
159 allocateDeviceBuffer(&buffer, numValues, deviceContext);
160 HostVector<TypeParam> valuesIn(numValues, { pinningPolicy });
161 HostVector<TypeParam> valuesOut(numValues, { pinningPolicy });
163 std::iota(valuesIn.begin(), valuesIn.end(), c_initialValue<TypeParam>);
165 copyToDeviceBuffer(&buffer, valuesIn.data(), 0, numValues, deviceStream, transferKind, nullptr);
166 copyFromDeviceBuffer(
167 valuesOut.data(), &buffer, 0, numValues, deviceStream, transferKind, nullptr);
168 if (transferKind == GpuApiCallBehavior::Async)
170 deviceStream.synchronize();
172 EXPECT_THAT(valuesOut, Pointwise(Eq(), valuesIn))
173 << "Changed after H2D and D2H " << enumValueToString(transferKind) << " copy.";
174 freeDeviceBuffer(&buffer);
179 TYPED_TEST(DeviceBufferTest, CanCopyToAndFromDeviceWithOffset)
181 for (auto transferKind : { GpuApiCallBehavior::Sync, GpuApiCallBehavior::Async })
183 PinningPolicy pinningPolicy = (transferKind == GpuApiCallBehavior::Async)
184 ? PinningPolicy::PinnedIfSupported
185 : PinningPolicy::CannotBePinned;
186 for (const auto& testDevice : getTestHardwareEnvironment()->getTestDeviceList())
188 const DeviceContext& deviceContext = testDevice->deviceContext();
189 const DeviceStream& deviceStream = testDevice->deviceStream();
190 setActiveDevice(testDevice->deviceInfo());
192 DeviceBuffer<TypeParam> buffer;
194 allocateDeviceBuffer(&buffer, 2 * numValues, deviceContext);
195 HostVector<TypeParam> valuesIn(numValues, { pinningPolicy });
196 HostVector<TypeParam> valuesOut(2 * numValues, { pinningPolicy });
198 std::iota(valuesIn.begin(), valuesIn.end(), c_initialValue<TypeParam>);
200 // Fill the buffer with two copies of valuesIn, one after the other.
201 copyToDeviceBuffer(&buffer, valuesIn.data(), 0, numValues, deviceStream, transferKind, nullptr);
203 &buffer, valuesIn.data(), numValues, numValues, deviceStream, transferKind, nullptr);
204 // Wait until GPU is done andd o the same copying on the CPU, so we can test it works correctly.
205 if (transferKind == GpuApiCallBehavior::Async)
207 deviceStream.synchronize();
209 valuesIn.insert(valuesIn.end(), valuesIn.begin(), valuesIn.end());
211 copyFromDeviceBuffer(
212 valuesOut.data(), &buffer, 0, 2 * numValues, deviceStream, transferKind, nullptr);
213 if (transferKind == GpuApiCallBehavior::Async)
215 deviceStream.synchronize();
217 EXPECT_THAT(valuesOut, Pointwise(Eq(), valuesIn))
218 << "Changed after H2D and D2H " << enumValueToString(transferKind) << " copy.";
220 SCOPED_TRACE("Checking the copy respects the output range");
222 // Remove the first element, and push another copy of the last
223 // element, so we can check that a copy of all of the data
224 // skipping the first element correctly over-writes exactly
225 // all but one of the old values.
226 valuesIn.erase(valuesIn.begin());
227 valuesIn.push_back(valuesIn.back());
228 copyFromDeviceBuffer(
229 valuesOut.data(), &buffer, 1, 2 * numValues - 1, deviceStream, transferKind, nullptr);
230 if (transferKind == GpuApiCallBehavior::Async)
232 deviceStream.synchronize();
234 EXPECT_THAT(valuesOut, Pointwise(Eq(), valuesIn))
235 << "Changed after H2D and D2H " << enumValueToString(transferKind) << " copy.";