/[escript]/trunk/escript/src/DataTypes.h
ViewVC logotype

Contents of /trunk/escript/src/DataTypes.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1747 - (show annotations)
Wed Sep 3 04:56:50 2008 UTC (11 years, 2 months ago) by jfenwick
Original Path: branches/arrayview_from_1695_trunk/escript/src/DataTypes.h
File MIME type: text/plain
File size: 9926 byte(s)
Branch commit.

Current status, this version compiles as passes run_tests (haven't tried 
all_tests), provided that the following files are moved out of the way:

DataAlgorithmAdapterTestCase.h 
DataTaggedTestCase.cpp 
DataTestCase.cpp 
DataTaggedTestCase.h

(They are not run by the unit tests at the moment).

I will fix those next.



1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2008 by ACceSS MNRF
7 * Copyright 2008 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 #if !defined escript_DataTypes_20080811_H
17 #define escript_DataTypes_20080811_H
18 #include "system_dep.h"
19 #include "DataVector.h"
20 #include <vector>
21 #include <string>
22 #include <boost/python/object.hpp>
23 #include <boost/python/extract.hpp>
24
25 namespace escript {
26
27 namespace DataTypes {
28 //
29 // Some basic types which define the data values and view shapes.
30 typedef DataVector ValueType;
31 typedef std::vector<int> ShapeType;
32 typedef std::vector<std::pair<int, int> > RegionType;
33 typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
34 static const int maxRank=4;
35 static const ShapeType scalarShape;
36
37 // This file contains static functions moved from DataArrayView
38 /**
39 \brief
40 Calculate the number of values for the given shape.
41 */
42 ESCRIPT_DLL_API
43 int
44 noValues(const DataTypes::ShapeType& shape);
45
46 /**
47 \brief
48 Calculate the number of values for the given region.
49 */
50 ESCRIPT_DLL_API
51 int
52 noValues(const DataTypes::RegionLoopRangeType& region);
53
54 /**
55 \brief
56 Return the given shape as a string.
57
58 \param shape - Input.
59 */
60 ESCRIPT_DLL_API
61 std::string
62 shapeToString(const DataTypes::ShapeType& shape);
63
64 /**
65 \brief
66 Determine the shape of the specified slice region.
67
68 \param region - Input -
69 Slice region.
70 */
71 ESCRIPT_DLL_API
72 DataTypes::ShapeType
73 getResultSliceShape(const DataTypes::RegionType& region);
74
75
76 /**
77 \brief
78 Determine the region specified by the given python slice object.
79
80 \param key - Input -
81 python slice object specifying region to be returned.
82
83 The slice object is a tuple of n python slice specifiers, where
84 n <= the rank of this Data object. Each slice specifier specifies the
85 range of indexes to be sliced from the corresponding dimension. The
86 first specifier corresponds to the first dimension, the second to the
87 second and so on. Where n < the rank, the remaining dimensions are
88 sliced across the full range of their indicies.
89
90 Each slice specifier is of the form "a:b", which specifies a slice
91 from index a, up to but not including index b. Where index a is ommitted
92 a is assumed to be 0. Where index b is ommitted, b is assumed to be the
93 length of this dimension. Where both are ommitted (eg: ":") the slice is
94 assumed to encompass that entire dimension.
95
96 Where one of the slice specifiers is a single integer, eg: [1], we
97 want to generate a rank-1 dimension object, as opposed to eg: [1,2]
98 which implies we want to take a rank dimensional object with one
99 dimension of size 1.
100
101 The return value is a vector of pairs with length equal to the rank of
102 this object. Each pair corresponds to the range of indicies from the
103 corresponding dimension to be sliced from, as specified in the input
104 slice object.
105
106 Examples:
107
108 For a rank 1 object of shape(5):
109
110 getSliceRegion(:) => < <0,5> >
111 getSliceRegion(2:3) => < <2,3> >
112 getSliceRegion(:3) => < <0,3> >
113 getSliceRegion(2:) => < <2,5> >
114
115 For a rank 2 object of shape(4,5):
116
117 getSliceRegion(2:3) => < <2,3> <0,5> >
118 getSliceRegion(2) => < <2,3> <0,5> >
119 NB: but return object requested will have rank 1, shape(5), with
120 values taken from index 2 of this object's first dimension.
121
122 For a rank 3 object of shape (2,4,6):
123
124 getSliceRegion(0:2,0:4,0:6) => < <0,2> <0,4> <0,6> >
125 getSliceRegion(:,:,:) => < <0,2> <0,4> <0,6> >
126 getSliceRegion(0:1) => < <0,1> <0,4> <0,6> >
127 getSliceRegion(:1,0:2) => < <0,1> <0,2> <0,6> >
128
129
130 Note: Not unit tested in c++.
131 */
132 ESCRIPT_DLL_API
133 DataTypes::RegionType
134 getSliceRegion(const DataTypes::ShapeType& shape, const boost::python::object& key);
135
136 /**
137 \brief
138 Modify region to copy from in order to
139 deal with the case where one range in the region contains identical indexes,
140 eg: <<1,1><0,3><0,3>>
141 This situation implies we want to copy from an object with rank greater than that of this
142 object. eg: we want to copy the values from a two dimensional slice out of a three
143 dimensional object into a two dimensional object.
144 We do this by taking a slice from the other object where one dimension of
145 the slice region is of size 1. So in the above example, we modify the above
146 region like so: <<1,2><0,3><0,3>> and take this slice.
147 */
148 DataTypes::RegionLoopRangeType
149 getSliceRegionLoopRange(const DataTypes::RegionType& region);
150
151 ESCRIPT_DLL_API
152 inline
153 int
154 getRank(const DataTypes::ShapeType& shape)
155 {
156 return shape.size();
157 }
158
159 ESCRIPT_DLL_API
160 inline
161 DataTypes::ValueType::size_type
162 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i)
163 {
164 EsysAssert((getRank(shape)==1),"Incorrect number of indices for the rank of this object.");
165 EsysAssert((i < DataTypes::noValues(shape)), "Error - Invalid index.");
166 return i;
167 }
168
169 ESCRIPT_DLL_API
170 inline
171 DataTypes::ValueType::size_type
172 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
173 DataTypes::ValueType::size_type j)
174 {
175 EsysAssert((getRank(shape)==2),"Incorrect number of indices for the rank of this object.");
176 DataTypes::ValueType::size_type temp=i+j*shape[0];
177 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
178 return temp;
179 }
180
181 ESCRIPT_DLL_API
182 inline
183 DataTypes::ValueType::size_type
184 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
185 DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k)
186 {
187 EsysAssert((getRank(shape)==3),"Incorrect number of indices for the rank of this object.");
188 DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0];
189 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
190 return temp;
191 }
192
193 ESCRIPT_DLL_API
194 inline
195 DataTypes::ValueType::size_type
196 getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
197 DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k,
198 DataTypes::ValueType::size_type m)
199 {
200 EsysAssert((getRank(shape)==4),"Incorrect number of indices for the rank of this object.");
201 DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0]+m*shape[2]*shape[1]*shape[0];
202 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
203 return temp;
204 }
205
206 ESCRIPT_DLL_API
207 inline
208 bool
209 checkShape(const ShapeType& s1, const ShapeType& s2)
210 {
211 return s1==s2;
212 }
213
214 std::string
215 createShapeErrorMessage(const std::string& messagePrefix,
216 const DataTypes::ShapeType& other,
217 const DataTypes::ShapeType& thisShape);
218
219
220 /**
221 \brief
222 Copy a data slice specified by the given region and offset from the
223 given view into this view at the given offset.
224
225 \param thisOffset - Input -
226 Copy the slice to this offset in this view.
227 \param other - Input -
228 View to copy data from.
229 \param otherOffset - Input -
230 Copy the slice from this offset in the given view.
231 \param region - Input -
232 Region in other view to copy data from.
233 */
234 void
235 copySlice(ValueType& left,
236 const ShapeType& leftShape,
237 ValueType::size_type thisOffset,
238 const ValueType& other,
239 const ShapeType& otherShape,
240 ValueType::size_type otherOffset,
241 const RegionLoopRangeType& region);
242
243 /**
244 \brief
245 Copy data into a slice specified by the given region and offset in
246 this view from the given view at the given offset.
247
248 \param thisOffset - Input -
249 Copy the slice to this offset in this view.
250 \param other - Input -
251 View to copy data from.
252 \param otherOffset - Input -
253 Copy the slice from this offset in the given view.
254 \param region - Input -
255 Region in this view to copy data to.
256 */
257 void
258 copySliceFrom(ValueType& left,
259 const ShapeType& leftShape,
260 ValueType::size_type thisOffset,
261 const ValueType& other,
262 const ShapeType& otherShape,
263 ValueType::size_type otherOffset,
264 const RegionLoopRangeType& region);
265
266
267 /**
268 \brief Display a single value (with the specified shape) from the data
269 */
270 std::string
271 pointToString(const ValueType& data,const ShapeType& shape, int offset, const std::string& suffix);
272
273
274 /**
275 \brief Extract shape information from the supplied numarray.
276 */
277 inline
278 ShapeType
279 shapeFromNumArray(const boost::python::numeric::array& value)
280 {
281 // extract the shape of the numarray
282 DataTypes::ShapeType tempShape;
283 for (int i=0; i < value.getrank(); i++) {
284 tempShape.push_back(boost::python::extract<int>(value.getshape()[i]));
285 }
286 return tempShape;
287 }
288
289
290 /**
291 \brief Copy a point from one vector to another. Note: This version does not check to see if shapes are the same.
292 */
293 void copyPoint(ValueType& dest, ValueType::size_type doffset, ValueType::size_type nvals, const ValueType& src, ValueType::size_type soffset);
294
295 } // End namespace DataTypes
296
297
298 } // end of namespace escipt
299
300 #endif

  ViewVC Help
Powered by ViewVC 1.1.26