QMCPACK
test_dual_allocators_ohmms_containers.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include <catch.hpp>
13 
14 #include <algorithm>
15 #include "config.h"
16 #include "PinnedAllocator.h"
18 #if defined(ENABLE_CUDA) || defined(ENABLE_SYCL)
19 #include <DualAllocatorAliases.hpp>
20 #endif
21 #include "OhmmsPETE/OhmmsMatrix.h"
22 #include "OhmmsPETE/OhmmsArray.h"
24 
25 #include "makeRngSpdMatrix.hpp"
27 
28 namespace qmcplusplus
29 {
30 // This naming is pretty bad but consistent with the naming over much of the code
31 // its defined locally all over the place.
32 template<typename T>
34 #if defined(ENABLE_CUDA) || defined(ENABLE_SYCL)
35 template<typename T>
36 using VendorDualPinnedAllocator = PinnedDualAllocator<T>;
37 #endif
38 
39 template<class OPA>
41 {
42  constexpr int nd = 3;
43  using Value = typename OPA::value_type;
45 
46  int n = 12;
47  Matrix<Value> mat_spd;
48  mat_spd.resize(n, n);
49  // Resize so each "dimension" can hold an n x n matrix
50  vcsoa.resize(n * n);
51 
53 
54  for (int iw = 0; iw < nd; ++iw)
55  {
56  makeRngSpdMatrix(mat_spd);
57  std::copy_n(mat_spd.data(), n * n, vcsoa.data(iw));
58  }
59 
60  vcsoa.updateTo();
61 
62  Matrix<Value, OPA> matrix_view(vcsoa, vcsoa.data(0), n, n);
63  Matrix<Value, OPA> matrix_view2(vcsoa, vcsoa.data(1), n, n);
64  Matrix<Value, OPA> matrix_view3(vcsoa, vcsoa.data(2), n, n);
65 
66 
67  // Without copying allocator this causes a segfault.
68  auto device_ptr = matrix_view.device_data();
69  auto device_ptr2 = matrix_view2.device_data();
70  auto device_ptr3 = matrix_view3.device_data();
71 
72  CHECK(device_ptr != nullptr);
73  CHECK(device_ptr2 != nullptr);
74  CHECK(device_ptr3 != nullptr);
75 
76  std::ptrdiff_t distance_host = matrix_view.data() - vcsoa.data();
77  std::ptrdiff_t distance_device = matrix_view.device_data() - vcsoa.device_data();
78  CHECK(distance_host == distance_device);
79 
80  distance_host = matrix_view2.data() - vcsoa.data();
81  distance_device = matrix_view2.device_data() - vcsoa.device_data();
82  CHECK(distance_host == distance_device);
83 
84  distance_host = matrix_view3.data() - vcsoa.data();
85  distance_device = matrix_view3.device_data() - vcsoa.device_data();
86  CHECK(distance_host == distance_device);
87 
88  int ifrom = 2;
89  int ito = 0;
90 
91  vcsoa.copyDeviceDataByIndex(0, 2);
92  vcsoa.updateFrom();
93 
94  auto check_matrix_result = checkMatrix(mat_spd, matrix_view);
95  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
96 
97  matrix_view2(0, 0) = 0.0;
98  matrix_view2(1, 0) = 1.0;
99 
100  matrix_view2.updateTo();
101  vcsoa.copyDeviceDataByIndex(0, 1);
102  matrix_view(0, 1) = 3.0;
103  matrix_view.updateTo(1, 1);
104  matrix_view.updateFrom();
105  CHECK(matrix_view(0, 0) == 0.0);
106  CHECK(matrix_view(0, 1) == 3.0);
107  CHECK(matrix_view(1, 0) == 1.0);
108 
110  aa.resize(2, 2, 3);
111  CHECK(aa.size() == 12);
112 }
113 
114 TEST_CASE("OhmmsMatrix_VectorSoaContainer_View", "[Integration][Allocators]")
115 {
116  testDualAllocator<OffloadPinnedAllocator<double>>();
117  testDualAllocator<OffloadPinnedAllocator<std::complex<double>>>();
118 #if defined(ENABLE_CUDA) || defined(ENABLE_SYCL)
119  testDualAllocator<VendorDualPinnedAllocator<double>>();
120  testDualAllocator<VendorDualPinnedAllocator<std::complex<double>>>();
121 #endif
122 }
123 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
Soa Container for D-dim vectors.
T * device_data()
return the base, device
void copyDeviceDataByIndex(unsigned to, unsigned from)
CHECKED_ELSE(check_matrix_result.result)
SoA adaptor class for Vector<TinyVector<T,D> >
auto check_matrix_result
void resize(size_type n, size_type m)
Resize the container.
Definition: OhmmsMatrix.h:99
void resize(const std::array< SIZET, D > &dims)
Resize the container.
Definition: OhmmsArray.h:65
void makeRngSpdMatrix(testing::RandomForTest< RngValueType< T >> &rng, Matrix< T > &mat_spd)
These allocators are to make code that should be generic with the respect to accelerator code flavor ...
size_t size() const
Definition: OhmmsArray.h:57
OMPallocator is an allocator with fused device and dualspace allocator functionality.
pointer device_data()
Definition: OhmmsMatrix.h:188
sycl::event copy_n(sycl::queue &aq, const T1 *restrict VA, size_t array_size, T2 *restrict VC, const std::vector< sycl::event > &events)
Definition: syclBLAS.cpp:548
CheckMatrixResult checkMatrix(M1 &a_mat, M2 &b_mat, const bool check_all=false, std::optional< const double > eps=std::nullopt)
This function checks equality a_mat and b_mat elements M1, M2 need to have their element type declare...
Definition: checkMatrix.hpp:63
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void resize(size_type n)
resize myData
A D-dimensional Array class based on PETE.
Definition: OhmmsArray.h:25
QMCTraits::FullPrecRealType value_type
Functor to provide scope for rng when making SpdMatrix for testing.