QMCPACK
test_DiracMatrixComputeOMPTarget.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 #include <algorithm>
14 #include "Configuration.h"
15 #include "OhmmsData/Libxml2Doc.h"
16 #include "OhmmsPETE/OhmmsMatrix.h"
17 #include "OhmmsPETE/OhmmsVector.h"
20 #include "makeRngSpdMatrix.hpp"
21 #include "Utilities/Resource.h"
25 
26 // Legacy CPU inversion for temporary testing
28 
29 
30 namespace qmcplusplus
31 {
32 template<typename T>
33 using OffloadPinnedAllocator = OMPallocator<T, PinnedAlignedAllocator<T>>;
34 
35 template<typename T>
36 using OffloadPinnedMatrix = Matrix<T, OffloadPinnedAllocator<T>>;
37 template<typename T>
38 using OffloadPinnedVector = Vector<T, OffloadPinnedAllocator<T>>;
39 
40 TEST_CASE("DiracMatrixComputeOMPTarget_different_batch_sizes", "[wavefunction][fermion]")
41 {
43  mat_a.resize(4, 4);
44  std::vector<double> A{2, 5, 8, 7, 5, 2, 2, 8, 7, 5, 6, 6, 5, 4, 4, 8};
45  std::copy_n(A.data(), 16, mat_a.data());
47  log_values.resize(1);
49  inv_mat_a.resize(4, 4);
51 
53  std::complex<double> log_value;
54  dmc_omp.invert_transpose(queue, mat_a, inv_mat_a, log_value);
55  CHECK(log_value == LogComplexApprox(std::complex<double>{5.267858159063328, 6.283185307179586}));
56 
57 
59  mat_b.resize(4, 4);
60  double invA[16]{-0.08247423, -0.26804124, 0.26804124, 0.05154639, 0.18556701, -0.89690722, 0.39690722, 0.13402062,
61  0.24742268, -0.19587629, 0.19587629, -0.15463918, -0.29896907, 1.27835052, -0.77835052, 0.06185567};
62  std::copy_n(invA, 16, mat_b.data());
63 
64  auto check_matrix_result = checkMatrix(inv_mat_a, mat_b);
65  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
66 
68  mat_a2.resize(4, 4);
69  std::copy_n(A.begin(), 16, mat_a2.data());
70  OffloadPinnedMatrix<double> inv_mat_a2;
71  inv_mat_a2.resize(4, 4);
72 
73  RefVector<const OffloadPinnedMatrix<double>> a_mats{mat_a, mat_a2};
74  RefVector<OffloadPinnedMatrix<double>> inv_a_mats{inv_mat_a, inv_mat_a2};
75 
76  log_values.resize(2);
77  dmc_omp.mw_invertTranspose(queue, a_mats, inv_a_mats, log_values);
78 
79  check_matrix_result = checkMatrix(inv_mat_a, mat_b);
80  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
81  check_matrix_result = checkMatrix(inv_mat_a2, mat_b);
82  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
83 
84  CHECK(log_values[0] == ComplexApprox(std::complex<double>{5.267858159063328, 6.283185307179586}));
85  CHECK(log_values[1] == ComplexApprox(std::complex<double>{5.267858159063328, 6.283185307179586}));
86 
88  mat_a3.resize(4, 4);
89  std::copy_n(A.begin(), 16, mat_a3.data());
90  OffloadPinnedMatrix<double> inv_mat_a3;
91  inv_mat_a3.resize(4, 4);
92 
93  a_mats[1] = mat_a3;
94 
95  RefVector<const OffloadPinnedMatrix<double>> a_mats3{mat_a, mat_a2, mat_a3};
96  RefVector<OffloadPinnedMatrix<double>> inv_a_mats3{inv_mat_a, inv_mat_a2, inv_mat_a3};
97 
98  log_values.resize(3);
99  dmc_omp.mw_invertTranspose(queue, a_mats3, inv_a_mats3, log_values);
100 
101  check_matrix_result = checkMatrix(inv_mat_a, mat_b);
102  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
103  check_matrix_result = checkMatrix(inv_mat_a2, mat_b);
104  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
105  check_matrix_result = checkMatrix(inv_mat_a3, mat_b);
106  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
107 
108  CHECK(log_values[0] == ComplexApprox(std::complex<double>{5.267858159063328, 6.283185307179586}));
109  CHECK(log_values[1] == ComplexApprox(std::complex<double>{5.267858159063328, 6.283185307179586}));
110  CHECK(log_values[2] == ComplexApprox(std::complex<double>{5.267858159063328, 6.283185307179586}));
111 }
112 
113 TEST_CASE("DiracMatrixComputeOMPTarget_large_determinants_against_legacy", "[wavefunction][fermion]")
114 {
115  int n = 64;
116 
118 
119  Matrix<double> mat_spd;
120  mat_spd.resize(n, n);
122  makeRngSpdMatrix(mat_spd);
123  // You would hope you could do this
124  // OffloadPinnedMatrix<double> mat_a(mat_spd);
125  // But you can't
127  for (int i = 0; i < n; ++i)
128  for (int j = 0; j < n; ++j)
129  mat_a(i, j) = mat_spd(i, j);
130 
131  Matrix<double> mat_spd2;
132  mat_spd2.resize(n, n);
133  makeRngSpdMatrix(mat_spd2);
134  // You would hope you could do this
135  // OffloadPinnedMatrix<double> mat_a(mat_spd);
136  // But you can't
138  for (int i = 0; i < n; ++i)
139  for (int j = 0; j < n; ++j)
140  mat_a2(i, j) = mat_spd2(i, j);
141 
143  log_values.resize(2);
144  OffloadPinnedMatrix<double> inv_mat_a;
145  inv_mat_a.resize(n, n);
146  OffloadPinnedMatrix<double> inv_mat_a2;
147  inv_mat_a2.resize(n, n);
148 
149  RefVector<const OffloadPinnedMatrix<double>> a_mats{mat_a, mat_a2};
150  RefVector<OffloadPinnedMatrix<double>> inv_a_mats{inv_mat_a, inv_mat_a2};
151 
153  dmc_omp.mw_invertTranspose(queue, a_mats, inv_a_mats, log_values);
154 
155  DiracMatrix<double> dmat;
156  Matrix<double> inv_mat_test(n, n);
157  std::complex<double> det_log_value;
158  dmat.invert_transpose(mat_spd, inv_mat_test, det_log_value);
159 
160  auto check_matrix_result = checkMatrix(inv_mat_a, inv_mat_test);
161  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
162 
163  dmat.invert_transpose(mat_spd2, inv_mat_test, det_log_value);
164  check_matrix_result = checkMatrix(inv_mat_a2, inv_mat_test);
165  CHECKED_ELSE(check_matrix_result.result) { FAIL(check_matrix_result.result_message); }
166 }
167 
168 
169 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
class to compute matrix inversion and the log value of determinant of a batch of DiracMatrixes.
Vector< T, PinnedDualAllocator< T > > OffloadPinnedVector
std::vector< StdComp, CUDAHostAllocator< StdComp > > log_values(batch_size)
void mw_invertTranspose(compute::Queue< PL > &resource_ignored, const RefVector< const OffloadPinnedMatrix< TMAT >> &a_mats, const RefVector< OffloadPinnedMatrix< TMAT >> &inv_a_mats, OffloadPinnedVector< LogValue > &log_values)
This covers both mixed and Full precision case.
TEST_CASE("complex_helper", "[type_traits]")
CHECKED_ELSE(check_matrix_result.result)
auto check_matrix_result
void resize(size_type n, size_type m)
Resize the container.
Definition: OhmmsMatrix.h:99
Catch::Detail::LogComplexApprox LogComplexApprox
void makeRngSpdMatrix(testing::RandomForTest< RngValueType< T >> &rng, Matrix< T > &mat_spd)
helper class to compute matrix inversion and the log value of determinant
Definition: DiracMatrix.h:111
std::enable_if_t< std::is_same< VALUE_FP, TMAT >::value > invert_transpose(HandleResource &resource, const OffloadPinnedMatrix< TMAT > &a_mat, OffloadPinnedMatrix< TMAT > &inv_a_mat, LogValue &log_value)
compute the inverse of the transpose of matrix A and its determinant value in log when VALUE_FP and T...
std::vector< std::reference_wrapper< T > > RefVector
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
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 }))
Declaration of WaveFunctionComponent.
OMPallocator< T, PinnedAlignedAllocator< T > > OffloadPinnedAllocator
Matrix< T, PinnedDualAllocator< T > > OffloadPinnedMatrix
std::enable_if_t< std::is_same< T_FP, TMAT >::value > invert_transpose(const Matrix< TMAT, ALLOC1 > &amat, Matrix< TMAT, ALLOC2 > &invMat, std::complex< TREAL > &LogDet)
compute the inverse of the transpose of matrix A and its determinant value in log when T_FP and TMAT ...
Definition: DiracMatrix.h:188
Functor to provide scope for rng when making SpdMatrix for testing.