OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 #ifdef IPPL_USE_MEMBER_TEMPLATES
113  template<unsigned D>
114  NDIndex<Dim> plugBase(const NDIndex<D>& i)const {
115  return ::plugBase(*this,i);
116  }
117 #else
125 #endif
126 
127  // useful functions with DomainMap.
128  bool touches(const NDIndex<Dim>&) const;
129  bool contains(const NDIndex<Dim>& a) const;
130  bool containsAllPoints(const NDIndex<Dim> &b) const;
131 
132  // Split on dimension d with the given ratio 0<a<1.
133  bool split(NDIndex<Dim>& l, NDIndex<Dim>& r, unsigned d, double a) const;
134  // Split on dimension d, or the longest dimension.
135  bool split(NDIndex<Dim>& l, NDIndex<Dim>& r, unsigned d) const;
136  bool split(NDIndex<Dim>& l, NDIndex<Dim>& r) const;
137 
138  // put data into a message to send to another node
140  unsigned d;
141  for ( d = 0 ; d < Dim ; ++d )
142  p[d].putMessage(m);
143  return m;
144  }
145 
146  // get data out from a message
148  unsigned d;
149  for ( d = 0 ; d < Dim ; ++d )
150  p[d].getMessage(m);
151  return m;
152  }
153 private:
154  Index p[Dim==0?1:Dim]; // Pointer to the indexes.
155 
156 };
157 
158 
159 // Additive operations.
160 template <unsigned Dim>
161 inline
162 NDIndex<Dim> operator+(const NDIndex<Dim>& ndi, const int * off)
163 {
164  NDIndex<Dim> newNdi;
165  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d] + off[d];
166  return newNdi;
167 }
168 template <unsigned Dim>
169 inline
170 NDIndex<Dim> operator+(const int * off, const NDIndex<Dim>& ndi)
171 {
172  NDIndex<Dim> newNdi;
173  for (unsigned d=0; d<Dim; d++) newNdi[d] = off[d] + ndi[d];
174  return newNdi;
175 }
176 template <unsigned Dim>
177 inline
178 NDIndex<Dim> operator-(const NDIndex<Dim>& ndi, const int * off)
179 {
180  NDIndex<Dim> newNdi;
181  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d] - off[d];
182  return newNdi;
183 }
184 template <unsigned Dim>
185 inline
186 NDIndex<Dim> operator-(const int * off, const NDIndex<Dim>& ndi)
187 {
188  NDIndex<Dim> newNdi;
189  for (unsigned d=0; d<Dim; d++) newNdi[d] = off[d] - ndi[d];
190  return newNdi;
191 }
192 
193 // Multipplicative operations.
194 template <unsigned Dim>
195 inline
197 {
198  NDIndex<Dim> newNdi;
199  for (unsigned d=0; d<Dim; d++) newNdi[d] = -ndi[d];
200  return newNdi;
201 }
202 template <unsigned Dim>
203 inline
204 NDIndex<Dim> operator*(const NDIndex<Dim>& ndi, const int * mult)
205 {
206  NDIndex<Dim> newNdi;
207  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d] * mult[d];
208  return newNdi;
209 }
210 template <unsigned Dim>
211 inline
212 NDIndex<Dim> operator*(const int * mult, const NDIndex<Dim>& ndi)
213 {
214  NDIndex<Dim> newNdi;
215  for (unsigned d=0; d<Dim; d++) newNdi[d] = mult[d] * ndi[d];
216  return newNdi;
217 }
218 template <unsigned Dim>
219 inline
220 NDIndex<Dim> operator/(const NDIndex<Dim>& ndi, const int *denom)
221 {
222  NDIndex<Dim> newNdi;
223  for (unsigned d=0; d<Dim; d++) newNdi[d] = ndi[d]/denom[d];
224  return newNdi;
225 }
226 
227 // Comparison operators so we can use a map container
228 // Just compare the Indexes in turn.
229 template <unsigned Dim>
230 inline
231 bool operator<(const NDIndex<Dim>& lhs, const NDIndex<Dim>& rhs) {
232  for (unsigned d=0; d<Dim; ++d) {
233  if (lhs[d] < rhs[d]) return true;
234  if ( !(lhs[d]==rhs[d]) ) return false;
235  }
236  return false;
237 }
238 
239 template <unsigned Dim, unsigned Dim2>
240 inline
241 bool operator==(const NDIndex<Dim>& lhs, const NDIndex<Dim2>& rhs) {
242  if (Dim != Dim2) {
243  return false;
244  } else {
245  for (unsigned d=0; d<Dim; ++d)
246  if ( !(lhs[d]==rhs[d]) ) return false;
247  return true;
248  }
249 }
250 
251 // write NDIndex out to the given stream
252 template <unsigned Dim>
253 inline std::ostream&
254 operator<<(std::ostream& out, const NDIndex<Dim>& idx) {
255  unsigned d;
256  out << '{';
257  for (d = 0; d < Dim; ++d)
258  out << idx[d] << ((d==Dim-1) ? '}' : ',');
259  return out;
260 }
261 
262 
263 
264 
266 
267 // Build some helper objects for use in maps.
268 
269 template<unsigned Dim>
270 class Touches
271 {
272 public:
273  Touches() {}
274  static bool test(const NDIndex<Dim>& a, const NDIndex<Dim>& b)
275  {
276  return a.touches(b);
277  }
278 };
279 
280 template<unsigned Dim>
281 class Contains
282 {
283 public:
284  Contains() {}
285  static bool test(const NDIndex<Dim>& a, const NDIndex<Dim>& b)
286  {
287  return a.contains(b);
288  }
289 };
290 
291 template<unsigned Dim>
292 class Split
293 {
294 public:
295  Split() {}
296  static bool test(NDIndex<Dim>& l,
297  NDIndex<Dim>& r,
298  const NDIndex<Dim>& a)
299  {
300  return a.split(l,r);
301  }
302 };
303 
305 
306 #ifndef NDINDEX_INLINES_H
307 #include "Index/NDIndexInlines.h"
308 #endif
309 
311 
312 #endif // NDINDEX_H
313 
314 /***************************************************************************
315  * $RCSfile: NDIndex.h,v $ $Author: adelmann $
316  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:27 $
317  * IPPL_VERSION_ID: $Id: NDIndex.h,v 1.1.1.1 2003/01/23 07:40:27 adelmann Exp $
318  ***************************************************************************/
bool split(NDIndex< Dim > &l, NDIndex< Dim > &r, unsigned d, double a) const
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
bool containsAllPoints(const NDIndex< Dim > &b) const
Matrix< T > operator/(const Matrix< T > &, const T &)
Matrix divided by scalar.
Definition: Matrix.h:329
Index & operator[](unsigned d)
Definition: NDIndex.h:101
Message & getMessage(Message &m)
Definition: NDIndex.h:147
Matrix< T > operator*(const Matrix< T > &, const Matrix< T > &)
Matrix multiply.
Definition: Matrix.h:297
unsigned size() const
NDIndex< D1 > plugBase(const NDIndex< D1 > &, const NDIndex< D2 > &)
const Index & operator[](unsigned d) const
Definition: NDIndex.h:97
NDIndex< Dim > plugBase(const NDIndex< 1 > &i) const
Definition: NDIndex.h:118
static bool test(const NDIndex< Dim > &a, const NDIndex< Dim > &b)
Definition: NDIndex.h:274
Split()
Definition: NDIndex.h:295
Touches()
Definition: NDIndex.h:273
Definition: Index.h:236
bool empty() const
Index p[Dim==0?1:Dim]
Definition: NDIndex.h:154
bool touches(const NDIndex< Dim > &) const
NDIndex< Dim > plugBase(const NDIndex< 2 > &i) const
Definition: NDIndex.h:119
static bool test(NDIndex< Dim > &l, NDIndex< Dim > &r, const NDIndex< Dim > &a)
Definition: NDIndex.h:296
NDIndex< Dim > plugBase(const NDIndex< 7 > &i) const
Definition: NDIndex.h:124
NDIndex< Dim > plugBase(const NDIndex< 6 > &i) const
Definition: NDIndex.h:123
bool contains(const NDIndex< Dim > &a) const
NDIndex< Dim > plugBase(const NDIndex< 4 > &i) const
Definition: NDIndex.h:121
NDIndex< Dim > intersect(const NDIndex< Dim > &) const
NDIndex< Dim > plugBase(const NDIndex< 5 > &i) const
Definition: NDIndex.h:122
Message & putMessage(Message &m) const
Definition: NDIndex.h:139
const unsigned Dim
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282
Contains()
Definition: NDIndex.h:284
NDIndex< Dim > plugBase(const NDIndex< 3 > &i) const
Definition: NDIndex.h:120
NDIndex()
Definition: NDIndex.h:78
Definition: NDIndex.h:292
static bool test(const NDIndex< Dim > &a, const NDIndex< Dim > &b)
Definition: NDIndex.h:285
bool operator==(const TwoPolynomial &left, const TwoPolynomial &right)