XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
host_array.hpp
Go to the documentation of this file.
1 #ifndef HOST_ARRAY_HPP
2 #define HOST_ARRAY_HPP
3 
4 /* The purpose of this class is to allow an object that gets sent to device via a lambda function (copy by value) to also contain an array of member objects (in host memory only) that contain Kokkos Views.
5  * This is not allowed by std::vector due to the vector destructor not compiling with cuda, and using a Kokkos::View creates warnings and is generally overkill.
6  * All we want is an array that does not get deallocated when a copy of the array is destroyed.
7  * The default constructor, copy assignment, copy constructor, and destructor can be safely called from device; none of these allocate or deallocate memory.
8  * All other operations are host-only.
9  */
10 template<typename T>
11 class HostArray{
12 
13  T* array;
14 
15  bool owns_array;
16  size_t n;
17 
18  public:
19 
20  // Default constructor
21  KOKKOS_INLINE_FUNCTION HostArray()
22  : owns_array(false),
23  n(0) {}
24 
25  // Constructor allocates
26  HostArray(size_t n_in)
27  : owns_array(true),
28  n(n_in)
29  {
30  array = new T[n_in];
31  }
32 
33  // Destructor
34  KOKKOS_INLINE_FUNCTION ~HostArray(){
35  // Deallocate only if this was the object that did the allocation
36  if(owns_array) delete[] array;
37  }
38 
39  // Copy constructor
40  KOKKOS_INLINE_FUNCTION HostArray(const HostArray& other)
41  : array(other.array),
42  owns_array(false),
43  n(other.n) {}
44 
45  // Copy assignment
46  KOKKOS_INLINE_FUNCTION HostArray& operator=(const HostArray& other){
47  HostArray new_array(other);
48  new_array.owns_array = false;
49  new_array.n = other.n;
50  return *this = new_array;
51  }
52 
53  // Move constructor
54  HostArray(HostArray&& other) noexcept
55  : owns_array(other.owns_array),
56  n(other.n),
57  array(std::exchange(other.array, nullptr))
58  {
59  other.owns_array = false;
60  other.n = 0;
61  }
62 
63  // Move assignment
64  HostArray& operator=(HostArray&& other) noexcept{
65  n = other.n;
66  owns_array = other.owns_array;
67  std::swap(array, other.array);
68  other.owns_array = false;
69  other.n = 0;
70  return *this;
71  }
72 
73  // Accessors ()
74  T operator ()(int i) const {return array[i];}
75  T& operator ()(int i) {return array[i];}
76 
77  // Size
78  size_t size() const{
79  return n;
80  }
81 };
82 
83 #endif
KOKKOS_INLINE_FUNCTION HostArray(const HostArray &other)
Definition: host_array.hpp:40
KOKKOS_INLINE_FUNCTION ~HostArray()
Definition: host_array.hpp:34
KOKKOS_INLINE_FUNCTION HostArray()
Definition: host_array.hpp:21
T * array
Definition: host_array.hpp:13
HostArray(HostArray &&other) noexcept
Definition: host_array.hpp:54
bool owns_array
Definition: host_array.hpp:15
HostArray(size_t n_in)
Definition: host_array.hpp:26
T operator()(int i) const
Definition: host_array.hpp:74
HostArray & operator=(HostArray &&other) noexcept
Definition: host_array.hpp:64
KOKKOS_INLINE_FUNCTION HostArray & operator=(const HostArray &other)
Definition: host_array.hpp:46
size_t n
Definition: host_array.hpp:16
size_t size() const
Definition: host_array.hpp:78
Definition: host_array.hpp:11