CUDAArrayView-Impl.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_VIEW_IMPL_HPP
12 #define CUBBYFLOW_CUDA_ARRAY_VIEW_IMPL_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
16 namespace CubbyFlow
17 {
18 template <typename T, size_t N>
19 CUDAArrayView<T, N>::CUDAArrayView() : Base()
20 {
21  // Do nothing
22 }
23 
24 template <typename T, size_t N>
25 CUDAArrayView<T, N>::CUDAArrayView(T* ptr, const CUDAStdArray<size_t, N>& size)
26  : CUDAArrayView{}
27 {
28  Base::SetPtrAndSize(ptr, size);
29 }
30 
31 template <typename T, size_t N>
32 template <size_t M>
33 CUDAArrayView<T, N>::CUDAArrayView(
34  typename std::enable_if<(M == 1), T>::type* ptr, size_t size)
35  : CUDAArrayView(ptr, CUDAStdArray<size_t, N>{ size })
36 {
37  // Do nothing
38 }
39 
40 template <typename T, size_t N>
41 CUDAArrayView<T, N>::CUDAArrayView(CUDAArray<T, N>& other) : CUDAArrayView{}
42 {
43  Set(other);
44 }
45 
46 template <typename T, size_t N>
47 CUDAArrayView<T, N>::CUDAArrayView(const CUDAArrayView& other)
48 {
49  Set(other);
50 }
51 
52 template <typename T, size_t N>
53 CUDAArrayView<T, N>::CUDAArrayView(CUDAArrayView&& other) noexcept
54  : CUDAArrayView{}
55 {
56  *this = std::move(other);
57 }
58 
59 template <typename T, size_t N>
60 CUDAArrayView<T, N>& CUDAArrayView<T, N>::operator=(const CUDAArrayView& other)
61 {
62  Set(other);
63  return *this;
64 }
65 
66 template <typename T, size_t N>
67 CUDAArrayView<T, N>& CUDAArrayView<T, N>::operator=(
68  CUDAArrayView&& other) noexcept
69 {
70  Base::SetPtrAndSize(other.data(), other.Size());
71  other.SetPtrAndSize(nullptr, CUDAStdArray<size_t, N>{});
72  return *this;
73 }
74 
75 template <typename T, size_t N>
76 void CUDAArrayView<T, N>::Set(CUDAArray<T, N>& other)
77 {
78  Base::SetPtrAndSize(other.data(), other.Size());
79 }
80 
81 template <typename T, size_t N>
82 void CUDAArrayView<T, N>::Set(const CUDAArrayView& other)
83 {
84  Base::SetPtrAndSize(const_cast<T*>(other.data()), other.Size());
85 }
86 
87 template <typename T, size_t N>
88 void CUDAArrayView<T, N>::Fill(const T& val)
89 {
90  CUDAFill(data(), val);
91 }
92 
93 template <typename T, size_t N>
94 CUDAArrayView<const T, N>::CUDAArrayView() : Base()
95 {
96  // Do nothing
97 }
98 
99 template <typename T, size_t N>
100 CUDAArrayView<const T, N>::CUDAArrayView(const T* ptr,
101  const CUDAStdArray<size_t, N>& size)
102  : CUDAArrayView{}
103 {
104  Base::SetPtrAndSize(MemoryHandle(ptr), size);
105 }
106 
107 template <typename T, size_t N>
108 template <size_t M>
109 CUDAArrayView<const T, N>::CUDAArrayView(
110  const typename std::enable_if<(M == 1), T>::type* ptr, size_t size)
111  : CUDAArrayView(ptr, CUDAStdArray<size_t, N>{ size })
112 {
113  // Do nothing
114 }
115 
116 template <typename T, size_t N>
117 CUDAArrayView<const T, N>::CUDAArrayView(const CUDAArray<T, N>& other)
118  : CUDAArrayView{}
119 {
120  Set(other);
121 }
122 
123 template <typename T, size_t N>
124 CUDAArrayView<const T, N>::CUDAArrayView(const CUDAArrayView<T, N>& other)
125 {
126  Set(other);
127 }
128 
129 template <typename T, size_t N>
130 CUDAArrayView<const T, N>::CUDAArrayView(const CUDAArrayView& other)
131 {
132  Set(other);
133 }
134 
135 template <typename T, size_t N>
136 CUDAArrayView<const T, N>::CUDAArrayView(CUDAArrayView&& other) noexcept
137  : CUDAArrayView{}
138 {
139  *this = std::move(other);
140 }
141 
142 template <typename T, size_t N>
143 CUDAArrayView<const T, N>& CUDAArrayView<const T, N>::operator=(
144  const CUDAArrayView<T, N>& other)
145 {
146  Set(other);
147  return *this;
148 }
149 
150 template <typename T, size_t N>
151 CUDAArrayView<const T, N>& CUDAArrayView<const T, N>::operator=(
152  const CUDAArrayView& other)
153 {
154  Set(other);
155  return *this;
156 }
157 
158 template <typename T, size_t N>
159 CUDAArrayView<const T, N>& CUDAArrayView<const T, N>::operator=(
160  CUDAArrayView&& other) noexcept
161 {
162  Base::SetPtrAndSize(other.data(), other.Size());
163  other.SetPtrAndSize(nullptr, CUDAStdArray<size_t, N>{});
164  return *this;
165 }
166 
167 template <typename T, size_t N>
168 void CUDAArrayView<const T, N>::Set(const CUDAArray<T, N>& other)
169 {
170  Base::SetPtrAndSize(other.data(), other.Size());
171 }
172 
173 template <typename T, size_t N>
174 void CUDAArrayView<const T, N>::Set(const CUDAArrayView<T, N>& other)
175 {
176  Base::SetPtrAndSize(other.data(), other.Size());
177 }
178 
179 template <typename T, size_t N>
180 void CUDAArrayView<const T, N>::Set(const CUDAArrayView& other)
181 {
182  Base::SetPtrAndSize(other.data(), other.Size());
183 }
184 } // namespace CubbyFlow
185 
186 #endif
187 
188 #endif
void Set(Array< T, N > &other)
Definition: ArrayView-Impl.hpp:74
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
Pointer data()
Definition: ArrayBase-Impl.hpp:39
void SetPtrAndSize(Pointer ptr, size_t ni, Args... args)
Definition: ArrayBase-Impl.hpp:250