OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
NDRegion.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 NDREGION_H
12#define NDREGION_H
13
14/***********************************************************************
15 * NDRegion is a simple container of N PRegion objects. It is templated
16 * on the type of data (T) and the number of PRegions (Dim). It can also
17 * be templated on the Mesh type, in order to provide a mesh for converting
18 * an NDIndex into an NDRegion.
19 ***********************************************************************/
20
21// include files
22#include "Region/PRegion.h"
23#include "Utility/PAssert.h"
24
25#include <iostream>
26
27// forward declarations
28class Message;
29template < class T, unsigned Dim > class NDRegion;
30template <class T, unsigned Dim>
32template <class T, unsigned Dim>
34template <class T, unsigned Dim>
36template <class T, unsigned Dim>
38template <class T, unsigned Dim>
40template <class T, unsigned Dim>
42template <class T, unsigned Dim>
44template <class T, unsigned Dim>
46template <class T, unsigned Dim>
47bool operator<(const NDRegion<T,Dim>&, const NDRegion<T,Dim>&);
48template <class T, unsigned Dim>
49bool operator==(const NDRegion<T,Dim>&, const NDRegion<T,Dim>&);
50template <class T, unsigned Dim>
51bool operator!=(const NDRegion<T,Dim>&, const NDRegion<T,Dim>&);
52template < class T, unsigned Dim >
53std::ostream& operator<<(std::ostream&, const NDRegion<T,Dim>&);
54
55template < class T, unsigned Dim >
56class NDRegion {
57
58public:
59 // Null ctor does nothing.
61
62 // Construct from a simple array of PRegions
64 for (unsigned int i=0; i < Dim; i++)
65 p[i] = idx[i];
66 }
67
68 // Construct from individual PRegions, for 1D thru 6D cases.
69 NDRegion(const PRegion<T>& r1) {
70 PInsist(Dim==1, "Number of arguments does not match NDRegion dimension!!");
71 p[0] = r1;
72 }
73 NDRegion(const PRegion<T>& r1, const PRegion<T>& r2) {
74 PInsist(Dim==2, "Number of arguments does not match NDRegion dimension!!");
75 p[0] = r1;
76 p[1] = r2;
77 }
78 NDRegion(const PRegion<T>& r1, const PRegion<T>& r2, const PRegion<T>& r3) {
79 PInsist(Dim==3, "Number of arguments does not match NDRegion dimension!!");
80 p[0] = r1;
81 p[1] = r2;
82 p[2] = r3;
83 }
84 NDRegion(const PRegion<T>& r1, const PRegion<T>& r2, const PRegion<T>& r3,
85 const PRegion<T>& r4) {
86 PInsist(Dim==4, "Number of arguments does not match NDRegion dimension!!");
87 p[0] = r1;
88 p[1] = r2;
89 p[2] = r3;
90 p[3] = r4;
91 }
92 NDRegion(const PRegion<T>& r1, const PRegion<T>& r2, const PRegion<T>& r3,
93 const PRegion<T>& r4, const PRegion<T>& r5) {
94 PInsist(Dim==5, "Number of arguments does not match NDRegion dimension!!");
95 p[0] = r1;
96 p[1] = r2;
97 p[2] = r3;
98 p[3] = r4;
99 p[4] = r5;
100 }
101 NDRegion(const PRegion<T>& r1, const PRegion<T>& r2, const PRegion<T>& r3,
102 const PRegion<T>& r4, const PRegion<T>& r5, const PRegion<T>& r6) {
103 PInsist(Dim==6, "Number of arguments does not match NDRegion dimension!!");
104 p[0] = r1;
105 p[1] = r2;
106 p[2] = r3;
107 p[3] = r4;
108 p[4] = r5;
109 p[5] = r6;
110 }
111
112 // copy constructor
114 for (unsigned int i=0; i < Dim; i++)
115 p[i] = nr.p[i];
116 }
117
118 // operator= definitions
120 for (unsigned int i=0; i < Dim; i++)
121 p[i] = nr.p[i];
122 return *this;
123 }
124
125 // Return a reference to any of the PRegion<T> objects.
126 const PRegion<T>& operator[](unsigned d) const { return p[d]; }
127 PRegion<T>& operator[](unsigned d) { return p[d]; }
128
129 // return the volume of this region
130 T volume() const {
131 T v = p[0].length();
132 for (unsigned int i=1; i < Dim; i++)
133 v *= p[i].length();
134 return v;
135 }
136
137 // compute-assign operators
139 for (unsigned int i=0; i < Dim; i++)
140 p[i] += t;
141 return *this;
142 }
144 for (unsigned int i=0; i < Dim; i++)
145 p[i] -= t;
146 return *this;
147 }
149 for (unsigned int i=0; i < Dim; i++)
150 p[i] *= t;
151 return *this;
152 }
154 if (t != 0) {
155 for (unsigned int i=0; i < Dim; i++) p[i] /= t;
156 }
157 return *this;
158 }
159
160 bool empty() const {
161 for (unsigned int i=0; i < Dim; i++)
162 if ( ! p[i].empty() )
163 return false;
164 return true;
165 }
166
167 // useful functions with DomainMap.
169 NDRegion<T,Dim> retval;
170 for (unsigned int i=0; i < Dim; i++)
171 retval.p[i] = p[i].intersect(nr.p[i]);
172 return retval;
173 }
174
175 bool touches(const NDRegion<T,Dim>& nr) const {
176 for (unsigned int i=0; i < Dim; i++)
177 if ( ! (p[i].touches(nr.p[i])) )
178 return false;
179 return true;
180 }
181
182 bool contains(const NDRegion<T,Dim>& nr) const {
183 for (unsigned int i=0; i < Dim; i++)
184 if ( ! (p[i].contains(nr.p[i])) )
185 return false;
186 return true;
187 }
188
189 // Split on dimension d, or the longest dimension.
191 unsigned d) const {
192 for (unsigned int i=0; i < Dim; i++) {
193 if (i == d) {
194 p[i].split(l.p[i], r.p[i]);
195 }
196 else {
197 l.p[i] = p[i];
198 r.p[i] = p[i];
199 }
200 }
201 return true;
202 }
203
205 // find longest dimension
206 unsigned d = 0;
207 T maxlen = p[0].length();
208 for (unsigned i=1; i < Dim; i++) {
209 if (p[i].length() > maxlen) {
210 maxlen = p[i].length();
211 d = i;
212 }
213 }
214 // split on this dimension
215 return split(l, r, d);
216 }
217
218 // put data into a message to send to another node
220 for ( unsigned d = 0 ; d < Dim ; ++d )
221 p[d].putMessage(m);
222 return m;
223 }
224
225 // get data out from a message
227 for ( unsigned d = 0 ; d < Dim ; ++d )
228 p[d].getMessage(m);
229 return m;
230 }
231
232private:
233 PRegion<T> p[Dim]; // Array of PRegions
234
235};
236
237
238// Additive operations.
239template <class T, unsigned Dim>
240inline
242 NDRegion<T,Dim> retval(nr);
243 retval += t;
244 return retval;
245}
246template <class T, unsigned Dim>
247inline
249 return (nr + t);
250}
251template <class T, unsigned Dim>
252inline
254 return (nr + (-t));
255}
256template <class T, unsigned Dim>
257inline
259 return (-nr + t);
260}
261
262// Multipplicative operations.
263template <class T, unsigned Dim>
264inline
266 NDRegion<T,Dim> retval;
267 for (unsigned int i=0; i < Dim; i++)
268 retval[i] = -nr[i];
269 return retval;
270}
271template <class T, unsigned Dim>
272inline
274 NDRegion<T,Dim> retval(nr);
275 retval *= t;
276 return retval;
277}
278template <class T, unsigned Dim>
279inline
281 return (nr * t);
282}
283template <class T, unsigned Dim>
284inline
286 return (t != 0 ? (nr * (1/t)) : nr);
287}
288
289// Comparison operators so we can use a map container
290// Just compare the PRegions in turn.
291template <class T, unsigned Dim>
292inline
293bool operator<(const NDRegion<T,Dim>& A, const NDRegion<T,Dim>& B) {
294 for (unsigned int i=0; i < Dim; i++)
295 if ( !(A[i] < B[i]) ) return false;
296 return true;
297}
298template <class T, unsigned Dim>
299inline
301 for (unsigned int i=0; i < Dim; i++)
302 if ( !(A[i] == B[i]) ) return false;
303 return true;
304}
305template <class T, unsigned Dim>
306inline
308 return !(A == B);
309}
310
311// write NDRegion out to the given stream
312template <class T, unsigned Dim>
313inline
314std::ostream& operator<<(std::ostream& out, const NDRegion<T,Dim>& idx) {
315 out << '{';
316 for (unsigned d = 0; d < Dim; ++d)
317 out << idx[d] << ((d==Dim-1) ? '}' : ',');
318 return out;
319}
320
321
323
324// Build some helper objects for use in DomainMap
325template < class T, unsigned Dim >
327public:
329 static bool test(const NDRegion<T,Dim>& a,
330 const NDRegion<T,Dim>& b) {
331 return a.touches(b);
332 }
333};
334
335template < class T, unsigned Dim >
337public:
339 static bool test(const NDRegion<T,Dim>& a,
340 const NDRegion<T,Dim>& b) {
341 return a.contains(b);
342 }
343};
344
345template < class T, unsigned Dim >
347public:
350 const NDRegion<T,Dim>& a) {
351 return a.split(l,r);
352 }
353};
354
355
356#endif // NDREGION_H
357
358/***************************************************************************
359 * $RCSfile: NDRegion.h,v $ $Author: adelmann $
360 * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:32 $
361 * IPPL_VERSION_ID: $Id: NDRegion.h,v 1.1.1.1 2003/01/23 07:40:32 adelmann Exp $
362 ***************************************************************************/
const int nr
Definition: ClassicRandom.h:24
const unsigned Dim
NDRegion< T, Dim > operator-(const NDRegion< T, Dim > &, T)
Definition: NDRegion.h:253
bool operator<(const NDRegion< T, Dim > &, const NDRegion< T, Dim > &)
Definition: NDRegion.h:293
bool operator!=(const NDRegion< T, Dim > &, const NDRegion< T, Dim > &)
Definition: NDRegion.h:307
std::ostream & operator<<(std::ostream &, const NDRegion< T, Dim > &)
Definition: NDRegion.h:314
bool operator==(const NDRegion< T, Dim > &, const NDRegion< T, Dim > &)
Definition: NDRegion.h:300
NDRegion< T, Dim > operator/(const NDRegion< T, Dim > &, T)
Definition: NDRegion.h:285
NDRegion< T, Dim > operator+(const NDRegion< T, Dim > &, T)
Definition: NDRegion.h:241
NDRegion< T, Dim > operator*(const NDRegion< T, Dim > &, T)
Definition: NDRegion.h:273
std::complex< double > a
#define PInsist(c, m)
Definition: PAssert.h:120
Message & getMessage(Message &m)
Definition: NDRegion.h:226
NDRegion< T, Dim > & operator=(const NDRegion< T, Dim > &nr)
Definition: NDRegion.h:119
NDRegion(const PRegion< T > &r1, const PRegion< T > &r2, const PRegion< T > &r3)
Definition: NDRegion.h:78
NDRegion< T, Dim > & operator/=(const T t)
Definition: NDRegion.h:153
NDRegion(const PRegion< T > &r1)
Definition: NDRegion.h:69
NDRegion(const NDRegion< T, Dim > &nr)
Definition: NDRegion.h:113
NDRegion(const PRegion< T > &r1, const PRegion< T > &r2, const PRegion< T > &r3, const PRegion< T > &r4, const PRegion< T > &r5)
Definition: NDRegion.h:92
NDRegion(PRegion< T > *idx)
Definition: NDRegion.h:63
bool empty() const
Definition: NDRegion.h:160
bool touches(const NDRegion< T, Dim > &nr) const
Definition: NDRegion.h:175
NDRegion< T, Dim > & operator*=(const T t)
Definition: NDRegion.h:148
PRegion< T > p[Dim]
Definition: NDRegion.h:233
T volume() const
Definition: NDRegion.h:130
NDRegion()
Definition: NDRegion.h:60
NDRegion(const PRegion< T > &r1, const PRegion< T > &r2, const PRegion< T > &r3, const PRegion< T > &r4)
Definition: NDRegion.h:84
PRegion< T > & operator[](unsigned d)
Definition: NDRegion.h:127
NDRegion< T, Dim > & operator+=(const T t)
Definition: NDRegion.h:138
bool contains(const NDRegion< T, Dim > &nr) const
Definition: NDRegion.h:182
bool split(NDRegion< T, Dim > &l, NDRegion< T, Dim > &r, unsigned d) const
Definition: NDRegion.h:190
NDRegion< T, Dim > intersect(const NDRegion< T, Dim > &nr) const
Definition: NDRegion.h:168
Message & putMessage(Message &m)
Definition: NDRegion.h:219
NDRegion(const PRegion< T > &r1, const PRegion< T > &r2)
Definition: NDRegion.h:73
const PRegion< T > & operator[](unsigned d) const
Definition: NDRegion.h:126
bool split(NDRegion< T, Dim > &l, NDRegion< T, Dim > &r) const
Definition: NDRegion.h:204
NDRegion(const PRegion< T > &r1, const PRegion< T > &r2, const PRegion< T > &r3, const PRegion< T > &r4, const PRegion< T > &r5, const PRegion< T > &r6)
Definition: NDRegion.h:101
NDRegion< T, Dim > & operator-=(const T t)
Definition: NDRegion.h:143
static bool test(const NDRegion< T, Dim > &a, const NDRegion< T, Dim > &b)
Definition: NDRegion.h:329
static bool test(const NDRegion< T, Dim > &a, const NDRegion< T, Dim > &b)
Definition: NDRegion.h:339
static bool test(NDRegion< T, Dim > &l, NDRegion< T, Dim > &r, const NDRegion< T, Dim > &a)
Definition: NDRegion.h:349