[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>
|
---|
[2322] | 10 | #include <iostream>
|
---|
[787] | 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
|
---|
[1156] | 42 | static void SetOpenMPSizeThreshold(sa_size_t thr=200000);
|
---|
[890] | 43 | //! Get Size threshold for parallel routine call
|
---|
[1156] | 44 | static inline sa_size_t GetOpenMPSizeThreshold() { return openmp_size_threshold; }
|
---|
[890] | 45 |
|
---|
[1581] | 46 | static void SetMaxPrint(sa_size_t nprt=50, int_4 lev=0);
|
---|
[890] | 47 | //! Get maximum number of printed elements
|
---|
[1581] | 48 | static inline sa_size_t GetMaxPrint() { return max_nprt_; }
|
---|
[1156] | 49 | //! Get print level
|
---|
| 50 | static inline int_4 GetPrintLevel() { return prt_lev_; }
|
---|
[813] | 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; }
|
---|
[2583] | 58 |
|
---|
| 59 | //! Optimization choice for matrix product
|
---|
| 60 | static void SetMatProdOpt(uint_2 opt=1);
|
---|
| 61 | static inline uint_2 GetMatProdOpt(void) {return matrix_product_optim;}
|
---|
[804] | 62 |
|
---|
[890] | 63 | // Creator / destructor
|
---|
[787] | 64 | BaseArray();
|
---|
| 65 | virtual ~BaseArray();
|
---|
| 66 |
|
---|
| 67 | // Returns true if ndim and sizes are equal
|
---|
[1517] | 68 | virtual bool CompareSizes(const BaseArray& a, bool& smo) const;
|
---|
[787] | 69 |
|
---|
| 70 | // Array dimensions
|
---|
[1550] | 71 | //! Return true if the array was allocated ( Rank() > 0 )
|
---|
| 72 | inline bool IsAllocated() const { return( (ndim_ > 0) ? true : false ); }
|
---|
[1389] | 73 | //! Return number of dimensions (array rank)
|
---|
[1156] | 74 | inline int_4 NbDimensions() const { return( ndim_ ); }
|
---|
[1389] | 75 | //! Return array rank (number of dimensions)
|
---|
| 76 | inline int_4 Rank() const { return( ndim_ ); }
|
---|
[787] | 77 |
|
---|
[890] | 78 | //! Return total size of the array
|
---|
[1156] | 79 | inline sa_size_t Size() const { return(totsize_); }
|
---|
[890] | 80 | //! Return size along the first dimension
|
---|
[1156] | 81 | inline sa_size_t SizeX() const { return(size_[0]); }
|
---|
[890] | 82 | //! Return size along the second dimension
|
---|
[1156] | 83 | inline sa_size_t SizeY() const { return(size_[1]); }
|
---|
[890] | 84 | //! Return size along the third dimension
|
---|
[1156] | 85 | inline sa_size_t SizeZ() const { return(size_[2]); }
|
---|
[890] | 86 | //! Return size along the \b ka th dimension
|
---|
[1156] | 87 | inline sa_size_t Size(int_4 ka) const { return(size_[CheckDI(ka,1)]); }
|
---|
[787] | 88 |
|
---|
[1156] | 89 | int_4 MaxSizeKA() const ;
|
---|
[787] | 90 |
|
---|
[890] | 91 | //! Get memory organization
|
---|
[1099] | 92 | inline short GetMemoryMapping() const
|
---|
[890] | 93 | { return ( (marowi_ == 1) ? CMemoryMapping : FortranMemoryMapping) ; }
|
---|
| 94 | //! line index dimension
|
---|
[1156] | 95 | inline int_4 RowsKA() const {return marowi_; }
|
---|
[1005] | 96 | //! column index dimension
|
---|
[1156] | 97 | inline int_4 ColsKA() const {return macoli_; }
|
---|
[890] | 98 | //! Index dimension of the elements of a vector
|
---|
[1156] | 99 | inline int_4 VectKA() const {return veceli_; }
|
---|
[813] | 100 | void SetMemoryMapping(short mm=AutoMemoryMapping);
|
---|
[804] | 101 |
|
---|
[890] | 102 | //! Get Vector type ( \b Line or \b Column vector )
|
---|
[813] | 103 | inline short GetVectorType() const
|
---|
| 104 | { return((marowi_ == veceli_) ? ColumnVector : RowVector); }
|
---|
[890] | 105 | void SetVectorType(short vt=AutoVectorType);
|
---|
[813] | 106 |
|
---|
[890] | 107 | // memory organisation - packing information
|
---|
| 108 | //! return true if array is packed in memory
|
---|
[787] | 109 | inline bool IsPacked() const { return(moystep_ == 1); }
|
---|
[890] | 110 | //! return true if array is packed along the first dimension
|
---|
[787] | 111 | inline bool IsPackedX() const { return(step_[0] == 1); }
|
---|
[890] | 112 | //! return true if array is packed along the second dimension
|
---|
[787] | 113 | inline bool IsPackedY() const { return(step_[1] == 1); }
|
---|
[890] | 114 | //! return true if array is packed along the third dimension
|
---|
[787] | 115 | inline bool IsPackedZ() const { return(step_[2] == 1); }
|
---|
[890] | 116 | //! return true if array is packed along the \b ka th dimension
|
---|
[1156] | 117 | inline bool IsPacked(int_4 ka) const { return(step_[CheckDI(ka,2)] == 1); }
|
---|
[787] | 118 |
|
---|
[890] | 119 | //! return the minimum step value along all the dimensions
|
---|
[1156] | 120 | inline sa_size_t MinStep() const { return(minstep_); }
|
---|
[890] | 121 | //! return the average step value along all the dimensions
|
---|
[1156] | 122 | inline sa_size_t AvgStep() const { return(moystep_); }
|
---|
[890] | 123 | //! return the step along the first dimension
|
---|
[1156] | 124 | inline sa_size_t StepX() const { return(step_[0]); }
|
---|
[890] | 125 | //! return the step along the second dimension
|
---|
[1156] | 126 | inline sa_size_t StepY() const { return(step_[1]); }
|
---|
[890] | 127 | //! return the step along the third dimension
|
---|
[1156] | 128 | inline sa_size_t StepZ() const { return(step_[2]); }
|
---|
[890] | 129 | //! return the step along the \b ka th dimension
|
---|
[1156] | 130 | inline sa_size_t Step(int_4 ka) const { return(step_[CheckDI(ka,3)]); }
|
---|
[787] | 131 |
|
---|
[1156] | 132 | int_4 MinStepKA() const ;
|
---|
[787] | 133 |
|
---|
[813] | 134 | // Offset of element ip
|
---|
[1156] | 135 | sa_size_t Offset(sa_size_t ip=0) const ;
|
---|
[813] | 136 | // Offset of the i'th vector along axe ka
|
---|
[1156] | 137 | sa_size_t Offset(int_4 ka, sa_size_t i) const ;
|
---|
| 138 | 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;
|
---|
[1314] | 139 | // Index values of element ip
|
---|
| 140 | void IndexAtPosition(sa_size_t ip, sa_size_t & ix, sa_size_t & iy, sa_size_t & iz,
|
---|
| 141 | sa_size_t & it, sa_size_t & iu) const;
|
---|
[890] | 142 | // an abstract element acces methode
|
---|
[1156] | 143 | virtual MuTyV & ValueAtPosition(sa_size_t ip) const = 0;
|
---|
[2888] | 144 | virtual MuTyV & ValueAtPositionDB(sa_size_t ip) const = 0;
|
---|
[787] | 145 |
|
---|
[1099] | 146 | // Pour recuperer pas et numero d'axe pour operations sur deux arrays
|
---|
[1156] | 147 | void GetOpeParams(const BaseArray& a, bool smo, int_4& ax, int_4& axa, sa_size_t& step,
|
---|
[1517] | 148 | sa_size_t& stepa, sa_size_t& gpas, sa_size_t& naxa) const;
|
---|
[1099] | 149 | // Impression, I/O, ...
|
---|
[804] | 150 | void Show(ostream& os, bool si=false) const;
|
---|
[890] | 151 | //! Show information on \b cout
|
---|
[804] | 152 | inline void Show() const { Show(cout); }
|
---|
[813] | 153 | virtual string InfoString() const;
|
---|
[787] | 154 |
|
---|
[2267] | 155 | // Lecture,Ecriture sur fichier ASCII
|
---|
| 156 | //! Reads an array from an ASCII stream
|
---|
[2286] | 157 | virtual sa_size_t ReadASCII(istream& is, sa_size_t & nr, sa_size_t & nc,
|
---|
| 158 | char clm='#', const char* sep=" \t") = 0;
|
---|
[2267] | 159 | //! Writes an array to an ASCII stream
|
---|
| 160 | virtual void WriteASCII(ostream& os) const = 0;
|
---|
| 161 |
|
---|
[894] | 162 | // DVList info Object
|
---|
| 163 | DVList& Info();
|
---|
[787] | 164 |
|
---|
| 165 | protected:
|
---|
[1156] | 166 | inline int_4 CheckDI(int_4 ka, int msg) const ;
|
---|
| 167 | 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 ;
|
---|
[2917] | 168 | // Compacts size=1 array dimensions
|
---|
| 169 | void CompactAllDim(); // suppresses all size==1 dimensions
|
---|
| 170 | void CompactTrailingDim(); // suppresses size==1 dimensions after the last size>1 dimension
|
---|
[787] | 171 | // Changing Sizes/NDim ... return true if OK
|
---|
[1324] | 172 | bool UpdateSizes(int_4 ndim, const sa_size_t * siz, sa_size_t step, sa_size_t offset, string & exmsg);
|
---|
| 173 | bool UpdateSizes(int_4 ndim, const sa_size_t * siz, const sa_size_t * step, sa_size_t offset, string & exmsg);
|
---|
[787] | 174 | bool UpdateSizes(const BaseArray& a, string & exmsg);
|
---|
[1324] | 175 | static sa_size_t ComputeTotalSize(int_4 ndim, const sa_size_t * siz, sa_size_t step, sa_size_t offset) ;
|
---|
[804] | 176 | // Organisation memoire
|
---|
| 177 | static short SelectMemoryMapping(short mm);
|
---|
[813] | 178 | static short SelectVectorType(short vt);
|
---|
| 179 | void UpdateMemoryMapping(short mm);
|
---|
[804] | 180 | void UpdateMemoryMapping(BaseArray const & a, short mm);
|
---|
[787] | 181 |
|
---|
[804] | 182 | // Pour Extraction de sous-tableau
|
---|
[1325] | 183 | virtual void UpdateSubArraySizes(BaseArray & ra, int_4 ndim, sa_size_t * siz, sa_size_t * pos, sa_size_t * step) const;
|
---|
[804] | 184 |
|
---|
[1156] | 185 | int_4 ndim_; //!< number of dimensions of array
|
---|
| 186 | sa_size_t size_[BASEARRAY_MAXNDIMS]; //!< array of the size in each dimension
|
---|
| 187 | sa_size_t totsize_; //!< Total number of elements
|
---|
| 188 | sa_size_t offset_; //!< global offset -\> position of elem[0] in DataBlock
|
---|
[890] | 189 | //! two consecutive elements distance in a given dimension
|
---|
[1156] | 190 | sa_size_t step_[BASEARRAY_MAXNDIMS];
|
---|
| 191 | sa_size_t minstep_; //!< minimal step (in any axes)
|
---|
| 192 | sa_size_t moystep_; //!< mean step, if == 0 --\> non regular steps
|
---|
[1099] | 193 | int_2 marowi_; //!< For matrices, Row index in dimensions
|
---|
| 194 | int_2 macoli_; //!< For matrices, Column index in dimensions
|
---|
| 195 | int_2 veceli_; //!< For vectors, dimension index = marowi_/macoli_ (Row/Col vectors)
|
---|
| 196 | int_2 arrtype_; //!< 0 a TArray, 1 TMatrix , 2 TVector
|
---|
[894] | 197 | DVList* mInfo; //!< Infos (variables) attached to the array
|
---|
[787] | 198 |
|
---|
[894] | 199 | static char * ck_op_msg_[6]; //!< Operation messages for CheckDI() CheckBound()
|
---|
[1581] | 200 | static sa_size_t max_nprt_; //!< maximum number of printed elements
|
---|
[1156] | 201 | static int_4 prt_lev_; //!< Print level
|
---|
[894] | 202 | static short default_memory_mapping; //!< Default memory mapping
|
---|
| 203 | static short default_vector_type; //!< Default vector type Row/Column
|
---|
[1156] | 204 | static sa_size_t openmp_size_threshold; //!< Size limit for parallel routine calls
|
---|
[2583] | 205 | static uint_2 matrix_product_optim; //!< optimization level for matrix product
|
---|
[787] | 206 | };
|
---|
| 207 |
|
---|
| 208 | // --------------------------------------------------
|
---|
| 209 | // Methodes inline de verification
|
---|
| 210 | // --------------------------------------------------
|
---|
[890] | 211 | //! to verify the compatibility of the dimension index
|
---|
[1156] | 212 | inline int_4 BaseArray::CheckDI(int_4 ka, int msg) const
|
---|
[787] | 213 | {
|
---|
[1156] | 214 | if ( (ka < 0) || (ka >= ndim_) ) {
|
---|
[787] | 215 | string txt = "BaseArray::CheckDimensionIndex/Error "; txt += ck_op_msg_[msg];
|
---|
[813] | 216 | throw(RangeCheckError(txt));
|
---|
[787] | 217 | }
|
---|
| 218 | return(ka);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[890] | 221 | //! to verify the compatibility of the indexes in all dimensions
|
---|
[1156] | 222 | inline 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
|
---|
[787] | 223 | {
|
---|
[1156] | 224 | if ( (ix >= size_[0]) || (ix < 0) || (iy >= size_[1]) || (iy < 0) ||
|
---|
| 225 | (iz >= size_[2]) || (iz < 0) || (it >= size_[3]) || (it < 0) ||
|
---|
| 226 | (iu >= size_[4]) || (iu < 0) ) {
|
---|
[787] | 227 | string txt = "BaseArray::CheckArrayBound/Error "; txt += ck_op_msg_[msg];
|
---|
[813] | 228 | throw(RangeCheckError(txt));
|
---|
[787] | 229 | }
|
---|
| 230 | return;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
[813] | 234 |
|
---|
[787] | 235 | // --------------------------------------------------
|
---|
| 236 | // Position d'un element
|
---|
| 237 | // --------------------------------------------------
|
---|
[890] | 238 | //! Offset of element (ix,iy,iz,it,iu)
|
---|
[1156] | 239 | inline 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
|
---|
[787] | 240 | {
|
---|
| 241 | #ifdef SO_BOUNDCHECKING
|
---|
| 242 | CheckBound(ix, iy, iz, it, iu, 4);
|
---|
| 243 | #endif
|
---|
| 244 | return ( offset_+ ix*step_[0] + iy*step_[1] + iz*step_[2] +
|
---|
| 245 | it*step_[3] + iu*step_[4] );
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 |
|
---|
| 249 | } // Fin du namespace
|
---|
| 250 |
|
---|
| 251 | #endif
|
---|