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