source: Sophya/trunk/SophyaLib/TArray/basarr.h@ 1415

Last change on this file since 1415 was 1389, checked in by ansari, 25 years ago

Correction bug initialisation vecteur ligne / colonne , Reza 9/2/2001

File size: 9.7 KB
Line 
1// This may look like C code, but it is really -*- C++ -*-
2// Base array class - Memory organisation management
3// R. Ansari, C.Magneville 03/2000
4
5#ifndef BaseArray_SEEN
6#define BaseArray_SEEN
7
8#include "machdefs.h"
9#include <math.h>
10#include <iostream.h>
11#include "anydataobj.h"
12#include "mutyv.h"
13#include "dvlist.h"
14
15
16//! Maximum number of dimensions for an array
17/*! \anchor BASEARRAY_MAXNDIMS */
18#define BASEARRAY_MAXNDIMS 5
19
20namespace SOPHYA {
21
22// ------------ classe template Array -----------
23//! Base class for template arrays
24class BaseArray : public AnyDataObj {
25public:
26 //! To define Array or Matrix memory mapping
27 enum MemoryMapping {
28 AutoMemoryMapping = -1, //!< define Auto Memory Mapping
29 SameMemoryMapping = 0, //!< define Same Memory Mapping
30 CMemoryMapping = 1, //!< define C Memory Mapping
31 FortranMemoryMapping = 2 //!< define Fortran Memory Mapping
32 };
33 //! To define Vector type
34 enum VectorType {
35 AutoVectorType = -1, //!< define Auto Vector Type
36 SameVectorType = 0, //!< define Same Vector Type
37 ColumnVector = 1, //!< define Column Vector Type
38 RowVector = 2 //!< define Row Vector Type
39 };
40
41 // threshold for parallel routine call
42 static void SetOpenMPSizeThreshold(sa_size_t thr=200000);
43 //! Get Size threshold for parallel routine call
44 static inline sa_size_t GetOpenMPSizeThreshold() { return openmp_size_threshold; }
45
46 static void SetMaxPrint(int_4 nprt=50, int_4 lev=0);
47 //! Get maximum number of printed elements
48 static inline int_4 GetMaxPrint() { return max_nprt_; }
49 //! Get print level
50 static inline int_4 GetPrintLevel() { return prt_lev_; }
51
52 static short SetDefaultMemoryMapping(short mm=CMemoryMapping);
53 //! Get Default Memory Mapping
54 static inline short GetDefaultMemoryMapping() { return default_memory_mapping; }
55 static short SetDefaultVectorType(short vt=ColumnVector);
56 //! Get Default Vector Type
57 static inline short GetDefaultVectorType() { return default_vector_type; }
58
59 // Creator / destructor
60 BaseArray();
61 virtual ~BaseArray();
62
63 // Returns true if ndim and sizes are equal
64 virtual bool CompareSizes(const BaseArray& a, bool& smo);
65
66 // Compacts \b size=1 array dimensions
67 virtual void CompactAllDim(); // suppresses all size==1 dimensions
68 virtual void CompactTrailingDim(); // suppresses size==1 dimensions after the last size>1 dimension
69
70 // Array dimensions
71 //! Return number of dimensions (array rank)
72 inline int_4 NbDimensions() const { return( ndim_ ); }
73 //! Return array rank (number of dimensions)
74 inline int_4 Rank() const { return( ndim_ ); }
75
76 //! Return total size of the array
77 inline sa_size_t Size() const { return(totsize_); }
78 //! Return size along the first dimension
79 inline sa_size_t SizeX() const { return(size_[0]); }
80 //! Return size along the second dimension
81 inline sa_size_t SizeY() const { return(size_[1]); }
82 //! Return size along the third dimension
83 inline sa_size_t SizeZ() const { return(size_[2]); }
84 //! Return size along the \b ka th dimension
85 inline sa_size_t Size(int_4 ka) const { return(size_[CheckDI(ka,1)]); }
86
87 int_4 MaxSizeKA() const ;
88
89 //! Get memory organization
90 inline short GetMemoryMapping() const
91 { return ( (marowi_ == 1) ? CMemoryMapping : FortranMemoryMapping) ; }
92 //! line index dimension
93 inline int_4 RowsKA() const {return marowi_; }
94 //! column index dimension
95 inline int_4 ColsKA() const {return macoli_; }
96 //! Index dimension of the elements of a vector
97 inline int_4 VectKA() const {return veceli_; }
98 void SetMemoryMapping(short mm=AutoMemoryMapping);
99
100 //! Get Vector type ( \b Line or \b Column vector )
101 inline short GetVectorType() const
102 { return((marowi_ == veceli_) ? ColumnVector : RowVector); }
103 void SetVectorType(short vt=AutoVectorType);
104
105 // memory organisation - packing information
106 //! return true if array is packed in memory
107 inline bool IsPacked() const { return(moystep_ == 1); }
108 //! return true if array is packed along the first dimension
109 inline bool IsPackedX() const { return(step_[0] == 1); }
110 //! return true if array is packed along the second dimension
111 inline bool IsPackedY() const { return(step_[1] == 1); }
112 //! return true if array is packed along the third dimension
113 inline bool IsPackedZ() const { return(step_[2] == 1); }
114 //! return true if array is packed along the \b ka th dimension
115 inline bool IsPacked(int_4 ka) const { return(step_[CheckDI(ka,2)] == 1); }
116
117 //! return the minimum step value along all the dimensions
118 inline sa_size_t MinStep() const { return(minstep_); }
119 //! return the average step value along all the dimensions
120 inline sa_size_t AvgStep() const { return(moystep_); }
121 //! return the step along the first dimension
122 inline sa_size_t StepX() const { return(step_[0]); }
123 //! return the step along the second dimension
124 inline sa_size_t StepY() const { return(step_[1]); }
125 //! return the step along the third dimension
126 inline sa_size_t StepZ() const { return(step_[2]); }
127 //! return the step along the \b ka th dimension
128 inline sa_size_t Step(int_4 ka) const { return(step_[CheckDI(ka,3)]); }
129
130 int_4 MinStepKA() const ;
131
132 // Offset of element ip
133 sa_size_t Offset(sa_size_t ip=0) const ;
134 // Offset of the i'th vector along axe ka
135 sa_size_t Offset(int_4 ka, sa_size_t i) const ;
136 inline sa_size_t Offset(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it=0, sa_size_t iu=0) const;
137 // Index values of element ip
138 void IndexAtPosition(sa_size_t ip, sa_size_t & ix, sa_size_t & iy, sa_size_t & iz,
139 sa_size_t & it, sa_size_t & iu) const;
140 // an abstract element acces methode
141 virtual MuTyV & ValueAtPosition(sa_size_t ip) const = 0;
142
143 // Pour recuperer pas et numero d'axe pour operations sur deux arrays
144 void GetOpeParams(const BaseArray& a, bool smo, int_4& ax, int_4& axa, sa_size_t& step,
145 sa_size_t& stepa, sa_size_t& gpas, sa_size_t& naxa);
146 // Impression, I/O, ...
147 void Show(ostream& os, bool si=false) const;
148 //! Show information on \b cout
149 inline void Show() const { Show(cout); }
150 virtual string InfoString() const;
151
152 // DVList info Object
153 DVList& Info();
154
155protected:
156 inline int_4 CheckDI(int_4 ka, int msg) const ;
157 inline void CheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu, int msg) const ;
158 // Changing Sizes/NDim ... return true if OK
159 bool UpdateSizes(int_4 ndim, const sa_size_t * siz, sa_size_t step, sa_size_t offset, string & exmsg);
160 bool UpdateSizes(int_4 ndim, const sa_size_t * siz, const sa_size_t * step, sa_size_t offset, string & exmsg);
161 bool UpdateSizes(const BaseArray& a, string & exmsg);
162 static sa_size_t ComputeTotalSize(int_4 ndim, const sa_size_t * siz, sa_size_t step, sa_size_t offset) ;
163 // Organisation memoire
164 static short SelectMemoryMapping(short mm);
165 static short SelectVectorType(short vt);
166 void UpdateMemoryMapping(short mm);
167 void UpdateMemoryMapping(BaseArray const & a, short mm);
168
169 // Pour Extraction de sous-tableau
170 virtual void UpdateSubArraySizes(BaseArray & ra, int_4 ndim, sa_size_t * siz, sa_size_t * pos, sa_size_t * step) const;
171
172 int_4 ndim_; //!< number of dimensions of array
173 sa_size_t size_[BASEARRAY_MAXNDIMS]; //!< array of the size in each dimension
174 sa_size_t totsize_; //!< Total number of elements
175 sa_size_t offset_; //!< global offset -\> position of elem[0] in DataBlock
176 //! two consecutive elements distance in a given dimension
177 sa_size_t step_[BASEARRAY_MAXNDIMS];
178 sa_size_t minstep_; //!< minimal step (in any axes)
179 sa_size_t moystep_; //!< mean step, if == 0 --\> non regular steps
180 int_2 marowi_; //!< For matrices, Row index in dimensions
181 int_2 macoli_; //!< For matrices, Column index in dimensions
182 int_2 veceli_; //!< For vectors, dimension index = marowi_/macoli_ (Row/Col vectors)
183 int_2 arrtype_; //!< 0 a TArray, 1 TMatrix , 2 TVector
184 DVList* mInfo; //!< Infos (variables) attached to the array
185
186 static char * ck_op_msg_[6]; //!< Operation messages for CheckDI() CheckBound()
187 static int_4 max_nprt_; //!< maximum number of printed elements
188 static int_4 prt_lev_; //!< Print level
189 static short default_memory_mapping; //!< Default memory mapping
190 static short default_vector_type; //!< Default vector type Row/Column
191 static sa_size_t openmp_size_threshold; //!< Size limit for parallel routine calls
192};
193
194// --------------------------------------------------
195// Methodes inline de verification
196// --------------------------------------------------
197//! to verify the compatibility of the dimension index
198inline int_4 BaseArray::CheckDI(int_4 ka, int msg) const
199{
200 if ( (ka < 0) || (ka >= ndim_) ) {
201 string txt = "BaseArray::CheckDimensionIndex/Error "; txt += ck_op_msg_[msg];
202 throw(RangeCheckError(txt));
203 }
204 return(ka);
205}
206
207//! to verify the compatibility of the indexes in all dimensions
208inline void BaseArray::CheckBound(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu, int msg) const
209{
210 if ( (ix >= size_[0]) || (ix < 0) || (iy >= size_[1]) || (iy < 0) ||
211 (iz >= size_[2]) || (iz < 0) || (it >= size_[3]) || (it < 0) ||
212 (iu >= size_[4]) || (iu < 0) ) {
213 string txt = "BaseArray::CheckArrayBound/Error "; txt += ck_op_msg_[msg];
214 throw(RangeCheckError(txt));
215 }
216 return;
217}
218
219
220
221// --------------------------------------------------
222// Position d'un element
223// --------------------------------------------------
224//! Offset of element (ix,iy,iz,it,iu)
225inline sa_size_t BaseArray::Offset(sa_size_t ix, sa_size_t iy, sa_size_t iz, sa_size_t it, sa_size_t iu) const
226{
227#ifdef SO_BOUNDCHECKING
228 CheckBound(ix, iy, iz, it, iu, 4);
229#endif
230 return ( offset_+ ix*step_[0] + iy*step_[1] + iz*step_[2] +
231 it*step_[3] + iu*step_[4] );
232}
233
234
235} // Fin du namespace
236
237#endif
Note: See TracBrowser for help on using the repository browser.