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