CUDAStdVector.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_CUDA_STD_VECTOR_HPP
12 #define CUBBYFLOW_CUDA_STD_VECTOR_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
17 
18 #include <vector>
19 
20 namespace CubbyFlow
21 {
22 template <typename T>
23 class CUDAStdVector final
24 {
25  public:
26  using ValueType = T;
27  using Reference = T&;
28  using ConstReference = const T&;
29  using Pointer = ValueType*;
30  using ConstPointer = const ValueType*;
31 
32  class ReferenceType
33  {
34  public:
35  CUBBYFLOW_CUDA_HOST_DEVICE ReferenceType(Pointer p) : m_ptr(p)
36  {
37  // Do nothing
38  }
39 
40  CUBBYFLOW_CUDA_HOST_DEVICE ReferenceType(const ReferenceType& other)
41  : m_ptr(other.m_ptr)
42  {
43  // Do nothing
44  }
45 
46  CUBBYFLOW_CUDA_HOST_DEVICE ReferenceType(ReferenceType&& other) noexcept
47  : m_ptr(std::move(other.m_ptr))
48  {
49  // Do nothing
50  }
51 
52  ~ReferenceType() = default;
53 
54  CUBBYFLOW_CUDA_HOST_DEVICE ReferenceType& operator=(
55  const ReferenceType& other)
56  {
57  m_ptr = other.m_ptr;
58  return *this;
59  }
60 
61  CUBBYFLOW_CUDA_HOST_DEVICE ReferenceType& operator=(
62  ReferenceType&& other) noexcept
63  {
64  m_ptr = std::move(other.m_ptr);
65  return *this;
66  }
67 
68 #ifdef __CUDA_ARCH__
69  __device__ ReferenceType& operator=(const ValueType& val)
70  {
71  *m_ptr = val;
72  return *this;
73  }
74 
75  __device__ operator ValueType() const
76  {
77  return *m_ptr;
78  }
79 #else
80  CUBBYFLOW_CUDA_HOST ReferenceType& operator=(const ValueType& val)
81  {
82  CUDACopyHostToDevice(&val, 1, m_ptr);
83  return *this;
84  }
85 
86  CUBBYFLOW_CUDA_HOST operator ValueType() const
87  {
88  std::remove_const_t<ValueType> tmp{};
89  CUDACopyDeviceToHost(m_ptr, 1, &tmp);
90  return tmp;
91  }
92 #endif
93 
94  private:
95  Pointer m_ptr;
96  };
97 
98  CUDAStdVector() = default;
99 
100  CUDAStdVector(size_t n, const ValueType& initVal = ValueType{});
101 
102  template <typename A>
103  CUDAStdVector(const std::vector<T, A>& other);
104 
105  CUDAStdVector(const CUDAStdVector& other);
106 
107  CUDAStdVector(CUDAStdVector&& other) noexcept;
108 
109  ~CUDAStdVector();
110 
111  template <typename A>
112  CUDAStdVector& operator=(const std::vector<T, A>& other);
113 
114  CUDAStdVector& operator=(const CUDAStdVector& other);
115 
116  CUDAStdVector& operator=(CUDAStdVector&& other) noexcept;
117 
118  Pointer data();
119 
120  ConstPointer data() const;
121 
122  size_t Size() const;
123 
124 #ifdef __CUDA_ARCH__
125  __device__ Reference At(size_t i);
126 
127  __device__ ConstReference At(size_t i) const;
128 #else
129  CUBBYFLOW_CUDA_HOST ReferenceType At(size_t i);
130 
131  CUBBYFLOW_CUDA_HOST T At(size_t i) const;
132 #endif
133 
134  void Clear();
135 
136  void Fill(const ValueType& val);
137 
138  void Resize(size_t n, const ValueType& initVal = ValueType{});
139 
140  void ResizeUninitialized(size_t n);
141 
142  void Swap(CUDAStdVector& other);
143 
144  void PushBack(const ValueType& val);
145 
146  void Append(const ValueType& val);
147 
148  void Append(const CUDAStdVector& other);
149 
150  template <typename A>
151  void CopyFrom(const std::vector<T, A>& other);
152 
153  void CopyFrom(const CUDAStdVector& other);
154 
155  template <typename A>
156  void CopyTo(std::vector<T, A>& other);
157 
158 #ifdef __CUDA_ARCH__
159  Reference operator[](size_t i);
160 
161  ConstReference operator[](size_t i) const;
162 #else
163  ReferenceType operator[](size_t i);
164 
165  T operator[](size_t i) const;
166 #endif
167 
168  private:
169  Pointer m_ptr = nullptr;
170  size_t m_size = 0;
171 };
172 } // namespace CubbyFlow
173 
175 
176 #endif
177 
178 #endif
Definition: pybind11Utils.hpp:20
void Fill(ArrayView< T, N > a, const Vector< size_t, N > &begin, const Vector< size_t, N > &end, const T &val)
Definition: ArrayUtils-Impl.hpp:19