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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1838 - (hide annotations)
Fri Oct 3 02:05:01 2008 UTC (11 years, 11 months ago) by phornby
File MIME type: text/plain
File size: 12424 byte(s)
add 4 missing  ESCRIPT_DLL_API that prevented linking.
1 jfenwick 1697
2     /*******************************************************
3 ksteube 1811 *
4     * Copyright (c) 2003-2008 by University of Queensland
5     * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 jfenwick 1697
14 ksteube 1811
15 jfenwick 1697 #if !defined escript_DataTypes_20080811_H
16     #define escript_DataTypes_20080811_H
17     #include "system_dep.h"
18     #include "DataVector.h"
19     #include <vector>
20 jfenwick 1724 #include <string>
21 jfenwick 1714 #include <boost/python/object.hpp>
22 jfenwick 1747 #include <boost/python/extract.hpp>
23 jfenwick 1697
24     namespace escript {
25    
26 jfenwick 1779 namespace DataTypes {
27    
28     /**
29     \namespace escript::DataTypes
30     \brief Contains the types to represent Shapes, Regions, RegionLoop ranges and vectors of data as well as the functions to manipulate them.
31     */
32 jfenwick 1697 //
33     // Some basic types which define the data values and view shapes.
34 jfenwick 1779 typedef DataVector ValueType;//!< Vector to store underlying data.
35     typedef std::vector<int> ShapeType;//!< The shape of a single datapoint.
36 jfenwick 1697 typedef std::vector<std::pair<int, int> > RegionType;
37     typedef std::vector<std::pair<int, int> > RegionLoopRangeType;
38 jfenwick 1779 static const int maxRank=4;//!< The maximum number of dimensions a datapoint can have.
39     static const ShapeType scalarShape;//!< Use this instead of creating empty shape objects for scalars.
40 jfenwick 1697
41 jfenwick 1698 /**
42     \brief
43 jfenwick 1779 Calculate the number of values in a datapoint with the given shape.
44    
45     \param shape
46 jfenwick 1698 */
47     ESCRIPT_DLL_API
48     int
49     noValues(const DataTypes::ShapeType& shape);
50    
51 jfenwick 1779 /**
52 jfenwick 1698 \brief
53     Calculate the number of values for the given region.
54 jfenwick 1779
55     \param region
56 jfenwick 1698 */
57     ESCRIPT_DLL_API
58     int
59     noValues(const DataTypes::RegionLoopRangeType& region);
60    
61     /**
62     \brief
63     Return the given shape as a string.
64    
65     \param shape - Input.
66     */
67     ESCRIPT_DLL_API
68     std::string
69     shapeToString(const DataTypes::ShapeType& shape);
70    
71 jfenwick 1714 /**
72     \brief
73     Determine the shape of the specified slice region.
74 jfenwick 1698
75 jfenwick 1714 \param region - Input -
76     Slice region.
77     */
78 jfenwick 1704 ESCRIPT_DLL_API
79 jfenwick 1714 DataTypes::ShapeType
80     getResultSliceShape(const DataTypes::RegionType& region);
81    
82    
83     /**
84     \brief
85     Determine the region specified by the given python slice object.
86    
87 jfenwick 1779 \param shape - Input - Shape of the object being sliced.
88 jfenwick 1714 \param key - Input -
89     python slice object specifying region to be returned.
90    
91     The slice object is a tuple of n python slice specifiers, where
92     n <= the rank of this Data object. Each slice specifier specifies the
93     range of indexes to be sliced from the corresponding dimension. The
94     first specifier corresponds to the first dimension, the second to the
95     second and so on. Where n < the rank, the remaining dimensions are
96     sliced across the full range of their indicies.
97    
98     Each slice specifier is of the form "a:b", which specifies a slice
99     from index a, up to but not including index b. Where index a is ommitted
100     a is assumed to be 0. Where index b is ommitted, b is assumed to be the
101     length of this dimension. Where both are ommitted (eg: ":") the slice is
102     assumed to encompass that entire dimension.
103    
104     Where one of the slice specifiers is a single integer, eg: [1], we
105     want to generate a rank-1 dimension object, as opposed to eg: [1,2]
106     which implies we want to take a rank dimensional object with one
107     dimension of size 1.
108    
109     The return value is a vector of pairs with length equal to the rank of
110     this object. Each pair corresponds to the range of indicies from the
111     corresponding dimension to be sliced from, as specified in the input
112     slice object.
113    
114     Examples:
115    
116     For a rank 1 object of shape(5):
117    
118     getSliceRegion(:) => < <0,5> >
119     getSliceRegion(2:3) => < <2,3> >
120     getSliceRegion(:3) => < <0,3> >
121     getSliceRegion(2:) => < <2,5> >
122    
123     For a rank 2 object of shape(4,5):
124    
125     getSliceRegion(2:3) => < <2,3> <0,5> >
126     getSliceRegion(2) => < <2,3> <0,5> >
127     NB: but return object requested will have rank 1, shape(5), with
128     values taken from index 2 of this object's first dimension.
129    
130     For a rank 3 object of shape (2,4,6):
131    
132     getSliceRegion(0:2,0:4,0:6) => < <0,2> <0,4> <0,6> >
133     getSliceRegion(:,:,:) => < <0,2> <0,4> <0,6> >
134     getSliceRegion(0:1) => < <0,1> <0,4> <0,6> >
135     getSliceRegion(:1,0:2) => < <0,1> <0,2> <0,6> >
136    
137 jfenwick 1734
138     Note: Not unit tested in c++.
139 jfenwick 1714 */
140     ESCRIPT_DLL_API
141     DataTypes::RegionType
142     getSliceRegion(const DataTypes::ShapeType& shape, const boost::python::object& key);
143    
144 jfenwick 1715 /**
145     \brief
146     Modify region to copy from in order to
147     deal with the case where one range in the region contains identical indexes,
148     eg: <<1,1><0,3><0,3>>
149     This situation implies we want to copy from an object with rank greater than that of this
150     object. eg: we want to copy the values from a two dimensional slice out of a three
151     dimensional object into a two dimensional object.
152     We do this by taking a slice from the other object where one dimension of
153     the slice region is of size 1. So in the above example, we modify the above
154     region like so: <<1,2><0,3><0,3>> and take this slice.
155     */
156 phornby 1838 ESCRIPT_DLL_API
157 jfenwick 1715 DataTypes::RegionLoopRangeType
158     getSliceRegionLoopRange(const DataTypes::RegionType& region);
159 jfenwick 1714
160 jfenwick 1779 /**
161     \brief Return the rank (number of dimensions) of the given shape.
162    
163     \param shape
164     \return the rank.
165     */
166 jfenwick 1714 ESCRIPT_DLL_API
167 jfenwick 1704 inline
168     int
169     getRank(const DataTypes::ShapeType& shape)
170     {
171     return shape.size();
172     }
173 jfenwick 1698
174 jfenwick 1779
175     /**
176     \brief Compute the offset (in 1D vector) of a given subscript with a shape.
177    
178     \param shape - Input - Shape of the datapoint.
179     \param i - Input - subscript to locate.
180     \return offset relative to the beginning of the datapoint.
181     */
182 jfenwick 1714 ESCRIPT_DLL_API
183     inline
184     DataTypes::ValueType::size_type
185     getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i)
186     {
187     EsysAssert((getRank(shape)==1),"Incorrect number of indices for the rank of this object.");
188     EsysAssert((i < DataTypes::noValues(shape)), "Error - Invalid index.");
189     return i;
190     }
191 jfenwick 1697
192 jfenwick 1779 /**
193     \brief Compute the offset (in 1D vector) of a given subscript with a shape.
194    
195     \param shape - Input - Shape of the datapoint.
196     \param i,j - Input - subscripts to locate.
197     \return offset relative to the beginning of the datapoint.
198     */
199 jfenwick 1714 ESCRIPT_DLL_API
200     inline
201     DataTypes::ValueType::size_type
202     getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
203     DataTypes::ValueType::size_type j)
204     {
205 jfenwick 1734 EsysAssert((getRank(shape)==2),"Incorrect number of indices for the rank of this object.");
206 jfenwick 1714 DataTypes::ValueType::size_type temp=i+j*shape[0];
207     EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
208     return temp;
209     }
210 jfenwick 1697
211 jfenwick 1779 /**
212     \brief Compute the offset (in 1D vector) of a given subscript with a shape.
213    
214     \param shape - Input - Shape of the datapoint.
215     \param i,j,k - Input - subscripts to locate.
216     \return offset relative to the beginning of the datapoint.
217     */
218 jfenwick 1714 ESCRIPT_DLL_API
219     inline
220     DataTypes::ValueType::size_type
221     getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
222     DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k)
223     {
224 jfenwick 1734 EsysAssert((getRank(shape)==3),"Incorrect number of indices for the rank of this object.");
225 jfenwick 1714 DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0];
226     EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
227     return temp;
228     }
229 jfenwick 1697
230 jfenwick 1779 /**
231     \brief Compute the offset (in 1D vector) of a given subscript with a shape.
232    
233     \param shape - Input - Shape of the datapoint.
234     \param i,j,k,m - Input - subscripts to locate.
235     \return offset relative to the beginning of the datapoint.
236     */
237 jfenwick 1714 ESCRIPT_DLL_API
238     inline
239     DataTypes::ValueType::size_type
240     getRelIndex(const DataTypes::ShapeType& shape, DataTypes::ValueType::size_type i,
241     DataTypes::ValueType::size_type j, DataTypes::ValueType::size_type k,
242     DataTypes::ValueType::size_type m)
243     {
244 jfenwick 1734 EsysAssert((getRank(shape)==4),"Incorrect number of indices for the rank of this object.");
245 jfenwick 1714 DataTypes::ValueType::size_type temp=i+j*shape[0]+k*shape[1]*shape[0]+m*shape[2]*shape[1]*shape[0];
246 jfenwick 1734 EsysAssert((temp < DataTypes::noValues(shape)), "Error - Invalid index.");
247 jfenwick 1714 return temp;
248     }
249    
250 jfenwick 1779 /**
251     \brief Test if two shapes are equal.
252     */
253 jfenwick 1715 ESCRIPT_DLL_API
254     inline
255     bool
256     checkShape(const ShapeType& s1, const ShapeType& s2)
257     {
258     return s1==s2;
259     }
260 jfenwick 1714
261 jfenwick 1779 /**
262     \brief Produce a string containing two shapes.
263    
264     \param messagePrefix - Beginning of the message.
265     \param other - displayed in the message as "Other shape"
266     \param thisShape - displayed in the message as "This shape"
267     */
268 phornby 1838 ESCRIPT_DLL_API
269 jfenwick 1724 std::string
270     createShapeErrorMessage(const std::string& messagePrefix,
271     const DataTypes::ShapeType& other,
272     const DataTypes::ShapeType& thisShape);
273    
274 jfenwick 1734
275     /**
276     \brief
277     Copy a data slice specified by the given region and offset from the
278 jfenwick 1779 "other" view into the "left" view at the given offset.
279    
280     \param left - vector to copy into
281     \param leftShape - shape of datapoints for the left vector
282     \param leftOffset - location within left to start copying to
283     \param other - vector to copy from
284     \param otherShape - shape of datapoints for the other vector
285     \param otherOffset - location within other vector to start copying from
286 jfenwick 1734 \param region - Input -
287     Region in other view to copy data from.
288     */
289 phornby 1838 ESCRIPT_DLL_API
290 jfenwick 1734 void
291     copySlice(ValueType& left,
292     const ShapeType& leftShape,
293 jfenwick 1779 ValueType::size_type leftOffset,
294 jfenwick 1734 const ValueType& other,
295     const ShapeType& otherShape,
296     ValueType::size_type otherOffset,
297     const RegionLoopRangeType& region);
298    
299     /**
300     \brief
301     Copy data into a slice specified by the given region and offset in
302 jfenwick 1779 the left vector from the other vector at the given offset.
303 jfenwick 1734
304 jfenwick 1779 \param left - vector to copy into
305     \param leftShape - shape of datapoints for the left vector
306     \param leftOffset - location within left to start copying to
307     \param other - vector to copy from
308     \param otherShape - shape of datapoints for the other vector
309     \param otherOffset - location within other vector to start copying from
310 jfenwick 1734 \param region - Input -
311 jfenwick 1779 Region in the left vector to copy data to.
312 jfenwick 1734 */
313 phornby 1838 ESCRIPT_DLL_API
314 jfenwick 1734 void
315     copySliceFrom(ValueType& left,
316     const ShapeType& leftShape,
317 jfenwick 1779 ValueType::size_type leftOffset,
318 jfenwick 1734 const ValueType& other,
319     const ShapeType& otherShape,
320     ValueType::size_type otherOffset,
321     const RegionLoopRangeType& region);
322    
323    
324 jfenwick 1747 /**
325 jfenwick 1779 \brief Display a single value (with the specified shape) from the data.
326    
327     \param data - vector containing the datapoint
328     \param shape - shape of the datapoint
329     \param offset - start of the datapoint within data
330     \param prefix - string to prepend to the output
331 jfenwick 1747 */
332     std::string
333 jfenwick 1779 pointToString(const ValueType& data,const ShapeType& shape, int offset, const std::string& prefix);
334 jfenwick 1747
335    
336     /**
337     \brief Extract shape information from the supplied numarray.
338     */
339     inline
340     ShapeType
341     shapeFromNumArray(const boost::python::numeric::array& value)
342     {
343     // extract the shape of the numarray
344     DataTypes::ShapeType tempShape;
345     for (int i=0; i < value.getrank(); i++) {
346     tempShape.push_back(boost::python::extract<int>(value.getshape()[i]));
347     }
348     return tempShape;
349     }
350    
351    
352     /**
353     \brief Copy a point from one vector to another. Note: This version does not check to see if shapes are the same.
354 jfenwick 1779
355     \param dest - vector to copy to
356     \param doffset - beginning of the target datapoint in dest
357     \param nvals - the number of values comprising the datapoint
358     \param src - vector to copy from
359     \param soffset - beginning of the datapoint in src
360 jfenwick 1747 */
361     void copyPoint(ValueType& dest, ValueType::size_type doffset, ValueType::size_type nvals, const ValueType& src, ValueType::size_type soffset);
362    
363 jfenwick 1714 } // End namespace DataTypes
364    
365    
366     } // end of namespace escipt
367    
368 jfenwick 1697 #endif
369 jfenwick 1781

  ViewVC Help
Powered by ViewVC 1.1.26