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