Block-Structured AMR Software Framework
AMReX_Mask.H
Go to the documentation of this file.
1 
2 #ifndef AMREX_MASK_H_
3 #define AMREX_MASK_H_
4 #include <AMReX_Config.H>
5 
6 #include <AMReX_BaseFab.H>
7 #include <AMReX_FArrayBox.H>
8 
9 namespace amrex {
10 
25 class Mask final
26  :
27  public BaseFab<int>
28 {
29 public:
30 
31  Mask () noexcept = default;
32 
33  explicit Mask (Arena* ar) noexcept;
34 
35  Mask (const Box& bx, int nc, Arena* ar);
36 
37  explicit Mask (const Box& bx,
38  int nc = 1,
39  bool alloc=true,
40  bool shared=false,
41  Arena* ar = nullptr);
42 
46  explicit Mask (std::istream& is);
47 
48  explicit Mask (Array4<int> const& a) noexcept : BaseFab<int>(a) {}
49 
50  explicit Mask (Array4<int> const& a, IndexType t) noexcept : BaseFab<int>(a,t) {}
51 
52  explicit Mask (Array4<int const> const& a) noexcept : BaseFab<int>(a) {}
53 
54  explicit Mask (Array4<int const> const& a, IndexType t) noexcept : BaseFab<int>(a,t) {}
55 
56  ~Mask () noexcept override = default;
57 
58  Mask (Mask&& rhs) noexcept = default;
59 
60  Mask (Mask const& rhs, MakeType make_type, int scomp, int ncomp);
61 
62  Mask (const Mask&) = delete;
63  Mask& operator= (const Mask&) = delete;
64  Mask& operator= (Mask&&) = delete;
65 
66 
71  friend std::istream& operator>> (std::istream&, Mask&);
72 
77  void readFrom (std::istream&);
78 
83  friend std::ostream& operator<< (std::ostream&, const Mask&);
84 
89  void writeOn (std::ostream&) const;
90  //
92 #if defined(AMREX_USE_GPU)
93  template <RunOn run_on>
94 #else
95  template <RunOn run_on=RunOn::Host>
96 #endif
97  Mask& operator&= (const Mask& src) noexcept { return And<run_on>(src); }
98 
104 #if defined(AMREX_USE_GPU)
105  template <RunOn run_on>
106 #else
107  template <RunOn run_on=RunOn::Host>
108 #endif
109  Mask& And (const Mask& src) noexcept;
110 
119 #if defined(AMREX_USE_GPU)
120  template <RunOn run_on>
121 #else
122  template <RunOn run_on=RunOn::Host>
123 #endif
124  Mask& And (const Mask& src,
125  int srccomp,
126  int destcomp,
127  int numcomp = 1) noexcept;
137 #if defined(AMREX_USE_GPU)
138  template <RunOn run_on>
139 #else
140  template <RunOn run_on=RunOn::Host>
141 #endif
142  Mask& And (const Mask& src,
143  const Box& subbox,
144  int srccomp,
145  int destcomp,
146  int numcomp = 1) noexcept;
158 #if defined(AMREX_USE_GPU)
159  template <RunOn run_on>
160 #else
161  template <RunOn run_on=RunOn::Host>
162 #endif
163  Mask& And (const Mask& src,
164  const Box& srcbox,
165  const Box& destbox,
166  int srccomp,
167  int destcomp,
168  int numcomp = 1) noexcept;
169 
171 #if defined(AMREX_USE_GPU)
172  template <RunOn run_on>
173 #else
174  template <RunOn run_on=RunOn::Host>
175 #endif
176  Mask& operator|= (const Mask& src) noexcept { return Or<run_on>(src); }
177 
183 #if defined(AMREX_USE_GPU)
184  template <RunOn run_on>
185 #else
186  template <RunOn run_on=RunOn::Host>
187 #endif
188  Mask& Or (const Mask& src) noexcept;
189 
198 #if defined(AMREX_USE_GPU)
199  template <RunOn run_on>
200 #else
201  template <RunOn run_on=RunOn::Host>
202 #endif
203  Mask& Or (const Mask& src,
204  int srccomp,
205  int destcomp,
206  int numcomp = 1) noexcept;
216 #if defined(AMREX_USE_GPU)
217  template <RunOn run_on>
218 #else
219  template <RunOn run_on=RunOn::Host>
220 #endif
221  Mask& Or (const Mask& src,
222  const Box& subbox,
223  int srccomp,
224  int destcomp,
225  int numcomp = 1) noexcept;
237 #if defined(AMREX_USE_GPU)
238  template <RunOn run_on>
239 #else
240  template <RunOn run_on=RunOn::Host>
241 #endif
242  Mask& Or (const Mask& src,
243  const Box& srcbox,
244  const Box& destbox,
245  int srccomp,
246  int destcomp,
247  int numcomp = 1) noexcept;
248 };
249 
250 template <RunOn run_on>
251 Mask&
252 Mask::And (const Mask& src) noexcept
253 {
254  return this->And<run_on>(src,domain,domain,0,0,nvar);
255 }
256 
257 template <RunOn run_on>
258 Mask&
259 Mask::And (const Mask& src,
260  int srccomp,
261  int destcomp,
262  int numcomp) noexcept
263 {
264  return this->And<run_on>(src,domain,domain,srccomp,destcomp,numcomp);
265 }
266 
267 template <RunOn run_on>
268 Mask&
269 Mask::And (const Mask& src,
270  const Box& subbox,
271  int srccomp,
272  int destcomp,
273  int numcomp) noexcept
274 {
275  return this->And<run_on>(src,subbox,subbox,srccomp,destcomp,numcomp);
276 }
277 
278 template <RunOn run_on>
279 Mask&
280 Mask::And (const Mask& src,
281  const Box& srcbox,
282  const Box& destbox,
283  int srccomp,
284  int destcomp,
285  int numcomp) noexcept
286 {
287  auto const& d = this->array();
288  auto const& s = src.const_array();
289  const auto dlo = amrex::lbound(destbox);
290  const auto slo = amrex::lbound(srcbox);
291  const Dim3 offset{slo.x-dlo.x,slo.y-dlo.y,slo.z-dlo.z};
292  AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
293  {
294  d(i,j,k,n+destcomp) = d(i,j,k,n+destcomp) ? s(i+offset.x,j+offset.y,k+offset.z,n+srccomp) : 0;
295  });
296  return *this;
297 }
298 
299 template <RunOn run_on>
300 Mask&
301 Mask::Or (const Mask& src) noexcept
302 {
303  return this->Or<run_on>(src,domain,domain,0,0,nvar);
304 }
305 
306 template <RunOn run_on>
307 Mask&
308 Mask::Or (const Mask& src,
309  int srccomp,
310  int destcomp,
311  int numcomp) noexcept
312 {
313  return this->Or<run_on>(src,domain,domain,srccomp,destcomp,numcomp);
314 }
315 
316 template <RunOn run_on>
317 Mask&
318 Mask::Or (const Mask& src,
319  const Box& subbox,
320  int srccomp,
321  int destcomp,
322  int numcomp) noexcept
323 {
324  return this->Or<run_on>(src,subbox,subbox,srccomp,destcomp,numcomp);
325 }
326 
327 template <RunOn run_on>
328 Mask&
329 Mask::Or (const Mask& src,
330  const Box& srcbox,
331  const Box& destbox,
332  int srccomp,
333  int destcomp,
334  int numcomp) noexcept
335 {
336  auto const& d = this->array();
337  auto const& s = src.const_array();
338  const auto dlo = amrex::lbound(destbox);
339  const auto slo = amrex::lbound(srcbox);
340  const Dim3 offset{slo.x-dlo.x,slo.y-dlo.y,slo.z-dlo.z};
341  AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(run_on, destbox, numcomp, i, j, k, n,
342  {
343  d(i,j,k,n+destcomp) = d(i,j,k,n+destcomp) ? 1: s(i+offset.x,j+offset.y,k+offset.z,n+srccomp);
344  });
345  return *this;
346 }
347 
348 }
349 
350 #endif /*_MASK_H_*/
#define AMREX_HOST_DEVICE_PARALLEL_FOR_4D_FLAG(where_to_run, box, nc, i, j, k, n, block)
Definition: AMReX_GpuLaunch.nolint.H:89
Array4< int const > offset
Definition: AMReX_HypreMLABecLap.cpp:1089
A virtual base class for objects that manage their own dynamic memory allocation.
Definition: AMReX_Arena.H:100
A FortranArrayBox(FAB)-like object.
Definition: AMReX_BaseFab.H:183
AMREX_FORCE_INLINE Array4< T const > const_array() const noexcept
Definition: AMReX_BaseFab.H:415
AMREX_FORCE_INLINE Array4< int const > array() const noexcept
Definition: AMReX_BaseFab.H:379
Box domain
My index space.
Definition: AMReX_BaseFab.H:1650
int nvar
Number components.
Definition: AMReX_BaseFab.H:1651
Definition: AMReX_Mask.H:28
Mask & Or(const Mask &src) noexcept
in-place Or
Definition: AMReX_Mask.H:301
Mask & And(const Mask &src) noexcept
in-place And
Definition: AMReX_Mask.H:252
Mask(Array4< int const > const &a, IndexType t) noexcept
Definition: AMReX_Mask.H:54
Mask & operator&=(const Mask &src) noexcept
in-place And operator
Definition: AMReX_Mask.H:97
~Mask() noexcept override=default
Mask(Array4< int > const &a, IndexType t) noexcept
Definition: AMReX_Mask.H:50
void writeOn(std::ostream &) const
output to stream, FAB-style
Definition: AMReX_Mask.cpp:78
Mask(Array4< int const > const &a) noexcept
Definition: AMReX_Mask.H:52
void readFrom(std::istream &)
initialize from stream, FAB-style
Definition: AMReX_Mask.cpp:88
Mask() noexcept=default
Mask & operator|=(const Mask &src) noexcept
in-place Or operator
Definition: AMReX_Mask.H:176
Definition: AMReX_Amr.cpp:49
MakeType
Definition: AMReX_MakeType.H:7
RunOn
Definition: AMReX_GpuControl.H:69
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 lbound(Array4< T > const &a) noexcept
Definition: AMReX_Array4.H:308
Definition: AMReX_Array4.H:61
void * alloc(std::size_t sz) const noexcept
Definition: AMReX_DataAllocator.H:16
Definition: AMReX_Dim3.H:12