OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
SubFieldTraits.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 SUB_FIELD_TRAITS_H
12 #define SUB_FIELD_TRAITS_H
13 
14 
16 // a templated traits class describing how to combine different subset
17 // objects together. The general case generates errors if you try to use
18 // subset objects in pairs which are not supported; partial specializations
19 // of this class indicate which combinations actually work and do the
20 // right thing in those cases.
21 //
22 // This class will deal with two classes, call them A and B. The class
23 // tells how to construct B from A (A --> B), or how to combine A into B.
24 // So you can always think of it as A --> B. The order of these classes in
25 // the template parameter list is always Dim(B), Dim(A), B, A.
26 //
27 // There are two functions in this traits class:
28 // a) construct:
29 // static method used to set up the subset object based on an input object.
30 // Arguments:
31 // Subset object (B) which is being constructed. Assumed to be empty.
32 // Input subset object (A) to be used to construct B.
33 // BareField with data we are subsetting, if needed.
34 // Returns:
35 // Number of brackets which this construction adds.
36 // b) combine:
37 // static methods to combine subset objects.
38 // Arguments:
39 // Existing subset object (B)
40 // The subset object (A) to add to the first argument.
41 // The return object, constructed from A and B (of type Return_t)
42 // The current number of brackets already used
43 // BareField with data we are subsetting, if needed.
44 
45 // include files
46 #include "Index/NDIndex.h"
47 #include "Index/SIndex.h"
48 #include "Field/BareField.h"
49 #include "Field/LField.h"
50 
51 
52 template<class T, unsigned int Dim, class S1, class S2>
54  // static contruct method
55  static int construct(S1&, const S2&, BareField<T,Dim>&) {
56  PInsist(false,"Unsupported indexing attempted.");
57  return 0;
58  }
59 
60  // a typedef for the return type when you combine two subset objects
61  typedef S2 Return_t;
62 
63  // an enum for how many brackets this combination will add
64  enum { Brackets_u = 0 };
65 
66  // static combine method
67  template<class S3>
68  static void combine(const S1&, const S2&, S3&,
69  unsigned int&, BareField<T,Dim>&) {
70  PInsist(false,"Unsupported indexing attempted.");
71  }
72 };
73 
74 
76 // NDIndex related specializations
77 
78 // construct NDIndex<Dim> from NDIndex<Dim2>
79 // combine [NDIndex<Dim>][NDIndex<Dim2>] --> NDIndex<Dim>
80 template<class T, unsigned int Dim, unsigned int Dim2>
81 struct SubFieldTraits<T, Dim, NDIndex<Dim>, NDIndex<Dim2> > {
82  static int construct(NDIndex<Dim>& out, const NDIndex<Dim2>& s,
84  CTAssert(Dim2 <= Dim);
85  for (unsigned int d=0; d < Dim2; ++d)
86  out[d] = s[d];
87  return Dim2;
88  }
90  enum { Brackets_u = Dim2 };
91  static void combine(const NDIndex<Dim>& s1, const NDIndex<Dim2>& s2,
92  Return_t& out, unsigned int B, BareField<T,Dim>&) {
93  CTAssert(Dim2 <= Dim);
94  unsigned int d;
95  for (d=0; d < B; ++d)
96  out[d] = s1[d];
97  for (d=0; d < Dim2; ++d)
98  out[d+B] = s2[d];
99  }
100 };
101 
102 
103 // construct NDIndex<Dim> from Index (B from A)
104 // combine [NDIndex<Dim>][Index] --> NDIndex<Dim>
105 template<class T, unsigned int Dim>
107  static int construct(NDIndex<Dim>& out, const Index& s,
108  BareField<T,Dim>&) {
109  out[0] = s;
110  return 1;
111  }
113  enum { Brackets_u = 1 };
114  static void combine(const NDIndex<Dim>& s1, const Index& s2,
115  Return_t& out, unsigned int B, BareField<T,Dim>&) {
116  unsigned int d;
117  for (d=0; d < B; ++d)
118  out[d] = s1[d];
119  out[d] = s2;
120  }
121 };
122 
123 
124 // construct NDIndex<Dim> from int (B from A)
125 // combine [NDIndex<Dim>][int] --> NDIndex<Dim>
126 template<class T, unsigned int Dim>
127 struct SubFieldTraits<T, Dim, NDIndex<Dim>, int> {
128  static int construct(NDIndex<Dim>& out, const int& s,
129  BareField<T,Dim>&) {
130  out[0] = Index(s,s);
131  return 1;
132  }
134  enum { Brackets_u = 1 };
135  static void combine(const NDIndex<Dim>& s1, const int& s2,
136  Return_t& out, unsigned int B, BareField<T,Dim>&) {
137  unsigned int d;
138  for (d=0; d < B; ++d)
139  out[d] = s1[d];
140  out[d] = Index(s2, s2);
141  }
142 };
143 
144 
145 // construct NDIndex<Dim> from SOffset<Dim2> (B from A)
146 // combine [NDIndex<Dim>][SOffset<Dim2>] --> NDIndex<Dim>
147 template<class T, unsigned int Dim, unsigned int Dim2>
148 struct SubFieldTraits<T, Dim, NDIndex<Dim>, SOffset<Dim2> > {
149  static int construct(NDIndex<Dim>& out, const SOffset<Dim2>& s,
150  BareField<T,Dim>&) {
151  CTAssert(Dim2 <= Dim);
152  for (unsigned int d=0; d < Dim2; ++d)
153  out[d] = Index(s[d], s[d]);
154  return Dim;
155  }
157  enum { Brackets_u = Dim2 };
158  static void combine(const NDIndex<Dim>& s1, const SOffset<Dim2>& s2,
159  Return_t& out, unsigned int B, BareField<T,Dim>&) {
160  CTAssert(Dim2 <= Dim);
161  unsigned int d;
162  for (d=0; d < B; ++d)
163  out[d] = s1[d];
164  for (d=0; d < Dim2; ++d)
165  out[d+B] = Index(s2[d], s2[d]);
166  }
167 };
168 
169 
171 // SIndex related specializations
172 
173 // construct SIndex<Dim> from SIndex<Dim> (B from A)
174 // combine [SIndex<Dim>][SIndex<Dim>] --> SIndex<Dim> (intersection)
175 template<class T, unsigned int Dim>
177  static int construct(SIndex<Dim>& out, const SIndex<Dim>& s,
178  BareField<T,Dim>&) {
179  // assigning SIndex to SIndex also will initialize things if needed
180  out = s;
181  return Dim;
182  }
184  enum { Brackets_u = 0 };
185  static void combine(const SIndex<Dim>& s1, const SIndex<Dim>& s2,
186  Return_t& out, unsigned int, BareField<T,Dim>& A) {
187  out = s1;
188  out &= s2;
189  }
190 };
191 
192 
193 // construct SIndex<Dim> from NDIndex<Dim> (B from A)
194 // combine [SIndex<Dim>][NDIndex<Dim>] --> SIndex<Dim> (intersection)
195 template<class T, unsigned int Dim>
197  static int construct(SIndex<Dim>& out, const NDIndex<Dim>& s,
198  BareField<T,Dim>& A) {
199  if (out.needInitialize())
200  out.initialize(A.getLayout());
201  out = s;
202  return Dim;
203  }
205  enum { Brackets_u = 0 };
206  static void combine(const SIndex<Dim>& s1, const NDIndex<Dim>& s2,
207  Return_t& out, unsigned int, BareField<T,Dim>& A) {
208  out = s1;
209  out &= s2;
210  }
211 };
212 
213 
214 // construct SIndex<Dim> from SOffset<Dim> (B from A)
215 // combine [SIndex<Dim>][SOffset<Dim>] --> SIndex<Dim> (intersection)
216 template<class T, unsigned int Dim>
218  static int construct(SIndex<Dim>& out, const SOffset<Dim>& s,
219  BareField<T,Dim>& A) {
220  if (out.needInitialize())
221  out.initialize(A.getLayout());
222  out = s;
223  return Dim;
224  }
226  enum { Brackets_u = 0 };
227  static void combine(const SIndex<Dim>& s1, const SOffset<Dim>& s2,
228  Return_t& out, unsigned int, BareField<T,Dim>& A) {
229  out = s1;
230  out &= s2;
231  }
232 };
233 
234 
236 // SOffset related specializations
237 
238 // construct SOffset<Dim> from SOffset<Dim2>
239 // combine [SOffset<Dim>][SOffset<Dim2>] --> SOffset<Dim>
240 template<class T, unsigned int Dim, unsigned int Dim2>
241 struct SubFieldTraits<T, Dim, SOffset<Dim>, SOffset<Dim2> > {
242  static int construct(SOffset<Dim>& out, const SOffset<Dim2>& s,
243  BareField<T,Dim>&) {
244  CTAssert(Dim2 <= Dim);
245  for (unsigned int d=0; d < Dim2; ++d)
246  out[d] = s[d];
247  return Dim;
248  }
250  enum { Brackets_u = Dim2 };
251  static void combine(const SOffset<Dim>& s1, const SOffset<Dim2>& s2,
252  Return_t& out, unsigned int B, BareField<T,Dim>&) {
253  CTAssert(Dim2 <= Dim);
254  unsigned int d;
255  for (d=0; d < B; ++d)
256  out[d] = s1[d];
257  for (d=0; d < Dim2; ++d)
258  out[d+B] = s2[d];
259  }
260 };
261 
262 
263 // construct SOffset<Dim> from int
264 // combine [SOffset<Dim>][int] --> SOffset<Dim>
265 template<class T, unsigned int Dim>
266 struct SubFieldTraits<T, Dim, SOffset<Dim>, int> {
267  static int construct(SOffset<Dim>& out, const int& s,
268  BareField<T,Dim>&) {
269  out[0] = s;
270  return 1;
271  }
273  enum { Brackets_u = 1 };
274  static void combine(const SOffset<Dim>& s1, const int& s2,
275  Return_t& out, unsigned int B, BareField<T,Dim>&) {
276  unsigned int d;
277  for (d=0; d < B; ++d)
278  out[d] = s1[d];
279  out[d] = s2;
280  }
281 };
282 
283 
284 // combine [SOffset<Dim>][Index] --> NDIndex<Dim>
285 template<class T, unsigned int Dim>
288  enum { Brackets_u = 1 };
289  static void combine(const SOffset<Dim>& s1, const Index& s2,
290  Return_t& out, unsigned int B, BareField<T,Dim>&) {
291  unsigned int d;
292  for (d=0; d < B; ++d)
293  out[d] = Index(s1[d], s1[d]);
294  out[d] = s2;
295  }
296 };
297 
298 
299 // combine [SOffset<Dim>][NDIndex<Dim2>] --> NDIndex<Dim>
300 template<class T, unsigned int Dim, unsigned int Dim2>
301 struct SubFieldTraits<T, Dim, SOffset<Dim>, NDIndex<Dim2> > {
303  enum { Brackets_u = Dim2 };
304  static void combine(const SOffset<Dim>& s1, const NDIndex<Dim2>& s2,
305  Return_t& out, unsigned int B, BareField<T,Dim>&) {
306  CTAssert(Dim2 <= Dim);
307  unsigned int d;
308  for (d=0; d < B; ++d)
309  out[d] = Index(s1[d], s1[d]);
310  for (d=0; d < Dim2; ++d)
311  out[d+B] = s2[d];
312  }
313 };
314 
315 
316 #endif // SUB_FIELD_TRAITS_H
317 
318 /***************************************************************************
319  * $RCSfile: SubFieldTraits.h,v $ $Author: adelmann $
320  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
321  * IPPL_VERSION_ID: $Id: SubFieldTraits.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
322  ***************************************************************************/
Layout_t & getLayout() const
Definition: BareField.h:130
static void combine(const NDIndex< Dim > &s1, const NDIndex< Dim2 > &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
static void combine(const NDIndex< Dim > &s1, const Index &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
Definition: rbendmap.h:8
static void combine(const SOffset< Dim > &s1, const SOffset< Dim2 > &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
Definition: SIndex.h:28
static int construct(NDIndex< Dim > &out, const int &s, BareField< T, Dim > &)
static int construct(SIndex< Dim > &out, const SOffset< Dim > &s, BareField< T, Dim > &A)
static int construct(NDIndex< Dim > &out, const NDIndex< Dim2 > &s, BareField< T, Dim > &)
#define CTAssert(c)
Definition: PAssert.h:40
static int construct(NDIndex< Dim > &out, const SOffset< Dim2 > &s, BareField< T, Dim > &)
static int construct(SOffset< Dim > &out, const int &s, BareField< T, Dim > &)
static void combine(const SOffset< Dim > &s1, const NDIndex< Dim2 > &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
static void combine(const NDIndex< Dim > &s1, const int &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
Definition: Index.h:236
static void combine(const SIndex< Dim > &s1, const NDIndex< Dim > &s2, Return_t &out, unsigned int, BareField< T, Dim > &A)
static int construct(NDIndex< Dim > &out, const Index &s, BareField< T, Dim > &)
static int construct(SIndex< Dim > &out, const SIndex< Dim > &s, BareField< T, Dim > &)
static void combine(const SOffset< Dim > &s1, const Index &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
static int construct(S1 &, const S2 &, BareField< T, Dim > &)
static int construct(SIndex< Dim > &out, const NDIndex< Dim > &s, BareField< T, Dim > &A)
static void combine(const SOffset< Dim > &s1, const int &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
static void combine(const SIndex< Dim > &s1, const SIndex< Dim > &s2, Return_t &out, unsigned int, BareField< T, Dim > &A)
Definition: FFT.h:30
void initialize(FieldLayout< Dim > &)
Definition: SIndex.hpp:124
static int construct(SOffset< Dim > &out, const SOffset< Dim2 > &s, BareField< T, Dim > &)
static void combine(const S1 &, const S2 &, S3 &, unsigned int &, BareField< T, Dim > &)
static void combine(const NDIndex< Dim > &s1, const SOffset< Dim2 > &s2, Return_t &out, unsigned int B, BareField< T, Dim > &)
static void combine(const SIndex< Dim > &s1, const SOffset< Dim > &s2, Return_t &out, unsigned int, BareField< T, Dim > &A)
#define PInsist(c, m)
Definition: PAssert.h:135
const unsigned Dim
bool needInitialize() const
Definition: SIndex.h:95