CUDAStdArray-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_STD_ARRAY_IMPL_HPP
12 #define CUBBYFLOW_CUDA_STD_ARRAY_IMPL_HPP
13 
14 #ifdef CUBBYFLOW_USE_CUDA
15 
16 namespace CubbyFlow
17 {
18 template <typename T, size_t N>
19 CUDAStdArray<T, N>::CUDAStdArray()
20 {
21  Fill(T{});
22 }
23 
24 template <typename T, size_t N>
25 template <typename... Args>
26 CUDAStdArray<T, N>::CUDAStdArray(ConstReference first, Args... rest)
27 {
28  static_assert(
29  sizeof...(Args) == N - 1,
30  "Number of arguments should be equal to the size of the vector.");
31  SetAt(0, first, rest...);
32 }
33 
34 template <typename T, size_t N>
35 CUDAStdArray<T, N>::CUDAStdArray(const std::array<T, N>& other)
36 {
37  for (size_t i = 0; i < N; ++i)
38  {
39  m_elements[i] = other[i];
40  }
41 }
42 
43 template <typename T, size_t N>
44 CUDAStdArray<T, N>::CUDAStdArray(const Vector<T, N>& other)
45 {
46  for (size_t i = 0; i < N; ++i)
47  {
48  m_elements[i] = other[i];
49  }
50 }
51 
52 template <typename T, size_t N>
53 CUDAStdArray<T, N>::CUDAStdArray(const CUDAStdArray& other)
54 {
55  for (size_t i = 0; i < N; ++i)
56  {
57  m_elements[i] = other[i];
58  }
59 }
60 
61 template <typename T, size_t N>
62 CUDAStdArray<T, N>::CUDAStdArray(CUDAStdArray&& other) noexcept
63 {
64  for (size_t i = 0; i < N; ++i)
65  {
66  m_elements[i] = std::move(other[i]);
67  }
68 }
69 
70 template <typename T, size_t N>
71 CUDAStdArray<T, N>& CUDAStdArray<T, N>::operator=(const CUDAStdArray& other)
72 {
73  for (size_t i = 0; i < N; ++i)
74  {
75  m_elements[i] = other[i];
76  }
77 
78  return *this;
79 }
80 
81 template <typename T, size_t N>
82 CUDAStdArray<T, N>& CUDAStdArray<T, N>::operator=(CUDAStdArray&& other) noexcept
83 {
84  for (size_t i = 0; i < N; ++i)
85  {
86  m_elements[i] = std::move(other[i]);
87  }
88 
89  return *this;
90 }
91 
92 template <typename T, size_t N>
93 void CUDAStdArray<T, N>::Fill(ConstReference val)
94 {
95  for (size_t i = 0; i < N; ++i)
96  {
97  m_elements[i] = val;
98  }
99 }
100 
101 template <typename T, size_t N>
102 CUBBYFLOW_CUDA_HOST Vector<T, N> CUDAStdArray<T, N>::ToVector() const
103 {
104  Vector<T, N> vec;
105 
106  for (size_t i = 0; i < N; ++i)
107  {
108  vec[i] = m_elements[i];
109  }
110 
111  return vec;
112 }
113 
114 template <typename T, size_t N>
115 typename CUDAStdArray<T, N>::Reference CUDAStdArray<T, N>::operator[](size_t i)
116 {
117  return m_elements[i];
118 }
119 
120 template <typename T, size_t N>
121 typename CUDAStdArray<T, N>::ConstReference CUDAStdArray<T, N>::operator[](
122  size_t i) const
123 {
124  return m_elements[i];
125 }
126 
127 template <typename T, size_t N>
128 bool CUDAStdArray<T, N>::operator==(const CUDAStdArray& other) const
129 {
130  for (size_t i = 0; i < N; ++i)
131  {
132  if (m_elements[i] != other.m_elements[i])
133  {
134  return false;
135  }
136  }
137 
138  return true;
139 }
140 
141 template <typename T, size_t N>
142 bool CUDAStdArray<T, N>::operator!=(const CUDAStdArray& other) const
143 {
144  return !(*this == other);
145 }
146 
147 template <typename T, size_t N>
148 template <typename... Args>
149 void CUDAStdArray<T, N>::SetAt(size_t i, ConstReference first, Args... rest)
150 {
151  m_elements[i] = first;
152  SetAt(i + 1, rest...);
153 }
154 
155 template <typename T, size_t N>
156 template <typename... Args>
157 void CUDAStdArray<T, N>::SetAt(size_t i, ConstReference first)
158 {
159  m_elements[i] = first;
160 }
161 } // namespace CubbyFlow
162 
163 #endif
164 
165 #endif
constexpr std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >), bool > operator==(const MatrixExpression< T, Rows, Cols, M1 > &a, const MatrixExpression< T, Rows, Cols, M2 > &b)
Definition: Matrix-Impl.hpp:1408
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
bool operator!=(const MatrixExpression< T, R1, C1, M1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1443