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