2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2017,2018,2019, 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.
37 * Tests for GPU host allocator.
39 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 #include "gromacs/gpu_utils/hostallocator.h"
47 #include <type_traits>
50 #include <gtest/gtest.h>
52 #include "gromacs/gpu_utils/gpu_utils.h"
53 #include "gromacs/math/vectypes.h"
54 #include "gromacs/utility/arrayref.h"
55 #include "gromacs/utility/real.h"
57 #include "gromacs/math/tests/testarrayrefs.h"
59 #include "devicetransfers.h"
68 /*! \internal \brief Typed test fixture for infrastructure for
69 * host-side memory used for GPU transfers. */
71 class HostMemoryTest : public test::GpuTest
78 /*! \brief Convenience function to transform a view into one with base
79 * type of (non-const) char.
81 * This transformation is useful for using containers with C APIs
82 * where the function signature is not declared const even where the
83 * semantics of the usage actually are const.
85 * \param[in] data The data pointer.
86 * \param[in] size The size of the data pointer (in T).
87 * \tparam T The base type of the container
90 ArrayRef<char> charArrayRefFromArray(T* data, size_t size)
92 // Make a type like T, but without its possible const qualifier.
93 using NonConstT = std::remove_const_t<T>;
94 return arrayRefFromArray<char>(reinterpret_cast<char*>(const_cast<NonConstT*>(data)),
98 //! Does a device transfer of \c input to the device in \c gpuInfo, and back to \c output.
100 void runTest(const gmx_gpu_info_t& gpuInfo, ArrayRef<T> input, ArrayRef<T> output)
102 // Convert the views of input and output to flat non-const chars,
103 // so that there's no templating when we call doDeviceTransfers.
104 auto inputRef = charArrayRefFromArray(input.data(), input.size());
105 auto outputRef = charArrayRefFromArray(output.data(), output.size());
107 ASSERT_EQ(inputRef.size(), outputRef.size());
108 doDeviceTransfers(gpuInfo, inputRef, outputRef);
109 compareViews(input, output);
114 MoveOnly(real x = 0) : x(x) {}
115 MoveOnly(const MoveOnly&) = delete;
116 MoveOnly(MoveOnly&&) = default;
117 MoveOnly& operator=(const MoveOnly&) = delete;
118 MoveOnly& operator=(MoveOnly&&) = default;
119 bool operator==(const MoveOnly& o) const { return x == o.x; }
120 real operator*=(int scaleFactor) { return x *= scaleFactor; }
130 struct PaddingTraits<test::MoveOnly>
132 using SimdBaseType = real;
133 static constexpr int maxSimdWidthOfBaseType = GMX_REAL_MAX_SIMD_WIDTH;
136 } // namespace detail
141 //! The types used in testing of all operations.
142 typedef ::testing::Types<int32_t, real, RVec, test::MoveOnly> TestTypes;
144 //! Typed test fixture
146 struct HostAllocatorTest : HostMemoryTest<T>
148 using VectorType = PaddedHostVector<T>; //!< PaddedHostVector of type tested
150 TYPED_TEST_CASE(HostAllocatorTest, TestTypes);
152 //! Typed test fixture (no mem/gpu initializtion - much faster)
154 struct HostAllocatorTestNoMem : ::testing::Test
156 using VectorType = PaddedHostVector<T>; //!< PaddedHostVector of type tested
158 TYPED_TEST_CASE(HostAllocatorTestNoMem, TestTypes);
160 //! Typed test fixture for tests requiring a copyable type
162 struct HostAllocatorTestNoMemCopyable : HostAllocatorTestNoMem<T>
165 //! The types used in testing minus move only types
166 using TestTypesCopyable = ::testing::Types<int32_t, real, RVec>;
168 TYPED_TEST_CASE(HostAllocatorTestNoMemCopyable, TestTypesCopyable);
170 //! Typed test fixture for tests requiring a copyable type
172 using HostAllocatorTestCopyable = HostAllocatorTest<T>;
173 TYPED_TEST_CASE(HostAllocatorTestCopyable, TestTypesCopyable);
175 // Note that in GoogleTest typed tests, the use of TestFixture:: and
176 // this-> is sometimes required to get access to things in the fixture
177 // class (or its base classes).
179 // Note also that aspects of this code can be tested even when a GPU
180 // device is not available.
182 TYPED_TEST(HostAllocatorTest, EmptyMemoryAlwaysWorks)
184 typename TestFixture::VectorType v;
187 TYPED_TEST(HostAllocatorTestCopyable, VectorsWithDefaultHostAllocatorAlwaysWorks)
189 typename TestFixture::VectorType input(3), output;
190 output.resizeWithPadding(input.size());
193 // Several tests actually do CUDA transfers. This is not necessary
194 // because the state of page alignment or pinning is not currently
195 // relevant to the success of a CUDA transfer. CUDA checks happen only
196 // during cudaHostRegister and cudaHostUnregister. Such tests are of
197 // value only when this behaviour changes, if ever.
199 TYPED_TEST(HostAllocatorTestCopyable, TransfersWithoutPinningWork)
201 typename TestFixture::VectorType input;
202 fillInput(&input, 1);
203 typename TestFixture::VectorType output;
204 output.resizeWithPadding(input.size());
206 runTest(*this->gpuInfo_, makeArrayRef(input), makeArrayRef(output));
209 TYPED_TEST(HostAllocatorTestCopyable, FillInputAlsoWorksAfterCallingReserve)
211 typename TestFixture::VectorType input;
212 input.reserveWithPadding(3);
213 fillInput(&input, 1);
216 TYPED_TEST(HostAllocatorTestNoMem, CreateVector)
218 typename TestFixture::VectorType input1;
219 EXPECT_FALSE(input1.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
220 typename TestFixture::VectorType input2({ PinningPolicy::PinnedIfSupported });
221 EXPECT_TRUE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
224 TYPED_TEST(HostAllocatorTestNoMem, MoveAssignment)
226 typename TestFixture::VectorType input1({ PinningPolicy::PinnedIfSupported });
227 input1 = typename TestFixture::VectorType();
228 EXPECT_FALSE(input1.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
230 typename TestFixture::VectorType input2;
231 input2 = typename TestFixture::VectorType({ PinningPolicy::PinnedIfSupported });
232 EXPECT_TRUE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
235 TYPED_TEST(HostAllocatorTestNoMem, MoveConstruction)
237 typename TestFixture::VectorType input1;
238 typename TestFixture::VectorType input2(std::move(input1));
239 EXPECT_FALSE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
241 typename TestFixture::VectorType input3({ PinningPolicy::PinnedIfSupported });
242 typename TestFixture::VectorType input4(std::move(input3));
243 EXPECT_TRUE(input4.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
246 TYPED_TEST(HostAllocatorTestNoMemCopyable, CopyAssignment)
248 typename TestFixture::VectorType input1;
249 typename TestFixture::VectorType input2({ PinningPolicy::PinnedIfSupported });
251 EXPECT_FALSE(input1.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
252 EXPECT_TRUE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
254 EXPECT_FALSE(input1.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
255 EXPECT_TRUE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
258 TYPED_TEST(HostAllocatorTestNoMemCopyable, CopyConstruction)
260 typename TestFixture::VectorType input1;
261 typename TestFixture::VectorType input2(input1); //NOLINT(performance-unnecessary-copy-initialization)
262 EXPECT_FALSE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
264 typename TestFixture::VectorType input3({ PinningPolicy::PinnedIfSupported });
265 typename TestFixture::VectorType input4(input3); //NOLINT(performance-unnecessary-copy-initialization)
266 EXPECT_FALSE(input4.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
269 TYPED_TEST(HostAllocatorTestNoMem, Swap)
271 typename TestFixture::VectorType input1;
272 typename TestFixture::VectorType input2({ PinningPolicy::PinnedIfSupported });
273 std::swap(input1, input2);
274 EXPECT_TRUE(input1.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
275 EXPECT_FALSE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
276 std::swap(input2, input1);
277 EXPECT_FALSE(input1.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
278 EXPECT_TRUE(input2.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
281 TYPED_TEST(HostAllocatorTestNoMem, Comparison)
283 using AllocatorType = typename TestFixture::VectorType::allocator_type;
284 EXPECT_EQ(AllocatorType{}, AllocatorType{});
285 // Should be false for different pinning policy
286 EXPECT_NE(AllocatorType{}, AllocatorType{ PinningPolicy::PinnedIfSupported });
289 #if GMX_GPU == GMX_GPU_CUDA
291 // Policy suitable for pinning is only supported for a CUDA build
293 TYPED_TEST(HostAllocatorTestCopyable, TransfersWithPinningWorkWithCuda)
295 if (!this->haveValidGpus())
300 typename TestFixture::VectorType input;
301 changePinningPolicy(&input, PinningPolicy::PinnedIfSupported);
302 fillInput(&input, 1);
303 typename TestFixture::VectorType output;
304 changePinningPolicy(&output, PinningPolicy::PinnedIfSupported);
305 output.resizeWithPadding(input.size());
307 runTest(*this->gpuInfo_, makeArrayRef(input), makeArrayRef(output));
310 //! Helper function for wrapping a call to isHostMemoryPinned.
311 template<typename VectorType>
312 bool isPinned(const VectorType& v)
314 void* data = const_cast<void*>(static_cast<const void*>(v.data()));
315 return isHostMemoryPinned(data);
318 TYPED_TEST(HostAllocatorTestCopyable, ManualPinningOperationsWorkWithCuda)
320 if (!this->haveValidGpus())
325 typename TestFixture::VectorType input;
326 changePinningPolicy(&input, PinningPolicy::PinnedIfSupported);
327 EXPECT_TRUE(input.get_allocator().pinningPolicy() == PinningPolicy::PinnedIfSupported);
328 EXPECT_EQ(0, input.size());
329 EXPECT_EQ(0, input.paddedSize());
330 EXPECT_TRUE(input.empty());
331 EXPECT_FALSE(isPinned(input));
333 // Fill some contents, which will be pinned because of the policy.
334 fillInput(&input, 1);
335 EXPECT_TRUE(isPinned(input));
337 // Switching policy to CannotBePinned must unpin the buffer (via
338 // realloc and copy).
339 auto oldInputData = input.data();
340 changePinningPolicy(&input, PinningPolicy::CannotBePinned);
341 EXPECT_FALSE(isPinned(input));
342 // These cannot be equal as both had to be allocated at the same
343 // time for the contents to be able to be copied.
344 EXPECT_NE(oldInputData, input.data());
346 // Switching policy to PinnedIfSupported must pin the buffer (via
347 // realloc and copy).
348 oldInputData = input.data();
349 changePinningPolicy(&input, PinningPolicy::PinnedIfSupported);
350 EXPECT_TRUE(isPinned(input));
351 // These cannot be equal as both had to be allocated at the same
352 // time for the contents to be able to be copied.
353 EXPECT_NE(oldInputData, input.data());
358 TYPED_TEST(HostAllocatorTest, StatefulAllocatorUsesMemory)
360 // The HostAllocator has state, so a container using it will be
361 // larger than a normal vector, whose default allocator is
363 EXPECT_LT(sizeof(std::vector<typename TestFixture::VectorType::value_type>),
364 sizeof(typename TestFixture::VectorType));
367 TEST(HostAllocatorUntypedTest, Comparison)
369 // Should always be true for the same policy, indpendent of value_type
370 EXPECT_EQ(HostAllocator<float>{}, HostAllocator<double>{});
373 //! Declare allocator types to test.
374 using AllocatorTypesToTest =
375 ::testing::Types<HostAllocator<real>, HostAllocator<int32_t>, HostAllocator<RVec>, HostAllocator<MoveOnly>>;
377 TYPED_TEST_CASE(AllocatorTest, AllocatorTypesToTest);
382 // Includes tests common to all allocation policies.
383 #include "gromacs/utility/tests/alignedallocator_impl.h"