CUDAArrayBase.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_ARRAY_BASE_HPP
12 #define CUBBYFLOW_CUDA_ARRAY_BASE_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
18 
19 namespace CubbyFlow
20 {
21 template <typename T, size_t N, typename DerivedArray>
22 class CUDAArrayBase
23 {
24  public:
25  using Derived = DerivedArray;
26  using ValueType = T;
27  using HostReference = typename CUDAStdVector<T>::ReferenceType;
28  using Reference = T&;
29  using ConstReference = const T&;
30  using Pointer = T*;
31  using ConstPointer = const T*;
32 
33  CUBBYFLOW_CUDA_HOST_DEVICE size_t Index(size_t i) const;
34 
35  template <typename... Args>
36  CUBBYFLOW_CUDA_HOST_DEVICE size_t Index(size_t i, Args... args) const;
37 
38  template <size_t... I>
39  CUBBYFLOW_CUDA_HOST_DEVICE size_t
40  Index(const CUDAStdArray<size_t, N>& idx) const;
41 
42  CUBBYFLOW_CUDA_HOST_DEVICE T* data();
43 
44  CUBBYFLOW_CUDA_HOST_DEVICE const T* data() const;
45 
46  CUBBYFLOW_CUDA_HOST_DEVICE const CUDAStdArray<size_t, N>& Size() const;
47 
48  template <size_t M = N>
49  CUBBYFLOW_CUDA_HOST_DEVICE std::enable_if_t<(M > 0), size_t> Width() const;
50 
51  template <size_t M = N>
52  CUBBYFLOW_CUDA_HOST_DEVICE std::enable_if_t<(M > 1), size_t> Height() const;
53 
54  template <size_t M = N>
55  CUBBYFLOW_CUDA_HOST_DEVICE std::enable_if_t<(M > 2), size_t> Depth() const;
56 
57  CUBBYFLOW_CUDA_HOST_DEVICE size_t Length() const;
58 
59 #ifdef __CUDA_ARCH__
60  CUBBYFLOW_CUDA_DEVICE Reference At(size_t i);
61 
62  CUBBYFLOW_CUDA_DEVICE ConstReference At(size_t i) const;
63 
64  template <typename... Args>
65  CUBBYFLOW_CUDA_DEVICE Reference At(size_t i, Args... args);
66 
67  template <typename... Args>
68  CUBBYFLOW_CUDA_DEVICE ConstReference At(size_t i, Args... args) const;
69 
70  CUBBYFLOW_CUDA_DEVICE Reference At(const CUDAStdArray<size_t, N>& idx);
71 
72  CUBBYFLOW_CUDA_DEVICE ConstReference
73  At(const CUDAStdArray<size_t, N>& idx) const;
74 
75  CUBBYFLOW_CUDA_DEVICE Reference operator[](size_t i);
76 
77  CUBBYFLOW_CUDA_DEVICE ConstReference operator[](size_t i) const;
78 
79  template <typename... Args>
80  CUBBYFLOW_CUDA_DEVICE Reference operator()(size_t i, Args... args);
81 
82  template <typename... Args>
83  CUBBYFLOW_CUDA_DEVICE ConstReference operator()(size_t i,
84  Args... args) const;
85 
86  CUBBYFLOW_CUDA_DEVICE Reference
87  operator()(const CUDAStdArray<size_t, N>& idx);
88 
89  CUBBYFLOW_CUDA_DEVICE ConstReference
90  operator()(const CUDAStdArray<size_t, N>& idx) const;
91 #else
92  CUBBYFLOW_CUDA_HOST HostReference At(size_t i);
93 
94  CUBBYFLOW_CUDA_HOST ValueType At(size_t i) const;
95 
96  template <typename... Args>
97  CUBBYFLOW_CUDA_HOST HostReference At(size_t i, Args... args);
98 
99  template <typename... Args>
100  CUBBYFLOW_CUDA_HOST ValueType At(size_t i, Args... args) const;
101 
102  CUBBYFLOW_CUDA_HOST HostReference At(const CUDAStdArray<size_t, N>& idx);
103 
104  CUBBYFLOW_CUDA_HOST ValueType At(const CUDAStdArray<size_t, N>& idx) const;
105 
106  CUBBYFLOW_CUDA_HOST HostReference operator[](size_t i);
107 
108  CUBBYFLOW_CUDA_HOST ValueType operator[](size_t i) const;
109 
110  template <typename... Args>
111  CUBBYFLOW_CUDA_HOST HostReference operator()(size_t i, Args... args);
112 
113  template <typename... Args>
114  CUBBYFLOW_CUDA_HOST ValueType operator()(size_t i, Args... args) const;
115 
116  CUBBYFLOW_CUDA_HOST HostReference
117  operator()(const CUDAStdArray<size_t, N>& idx);
118 
119  CUBBYFLOW_CUDA_HOST ValueType
120  operator()(const CUDAStdArray<size_t, N>& idx) const;
121 #endif
122 
123  protected:
124  CUBBYFLOW_CUDA_HOST_DEVICE CUDAArrayBase();
125 
126  CUBBYFLOW_CUDA_HOST_DEVICE CUDAArrayBase(const CUDAArrayBase& other);
127 
128  CUBBYFLOW_CUDA_HOST_DEVICE CUDAArrayBase(CUDAArrayBase&& other) noexcept;
129 
130  ~CUDAArrayBase() = default;
131 
132  CUBBYFLOW_CUDA_HOST_DEVICE CUDAArrayBase& operator=(
133  const CUDAArrayBase& other);
134 
135  CUBBYFLOW_CUDA_HOST_DEVICE CUDAArrayBase& operator=(
136  CUDAArrayBase&& other) noexcept;
137 
138  template <typename... Args>
139  CUBBYFLOW_CUDA_HOST_DEVICE void SetPtrAndSize(Pointer ptr, size_t ni,
140  Args... args);
141 
142  CUBBYFLOW_CUDA_HOST_DEVICE void SetPtrAndSize(Pointer data,
143  CUDAStdArray<size_t, N> size);
144 
145  CUBBYFLOW_CUDA_HOST_DEVICE void SwapPtrAndSize(CUDAArrayBase& other);
146 
147  CUBBYFLOW_CUDA_HOST_DEVICE void ClearPtrAndSize();
148 
149  Pointer m_ptr = nullptr;
150  CUDAStdArray<size_t, N> m_size;
151 
152  private:
153  template <typename... Args>
154  CUBBYFLOW_CUDA_HOST_DEVICE size_t IndexInternal(size_t d, size_t i,
155  Args... args) const;
156 
157  CUBBYFLOW_CUDA_HOST_DEVICE size_t IndexInternal(size_t, size_t i) const;
158 
159  template <size_t... I>
160  CUBBYFLOW_CUDA_HOST_DEVICE size_t IndexInternal(
161  const CUDAStdArray<size_t, N>& idx, std::index_sequence<I...>) const;
162 };
163 } // namespace CubbyFlow
164 
166 
167 #endif
168 
169 #endif
Definition: pybind11Utils.hpp:20