omega_h
Reliable mesh adaptation
Omega_h_array.hpp
1 #ifndef OMEGA_H_ARRAY_HPP
2 #define OMEGA_H_ARRAY_HPP
3 
4 #include <Omega_h_defines.hpp>
5 #include <Omega_h_fail.hpp>
6 #include <initializer_list>
7 #ifdef OMEGA_H_USE_KOKKOS
8 #include <Omega_h_kokkos.hpp>
9 #include <Omega_h_pool_kokkos.hpp>
10 #include <Omega_h_memory.hpp>
11 #else
12 #include <Omega_h_shared_alloc.hpp>
13 #include <memory> //shared_ptr
14 #include <string>
15 #endif
16 
17 namespace Omega_h {
18 
19 template <typename T>
20 T* nonnull(T* p);
21 
22 template <typename T>
23 class HostWrite;
24 
25 #ifdef OMEGA_H_USE_KOKKOS
26 template <typename T>
27 class KokkosViewWrapper {
28  public:
29  KokkosViewWrapper(size_t n, const std::string& name_in)
30  : view_(KokkosPool::getGlobalPool().allocateView<T>(n)),
31  label_(name_in) {}
32 
33  [[nodiscard]] auto label() const -> std::string { return label_; }
34 
35  [[nodiscard]] auto getView() const -> const View<T*>& {
36  return view_;
37  }
38 
39  ~KokkosViewWrapper() {
40  KokkosPool::getGlobalPool().deallocateView<T>(view_);
41  }
42 
43  View<T*> view_;
44  std::string label_;
45 };
46 
47 #endif
48 
49 template <typename T>
50 class Write {
51 #ifdef OMEGA_H_USE_KOKKOS
52  View<T*> view_;
53  SharedRef<KokkosViewWrapper<T>> manager_; // reference counting
54 #else
55  SharedAlloc shared_alloc_;
56 #endif
57 
58  public:
59  using value_type = T;
60  OMEGA_H_INLINE Write();
61 #ifdef OMEGA_H_USE_KOKKOS
62  Write(View<T*> view_in);
63 #endif
64  Write(LO size_in, std::string const& name = "");
65  Write(LO size_in, T value, std::string const& name = "");
66  Write(LO size_in, T offset, T stride, std::string const& name = "");
67  Write(std::initializer_list<T> l, std::string const& name = "");
68  Write(HostWrite<T> host_write);
69  OMEGA_H_INLINE LO size() const OMEGA_H_NOEXCEPT;
70  OMEGA_H_DEVICE T& operator[](LO i) const OMEGA_H_NOEXCEPT;
71  OMEGA_H_INLINE T* data() const noexcept;
72 #ifdef OMEGA_H_USE_KOKKOS
73  OMEGA_H_INLINE View<T*> const& view() const { return view_; }
74 #endif
75  void set(LO i, T value) const;
76  T get(LO i) const;
77  long use_count() const;
78  OMEGA_H_INLINE bool exists() const noexcept;
79 #ifdef OMEGA_H_USE_KOKKOS
80  std::string name() const;
81 #else
82  std::string const& name() const;
83 #endif
84  OMEGA_H_INLINE T* begin() const noexcept { return data(); }
85  OMEGA_H_INLINE T* end() const OMEGA_H_NOEXCEPT { return data() + size(); }
86 };
87 
88 template <typename T>
89 class Read {
90  Write<T> write_;
91 
92  public:
93  using value_type = T;
94  OMEGA_H_INLINE Read() {}
95  Read(Write<T> write);
96  Read(LO size, T value, std::string const& name = "");
97  Read(LO size, T offset, T stride, std::string const& name = "");
98  Read(std::initializer_list<T> l, std::string const& name = "");
99  OMEGA_H_INLINE LO size() const OMEGA_H_NOEXCEPT { return write_.size(); }
100  OMEGA_H_DEVICE T const& operator[](LO i) const OMEGA_H_NOEXCEPT {
101 #ifdef OMEGA_H_CHECK_BOUNDS
102  OMEGA_H_CHECK_OP(0, <=, i);
103  OMEGA_H_CHECK_OP(i, <, size());
104 #endif
105  return write_[i];
106  }
107  OMEGA_H_INLINE T const* data() const OMEGA_H_NOEXCEPT {
108  return write_.data();
109  }
110 #ifdef OMEGA_H_USE_KOKKOS
111  View<const T*> view() const;
112 #endif
113  T get(LO i) const;
114  T first() const;
115  T last() const;
116  OMEGA_H_INLINE bool exists() const OMEGA_H_NOEXCEPT {
117  return write_.exists();
118  }
119  std::string name() const { return write_.name(); }
120  OMEGA_H_INLINE T const* begin() const noexcept { return data(); }
121  OMEGA_H_INLINE T const* end() const noexcept { return data() + size(); }
122 };
123 
124 template <typename T>
125 Read<T> read(Write<T> a) {
126  return Read<T>(a);
127 }
128 
129 using Bytes = Read<Byte>;
130 using LOs = Read<LO>;
131 using GOs = Read<GO>;
132 using Reals = Read<Real>;
133 
134 template <typename T>
135 class HostRead {
136  Read<T> read_;
137 #if defined(OMEGA_H_USE_KOKKOS)
138  typename Kokkos::View<const T*, Kokkos::HostSpace> mirror_;
139 #endif
140  public:
141  using value_type = T;
142  HostRead() = default;
143  HostRead(Read<T> read);
144  LO size() const;
145  inline T const& operator[](LO i) const OMEGA_H_NOEXCEPT;
146  T const* data() const;
147  T get(LO i) const;
148  T last() const;
149  T const* begin() const noexcept { return data(); }
150  T const* end() const noexcept { return data() + size(); }
151 };
152 
153 template <typename T>
154 class HostWrite {
155  Write<T> write_;
156 #ifdef OMEGA_H_USE_KOKKOS
157  typename View<T*>::host_mirror_type mirror_;
158 #endif
159  public:
160  using value_type = T;
161  HostWrite() = default;
162  HostWrite(LO size_in, std::string const& name = "");
163 
176  HostWrite(LO size_in, T offset, T stride, std::string const& name = "");
177 
178  HostWrite(Write<T> write_in);
179  HostWrite(std::initializer_list<T> l, std::string const& name = "");
180  Write<T> write() const;
181  LO size() const OMEGA_H_NOEXCEPT;
182  inline T& operator[](LO i) const OMEGA_H_NOEXCEPT;
183  T* data() const;
184  OMEGA_H_INLINE bool exists() const OMEGA_H_NOEXCEPT {
185  return write_.exists();
186  }
187  void set(LO i, T value);
188  T get(LO i) const;
189  T* begin() const noexcept { return data(); }
190  T* end() const OMEGA_H_NOEXCEPT { return data() + size(); }
191 };
192 
193 template <typename T>
194 void fill(Write<T> a, T val);
195 template <typename T>
196 void fill_linear(Write<T> a, T offset, T stride);
197 template <class T>
198 void copy_into(Read<T> a, Write<T> b);
199 template <class T>
200 Write<T> deep_copy(Read<T> a, std::string const& name = "");
201 
202 /* begin explicit instantiation declarations */
203 #define OMEGA_H_EXPL_INST_DECL(T) \
204  extern template T* nonnull(T*); \
205  extern template T const* nonnull(T const*); \
206  extern template class Read<T>; \
207  extern template class Write<T>; \
208  extern template class HostRead<T>; \
209  extern template class HostWrite<T>; \
210  extern template void fill(Write<T> a, T val); \
211  extern template void fill_linear(Write<T> a, T, T); \
212  extern template void copy_into(Read<T> a, Write<T> b); \
213  extern template Write<T> deep_copy(Read<T> a, std::string const&);
214 OMEGA_H_EXPL_INST_DECL(I8)
215 OMEGA_H_EXPL_INST_DECL(I32)
216 OMEGA_H_EXPL_INST_DECL(I64)
217 OMEGA_H_EXPL_INST_DECL(Real)
218 #undef OMEGA_H_EXPL_INST_DECL
219 /* end explicit instantiation declarations */
220 
221 } // end namespace Omega_h
222 
223 #ifdef OMEGA_H_USE_KOKKOS
224 #include "Omega_h_array_kokkos.hpp"
225 #else
226 #include "Omega_h_array_default.hpp"
227 #endif
228 
229 #endif
Definition: Omega_h_array.hpp:135
Definition: Omega_h_array.hpp:154
Definition: Omega_h_array.hpp:89
Definition: Omega_h_memory.hpp:9
Definition: Omega_h_array.hpp:50
Definition: amr_mpi_test.cpp:6
Definition: Omega_h_shared_alloc.hpp:51
Definition: Omega_h_rbtree.hpp:1013