YAKL
YAKL_Array.h
Go to the documentation of this file.
1 
8 #pragma once
9 // Included by YAKL.h
10 
11 #include "YAKL_CSArray.h"
12 #include "YAKL_FSArray.h"
13 
15 namespace yakl {
16 
17  // Labels for Array styles. C has zero-based indexing with the last index varying the fastest.
18  // Fortran has 1-based indexing with arbitrary lower bounds and the index varying the fastest.
20  int constexpr styleC = 1;
22  int constexpr styleFortran = 2;
24  int constexpr styleDefault = styleC;
25 
28  int constexpr COLON = std::numeric_limits<int>::min(); // Label for the ":" from Fortrna array slicing
29 
30  // The one template to rule them all for the Array class
31  // This ultimately describes dynamics and static / stack Arrays in all types, ranks, memory spaces, and styles
40  template <class T, int rank, int myMem=memDefault, int myStyle=styleDefault> class Array;
41 
42 
43  // This class is used to describe a set of dimensions used for Array slicing. One can call a constructor
44  // with std::initialize_list (i.e., {1,2,3...} syntax)
50  class Dims {
51  public:
53  int data[8];
55  int rank;
56 
57  YAKL_INLINE Dims() {rank = 0;}
59  YAKL_INLINE Dims(int i0) {
60  data[0] = i0;
61  rank = 1;
62  }
64  YAKL_INLINE Dims(int i0, int i1) {
65  data[0] = i0;
66  data[1] = i1;
67  rank = 2;
68  }
70  YAKL_INLINE Dims(int i0, int i1, int i2) {
71  data[0] = i0;
72  data[1] = i1;
73  data[2] = i2;
74  rank = 3;
75  }
77  YAKL_INLINE Dims(int i0, int i1, int i2, int i3) {
78  data[0] = i0;
79  data[1] = i1;
80  data[2] = i2;
81  data[3] = i3;
82  rank = 4;
83  }
85  YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4) {
86  data[0] = i0;
87  data[1] = i1;
88  data[2] = i2;
89  data[3] = i3;
90  data[4] = i4;
91  rank = 5;
92  }
94  YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4, int i5) {
95  data[0] = i0;
96  data[1] = i1;
97  data[2] = i2;
98  data[3] = i3;
99  data[4] = i4;
100  data[5] = i5;
101  rank = 6;
102  }
104  YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4, int i5, int i6) {
105  data[0] = i0;
106  data[1] = i1;
107  data[2] = i2;
108  data[3] = i3;
109  data[4] = i4;
110  data[5] = i5;
111  data[6] = i6;
112  rank = 7;
113  }
115  YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
116  data[0] = i0;
117  data[1] = i1;
118  data[2] = i2;
119  data[3] = i3;
120  data[4] = i4;
121  data[5] = i5;
122  data[6] = i6;
123  data[7] = i7;
124  rank = 8;
125  }
126 
127  YAKL_INLINE Dims(Dims const &dims) {
128  rank = dims.rank;
129  for (int i=0; i < rank; i++) { data[i] = dims[i]; }
130  }
131  YAKL_INLINE Dims &operator=(Dims const &dims) {
132  if (this == &dims) { return *this; }
133  rank = dims.rank;
134  for (int i=0; i < rank; i++) { data[i] = dims[i]; }
135  return *this;
136  }
138  rank = dims.rank;
139  for (int i=0; i < rank; i++) { data[i] = dims[i]; }
140  }
142  if (this == &dims) { return *this; }
143  rank = dims.rank;
144  for (int i=0; i < rank; i++) { data[i] = dims[i]; }
145  return *this;
146  }
147 
149  template <class INT, typename std::enable_if< std::is_integral<INT>::value , bool>::type = false>
150  Dims(std::vector<INT> const dims) {
151  rank = dims.size();
152  for (int i=0; i < rank; i++) { data[i] = dims[i]; }
153  }
154 
156  template <class INT, unsigned int RANK, typename std::enable_if< std::is_integral<INT>::value , bool>::type = false>
157  Dims(CSArray<INT,1,RANK> const dims) {
158  rank = RANK;
159  for (int i=0; i < rank; i++) { data[i] = dims(i); }
160  }
161 
163  YAKL_INLINE int operator[] (int i) const { return data[i]; }
164 
166  YAKL_INLINE int size() const { return rank; }
167  };
168 
169 
170 
171  // Describes a single array bound. Used for Fortran array bounds
177  struct Bnd {
178  public:
180  int l;
182  int u;
184  YAKL_INLINE Bnd( ) { l = 1 ; u = 1 ; }
186  YAKL_INLINE Bnd( int u_in) { l = 1 ; u = u_in; }
188  YAKL_INLINE Bnd(int l_in, int u_in) { l = l_in; u = u_in; }
189  };
190 
191 
192 
193  // Describes a set of array bounds. use for Fortran array bounds
200  class Bnds {
201  public:
203  int l[8];
205  int u[8];
207  int rank;
208 
209  YAKL_INLINE Bnds() {rank = 0;}
212  l[0] = b0.l; u[0] = b0.u;
213  rank = 1;
214  }
217  l[0] = b0.l; u[0] = b0.u;
218  l[1] = b1.l; u[1] = b1.u;
219  rank = 2;
220  }
222  YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2) {
223  l[0] = b0.l; u[0] = b0.u;
224  l[1] = b1.l; u[1] = b1.u;
225  l[2] = b2.l; u[2] = b2.u;
226  rank = 3;
227  }
229  YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3) {
230  l[0] = b0.l; u[0] = b0.u;
231  l[1] = b1.l; u[1] = b1.u;
232  l[2] = b2.l; u[2] = b2.u;
233  l[3] = b3.l; u[3] = b3.u;
234  rank = 4;
235  }
237  YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4) {
238  l[0] = b0.l; u[0] = b0.u;
239  l[1] = b1.l; u[1] = b1.u;
240  l[2] = b2.l; u[2] = b2.u;
241  l[3] = b3.l; u[3] = b3.u;
242  l[4] = b4.l; u[4] = b4.u;
243  rank = 5;
244  }
246  YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4, Bnd b5) {
247  l[0] = b0.l; u[0] = b0.u;
248  l[1] = b1.l; u[1] = b1.u;
249  l[2] = b2.l; u[2] = b2.u;
250  l[3] = b3.l; u[3] = b3.u;
251  l[4] = b4.l; u[4] = b4.u;
252  l[5] = b5.l; u[5] = b5.u;
253  rank = 6;
254  }
256  YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4, Bnd b5, Bnd b6) {
257  l[0] = b0.l; u[0] = b0.u;
258  l[1] = b1.l; u[1] = b1.u;
259  l[2] = b2.l; u[2] = b2.u;
260  l[3] = b3.l; u[3] = b3.u;
261  l[4] = b4.l; u[4] = b4.u;
262  l[5] = b5.l; u[5] = b5.u;
263  l[6] = b6.l; u[6] = b6.u;
264  rank = 7;
265  }
267  YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4, Bnd b5, Bnd b6, Bnd b7) {
268  l[0] = b0.l; u[0] = b0.u;
269  l[1] = b1.l; u[1] = b1.u;
270  l[2] = b2.l; u[2] = b2.u;
271  l[3] = b3.l; u[3] = b3.u;
272  l[4] = b4.l; u[4] = b4.u;
273  l[5] = b5.l; u[5] = b5.u;
274  l[6] = b6.l; u[6] = b6.u;
275  l[7] = b7.l; u[7] = b7.u;
276  rank = 8;
277  }
278  YAKL_INLINE Bnds(Bnds const &bnds) {
279  rank = bnds.rank;
280  for (int i=0; i < rank; i++) { l[i] = bnds.l[i]; u[i] = bnds.u[i]; }
281  }
282  YAKL_INLINE Bnds &operator=(Bnds const &bnds) {
283  if (this == &bnds) { return *this; }
284  rank = bnds.rank;
285  for (int i=0; i < rank; i++) { l[i] = bnds.l[i]; u[i] = bnds.u[i]; }
286  return *this;
287  }
289  rank = bnds.rank;
290  for (int i=0; i < rank; i++) { l[i] = bnds.l[i]; u[i] = bnds.u[i]; }
291  }
293  if (this == &bnds) { return *this; }
294  rank = bnds.rank;
295  for (int i=0; i < rank; i++) { l[i] = bnds.l[i]; u[i] = bnds.u[i]; }
296  return *this;
297  }
299  Bnds(std::vector<Bnd> const bnds) {
300  rank = bnds.size();
301  for (int i=0; i < rank; i++) { l[i] = bnds[i].l; u[i] = bnds[i].u; }
302  }
304  template <class INT, typename std::enable_if< std::is_integral<INT>::value , bool>::type = false>
305  Bnds(std::vector<INT> const bnds) {
306  rank = bnds.size();
307  for (int i=0; i < rank; i++) { l[i] = 1; u[i] = bnds[i]; }
308  }
309 
312  template <class INT, unsigned int RANK, typename std::enable_if< std::is_integral<INT>::value , bool>::type = false>
313  Bnds(CSArray<INT,1,RANK> const dims) {
314  rank = RANK;
315  for (int i=0; i < rank; i++) { l[i] = 1; u[i] = dims(i); }
316  }
317 
320  template <class INT, int LOWER, int UPPER, typename std::enable_if< std::is_integral<INT>::value , bool>::type = false>
321  Bnds(FSArray<INT,1,SB<LOWER,UPPER>> const dims) {
322  rank = UPPER-LOWER+1;
323  for (int i=LOWER; i <= UPPER; i++) { l[i] = 1; u[i] = dims(i); }
324  }
325 
328  template <class INT, int LOWER1, int UPPER1, int LOWER2, int UPPER2, typename std::enable_if< std::is_integral<INT>::value , bool>::type = false>
329  Bnds(FSArray<INT,1,SB<LOWER1,UPPER1>> const lbounds, FSArray<INT,1,SB<LOWER2,UPPER2>> const ubounds) {
330  static_assert( UPPER1-LOWER1+1 == UPPER2-LOWER2+1 , "ERROR: lbounds and ubounds sizes are not equal" );
331  rank = UPPER1-LOWER1+1;
332  for (int i=LOWER1; i <= UPPER1; i++) { l[i] = lbounds(i); }
333  for (int i=LOWER2; i <= UPPER2; i++) { u[i] = ubounds(i); }
334  }
335 
337  YAKL_INLINE Bnd operator[] (int i) const { return Bnd(l[i],u[i]); }
338 
340  YAKL_INLINE int size() const { return rank; }
341  };
342 
343 }
345 
346 #include "YAKL_ArrayBase.h"
347 #include "YAKL_CArray.h"
348 #include "YAKL_FArray.h"
349 
350 
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1, int i2, int i3)
Construct a 4-D Dims object)
Definition: YAKL_Array.h:77
yakl::Bnds::Bnds
Bnds(std::vector< INT > const bnds)
Allows an initializer list or std::vector to be converted to a yakl::Bnds object.
Definition: YAKL_Array.h:305
yakl::Dims::Dims
YAKL_INLINE Dims(Dims const &dims)
Definition: YAKL_Array.h:127
yakl::Bnds::size
YAKL_INLINE int size() const
Get the number of dimensions.
Definition: YAKL_Array.h:340
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1)
Construct a 2-D Dims object)
Definition: YAKL_Array.h:64
yakl::Dims::Dims
Dims(std::vector< INT > const dims)
This constructor allows converting a std::vector or initializer list to a yakl::Dims object.
Definition: YAKL_Array.h:150
yakl::Bnds::Bnds
Bnds(CSArray< INT, 1, RANK > const dims)
Allows CSArray object to be converted to a yakl::Bnds object if default lower bounds of 1 are desired...
Definition: YAKL_Array.h:313
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnds const &bnds)
Definition: YAKL_Array.h:278
yakl::Dims::Dims
Dims(CSArray< INT, 1, RANK > const dims)
This constructor allows converting a CSArray object to a yakl::Dims object.
Definition: YAKL_Array.h:157
yakl::Bnds::Bnds
Bnds(FSArray< INT, 1, SB< LOWER, UPPER >> const dims)
Allows FSArray object to be converted to a yakl::Bnds object if default lower bounds of 1 are desired...
Definition: YAKL_Array.h:321
yakl::Bnds::operator=
YAKL_INLINE Bnds & operator=(Bnds &&bnds)
Definition: YAKL_Array.h:292
yakl::Dims::operator=
YAKL_INLINE Dims & operator=(Dims &&dims)
Definition: YAKL_Array.h:141
yakl::Dims::operator=
YAKL_INLINE Dims & operator=(Dims const &dims)
Definition: YAKL_Array.h:131
YAKL_ArrayBase.h
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4)
Construct an 5-D Dims object)
Definition: YAKL_Array.h:237
yakl::Bnds::Bnds
Bnds(std::vector< Bnd > const bnds)
Allows an initializer list or std::vector to be converted to a yakl::Bnds object.
Definition: YAKL_Array.h:299
yakl::Bnds::operator=
YAKL_INLINE Bnds & operator=(Bnds const &bnds)
Definition: YAKL_Array.h:282
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4, int i5)
Construct a 6-D Dims object)
Definition: YAKL_Array.h:94
yakl::Bnd::Bnd
YAKL_INLINE Bnd(int l_in, int u_in)
Create a dimension bound with a lower limit of l_in and an upper limit of u_in.
Definition: YAKL_Array.h:188
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2)
Construct an 3-D Dims object)
Definition: YAKL_Array.h:222
yakl::styleDefault
constexpr int styleDefault
Default style is C-style for yakl::Array objects.
Definition: YAKL_Array.h:24
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0)
Construct an 1-D Dims object)
Definition: YAKL_Array.h:211
yakl::Bnds::Bnds
YAKL_INLINE Bnds()
Definition: YAKL_Array.h:209
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1)
Construct an 2-D Dims object)
Definition: YAKL_Array.h:216
YAKL_FSArray.h
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4, Bnd b5)
Construct an 6-D Dims object)
Definition: YAKL_Array.h:246
yakl::SB
This specifies a set of bounds for a dimension when declaring a yakl::FSArray.
Definition: YAKL_FSArray.h:18
yakl::Dims::Dims
YAKL_INLINE Dims()
Definition: YAKL_Array.h:57
YAKL_CSArray.h
yakl::Bnd::Bnd
YAKL_INLINE Bnd(int u_in)
Create a dimension bound with a lower limit of 1 and an upper limit of u_in.
Definition: YAKL_Array.h:186
yakl::COLON
constexpr int COLON
This is just a convenience syntax for slicing yakl::Array objects to make it clear in the user level ...
Definition: YAKL_Array.h:28
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4, Bnd b5, Bnd b6)
Construct an 7-D Dims object)
Definition: YAKL_Array.h:256
yakl::Dims::Dims
YAKL_INLINE Dims(Dims &&dims)
Definition: YAKL_Array.h:137
yakl::Bnd
Describes a single bound for creating Fortran-style yakl::Array objects.
Definition: YAKL_Array.h:177
YAKL_INLINE
#define YAKL_INLINE
Used to decorate functions called from kernels (parallel_for and parallel_outer) or from CPU function...
Definition: YAKL_defines.h:140
yakl::Bnds::Bnds
Bnds(FSArray< INT, 1, SB< LOWER1, UPPER1 >> const lbounds, FSArray< INT, 1, SB< LOWER2, UPPER2 >> const ubounds)
Allows two FSArray objects (one for lower bounds, one for upper bounds) to be converted to a yakl::Bn...
Definition: YAKL_Array.h:329
yakl::Dims::size
YAKL_INLINE int size() const
Get the number of dimensions.
Definition: YAKL_Array.h:166
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnds &&bnds)
Definition: YAKL_Array.h:288
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1, int i2)
Construct a 3-D Dims object)
Definition: YAKL_Array.h:70
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
Construct an 8-D Dims object)
Definition: YAKL_Array.h:115
yakl::Bnds::operator[]
YAKL_INLINE Bnd operator[](int i) const
These objects are always indexed with square bracket notation like a std::vector or std::array.
Definition: YAKL_Array.h:337
yakl::styleC
constexpr int styleC
Template parameter for yakl::Array that specifies it should follow C-style behavior.
Definition: YAKL_Array.h:20
yakl::Dims::operator[]
YAKL_INLINE int operator[](int i) const
These objects are always indexed with square bracket notation like a std::vector or std::array.
Definition: YAKL_Array.h:163
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4, int i5, int i6)
Construct a 7-D Dims object)
Definition: YAKL_Array.h:104
yakl::Dims
This class holds C-style dimensions for using in yakl::Array objects.
Definition: YAKL_Array.h:50
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3, Bnd b4, Bnd b5, Bnd b6, Bnd b7)
Construct an 8-D Dims object)
Definition: YAKL_Array.h:267
yakl::Dims::Dims
YAKL_INLINE Dims(int i0, int i1, int i2, int i3, int i4)
Construct a 5-D Dims object)
Definition: YAKL_Array.h:85
yakl::Array
This declares the yakl::Array class. Please see the yakl::styleC and yakl::styleFortran template spec...
Definition: YAKL_Array.h:40
yakl::Bnds::Bnds
YAKL_INLINE Bnds(Bnd b0, Bnd b1, Bnd b2, Bnd b3)
Construct an 4-D Dims object)
Definition: YAKL_Array.h:229
yakl::CSArray
C-style array on the stack similar in nature to, e.g., float arr[ny][nx];
Definition: YAKL_CSArray.h:30
yakl::Bnd::Bnd
YAKL_INLINE Bnd()
Create a dimension bound with a lower limit of 1 and an upper limit of 1.
Definition: YAKL_Array.h:184
yakl::styleFortran
constexpr int styleFortran
Template parameter for yakl::Array that specifies it should follow Fortran-style behavior.
Definition: YAKL_Array.h:22
yakl
yakl::Dims::Dims
YAKL_INLINE Dims(int i0)
Construct a 1-D Dims object)
Definition: YAKL_Array.h:59
yakl::Bnds
This class holds Fortran-style dimensions for using in creating yakl::Array objects.
Definition: YAKL_Array.h:200
yakl::FSArray
Fortran-style array on the stack similar in nature to, e.g., float arr[ny][nx];
Definition: YAKL_FSArray.h:53
YAKL_FArray.h
YAKL_CArray.h