OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
NDIndex.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 NDINDEX_H
12 #define NDINDEX_H
13 
14 // include files
15 #include "Index/Index.h"
16 
17 #include <iostream>
18 
19 // forward declarations
20 template <unsigned Dim> class NDIndex;
21 
22 template <unsigned Dim>
23 NDIndex<Dim> operator+(const NDIndex<Dim>&, const int *);
24 template <unsigned Dim>
25 NDIndex<Dim> operator+(const int *,const NDIndex<Dim>&);
26 
27 template <unsigned Dim>
28 NDIndex<Dim> operator-(const NDIndex<Dim>&, const int *);
29 template <unsigned Dim>
30 NDIndex<Dim> operator-(const int *,const NDIndex<Dim>&);
31 
32 template <unsigned Dim>
34 
35 template <unsigned Dim>
36 NDIndex<Dim> operator*(const NDIndex<Dim>&, const int *);
37 template <unsigned Dim>
38 NDIndex<Dim> operator*(const int *,const NDIndex<Dim>&);
39 
40 template <unsigned Dim>
41 NDIndex<Dim> operator/(const NDIndex<Dim>&, const int *);
42 
43 template <unsigned Dim>
44 bool operator<(const NDIndex<Dim>&, const NDIndex<Dim>&);
45 
46 template <unsigned Dim, unsigned Dim2>
47 bool operator==(const NDIndex<Dim>&, const NDIndex<Dim2>&);
48 
49 template <unsigned Dim>
50 std::ostream& operator<<(std::ostream&, const NDIndex<Dim>&);
51 
53 
54 //
55 // Implementation of plugbase that the member template and enumerated
56 // plugbase member functions use.
57 //
58 
59 template<unsigned D1, unsigned D2>
61 
62 
63 
64 /***********************************************************************
65 
66 This is a simple wrapper around Index that just keeps track of
67 N of them and passes along requests for intersect, plugBase and
68 so on.
69 
70 ***********************************************************************/
71 
72 template<unsigned Dim>
73 class NDIndex
74 {
75 public:
76 
77  // Null ctor does nothing.
78  NDIndex() {}
79 
80  // Construct from a simple array of Indexes
81  NDIndex(const Index *idx);
82 
83  // Construct from individual indexes.
84  // Only instantiate the ones that make sense.
85  NDIndex(const Index&);
86  NDIndex(const Index&,const Index&);
87  NDIndex(const Index&,const Index&,const Index&);
88  NDIndex(const Index&,const Index&,const Index&,
89  const Index&);
90  NDIndex(const Index&,const Index&,const Index&,
91  const Index&,const Index&);
92  NDIndex(const Index&,const Index&,const Index&,
93  const Index&,const Index&,const Index&);
94  NDIndex(const NDIndex<Dim-1>&, const Index&);
95 
96  // Return a reference to any of the Indexes.
97  const Index& operator[](unsigned d) const
98  {
99  return p[d];
100  }
101  Index& operator[](unsigned d)
102  {
103  return p[d];
104  }
105 
106  // Get the total size.
107  unsigned size() const;
108 
109  // Stuff for doing index mapping calculations.
110  bool empty() const;
111  NDIndex<Dim> intersect(const NDIndex<Dim>&) const;
112 
113  template<unsigned D>
114  NDIndex<Dim> plugBase(const NDIndex<D>& i)const {
115  return ::plugBase(*this,i);
116  }
117 
118  // useful functions with DomainMap.
119  bool touches(const NDIndex<Dim>&) const;
120  bool contains(const NDIndex<Dim>& a) const;
121  bool containsAllPoints(const NDIndex<Dim> &b) const;
122 
123  // Split on dimension d with the given ratio 0<a<1.
124  bool split(NDIndex<Dim>& l, NDIndex<Dim>& r, unsigned d, double a) const;
125  // Split on dimension d, or the longest dimension.
126  bool split(NDIndex<Dim>& l, NDIndex<Dim>& r, unsigned d) const;
127  bool split(NDIndex<Dim>& l, NDIndex<Dim>& r) const;
128 
129  // put data into a message to send to another node
131  unsigned d;
132  for ( d = 0 ; d < Dim ; ++d )
133  p[d].putMessage(m);
134  return m;
135  }
136 
137  // get data out from a message
139  unsigned d;
140  for ( d = 0 ; d < Dim ; ++d )
141  p[d].getMessage(m);
142  return m;
143  }
144 private:
145  Index p[Dim==0?1:Dim]; // Pointer to the indexes.
146 
147 };
148 
149 
150 // Additive operations.
151 template <unsigned Dim>
152 inline
153 NDIndex<Dim> operator+(const NDIndex<Dim>& ndi, const int * off)
154 {
155  NDIndex<Dim> newNdi;
156  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d] + off[d];
157  return newNdi;
158 }
159 template <unsigned Dim>
160 inline
161 NDIndex<Dim> operator+(const int * off, const NDIndex<Dim>& ndi)
162 {
163  NDIndex<Dim> newNdi;
164  for (unsigned d=0; d<Dim; d++) newNdi[d] = off[d] + ndi[d];
165  return newNdi;
166 }
167 template <unsigned Dim>
168 inline
169 NDIndex<Dim> operator-(const NDIndex<Dim>& ndi, const int * off)
170 {
171  NDIndex<Dim> newNdi;
172  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d] - off[d];
173  return newNdi;
174 }
175 template <unsigned Dim>
176 inline
177 NDIndex<Dim> operator-(const int * off, const NDIndex<Dim>& ndi)
178 {
179  NDIndex<Dim> newNdi;
180  for (unsigned d=0; d<Dim; d++) newNdi[d] = off[d] - ndi[d];
181  return newNdi;
182 }
183 
184 // Multipplicative operations.
185 template <unsigned Dim>
186 inline
188 {
189  NDIndex<Dim> newNdi;
190  for (unsigned d=0; d<Dim; d++) newNdi[d] = -ndi[d];
191  return newNdi;
192 }
193 template <unsigned Dim>
194 inline
195 NDIndex<Dim> operator*(const NDIndex<Dim>& ndi, const int * mult)
196 {
197  NDIndex<Dim> newNdi;
198  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d] * mult[d];
199  return newNdi;
200 }
201 template <unsigned Dim>
202 inline
203 NDIndex<Dim> operator*(const int * mult, const NDIndex<Dim>& ndi)
204 {
205  NDIndex<Dim> newNdi;
206  for (unsigned d=0; d<Dim; d++) newNdi[d] = mult[d] * ndi[d];
207  return newNdi;
208 }
209 template <unsigned Dim>
210 inline
211 NDIndex<Dim> operator/(const NDIndex<Dim>& ndi, const int *denom)
212 {
213  NDIndex<Dim> newNdi;
214  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d]/denom[d];
215  return newNdi;
216 }
217 
218 // Comparison operators so we can use a map container
219 // Just compare the Indexes in turn.
220 template <unsigned Dim>
221 inline
222 bool operator<(const NDIndex<Dim>& lhs, const NDIndex<Dim>& rhs) {
223  for (unsigned d=0; d<Dim; ++d) {
224  if (lhs[d] < rhs[d]) return true;
225  if ( !(lhs[d]==rhs[d]) ) return false;
226  }
227  return false;
228 }
229 
230 template <unsigned Dim, unsigned Dim2>
231 inline
232 bool operator==(const NDIndex<Dim>& lhs, const NDIndex<Dim2>& rhs) {
233  if (Dim != Dim2) {
234  return false;
235  } else {
236  for (unsigned d=0; d<Dim; ++d)
237  if ( !(lhs[d]==rhs[d]) ) return false;
238  return true;
239  }
240 }
241 
242 // write NDIndex out to the given stream
243 template <unsigned Dim>
244 inline std::ostream&
245 operator<<(std::ostream& out, const NDIndex<Dim>& idx) {
246  unsigned d;
247  out << '{';
248  for (d = 0; d < Dim; ++d)
249  out << idx[d] << ((d==Dim-1) ? '}' : ',');
250  return out;
251 }
252 
253 
254 
255 
257 
258 // Build some helper objects for use in maps.
259 
260 template<unsigned Dim>
261 class Touches
262 {
263 public:
264  Touches() {}
265  static bool test(const NDIndex<Dim>& a, const NDIndex<Dim>& b)
266  {
267  return a.touches(b);
268  }
269 };
270 
271 template<unsigned Dim>
272 class Contains
273 {
274 public:
275  Contains() {}
276  static bool test(const NDIndex<Dim>& a, const NDIndex<Dim>& b)
277  {
278  return a.contains(b);
279  }
280 };
281 
282 template<unsigned Dim>
283 class Split
284 {
285 public:
286  Split() {}
287  static bool test(NDIndex<Dim>& l,
288  NDIndex<Dim>& r,
289  const NDIndex<Dim>& a)
290  {
291  return a.split(l,r);
292  }
293 };
294 
296 
297 #ifndef NDINDEX_INLINES_H
298 #include "Index/NDIndexInlines.h"
299 #endif
300 
302 
303 #endif // NDINDEX_H
304 
305 /***************************************************************************
306  * $RCSfile: NDIndex.h,v $ $Author: adelmann $
307  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:27 $
308  * IPPL_VERSION_ID: $Id: NDIndex.h,v 1.1.1.1 2003/01/23 07:40:27 adelmann Exp $
309  ***************************************************************************/
const unsigned Dim
NDIndex< Dim > operator/(const NDIndex< Dim > &, const int *)
Definition: NDIndex.h:211
bool operator<(const NDIndex< Dim > &, const NDIndex< Dim > &)
Definition: NDIndex.h:222
NDIndex< Dim > operator+(const NDIndex< Dim > &, const int *)
Definition: NDIndex.h:153
NDIndex< Dim > operator*(const NDIndex< Dim > &, const int *)
Definition: NDIndex.h:195
bool operator==(const NDIndex< Dim > &, const NDIndex< Dim2 > &)
Definition: NDIndex.h:232
NDIndex< Dim > operator-(const NDIndex< Dim > &, const int *)
Definition: NDIndex.h:169
NDIndex< D1 > plugBase(const NDIndex< D1 > &, const NDIndex< D2 > &)
std::ostream & operator<<(std::ostream &, const NDIndex< Dim > &)
Definition: NDIndex.h:245
std::complex< double > a
const Index & operator[](unsigned d) const
Definition: NDIndex.h:97
Message & getMessage(Message &m)
Definition: NDIndex.h:138
unsigned size() const
NDIndex()
Definition: NDIndex.h:78
NDIndex< Dim > plugBase(const NDIndex< D > &i) const
Definition: NDIndex.h:114
bool touches(const NDIndex< Dim > &) const
bool contains(const NDIndex< Dim > &a) const
Message & putMessage(Message &m) const
Definition: NDIndex.h:130
Index p[Dim==0?1:Dim]
Definition: NDIndex.h:145
NDIndex< Dim > intersect(const NDIndex< Dim > &) const
bool split(NDIndex< Dim > &l, NDIndex< Dim > &r, unsigned d, double a) const
NDIndex(const NDIndex< Dim-1 > &, const Index &)
bool containsAllPoints(const NDIndex< Dim > &b) const
Index & operator[](unsigned d)
Definition: NDIndex.h:101
bool empty() const
Definition: Index.h:237
static bool test(const NDIndex< Dim > &a, const NDIndex< Dim > &b)
Definition: NDIndex.h:265
Touches()
Definition: NDIndex.h:264
Contains()
Definition: NDIndex.h:275
static bool test(const NDIndex< Dim > &a, const NDIndex< Dim > &b)
Definition: NDIndex.h:276
Definition: NDIndex.h:284
static bool test(NDIndex< Dim > &l, NDIndex< Dim > &r, const NDIndex< Dim > &a)
Definition: NDIndex.h:287
Split()
Definition: NDIndex.h:286