QMCPACK
test_deep_copy.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) 2019 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include <memory>
16 #include <vector>
17 #include <iostream>
18 #include <omp.h>
20 
21 namespace qmcplusplus
22 {
23 struct container
24 {
25  double* data;
26  int size;
27 };
28 
29 TEST_CASE("OMPdeepcopy", "[OMP]")
30 {
31  const int MAX = 100;
32  auto* foo = new container;
33  foo->size = MAX;
34 
35  OMPallocator<double> myAlloc;
36 
37  foo->data = myAlloc.allocate(MAX);
38  for (int i = 0; i < MAX; i++)
39  foo->data[i] = i;
40 
41  auto* data_ptr = foo->data;
42 
43  PRAGMA_OFFLOAD("omp target enter data map(alloc:foo[0:1])")
44  PRAGMA_OFFLOAD("omp target map(always, to: foo[0:1], data_ptr[0:foo->size])") { foo->data = data_ptr; }
45 
46  int check_size(0);
47  double check_data1(0);
48  void* check_address1(nullptr);
49  void* check_address2(nullptr);
50  void* check_address3(nullptr);
51  // clang-format off
52  PRAGMA_OFFLOAD("omp target teams num_teams(1) \
53  map(from: check_size, check_data1, check_address1, check_address2, check_address3)")
54  // clang-format on
55  {
56  check_size = foo->size;
57  check_data1 = foo->data[1];
58  check_address1 = data_ptr;
59  check_address2 = foo->data;
60  check_address3 = foo;
61  }
62 
63  std::cout << "foo->data value on the host " << foo->data << std::endl;
64  std::cout << "foo->data value on the device " << check_address2 << std::endl;
65  std::cout << "foo->data mapped address on the device " << check_address1 << std::endl;
66  std::cout << "foo value on the host " << foo << std::endl;
67  std::cout << "foo mapped address on the device " << check_address3 << std::endl;
68 
69  REQUIRE(check_data1 == 1.0);
70  REQUIRE(check_size == MAX);
71 
72  PRAGMA_OFFLOAD("omp target teams num_teams(1) map(always,from:data_ptr[0:foo->size])") { data_ptr[1] = 2; }
73 
74  REQUIRE(data_ptr[1] == 2.0);
75 
76  myAlloc.deallocate(foo->data, MAX);
77  PRAGMA_OFFLOAD("omp target exit data map(delete:foo[0:1])")
78  delete foo;
79 }
80 
81 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void deallocate(value_type *pt, std::size_t n)
TEST_CASE("complex_helper", "[type_traits]")
REQUIRE(std::filesystem::exists(filename))
OMPallocator is an allocator with fused device and dualspace allocator functionality.
value_type * allocate(std::size_t n)