Chombo + EB  3.0
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 
24 ///An Iterator based on a BoxLayout object.
25 /**
26  An Iterator based on a BoxLayout object. It does not
27  support a dereferencing operation, since it is intended
28  to work with all of BoxLayouts, DisjointBoxLayouts, BoxLayoutDatas
29  LevelDatas, and any object that is built on top
30  of a BoxLayout object.
31 
32  DataIterator accesses the data in a BoxLayout-based
33  object in a <b> data-parallel </b> manner.
34  This means that it skips over entries for Boxes not
35  assigned to this processor. The order of access is not defined.
36 
37  In serial execution mode, there is no difference between
38  DataIterator and LayoutIterator.
39 */
40 class DataIterator
41 {
42  //Note: parallel version doesn't inherit LayoutIterator, serial version does
43 public:
44 
45  /// a null constructed DataIterator will return false on ok()
46  DataIterator();
47 
48  DataIterator(const BoxLayout& a_layout)
49  {
50  *this = a_layout.dataIterator();
51  }
52 
53  virtual ~DataIterator()
54  {}
55 
56  /// return the index that this iterator is at
57  /** Aborts if the iterator is not ok() */
58  inline const DataIndex& operator()() const ;
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  /// move the iterator to the next index in the layout
67  inline virtual void operator++();
68  /// move the iterator to the next index in the layout
69  void incr()
70  {
71  ++(*this);
72  }
73 
74  /// return true if this iterator is still in the layout
75  virtual bool ok() const;
76 
77  /// initialize this iterator to the first index in the layout
78  void begin();
79 
80  /// same as begin()
81  void reset();
82 
83  /// move this iterator to after the last index in the layout
84  /** The iterator will be !ok() afterwards. */
85  void end();
86 
87  int size() const
88  {
89  return m_indices->size();
90  }
91 
92 private:
93 
94  friend class BoxLayout;
95  friend class DisjointBoxLayout;
96  friend class TimedDataIterator;
97 
98  DataIterator(const BoxLayout& boxlayout, const int* layoutID);
99 
102  int m_current;
103 };
104 
106  : m_current(-1)
107 {
108 }
109 
110 inline const DataIndex& DataIterator::operator()() const
111 {
112  CH_assert(ok());
113  return m_indices->operator[](m_current);
114 }
115 
116 inline void DataIterator::operator++()
117 {
118  ++m_current;
119 }
120 
121 inline bool DataIterator::ok() const
122 {
123  return m_current < m_indices->size();
124 }
125 
126 inline void DataIterator::reset()
127 {
128  begin();
129 }
130 
131 inline void DataIterator::begin()
132 {
133  m_current=0;
134 }
135 
136 #else
137 
138 // serial version
139 
141 {
142 public:
143  virtual ~DataIterator()
144  {}
145 
147  {}
148 
149  DataIterator(const BoxLayout& a_layout)
150  {
151  *this = a_layout.dataIterator();
152  }
153 
154  /// return the index that this iterator is at
155  /** Aborts if the iterator is not ok() */
156  const DataIndex& operator()() const
157  {
158  return (const DataIndex&)(LayoutIterator::operator()());
159  };
160 
161  /// return a copy of the index that this iterator is at
162  /** Aborts if the iterator is not ok() */
163  DataIndex i() const
164  {
165  return this->operator()();
166  }
167 
168  int size() const
169  {
170  return this->m_layout.size();
171  }
172 
173 private:
174  friend class BoxLayout;
175  friend class DisjointBoxLayout;
176  friend class TimedDataIterator;
177 
178 protected:
179  DataIterator(const BoxLayout& boxlayout, const int* layoutID)
180  :LayoutIterator(boxlayout, layoutID)
181  {}
182 };
183 
184 #endif /*CH_MPI*/
185 
186 #define DATAITERATOR(CLASS, BOXLAYOUT) \
187  DataIterator dit = BOXLAYOUT .dataIterator(); \
188  for (dit.begin(); dit.ok(); ++dit) \
189  { \
190  DataIndex di = dit(); \
191  MT_BEGIN1(CLASS, DataIndex, di)
192 
193 #define ENDITERATOR(CLASS) \
194  MT_END1(CLASS, DataIndex, di) \
195  }
196 
197 #define DATAITERATOR1(CLASS, BOXLAYOUT, TYPE1, VAL1) \
198  DataIterator dit = BOXLAYOUT .dataIterator(); \
199  for (dit.begin(); dit.ok(); ++dit) \
200  { \
201  DataIndex di = dit(); \
202  MT_BEGIN2(CLASS, TYPE1, VAL1, DataIndex, di)
203 
204 #define ENDITERATOR1(CLASS, TYPE1, VAL1) \
205  MT_END2(CLASS, TYPE1, VAL1, DataIndex, di) \
206  }
207 
208 #define DATAITERATOR2(CLASS, BOXLAYOUT, TYPE1, VAL1, TYPE2, VAL2) \
209  DataIterator dit = BOXLAYOUT .dataIterator(); \
210  for (dit.begin(); dit.ok(); ++dit) \
211  { \
212  DataIndex di = dit(); \
213  MT_BEGIN3(CLASS, TYPE1, VAL1, TYPE2, VAL2, DataIndex, di)
214 
215 #define ENDITERATOR2(CLASS, TYPE1, VAL1, TYPE2, VAL2) \
216  MT_END3(CLASS, TYPE1, VAL1, TYPE2, VAL2, DataIndex, di) \
217  }
218 
219 #include "NamespaceFooter.H"
220 #endif
DataIterator(const BoxLayout &a_layout)
Definition: DataIterator.H:149
A reference-counting handle class.
Definition: RefCountedPtr.H:66
void incr()
move the iterator to the next Box in the layout
Definition: LayoutIterator.H:66
#define CH_assert(cond)
Definition: CHArray.H:37
const LayoutIndex & operator()() const
return the index that this iterator is at
Definition: LayoutIterator.H:99
A not-necessarily-disjoint collective of boxes.
Definition: BoxLayout.H:146
void end()
move this iterator to after the last Box in the layout
DataIterator(const BoxLayout &boxlayout, const int *layoutID)
Definition: DataIterator.H:179
int size() const
Definition: DataIterator.H:168
void begin()
initialize this iterator to the first Box in its Layout
Definition: LayoutIterator.H:115
int m_current
Definition: LayoutIterator.H:95
Definition: DataIterator.H:140
unsigned int size() const
Returns the total number of boxes in the BoxLayout.
An Iterator based on a BoxLayout object.
Definition: LayoutIterator.H:38
void reset()
same as begin()
DataIndex i() const
return a copy of the index that this iterator is at
Definition: DataIterator.H:163
BoxLayout m_layout
Definition: LayoutIterator.H:92
DataIterator()
Definition: DataIterator.H:146
virtual void operator++()
move the iterator to the next Box in the layout
Definition: LayoutIterator.H:105
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:31
DataIterator dataIterator() const
Parallel iterator.
Definition: DataIndex.H:112
virtual bool ok() const
return true if this iterator is still in its Layout
Definition: LayoutIterator.H:110
virtual ~DataIterator()
Definition: DataIterator.H:143
const DataIndex & operator()() const
return the index that this iterator is at
Definition: DataIterator.H:156
Definition: TimedDataIterator.H:23