OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
BrickIterator.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef BRICK_ITERATOR_H
12 #define BRICK_ITERATOR_H
13 
14 // include files
15 #include "Utility/Vec.h"
16 #include "Utility/RefCounted.h"
17 #include "Utility/Pooled.h"
18 #include "Message/Message.h"
19 #include "PETE/IpplExpressions.h"
20 
21 // forward declarations
22 template <unsigned Dim> class NDIndex;
23 
24 //----------------------------------------------------------------------
25 // A set of counters for a brick iterator.
26 template<unsigned Dim>
28 {
29 public:
30 
31  // Null ctor takes no action.
33 
34  // Construct to count over a contiguous block of user allocated memory.
35  BrickCounter(const NDIndex<Dim>&);
36 
37  // Go to the next element.
38  BrickCounter& operator++() { op_pp(); return *this; }
39 
40  // Finished stepping in dimension d?
41  bool done(unsigned d) const { return Counters[d] == Counts[d]; }
42 
43  // Step in dimension d.
44  void step(unsigned d) { Counters[d] += 1; }
45  void rewind(unsigned d) { Counters[d] = 0; }
46 
47  // How big is it in dimension d?
48  inline int size(unsigned d) const { return Counts[d]; }
49 
50  // Where are we now?
51  int GetOffset(unsigned d) const { return Counters[d]; }
52 
53 protected:
54  void op_pp();
55  vec<int,Dim> Counters; // Where are we now.
56  vec<int,Dim> Counts; // The number of elements in each direction.
57 
58 };
59 
60 
61 //----------------------------------------------------------------------
62 // An iterator for the elements of a brick of data.
63 template<class T, unsigned Dim>
64 class BrickIterator : public BrickCounter<Dim>
65 {
66 public:
67 
68  // Null ctor fills the current ptr with null.
69  BrickIterator() : Current(0), Whole(true) {}
70 
71  // Construct w/ an alloc ptr, Counted NDIndex and Alloc NDIndex
72  BrickIterator(T*, const NDIndex<Dim>&, const NDIndex<Dim>&);
73 
74  // Construct with ptr, sizes.
75  BrickIterator(T*, const vec<int,Dim>&);
76 
77  // Go to the next element.
78  BrickIterator& operator++() { op_pp(); return *this; }
79 
80  // Does this iterator iterate over the entire (whole) brick?
81  bool whole() const { return Whole; }
82 
83  // Two iterators are the same if they point to the same thing.
84  bool operator==(const BrickIterator<T,Dim>& a) const
85  {
86  return Current == a.Current;
87  }
88  bool operator!=(const BrickIterator<T,Dim>& a) const
89  {
90  return Current != a.Current;
91  }
92 
93  // Return what youre pointing to.
94  T& operator*() const
95  {
96  return *Current;
97  }
98 
99  // Move to new places.
100  void step(unsigned d)
101  {
103  Current += Strides[ d ];
104  }
105  void rewind(unsigned d)
106  {
109  }
110 
111  // Return something given an offset in one, two or three dimensions.
112  T& offset(int i) const
113  {
114  return Current[ i*Strides[0] ];
115  }
116  T& offset(int i, int j) const
117  {
118  return Current[ i*Strides[0] + j*Strides[1] ];
119  }
120  T& offset(int i,int j,int k) const
121  {
122  return Current[ i*Strides[0]+ j*Strides[1] + k*Strides[2] ];
123  }
124  T& offset(int *i) const
125  {
126  return Current[ vec<int,Dim>::dot(i,&Strides[0]) ];
127  }
128  T& unit_offset(int i) const
129  {
130  return Current[ i ];
131  }
132  T& unit_offset(int i, int j) const
133  {
134  return Current[ i + j*Strides[1] ];
135  }
136  T& unit_offset(int i,int j,int k) const
137  {
138  return Current[ i+ j*Strides[1] + k*Strides[2] ];
139  }
140  void moveBy(int i)
141  {
142  Current += i*Strides[0];
144  }
145  void moveBy(int i, int j)
146  {
147  Current += (i*Strides[0] + j*Strides[1]);
150  }
151  void moveBy(int i, int j, int k)
152  {
153  Current += (i*Strides[0] + j*Strides[1] + k*Strides[2]);
157  }
158  void moveBy(const int *i)
159  {
160  for (unsigned int d=0; d < Dim; ++d) {
161  Current += i[d] * Strides[d];
162  BrickCounter<Dim>::Counters[d] += i[d];
163  }
164  }
165  int Stride(int i) const { return Strides[i]; }
166 
167  // message passing interface ... for putMessage, the second argument
168  // is used for an optimization for when the entire brick is being
169  // sent. If that is so, do not copy data into a new buffer, just
170  // put the pointer into the message. USE WITH CARE. The default is
171  // tohave putMessage make a copy of the data in the brick before adding
172  // it to the message. In many situations, this is required since the
173  // data referred to by the iterator is not contiguous. getMessage
174  // has no such option, it always does the most efficient thing it can.
175  Message& putMessage(Message&, bool makecopy = true);
177 
178  // PETE interface
179  typedef T PETE_Return_t;
181  PETE_Expr_t MakeExpression() const { return *this; }
182 
183 protected:
184  void op_pp();
185  T* __restrict__ Current; // The current datum.
186  vec<int,Dim> Strides; // The strides in the data.
187  bool Whole; // True if iterating over whole brick
188 };
189 
191 
192 #include "Field/BrickIterator.hpp"
193 
194 #endif // BRICK_ITERATOR_H
195 
196 /***************************************************************************
197  * $RCSfile: BrickIterator.h,v $ $Author: adelmann $
198  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:26 $
199  * IPPL_VERSION_ID: $Id: BrickIterator.h,v 1.1.1.1 2003/01/23 07:40:26 adelmann Exp $
200  ***************************************************************************/
Message & putMessage(Message &, bool makecopy=true)
Definition: rbendmap.h:8
void rewind(unsigned d)
Definition: BrickIterator.h:45
T & offset(int i) const
T & offset(int i, int j) const
BrickCounter & operator++()
Definition: BrickIterator.h:38
T & unit_offset(int i, int j, int k) const
Message & getMessage(Message &)
BrickIterator< T, Dim > PETE_Expr_t
vec< int, Dim > Strides
int GetOffset(unsigned d) const
Definition: BrickIterator.h:51
PETE_Expr_t MakeExpression() const
void moveBy(int i, int j)
vec< int, Dim > Counters
Definition: BrickIterator.h:55
int Stride(int i) const
bool done(unsigned d) const
Definition: BrickIterator.h:41
void step(unsigned d)
Definition: BrickIterator.h:44
void rewind(unsigned d)
T & offset(int i, int j, int k) const
T & offset(int *i) const
void moveBy(int i, int j, int k)
T & unit_offset(int i, int j) const
int size(unsigned d) const
Definition: BrickIterator.h:48
bool whole() const
Definition: BrickIterator.h:81
void moveBy(const int *i)
void step(unsigned d)
T & operator*() const
Definition: BrickIterator.h:94
void moveBy(int i)
static T dot(const T *, const T *)
Definition: Vec.h:194
const unsigned Dim
T *__restrict__ Current
bool operator==(const BrickIterator< T, Dim > &a) const
Definition: BrickIterator.h:84
BrickIterator & operator++()
Definition: BrickIterator.h:78
T & unit_offset(int i) const
vec< int, Dim > Counts
Definition: BrickIterator.h:56
bool operator!=(const BrickIterator< T, Dim > &a) const
Definition: BrickIterator.h:88