OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
20template <unsigned Dim> class NDIndex;
21
22template <unsigned Dim>
23NDIndex<Dim> operator+(const NDIndex<Dim>&, const int *);
24template <unsigned Dim>
25NDIndex<Dim> operator+(const int *,const NDIndex<Dim>&);
26
27template <unsigned Dim>
28NDIndex<Dim> operator-(const NDIndex<Dim>&, const int *);
29template <unsigned Dim>
30NDIndex<Dim> operator-(const int *,const NDIndex<Dim>&);
31
32template <unsigned Dim>
34
35template <unsigned Dim>
36NDIndex<Dim> operator*(const NDIndex<Dim>&, const int *);
37template <unsigned Dim>
38NDIndex<Dim> operator*(const int *,const NDIndex<Dim>&);
39
40template <unsigned Dim>
41NDIndex<Dim> operator/(const NDIndex<Dim>&, const int *);
42
43template <unsigned Dim>
44bool operator<(const NDIndex<Dim>&, const NDIndex<Dim>&);
45
46template <unsigned Dim, unsigned Dim2>
47bool operator==(const NDIndex<Dim>&, const NDIndex<Dim2>&);
48
49template <unsigned Dim>
50std::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
59template<unsigned D1, unsigned D2>
61
62
63
64/***********************************************************************
65
66This is a simple wrapper around Index that just keeps track of
67N of them and passes along requests for intersect, plugBase and
68so on.
69
70***********************************************************************/
71
72template<unsigned Dim>
74{
75public:
76
77 // Null ctor does nothing.
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>
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 }
144private:
145 Index p[Dim==0?1:Dim]; // Pointer to the indexes.
146
147};
148
149
150// Additive operations.
151template <unsigned Dim>
152inline
153NDIndex<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}
159template <unsigned Dim>
160inline
161NDIndex<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}
167template <unsigned Dim>
168inline
169NDIndex<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}
175template <unsigned Dim>
176inline
177NDIndex<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.
185template <unsigned Dim>
186inline
188{
189 NDIndex<Dim> newNdi;
190 for (unsigned d=0; d<Dim; d++) newNdi[d] = -ndi[d];
191 return newNdi;
192}
193template <unsigned Dim>
194inline
195NDIndex<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}
201template <unsigned Dim>
202inline
203NDIndex<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}
209template <unsigned Dim>
210inline
211NDIndex<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.
220template <unsigned Dim>
221inline
222bool 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
230template <unsigned Dim, unsigned Dim2>
231inline
232bool 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
243template <unsigned Dim>
244inline std::ostream&
245operator<<(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
260template<unsigned Dim>
262{
263public:
265 static bool test(const NDIndex<Dim>& a, const NDIndex<Dim>& b)
266 {
267 return a.touches(b);
268 }
269};
270
271template<unsigned Dim>
273{
274public:
276 static bool test(const NDIndex<Dim>& a, const NDIndex<Dim>& b)
277 {
278 return a.contains(b);
279 }
280};
281
282template<unsigned Dim>
283class Split
284{
285public:
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< D1 > plugBase(const NDIndex< D1 > &, const NDIndex< D2 > &)
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
std::ostream & operator<<(std::ostream &, const NDIndex< Dim > &)
Definition: NDIndex.h:245
std::complex< double > a
unsigned size() const
NDIndex()
Definition: NDIndex.h:78
bool touches(const NDIndex< Dim > &) const
Message & putMessage(Message &m) const
Definition: NDIndex.h:130
NDIndex< Dim > plugBase(const NDIndex< D > &i) const
Definition: NDIndex.h:114
bool contains(const NDIndex< Dim > &a) const
Message & getMessage(Message &m)
Definition: NDIndex.h:138
const Index & operator[](unsigned d) const
Definition: NDIndex.h:97
Index p[Dim==0?1:Dim]
Definition: NDIndex.h:145
Index & operator[](unsigned d)
Definition: NDIndex.h:101
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
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