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