/[escript]/branches/trilinos_from_5897/escriptcore/src/DataTypes.h
ViewVC logotype

Contents of /branches/trilinos_from_5897/escriptcore/src/DataTypes.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5963 - (show annotations)
Mon Feb 22 06:59:27 2016 UTC (3 years, 4 months ago) by caltinay
File MIME type: text/plain
File size: 10429 byte(s)
sync and fix.

1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2016 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17
18 #if !defined escript_DataTypes_20080811_H
19 #define escript_DataTypes_20080811_H
20 #include "system_dep.h"
21 #include "esysUtils/EsysAssert.h"
22 #include <vector>
23 #include <string>
24 #include <boost/python/object.hpp>
25
26 namespace escript {
27
28 namespace DataTypes {
29
30 /**
31 \namespace escript::DataTypes
32 \brief Contains the types to represent Shapes, Regions, RegionLoop ranges and
33 vectors of data as well as the functions to manipulate them.
34 \note The contents of the namespace are spread between DataTypes.h and DataVector.h
35 */
36
37 //
38 // Some basic types which define the data values and view shapes.
39 typedef std::vector<int> ShapeType;//!< The shape of a single datapoint.
40 typedef std::vector<std::pair<int, int> > RegionType;
41 typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
42 static const int maxRank=4;//!< The maximum number of dimensions a datapoint can have.
43 static const ShapeType scalarShape;//!< Use this instead of creating empty shape objects for scalars.
44 typedef long vec_size_type;
45
46 /// type of all real-valued scalars in escript
47 typedef double real_t;
48
49 /// complex data type
50 typedef std::complex<real_t> cplx_t;
51
52 /// type for array/matrix indices used both globally and on each rank
53 #ifdef ESYS_INDEXTYPE_LONG
54 typedef long index_t;
55 #else
56 typedef int index_t;
57 #endif
58
59 typedef index_t dim_t;
60
61 /**
62 \brief
63 Returns the minimum finite value for the index_t type.
64 */
65 inline index_t index_t_min()
66 {
67 return std::numeric_limits<index_t>::min();
68 }
69
70 /**
71 \brief
72 Returns the maximum finite value for the index_t type.
73 */
74 inline index_t index_t_max()
75 {
76 return std::numeric_limits<index_t>::max();
77 }
78
79 /**
80 \brief
81 Returns the maximum finite value for the real_t type.
82 */
83 inline real_t real_t_max()
84 {
85 return std::numeric_limits<real_t>::max();
86 }
87
88 /**
89 \brief
90 Returns the machine epsilon for the real_t type.
91 */
92 inline real_t real_t_eps()
93 {
94 return std::numeric_limits<real_t>::epsilon();
95 }
96
97 /**
98 \brief
99 Calculate the number of values in a datapoint with the given shape.
100 */
101 ESCRIPT_DLL_API
102 int
103 noValues(const DataTypes::ShapeType& shape);
104
105 /**
106 \brief
107 Calculate the number of values for the given region.
108 */
109 ESCRIPT_DLL_API
110 int
111 noValues(const DataTypes::RegionLoopRangeType& region);
112
113 /**
114 \brief
115 Return the given shape as a string.
116
117 \param shape - Input.
118 */
119 ESCRIPT_DLL_API
120 std::string
121 shapeToString(const DataTypes::ShapeType& shape);
122
123 /**
124 \brief
125 Determine the shape of the specified slice region.
126
127 \param region - Input - Slice region
128 */
129 ESCRIPT_DLL_API
130 DataTypes::ShapeType
131 getResultSliceShape(const DataTypes::RegionType& region);
132
133
134 /**
135 \brief
136 Determine the region specified by the given python slice object.
137
138 \param shape - Input - Shape of the object being sliced.
139 \param key - Input -
140 python slice object specifying region to be returned.
141
142 The slice object is a tuple of n python slice specifiers, where
143 n <= the rank of this Data object. Each slice specifier specifies the
144 range of indexes to be sliced from the corresponding dimension. The
145 first specifier corresponds to the first dimension, the second to the
146 second and so on. Where n < the rank, the remaining dimensions are
147 sliced across the full range of their indices.
148
149 Each slice specifier is of the form "a:b", which specifies a slice
150 from index a, up to but not including index b. Where index a is omitted
151 a is assumed to be 0. Where index b is omitted, b is assumed to be the
152 length of this dimension. Where both are omitted (eg: ":") the slice is
153 assumed to encompass that entire dimension.
154
155 Where one of the slice specifiers is a single integer, eg: [1], we
156 want to generate a rank-1 dimension object, as opposed to eg: [1,2]
157 which implies we want to take a rank dimensional object with one
158 dimension of size 1.
159
160 The return value is a vector of pairs with length equal to the rank of
161 this object. Each pair corresponds to the range of indices from the
162 corresponding dimension to be sliced from, as specified in the input
163 slice object.
164
165 Examples:
166
167 For a rank 1 object of shape(5):
168
169 getSliceRegion(:) => < <0,5> >
170 getSliceRegion(2:3) => < <2,3> >
171 getSliceRegion(:3) => < <0,3> >
172 getSliceRegion(2:) => < <2,5> >
173
174 For a rank 2 object of shape(4,5):
175
176 getSliceRegion(2:3) => < <2,3> <0,5> >
177 getSliceRegion(2) => < <2,3> <0,5> >
178 NB: but return object requested will have rank 1, shape(5), with
179 values taken from index 2 of this object's first dimension.
180
181 For a rank 3 object of shape (2,4,6):
182
183 getSliceRegion(0:2,0:4,0:6) => < <0,2> <0,4> <0,6> >
184 getSliceRegion(:,:,:) => < <0,2> <0,4> <0,6> >
185 getSliceRegion(0:1) => < <0,1> <0,4> <0,6> >
186 getSliceRegion(:1,0:2) => < <0,1> <0,2> <0,6> >
187
188
189 Note: Not unit tested in c++.
190 */
191 ESCRIPT_DLL_API
192 DataTypes::RegionType
193 getSliceRegion(const DataTypes::ShapeType& shape, const boost::python::object& key);
194
195 /**
196 \brief
197 Modify region to copy from in order to
198 deal with the case where one range in the region contains identical indexes,
199 eg: <<1,1><0,3><0,3>>
200 This situation implies we want to copy from an object with rank greater than that of this
201 object. eg: we want to copy the values from a two dimensional slice out of a three
202 dimensional object into a two dimensional object.
203 We do this by taking a slice from the other object where one dimension of
204 the slice region is of size 1. So in the above example, we modify the above
205 region like so: <<1,2><0,3><0,3>> and take this slice.
206 */
207 ESCRIPT_DLL_API
208 DataTypes::RegionLoopRangeType
209 getSliceRegionLoopRange(const DataTypes::RegionType& region);
210
211 /**
212 \brief Return the rank (number of dimensions) of the given shape.
213
214 \param shape
215 \return the rank.
216 */
217 ESCRIPT_DLL_API
218 inline
219 int
220 getRank(const DataTypes::ShapeType& shape)
221 {
222 return shape.size();
223 }
224
225
226 /**
227 \brief Compute the offset (in 1D vector) of a given subscript with a shape.
228
229 \param shape - Input - Shape of the datapoint.
230 \param i - Input - subscript to locate.
231 \return offset relative to the beginning of the datapoint.
232 */
233 ESCRIPT_DLL_API
234 inline
235 vec_size_type
236 getRelIndex(const DataTypes::ShapeType& shape, vec_size_type i)
237 {
238 EsysAssert((getRank(shape)==1),"Incorrect number of indices for the rank of this object.");
239 EsysAssert((i < DataTypes::noValues(shape)), "Error - Invalid index.");
240 return i;
241 }
242
243 /**
244 \brief Compute the offset (in 1D vector) of a given subscript with a shape.
245
246 \param shape - Input - Shape of the datapoint.
247 \param i - Input - row
248 \param j - Input - column
249 \return offset relative to the beginning of the datapoint.
250 */
251 ESCRIPT_DLL_API
252 inline
253 vec_size_type
254 getRelIndex(const DataTypes::ShapeType& shape, vec_size_type i,
255 vec_size_type j)
256 {
257 // Warning: This is not C ordering. Do not try to figure out the params by looking at the code
258 EsysAssert((getRank(shape)==2),"Incorrect number of indices for the rank of this object.");
259 vec_size_type temp=i+j*shape[0];
260 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
261 return temp;
262 }
263
264 /**
265 \brief Compute the offset (in 1D vector) of a given subscript with a shape.
266
267 \param shape - Input - Shape of the datapoint.
268 \param i,j,k - Input - subscripts to locate.
269 \return offset relative to the beginning of the datapoint.
270 */
271 ESCRIPT_DLL_API
272 inline
273 vec_size_type
274 getRelIndex(const DataTypes::ShapeType& shape, vec_size_type i,
275 vec_size_type j, vec_size_type k)
276 {
277 // Warning: This is not C ordering. Do not try to figure out the params by looking at the code
278 EsysAssert((getRank(shape)==3),"Incorrect number of indices for the rank of this object.");
279 vec_size_type temp=i+j*shape[0]+k*shape[1]*shape[0];
280 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
281 return temp;
282 }
283
284 /**
285 \brief Compute the offset (in 1D vector) of a given subscript with a shape.
286
287 \param shape - Input - Shape of the datapoint.
288 \param i,j,k,m - Input - subscripts to locate.
289 \return offset relative to the beginning of the datapoint.
290 */
291 ESCRIPT_DLL_API
292 inline
293 vec_size_type
294 getRelIndex(const DataTypes::ShapeType& shape, vec_size_type i,
295 vec_size_type j, vec_size_type k,
296 vec_size_type m)
297 {
298 // Warning: This is not C ordering. Do not try to figure out the params by looking at the code
299 EsysAssert((getRank(shape)==4),"Incorrect number of indices for the rank of this object.");
300 vec_size_type temp=i+j*shape[0]+k*shape[1]*shape[0]+m*shape[2]*shape[1]*shape[0];
301 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
302 return temp;
303 }
304
305 /**
306 \brief Test if two shapes are equal.
307 */
308 ESCRIPT_DLL_API
309 inline
310 bool
311 checkShape(const ShapeType& s1, const ShapeType& s2)
312 {
313 return s1==s2;
314 }
315
316 /**
317 \brief Produce a string containing two shapes.
318
319 \param messagePrefix - Beginning of the message.
320 \param other - displayed in the message as "Other shape"
321 \param thisShape - displayed in the message as "This shape"
322 */
323 ESCRIPT_DLL_API
324 std::string
325 createShapeErrorMessage(const std::string& messagePrefix,
326 const DataTypes::ShapeType& other,
327 const DataTypes::ShapeType& thisShape);
328
329 inline
330 bool
331 checkOffset(vec_size_type offset, int size, int noval)
332 {
333 return (size >= (offset+noval));
334 }
335
336 } // End of namespace DataTypes
337
338
339 } // End of namespace escript
340
341 #endif
342

  ViewVC Help
Powered by ViewVC 1.1.26