Chombo + EB  3.2
DataIterator.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _DATAITERATOR_H_
12 #define _DATAITERATOR_H_
13 
14 #include "Vector.H"
15 #include "DataIndex.H"
16 #include "BoxLayout.H"
17 #include "SPMD.H"
18 #include "LayoutIterator.H"
19 #include "NamespaceHeader.H"
20 
21 
22 #ifdef CH_MPI
23 ///An Iterator based on a BoxLayout object.
24 /**
25  An Iterator based on a BoxLayout object. It does not
26  support a dereferencing operation, since it is intended
27  to work with all of BoxLayouts, DisjointBoxLayouts, BoxLayoutDatas
28  LevelDatas, and any object that is built on top
29  of a BoxLayout object.
30 
31  DataIterator accesses the data in a BoxLayout-based
32  object in a <b> data-parallel </b> manner.
33  This means that it skips over entries for Boxes not
34  assigned to this processor. The order of access is not defined.
35 
36  In serial execution mode, there is no difference between
37  DataIterator and LayoutIterator.
38 */
39 class DataIterator
40 {
41  //Note: parallel version doesn't inherit LayoutIterator, serial version does
42 public:
43 
44  /// a null constructed DataIterator will return false on ok()
45  DataIterator();
46 
47  DataIterator(const BoxLayout& a_layout)
48  {
49  *this = a_layout.dataIterator();
50  }
51 
52  virtual ~DataIterator()
53  {}
54 
55  /// return the index that this iterator is at
56  /** Aborts if the iterator is not ok() */
57  const DataIndex& operator()() const ;
58 
59  /// return a copy of the index that this iterator is at
60  /** Aborts if the iterator is not ok() */
61  DataIndex i() const
62  {
63  return this->operator()();
64  }
65 
66  DataIndex operator[](int ivec) const
67  {
68  return (DataIndex)((*m_indices)[ivec]);
69  }
70 
71 
72  /// move the iterator to the next index in the layout
73  virtual void operator++();
74 
75  /// move the iterator to the next index in the layout
76  void incr()
77  {
78  ++(*this);
79  }
80 
81  /// return true if this iterator is still in the layout
82  virtual bool ok() const;
83 
84  /// initialize this iterator to the first index in the layout
85  void begin();
86 
87  /// same as begin()
88  void reset();
89 
90  /// move this iterator to after the last index in the layout
91  /** The iterator will be !ok() afterwards. */
92  void end();
93 
94  int size() const
95  {
96  return m_indices->size();
97  }
98 
99  ///functions for using measurements of time and memory for load balancing.
100 
101  ///sets m_time values to zero
102  void clearTime();
103 
104  //sets memory measurement to zero
105  void clearPeak();
106 
107  ///gets current time data
109  {
110  return m_time;
111  }
112 
113  ///gets current memory data
115  {
116  return m_peak;
117  }
118 
120  {
121  return m_layout.boxArray();
122  }
123 
124  ///enables timing. does not set to zero. use clear time for that
125  void enableTime()
126  {
127  //only defines if not defined before
128  defineTime();
129  m_timeEnabled = true;
130  }
131 
132  ///enables measurements based on how much peak memory moves
133  void enablePeak()
134  {
135  //only defines if not defined before
136  definePeak();
137  m_peakEnabled = true;
138  }
139 
140  ///turns off timing
141  void disableTime()
142  {
143  m_timeEnabled = false;
144  }
145 
146  ///turns off memory measurement
147  void disablePeak()
148  {
149  m_peakEnabled = false;
150  }
151 
152  /// After you are finished timing your local elements, call mergeTimes to fill-in off-processor times.
153  void mergeTime();
154 
155  /// After you are finished measuring the memory your local elements, call mergeTimes to fill-in off-processor times.
156  void mergePeak();
157 
158 private:
159 
160  friend class BoxLayout;
161  friend class DisjointBoxLayout;
162  friend class TimedDataIterator;
163 
164  DataIterator(const BoxLayout& boxlayout, const int* layoutID);
165 
167  // RefCountedPtr<Vector<DataIndex> > m_indices;
168 protected:
169  const Vector<DataIndex>* m_indices;
170 
171  int m_current;
172 
173  void defineTime();
174  void definePeak();
175 
178 
179  bool m_timeEnabled;
180  bool m_timeDefined;
181 
182  bool m_peakEnabled;
183  bool m_peakDefined;
184  unsigned long long m_startTime;
185  unsigned long long m_startPeak;
186 };
187 #else
188 // serial version
189 
191 {
192 public:
193  virtual ~DataIterator()
194  {}
195 
197  {}
198 
199  DataIterator(const BoxLayout& a_layout)
200  {
201  *this = a_layout.dataIterator();
202  }
203 
204  /// return the index that this iterator is at
205  /** Aborts if the iterator is not ok() */
206  const DataIndex& operator()() const
207  {
208  return (const DataIndex&)(LayoutIterator::operator()());
209  };
210 
211  /// return a copy of the index that this iterator is at
212  /** Aborts if the iterator is not ok() */
213  DataIndex i() const
214  {
215  return this->operator()();
216  }
217 
218  int size() const
219  {
220  return this->m_layout.size();
221  }
222 
223  DataIndex operator[](int ivec) const
224  {
225  return (DataIndex)((*m_indicies)[ivec]);
226  }
227 
228 
229  ///load balancing function. no-op in serial
230  void clearTime()
231  {
232  }
233 
234  ///load balancing function. no-op in serial
235  void clearPeak()
236  {
237  }
238 
239  ///load balancing function. no-op in serial
241  {
243  return retval;
244  }
245 
247  {
249  return retval;
250  }
251 
253  {
254  return m_layout.boxArray();
255  }
256 
257  ///load balancing function. no-op in serial
258  void enableTime()
259  {
260  }
261 
262  void enablePeak()
263  {
264  }
265 
266  ///load balancing function. no-op in serial
267  void disableTime()
268  {
269  }
270 
271  void disablePeak()
272  {
273  }
274 
275  ///load balancing function. no-op in serial
276  void mergeTime()
277  {
278  }
279 
280  void mergePeak()
281  {
282  }
283 private:
284  friend class BoxLayout;
285  friend class DisjointBoxLayout;
286  friend class TimedDataIterator;
287 
288 protected:
289  DataIterator(const BoxLayout& boxlayout, const int* layoutID)
290  :LayoutIterator(boxlayout, layoutID)
291  {}
292 };
293 
294 
295 
296 #define DATAITERATOR(CLASS, BOXLAYOUT) \
297  DataIterator dit = BOXLAYOUT .dataIterator(); \
298  for (dit.begin(); dit.ok(); ++dit) \
299  { \
300  DataIndex di = dit(); \
301  MT_BEGIN1(CLASS, DataIndex, di)
302 
303 #define ENDITERATOR(CLASS) \
304  MT_END1(CLASS, DataIndex, di) \
305  }
306 
307 #define DATAITERATOR1(CLASS, BOXLAYOUT, TYPE1, VAL1) \
308  DataIterator dit = BOXLAYOUT .dataIterator(); \
309  for (dit.begin(); dit.ok(); ++dit) \
310  { \
311  DataIndex di = dit(); \
312  MT_BEGIN2(CLASS, TYPE1, VAL1, DataIndex, di)
313 
314 #define ENDITERATOR1(CLASS, TYPE1, VAL1) \
315  MT_END2(CLASS, TYPE1, VAL1, DataIndex, di) \
316  }
317 
318 #define DATAITERATOR2(CLASS, BOXLAYOUT, TYPE1, VAL1, TYPE2, VAL2) \
319  DataIterator dit = BOXLAYOUT .dataIterator(); \
320  for (dit.begin(); dit.ok(); ++dit) \
321  { \
322  DataIndex di = dit(); \
323  MT_BEGIN3(CLASS, TYPE1, VAL1, TYPE2, VAL2, DataIndex, di)
324 
325 #define ENDITERATOR2(CLASS, TYPE1, VAL1, TYPE2, VAL2) \
326  MT_END3(CLASS, TYPE1, VAL1, TYPE2, VAL2, DataIndex, di) \
327  }
328 
329 #endif
330 
331 #include "NamespaceFooter.H"
332 #endif
DataIterator(const BoxLayout &a_layout)
Definition: DataIterator.H:199
Vector< Box > getBoxes()
Definition: DataIterator.H:252
void enableTime()
load balancing function. no-op in serial
Definition: DataIterator.H:258
Vector< Box > boxArray() const
void incr()
move the iterator to the next Box in the layout
Definition: LayoutIterator.H:63
A not-necessarily-disjoint collective of boxes.
Definition: BoxLayout.H:145
const LayoutIndex & operator()() const
return the index that this iterator is at
Definition: LayoutIterator.H:106
DataIndex operator[](int ivec) const
Definition: DataIterator.H:223
void end()
move this iterator to after the last Box in the layout
DataIterator(const BoxLayout &boxlayout, const int *layoutID)
Definition: DataIterator.H:289
void clearPeak()
load balancing function. no-op in serial
Definition: DataIterator.H:235
unsigned int size() const
Returns the total number of boxes in the BoxLayout.
void disableTime()
load balancing function. no-op in serial
Definition: DataIterator.H:267
void begin()
initialize this iterator to the first Box in its Layout
Definition: LayoutIterator.H:122
virtual bool ok() const
return true if this iterator is still in its Layout
Definition: LayoutIterator.H:117
Definition: DataIterator.H:190
An Iterator based on a BoxLayout object.
Definition: LayoutIterator.H:35
void clearTime()
load balancing function. no-op in serial
Definition: DataIterator.H:230
void enablePeak()
Definition: DataIterator.H:262
void reset()
same as begin()
void mergePeak()
Definition: DataIterator.H:280
unsigned long long m_startTime
Definition: TimedDataIterator.H:79
int size() const
Definition: DataIterator.H:218
unsigned int m_current
Definition: LayoutIterator.H:103
void mergeTime()
load balancing function. no-op in serial
Definition: DataIterator.H:276
BoxLayout m_layout
Definition: LayoutIterator.H:100
DataIterator()
Definition: DataIterator.H:196
virtual void operator++()
move the iterator to the next Box in the layout
Definition: LayoutIterator.H:112
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:30
Vector< unsigned long long > getTime() const
load balancing function. no-op in serial
Definition: DataIterator.H:240
const DataIndex & operator()() const
return the index that this iterator is at
Definition: DataIterator.H:206
Vector< unsigned long long > m_time
Definition: TimedDataIterator.H:76
Definition: DataIndex.H:112
DataIndex i() const
return a copy of the index that this iterator is at
Definition: DataIterator.H:213
Vector< unsigned long long > getPeak() const
Definition: DataIterator.H:246
DataIterator dataIterator() const
Parallel iterator.
virtual ~DataIterator()
Definition: DataIterator.H:193
bool m_timeDefined
Definition: TimedDataIterator.H:78
Definition: TimedDataIterator.H:23
void disablePeak()
Definition: DataIterator.H:271
bool m_timeEnabled
Definition: TimedDataIterator.H:77