Block-Structured AMR Software Framework
AMReX_MFIter.H
Go to the documentation of this file.
1 #ifndef BL_MFITER_H_
2 #define BL_MFITER_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_FabArrayBase.H>
6 
7 #include <memory>
8 
9 namespace amrex {
10 
11 #ifdef AMREX_USE_GPU
12  inline bool TilingIfNotGPU () noexcept { return Gpu::notInLaunchRegion(); }
13 #else
14  inline constexpr bool TilingIfNotGPU () noexcept { return true; }
15 #endif
16 
17 template<class T> class FabArray;
18 
19 struct MFItInfo
20 {
21  bool do_tiling{false};
22  bool dynamic{false};
26  MFItInfo () noexcept
28  tilesize(IntVect::TheZeroVector()) {}
30  do_tiling = true;
31  tilesize = ts;
32  return *this;
33  }
34  MFItInfo& SetDynamic (bool f) noexcept {
35  dynamic = f;
36  return *this;
37  }
38  MFItInfo& DisableDeviceSync () noexcept {
39  device_sync = false;
40  return *this;
41  }
42  MFItInfo& SetDeviceSync (bool f) noexcept {
43  device_sync = f;
44  return *this;
45  }
46  MFItInfo& SetNumStreams (int n) noexcept {
47  num_streams = n;
48  return *this;
49  }
50  MFItInfo& UseDefaultStream () noexcept {
51  num_streams = 1;
52  return *this;
53  }
54 };
55 
56 class MFIter
57 {
58 public:
60  enum Flags {
62  Tiling = 0x01,
68  AllBoxes = 0x02,
70  NoTeamBarrier = 0x04
71  };
72 
78  explicit MFIter (const FabArrayBase& fabarray,
79  unsigned char flags_=0);
80 
82  MFIter (const FabArrayBase& fabarray,
83  bool do_tiling);
84 
86  MFIter (const FabArrayBase& fabarray,
87  const IntVect& tilesize,
88  unsigned char flags_=0);
89 
90  MFIter (const BoxArray& ba, const DistributionMapping& dm, unsigned char flags_=0);
91 
92  MFIter (const BoxArray& ba, const DistributionMapping& dm, bool do_tiling);
93 
94  MFIter (const BoxArray& ba, const DistributionMapping& dm,
95  const IntVect& tilesize, unsigned char flags_=0);
96 
97  MFIter (const FabArrayBase& fabarray, const MFItInfo& info);
98 
99  MFIter (const BoxArray& ba, const DistributionMapping& dm, const MFItInfo& info);
100 
101  MFIter (MFIter&& rhs) = default;
102  MFIter (MFIter const&) = delete;
103  MFIter& operator= (MFIter const&) = delete;
104  MFIter& operator= (MFIter &&) = delete;
105 
106  // dtor
107  ~MFIter ();
108 
110  [[nodiscard]] Box tilebox () const noexcept;
111 
113  [[nodiscard]] Box tilebox (const IntVect& nodal) const noexcept;
114 
116  [[nodiscard]] Box tilebox (const IntVect& nodal, const IntVect& ngrow) const noexcept;
117 
119  [[nodiscard]] Box nodaltilebox (int dir=-1) const noexcept;
120 
122  [[nodiscard]] Box growntilebox (int ng=-1000000) const noexcept;
123 
124  [[nodiscard]] Box growntilebox (const IntVect& ng) const noexcept;
125 
127  [[nodiscard]] Box grownnodaltilebox (int dir=-1, int ng=-1000000) const noexcept;
128 
129  [[nodiscard]] Box grownnodaltilebox (int dir, const IntVect& ng) const noexcept;
130 
132  [[nodiscard]] Box validbox () const noexcept { return fabArray->box((*index_map)[currentIndex]); }
133 
135  [[nodiscard]] Box fabbox () const noexcept { return fabArray->fabbox((*index_map)[currentIndex]); }
136 
138  void operator++ () noexcept;
139 
141  [[nodiscard]] bool isValid () const noexcept { return currentIndex < endIndex; }
142 
144  [[nodiscard]] int index () const noexcept { return (*index_map)[currentIndex]; }
145 
147  [[nodiscard]] int length () const noexcept { return (endIndex - beginIndex); }
148 
150  [[nodiscard]] int LocalTileIndex () const noexcept {return local_tile_index_map ? (*local_tile_index_map)[currentIndex] : 0;}
151 
153  [[nodiscard]] int numLocalTiles() const noexcept {return num_local_tiles ? (*num_local_tiles)[currentIndex] : 1;}
154 
159  [[nodiscard]] int LocalIndex () const noexcept { return local_index_map ? (*local_index_map)[currentIndex] : currentIndex; }
160 
162  [[nodiscard]] const FabArrayBase& theFabArrayBase () const noexcept { return *fabArray; }
163 
164  [[nodiscard]] int tileIndex () const noexcept {return currentIndex;}
165 
166  [[nodiscard]] const DistributionMapping& DistributionMap () const noexcept { return fabArray->DistributionMap(); }
167 
168  static int allowMultipleMFIters (int allow);
169 
170  static int currentDepth ();
171 
172  void Finalize ();
173 
174 protected:
175 
176  std::unique_ptr<FabArrayBase> m_fa;
177 
179 
181 
182  unsigned char flags;
185  int endIndex;
186  int streams;
188 
189  bool dynamic;
190  bool finalized = false;
191 
192  struct DeviceSync {
193  DeviceSync (bool f) : flag(f) {}
194  DeviceSync (DeviceSync&& rhs) noexcept : flag(std::exchange(rhs.flag,false)) {}
195  ~DeviceSync () = default;
196  DeviceSync (DeviceSync const&) = delete;
197  DeviceSync& operator= (DeviceSync const&) = delete;
199  explicit operator bool() const noexcept { return flag; }
200  bool flag = true;
201  };
203 
209 
211  static AMREX_EXPORT int depth;
213 
214  void Initialize ();
215 };
216 
219 inline bool isMFIterSafe (const FabArrayBase& x, const FabArrayBase& y) {
220  return x.DistributionMap() == y.DistributionMap()
221  && BoxArray::SameRefs(x.boxArray(), y.boxArray());
222 }
223 
224 }
225 
226 #endif
#define AMREX_EXPORT
Definition: AMReX_Extension.H:191
A collection of Boxes stored in an Array.
Definition: AMReX_BoxArray.H:549
static bool SameRefs(const BoxArray &lhs, const BoxArray &rhs)
whether two BoxArrays share the same data
Definition: AMReX_BoxArray.H:819
Calculates the distribution of FABs to MPI processes.
Definition: AMReX_DistributionMapping.H:41
Base class for FabArray.
Definition: AMReX_FabArrayBase.H:41
static AMREX_EXPORT IntVect mfiter_tile_size
Default tilesize in MFIter.
Definition: AMReX_FabArrayBase.H:288
const BoxArray & boxArray() const noexcept
Return a constant reference to the BoxArray that defines the valid region associated with this FabArr...
Definition: AMReX_FabArrayBase.H:94
const DistributionMapping & DistributionMap() const noexcept
Return constant reference to associated DistributionMapping.
Definition: AMReX_FabArrayBase.H:130
Box box(int K) const noexcept
Return the Kth Box in the BoxArray. That is, the valid region of the Kth grid.
Definition: AMReX_FabArrayBase.H:100
Box fabbox(int K) const noexcept
Return the Kth FABs Box in the FabArray. That is, the region the Kth fab is actually defined on.
Definition: AMReX_MFIter.H:57
int LocalTileIndex() const noexcept
The current local tile index in the current grid;.
Definition: AMReX_MFIter.H:150
static AMREX_EXPORT int nextDynamicIndex
Definition: AMReX_MFIter.H:210
const Vector< Box > * tile_array
Definition: AMReX_MFIter.H:206
unsigned char flags
Definition: AMReX_MFIter.H:182
IndexType typ
Definition: AMReX_MFIter.H:187
bool dynamic
Definition: AMReX_MFIter.H:189
Box growntilebox(int ng=-1000000) const noexcept
Return the tile box at the current index grown to include ghost cells.
Definition: AMReX_MFIter.cpp:472
Box fabbox() const noexcept
Return the Box of the FAB at which we currently point.
Definition: AMReX_MFIter.H:135
int length() const noexcept
The number of indices.
Definition: AMReX_MFIter.H:147
bool finalized
Definition: AMReX_MFIter.H:190
static int allowMultipleMFIters(int allow)
Definition: AMReX_MFIter.cpp:14
int tileIndex() const noexcept
Definition: AMReX_MFIter.H:164
int endIndex
Definition: AMReX_MFIter.H:185
Box tilebox() const noexcept
Return the tile Box at the current index.
Definition: AMReX_MFIter.cpp:385
static AMREX_EXPORT int depth
Definition: AMReX_MFIter.H:211
const DistributionMapping & DistributionMap() const noexcept
Definition: AMReX_MFIter.H:166
MFIter(MFIter const &)=delete
DeviceSync device_sync
Definition: AMReX_MFIter.H:202
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition: AMReX_MFIter.H:141
int streams
Definition: AMReX_MFIter.H:186
int beginIndex
Definition: AMReX_MFIter.H:184
MFIter(MFIter &&rhs)=default
const Vector< int > * local_tile_index_map
Definition: AMReX_MFIter.H:207
Box validbox() const noexcept
Return the valid Box in which the current tile resides.
Definition: AMReX_MFIter.H:132
Box nodaltilebox(int dir=-1) const noexcept
Return the dir-nodal (or all nodal if dir<0) Box at the current index.
Definition: AMReX_MFIter.cpp:444
void Finalize()
Definition: AMReX_MFIter.cpp:227
const FabArrayBase & theFabArrayBase() const noexcept
Constant reference to FabArray over which we're iterating.
Definition: AMReX_MFIter.H:162
const Vector< int > * local_index_map
Definition: AMReX_MFIter.H:205
const Vector< int > * num_local_tiles
Definition: AMReX_MFIter.H:208
int index() const noexcept
The index into the underlying BoxArray of the current FAB.
Definition: AMReX_MFIter.H:144
MFIter(const FabArrayBase &fabarray, unsigned char flags_=0)
The default constructor does not enable tiling (flags_ is set to 0 by default). However,...
Definition: AMReX_MFIter.cpp:31
~MFIter()
Definition: AMReX_MFIter.cpp:221
static AMREX_EXPORT int allow_multiple_mfiters
Definition: AMReX_MFIter.H:212
MFIter & operator=(MFIter const &)=delete
int LocalIndex() const noexcept
Return local index into the vector of fab pointers, m_fabs_v When AllBoxes is on, local_index_map is ...
Definition: AMReX_MFIter.H:159
int currentIndex
Definition: AMReX_MFIter.H:183
const Vector< int > * index_map
Definition: AMReX_MFIter.H:204
static int currentDepth()
Definition: AMReX_MFIter.cpp:21
void Initialize()
Definition: AMReX_MFIter.cpp:274
std::unique_ptr< FabArrayBase > m_fa
This must be the first member!
Definition: AMReX_MFIter.H:176
Box grownnodaltilebox(int dir=-1, int ng=-1000000) const noexcept
Return the dir-nodal (or all nodal if dir<0) box grown to include ghost cells.
Definition: AMReX_MFIter.cpp:506
IntVect tile_size
Definition: AMReX_MFIter.H:180
void operator++() noexcept
Increment iterator to the next tile we own.
Definition: AMReX_MFIter.cpp:522
Flags
Flags that specify tiling modes. All these flags are off by default.
Definition: AMReX_MFIter.H:60
@ Tiling
Tiling: Enabling the tiling mode.
Definition: AMReX_MFIter.H:62
@ AllBoxes
AllBoxes: If on, all threads/workers loop over all boxes without tiling. This essentially loops over ...
Definition: AMReX_MFIter.H:68
@ NoTeamBarrier
NoTeamBarrier: This option is for Team only. If on, there is no barrier in MFIter dtor.
Definition: AMReX_MFIter.H:70
int numLocalTiles() const noexcept
The the number of tiles in the current grid;.
Definition: AMReX_MFIter.H:153
const FabArrayBase * fabArray
Definition: AMReX_MFIter.H:178
bool notInLaunchRegion() noexcept
Definition: AMReX_GpuControl.H:87
int numGpuStreams() noexcept
Definition: AMReX_GpuDevice.H:225
bool inNoSyncRegion() noexcept
Definition: AMReX_GpuControl.H:146
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:44
Definition: AMReX_Amr.cpp:49
bool isMFIterSafe(const FabArrayBase &x, const FabArrayBase &y)
Definition: AMReX_MFIter.H:219
bool TilingIfNotGPU() noexcept
Definition: AMReX_MFIter.H:12
Definition: AMReX_MFIter.H:20
int num_streams
Definition: AMReX_MFIter.H:24
MFItInfo & SetDeviceSync(bool f) noexcept
Definition: AMReX_MFIter.H:42
MFItInfo & SetNumStreams(int n) noexcept
Definition: AMReX_MFIter.H:46
MFItInfo & EnableTiling(const IntVect &ts=FabArrayBase::mfiter_tile_size) noexcept
Definition: AMReX_MFIter.H:29
MFItInfo & SetDynamic(bool f) noexcept
Definition: AMReX_MFIter.H:34
MFItInfo() noexcept
Definition: AMReX_MFIter.H:26
MFItInfo & UseDefaultStream() noexcept
Definition: AMReX_MFIter.H:50
IntVect tilesize
Definition: AMReX_MFIter.H:25
bool do_tiling
Definition: AMReX_MFIter.H:21
MFItInfo & DisableDeviceSync() noexcept
Definition: AMReX_MFIter.H:38
bool device_sync
Definition: AMReX_MFIter.H:23
bool dynamic
Definition: AMReX_MFIter.H:22
Definition: AMReX_MFIter.H:192
DeviceSync(DeviceSync &&rhs) noexcept
Definition: AMReX_MFIter.H:194
DeviceSync(bool f)
Definition: AMReX_MFIter.H:193
DeviceSync & operator=(DeviceSync const &)=delete
DeviceSync(DeviceSync const &)=delete
bool flag
Definition: AMReX_MFIter.H:200