Block-Structured AMR Software Framework
AMReX_GpuRange.H
Go to the documentation of this file.
1 #ifndef AMREX_GPU_RANGE_H_
2 #define AMREX_GPU_RANGE_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_GpuQualifiers.H>
6 #include <AMReX_GpuControl.H>
7 #include <AMReX_GpuError.H>
8 #include <AMReX_Box.H>
9 #include <AMReX_TypeTraits.H>
10 
11 namespace amrex {
12 
13 template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
14 bool isEmpty (T n) noexcept { return n <= 0; }
15 
16 template<int dim>
17 AMREX_FORCE_INLINE bool isEmpty (BoxND<dim> const& b) noexcept { return b.isEmpty(); }
18 
19 namespace Gpu {
20 
21 namespace range_detail {
22 
24 template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
26 Long size (T const& b) noexcept { return static_cast<Long>(b); }
27 
28 template <typename T, std::enable_if_t<std::is_integral_v<T>,int> = 0>
30 Long at (T const& /*b*/, Long offset) noexcept { return offset; }
31 
33 template<int dim>
35 AMREX_FORCE_INLINE Long size (BoxND<dim> const& b) noexcept
36 {
37  AMREX_IF_ON_DEVICE((return b.numPts();))
40  return 1;
41  ))
42 }
43 
44 template<int dim>
47 {
49  auto iv = b.atOffset(offset);
50  return BoxND<dim>(iv,iv,b.ixType());
51  ))
54  return b;
55  ))
56 }
57 
58 template <typename T>
59 struct range_impl
60 {
62  explicit range_impl (T const& b) noexcept : m_b(b), m_n(range_detail::size(b)) {}
63 
64 #ifdef AMREX_USE_SYCL
65  range_impl (T const& b, Long gid, Long grange) noexcept
66  : m_b(b), m_n(range_detail::size(b)), m_gid(gid), m_grange(grange) {}
67 #endif
68 
69  struct iterator
70  {
72  iterator (T const& b, Long i, Long s) noexcept : mi_b(&b), mi_i(i), mi_s(s) {}
73 
75  void operator++ () noexcept { mi_i += mi_s; }
76 
78  bool operator!= (iterator const& rhs) const noexcept { return mi_i < rhs.mi_i; }
79 
81  T operator* () const noexcept { return range_detail::at(*mi_b,mi_i); }
82 
83  private:
84  T const* mi_b;
85  Long mi_i;
86  Long mi_s;
87  };
88 
89  [[nodiscard]] AMREX_GPU_HOST_DEVICE
90  iterator begin () const noexcept {
91 #if defined (__SYCL_DEVICE_ONLY__)
92  return iterator(m_b, m_gid, m_grange);
93 #else
95  return iterator(m_b, blockDim.x*blockIdx.x+threadIdx.x, blockDim.x*gridDim.x);
96  ))
98  return iterator(m_b,0,1);
99  ))
100 #endif
101  }
102 
103  [[nodiscard]] AMREX_GPU_HOST_DEVICE
104  iterator end () const noexcept { return iterator(m_b,m_n,0); }
105 
106 private:
107  T m_b;
108  Long m_n;
109 #ifdef AMREX_USE_SYCL
110  Long m_gid;
111  Long m_grange;
112 #endif
113 };
114 }
115 
116 #ifdef AMREX_USE_SYCL
117 template <typename T>
118 range_detail::range_impl<T> Range (T const& b, Long gid, Long grange) noexcept {
119  return range_detail::range_impl<T>(b,gid,grange);
120 }
121 #endif
122 
123 template <typename T>
126 
127 }}
128 
129 #endif
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_IF_ON_DEVICE(CODE)
Definition: AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition: AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition: AMReX_GpuQualifiers.H:20
Array4< int const > offset
Definition: AMReX_HypreMLABecLap.cpp:1089
A Rectangular Domain on an Integer Lattice.
Definition: AMReX_Box.H:43
AMREX_GPU_HOST_DEVICE Long at(T const &, Long offset) noexcept
Definition: AMReX_GpuRange.H:30
AMREX_GPU_HOST_DEVICE Long size(T const &b) noexcept
integer version
Definition: AMReX_GpuRange.H:26
AMREX_GPU_HOST_DEVICE range_detail::range_impl< T > Range(T const &b) noexcept
Definition: AMReX_GpuRange.H:125
Definition: AMReX_Amr.cpp:49
bool isEmpty(T n) noexcept
Definition: AMReX_GpuRange.H:14
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition: AMReX.H:111
Definition: AMReX_GpuRange.H:70
AMREX_GPU_HOST_DEVICE void operator++() noexcept
Definition: AMReX_GpuRange.H:75
AMREX_GPU_HOST_DEVICE T operator*() const noexcept
Definition: AMReX_GpuRange.H:81
Long mi_i
Definition: AMReX_GpuRange.H:85
T const * mi_b
Definition: AMReX_GpuRange.H:84
AMREX_GPU_HOST_DEVICE iterator(T const &b, Long i, Long s) noexcept
Definition: AMReX_GpuRange.H:72
AMREX_GPU_HOST_DEVICE bool operator!=(iterator const &rhs) const noexcept
Definition: AMReX_GpuRange.H:78
Long mi_s
Definition: AMReX_GpuRange.H:86
Definition: AMReX_GpuRange.H:60
T m_b
Definition: AMReX_GpuRange.H:107
AMREX_GPU_HOST_DEVICE iterator end() const noexcept
Definition: AMReX_GpuRange.H:104
AMREX_GPU_HOST_DEVICE iterator begin() const noexcept
Definition: AMReX_GpuRange.H:90
Long m_n
Definition: AMReX_GpuRange.H:108
AMREX_GPU_HOST_DEVICE range_impl(T const &b) noexcept
Definition: AMReX_GpuRange.H:62