[787] | 1 | // Base class for numerical arrays
|
---|
| 2 | // R. Ansari, C.Magneville 03/2000
|
---|
| 3 |
|
---|
| 4 | #include "machdefs.h"
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <stdlib.h>
|
---|
| 7 | #include "pexceptions.h"
|
---|
| 8 | #include "basarr.h"
|
---|
| 9 |
|
---|
[926] | 10 | /*!
|
---|
| 11 | \class SOPHYA::BaseArray
|
---|
| 12 | \ingroup TArray
|
---|
| 13 | Base class for template arrays
|
---|
| 14 | No data are connected to this class.
|
---|
| 15 |
|
---|
| 16 | Define base methods, enum and defaults for TArray , TMatrix and TVector.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
[787] | 19 | // Variables statiques globales
|
---|
[894] | 20 | char * BaseArray::ck_op_msg_[6] =
|
---|
| 21 | {"???", "Size(int )", "IsPacked(int )"
|
---|
| 22 | ,"Stride(int )", "ElemCheckBound()", "operator()" };
|
---|
[787] | 23 | uint_4 BaseArray::max_nprt_ = 50;
|
---|
[813] | 24 | uint_4 BaseArray::prt_lev_ = 0;
|
---|
[804] | 25 | short BaseArray::default_memory_mapping = CMemoryMapping;
|
---|
[813] | 26 | short BaseArray::default_vector_type = ColumnVector;
|
---|
| 27 | uint_8 BaseArray::openmp_size_threshold = 200000;
|
---|
[787] | 28 |
|
---|
[813] | 29 | // ------ Methodes statiques globales --------
|
---|
| 30 |
|
---|
[890] | 31 | //! Set maximum number of printed elements and print level
|
---|
| 32 | /*!
|
---|
| 33 | \param nprt : maximum number of print
|
---|
| 34 | \param lev : print level
|
---|
| 35 | */
|
---|
[813] | 36 | void BaseArray::SetMaxPrint(uint_4 nprt, uint_4 lev)
|
---|
[787] | 37 | {
|
---|
| 38 | max_nprt_ = nprt;
|
---|
[813] | 39 | prt_lev_ = (lev < 3) ? lev : 3;
|
---|
[787] | 40 | }
|
---|
| 41 |
|
---|
[890] | 42 | //! Set Size threshold for parallel routine call
|
---|
| 43 | /*!
|
---|
| 44 | \param thr : thresold value
|
---|
| 45 | */
|
---|
[813] | 46 | void BaseArray::SetOpenMPSizeThreshold(uint_8 thr)
|
---|
| 47 | {
|
---|
| 48 | openmp_size_threshold = thr;
|
---|
| 49 | }
|
---|
[787] | 50 |
|
---|
[813] | 51 |
|
---|
[894] | 52 | //! Compute totale size
|
---|
| 53 | /*!
|
---|
| 54 | \param ndim : number of dimensions
|
---|
| 55 | \param siz : array of size along the \b ndim dimensions
|
---|
| 56 | \param step[ndim] : step value
|
---|
| 57 | \param offset : offset value
|
---|
| 58 | \return Total size of the array
|
---|
| 59 | */
|
---|
[804] | 60 | uint_8 BaseArray::ComputeTotalSize(uint_4 ndim, const uint_4 * siz, uint_4 step, uint_8 offset)
|
---|
[787] | 61 | {
|
---|
| 62 | uint_8 rs = step;
|
---|
[958] | 63 | for(uint_4 k=0; k<ndim; k++) rs *= siz[k];
|
---|
[787] | 64 | return(rs+offset);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[894] | 67 | //! Set Default Memory Mapping
|
---|
| 68 | /*!
|
---|
| 69 | \param mm : Memory Mapping type
|
---|
| 70 | \verbatim
|
---|
| 71 | mm == CMemoryMapping : C like memory mapping
|
---|
| 72 | mm == FortranMemoryMapping : Fortran like memory mapping
|
---|
| 73 | \endverbatim
|
---|
| 74 | \verbatim
|
---|
| 75 | # ===== For Matrices
|
---|
| 76 | *** MATHEMATICS: m(row,column) with indexes running [1,n])
|
---|
| 77 | | 11 12 13 |
|
---|
| 78 | matrix Math = Mmath= | 21 22 23 |
|
---|
| 79 | | 31 32 33 |
|
---|
| 80 | *** IDL, \b FORTRAN: indexes data in \b row-major format:
|
---|
| 81 | indexes arrays in (column,row) order.
|
---|
| 82 | index IDL running [0,n[ ; index FORTRAN running [1,n]
|
---|
| 83 | M in memory: [ 11 12 13 : 21 22 23 : 31 32 33 : ... ]
|
---|
| 84 | line 1 : line 2 : line 3 : ...
|
---|
| 85 | ex: Midl(0,2) = Mfor(1,3) = Mmath(3,1) = 31
|
---|
| 86 | Midl(2,0) = Mfor(3,1) = Mmath(1,3) = 13
|
---|
| 87 | *** C: indexes data in \b column-major format:
|
---|
| 88 | indexes arrays in [row][column] order.
|
---|
| 89 | index C running [0,n[
|
---|
| 90 | M in memory: [ 11 21 31 : 12 22 32 : 13 23 33 : ... ]
|
---|
| 91 | column 1 : column 2 : column 3 : ...
|
---|
| 92 | ex: Mc[2][0] = Mmath(3,1) = 31
|
---|
| 93 | Mc[0][2] = Mmath(1,3) = 13
|
---|
| 94 | *** RESUME diff Idl/Fortan/C/Math:
|
---|
| 95 | Midl(col-1,row-1) = Mfor(col,row) = Mc[row-1][col-1] = Mmath(row,col)
|
---|
| 96 | TRANSPOSE(column-major array) --> row-major array
|
---|
| 97 | \endverbatim
|
---|
| 98 | \return default memory mapping value
|
---|
| 99 | */
|
---|
[804] | 100 | short BaseArray::SetDefaultMemoryMapping(short mm)
|
---|
| 101 | {
|
---|
[813] | 102 | default_memory_mapping = (mm != CMemoryMapping) ? FortranMemoryMapping : CMemoryMapping;
|
---|
[804] | 103 | return default_memory_mapping;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[894] | 106 | //! Set Default Vector Type
|
---|
| 107 | /*!
|
---|
| 108 | \param vt : vector type (ColumnVector,RowVector)
|
---|
| 109 | \return default vector type value
|
---|
| 110 | */
|
---|
[813] | 111 | short BaseArray::SetDefaultVectorType(short vt)
|
---|
| 112 | {
|
---|
| 113 | default_vector_type = (vt != ColumnVector) ? RowVector : ColumnVector ;
|
---|
| 114 | return default_vector_type;
|
---|
| 115 | }
|
---|
[804] | 116 |
|
---|
[894] | 117 | //! Select Memory Mapping
|
---|
| 118 | /*!
|
---|
| 119 | Do essentially nothing.
|
---|
| 120 | \param mm : type of Memory Mapping (CMemoryMapping,FortranMemoryMapping)
|
---|
| 121 | \return return \b mm if it makes sense or default memory mapping value
|
---|
| 122 | \sa SetDefaultMemoryMapping
|
---|
| 123 | */
|
---|
[804] | 124 | short BaseArray::SelectMemoryMapping(short mm)
|
---|
| 125 | {
|
---|
| 126 | if ( (mm == CMemoryMapping) || (mm == FortranMemoryMapping) ) return (mm) ;
|
---|
| 127 | else return (default_memory_mapping);
|
---|
| 128 | }
|
---|
[894] | 129 |
|
---|
| 130 | //! Select Vector type
|
---|
| 131 | /*!
|
---|
| 132 | Do essentially nothing.
|
---|
| 133 | \param vt : vector type (ColumnVector,RowVector)
|
---|
| 134 | \return return \b vt if it makes sense or default vector type
|
---|
| 135 | \sa SetDefaultVectorType
|
---|
| 136 | */
|
---|
[813] | 137 | short BaseArray::SelectVectorType(short vt)
|
---|
| 138 | {
|
---|
| 139 | if ((vt == ColumnVector) || (vt == RowVector)) return(vt);
|
---|
| 140 | else return(default_vector_type);
|
---|
| 141 | }
|
---|
[804] | 142 |
|
---|
[894] | 143 | //! Update Memory Mapping
|
---|
| 144 | /*!
|
---|
| 145 | Update variables marowi_ macoli_ veceli_
|
---|
| 146 | \param mm : type of Memory Mapping (CMemoryMapping,FortranMemoryMapping)
|
---|
| 147 | \sa SetDefaultMemoryMapping
|
---|
| 148 | */
|
---|
[813] | 149 | void BaseArray::UpdateMemoryMapping(short mm)
|
---|
[804] | 150 | {
|
---|
[813] | 151 | short vt = default_vector_type;
|
---|
[804] | 152 | if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) ) mm = default_memory_mapping;
|
---|
| 153 | if (mm == CMemoryMapping) {
|
---|
[813] | 154 | marowi_ = 1; macoli_ = 0;
|
---|
[804] | 155 | }
|
---|
| 156 | else {
|
---|
[813] | 157 | marowi_ = 0; macoli_ = 1;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | if ( (ndim_ == 2) && ((size_[0] == 1) || (size_[1] == 1)) ) {
|
---|
| 161 | // Choix automatique Vecteur ligne ou colonne
|
---|
| 162 | if ( size_[macoli_] == 1) veceli_ = marowi_;
|
---|
| 163 | else veceli_ = macoli_;
|
---|
| 164 | }
|
---|
| 165 | else veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 166 | }
|
---|
| 167 |
|
---|
[894] | 168 | //! Update Memory Mapping
|
---|
| 169 | /*!
|
---|
| 170 | \param a : Array to be compared with
|
---|
| 171 | \param mm : type of Memory Mapping or memory mapping transfert
|
---|
| 172 | (SameMemoryMapping,AutoMemoryMapping,CMemoryMapping,FortranMemoryMapping)
|
---|
| 173 | \sa SetDefaultMemoryMapping
|
---|
| 174 | */
|
---|
[804] | 175 | void BaseArray::UpdateMemoryMapping(BaseArray const & a, short mm)
|
---|
| 176 | {
|
---|
[813] | 177 | short vt = default_vector_type;
|
---|
| 178 | if (mm == SameMemoryMapping) {
|
---|
[804] | 179 | mm = ((a.marowi_ == 1) ? CMemoryMapping : FortranMemoryMapping);
|
---|
[813] | 180 | vt = (a.marowi_ == a.veceli_) ? ColumnVector : RowVector;
|
---|
| 181 | }
|
---|
| 182 | else if (mm == AutoMemoryMapping) mm = default_memory_mapping;
|
---|
| 183 |
|
---|
[804] | 184 | if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) ) mm = default_memory_mapping;
|
---|
| 185 | if (mm == CMemoryMapping) {
|
---|
[813] | 186 | marowi_ = 1; macoli_ = 0;
|
---|
[804] | 187 | }
|
---|
| 188 | else {
|
---|
[813] | 189 | marowi_ = 0; macoli_ = 1;
|
---|
| 190 | }
|
---|
| 191 | if ( (ndim_ == 2) && ((size_[0] == 1) || (size_[1] == 1)) ) {
|
---|
| 192 | // Choix automatique Vecteur ligne ou colonne
|
---|
| 193 | if ( size_[macoli_] == 1) veceli_ = marowi_;
|
---|
| 194 | else veceli_ = marowi_;
|
---|
| 195 | }
|
---|
| 196 | else veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
[804] | 197 | }
|
---|
| 198 |
|
---|
[894] | 199 | //! Set Memory Mapping type
|
---|
| 200 | /*!
|
---|
| 201 | Compute values for variables marowi_ macoli_ veceli_
|
---|
| 202 | \param mm : Memory Mapping type (SameMemoryMapping,AutoMemoryMapping
|
---|
| 203 | ,CMemoryMapping,FortranMemoryMapping)
|
---|
| 204 | \sa SetDefaultMemoryMapping
|
---|
| 205 | */
|
---|
[813] | 206 | void BaseArray::SetMemoryMapping(short mm)
|
---|
| 207 | {
|
---|
| 208 | if (mm == SameMemoryMapping) return;
|
---|
| 209 | if (mm == AutoMemoryMapping) mm = default_memory_mapping;
|
---|
| 210 | if ( (mm != CMemoryMapping) && (mm != FortranMemoryMapping) ) return;
|
---|
| 211 | short vt = (marowi_ == veceli_) ? ColumnVector : RowVector;
|
---|
| 212 | if (mm == CMemoryMapping) {
|
---|
| 213 | marowi_ = 1; macoli_ = 0;
|
---|
| 214 | }
|
---|
| 215 | else {
|
---|
| 216 | marowi_ = 0; macoli_ = 1;
|
---|
| 217 | }
|
---|
| 218 | if ( (ndim_ == 2) && ((size_[0] == 1) || (size_[1] == 1)) ) {
|
---|
| 219 | // Choix automatique Vecteur ligne ou colonne
|
---|
| 220 | if ( size_[macoli_] == 1) veceli_ = marowi_;
|
---|
| 221 | else veceli_ = macoli_;
|
---|
| 222 | }
|
---|
| 223 | else veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
| 224 | }
|
---|
[804] | 225 |
|
---|
[894] | 226 | //! Set Vector Type
|
---|
| 227 | /*!
|
---|
| 228 | Compute values for variable veceli_
|
---|
| 229 | \param vt : vector type ()
|
---|
| 230 | \sa SetDefaultVectorType
|
---|
| 231 | */
|
---|
[813] | 232 | void BaseArray::SetVectorType(short vt)
|
---|
| 233 | {
|
---|
| 234 | if (vt == SameVectorType) return;
|
---|
| 235 | if (vt == AutoVectorType) vt = default_vector_type;
|
---|
| 236 | if ( (ndim_ == 2) && ((size_[0] == 1) || (size_[1] == 1)) ) {
|
---|
| 237 | // Choix automatique Vecteur ligne ou colonne
|
---|
| 238 | if ( size_[macoli_] == 1) veceli_ = marowi_;
|
---|
| 239 | else veceli_ = macoli_;
|
---|
| 240 | }
|
---|
| 241 | else veceli_ = (vt == ColumnVector ) ? marowi_ : macoli_;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[787] | 244 | // -------------------------------------------------------
|
---|
| 245 | // Methodes de la classe
|
---|
| 246 | // -------------------------------------------------------
|
---|
| 247 |
|
---|
[890] | 248 | //! Default constructor
|
---|
[787] | 249 | BaseArray::BaseArray()
|
---|
| 250 | : mInfo(NULL)
|
---|
| 251 | {
|
---|
| 252 | ndim_ = 0;
|
---|
| 253 | for(int k=0; k<BASEARRAY_MAXNDIMS; k++) step_[k] = size_[k] = 0;
|
---|
| 254 | totsize_ = 0;
|
---|
| 255 | minstep_ = 0;
|
---|
| 256 | moystep_ = 0;
|
---|
| 257 | offset_ = 0;
|
---|
[813] | 258 | // Default for matrices : Memory organisation and Vector type
|
---|
| 259 | if (default_memory_mapping == CMemoryMapping) {
|
---|
| 260 | marowi_ = 1; macoli_ = 0;
|
---|
| 261 | }
|
---|
| 262 | else {
|
---|
| 263 | marowi_ = 0; macoli_ = 1;
|
---|
| 264 | }
|
---|
| 265 | veceli_ = (default_vector_type == ColumnVector ) ? marowi_ : macoli_;
|
---|
[1099] | 266 | arrtype_ = 0; // Default Array type, not a Matrix or Vector
|
---|
| 267 |
|
---|
[787] | 268 | }
|
---|
| 269 |
|
---|
[890] | 270 | //! Destructor
|
---|
[787] | 271 | BaseArray::~BaseArray()
|
---|
| 272 | {
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
[1099] | 276 | //! Returns true if the two arrays have compatible dimensions.
|
---|
[890] | 277 | /*!
|
---|
| 278 | \param a : array to be compared
|
---|
[1099] | 279 | \param smo : Return flag = true if the two arrays have the same memory organisation
|
---|
| 280 | \return true if \c NbDimensions() and \c Size() are equal, false if not
|
---|
| 281 |
|
---|
| 282 | If the array (on which the operation is being performed, \c this)
|
---|
| 283 | is a \b Matrix or a \b Vector, the matrix dimensions \c NRows() \c NCols()
|
---|
| 284 | are checked. The flag \c smo is returned true if the two arrays, viewed
|
---|
| 285 | as a matrix have the same memory organisation.
|
---|
| 286 | Otherwise, (if the array is of not a Matrix or a Vector)
|
---|
| 287 | the size compatibility viewed as a TArray is checked <tt>
|
---|
| 288 | (Size(k) == a.Size(k), k=0,...NbDimensions()), </tt> disregard of the memory
|
---|
| 289 | organisation and the row and column index. The flag \c smo is returned true
|
---|
| 290 | in this case.
|
---|
[890] | 291 | */
|
---|
[1099] | 292 | bool BaseArray::CompareSizes(const BaseArray& a, bool& smo)
|
---|
[787] | 293 | {
|
---|
| 294 | if (ndim_ != a.ndim_) return(false);
|
---|
[1099] | 295 | if (arrtype_ == 0) { // Simple TArray, not a matrix
|
---|
| 296 | smo = true;
|
---|
| 297 | for(uint_4 k=0; k<ndim_; k++)
|
---|
| 298 | if (size_[k] != a.size_[k]) return(false);
|
---|
| 299 | return(true);
|
---|
| 300 | }
|
---|
| 301 | else {
|
---|
| 302 | smo = false;
|
---|
[1103] | 303 | if ( (size_[marowi_] != a.size_[a.marowi_]) ||
|
---|
| 304 | (size_[macoli_] != a.size_[a.macoli_]) ) return(false);
|
---|
| 305 | if (ndim_ > 2)
|
---|
| 306 | for(uint_4 k=2; k<ndim_; k++)
|
---|
| 307 | if (size_[k] != a.size_[k]) return(false);
|
---|
[1099] | 308 | if ( (macoli_ == a.macoli_) && (marowi_ == a.marowi_) ||
|
---|
| 309 | (veceli_ == a.veceli_) ) smo = true;
|
---|
| 310 | return(true);
|
---|
| 311 | }
|
---|
[787] | 312 | }
|
---|
| 313 |
|
---|
[894] | 314 | //! Change dimension if some size == 1
|
---|
[787] | 315 | void BaseArray::CompactAllDim()
|
---|
| 316 | {
|
---|
| 317 | if (ndim_ < 2) return;
|
---|
| 318 | uint_4 ndim = 0;
|
---|
| 319 | uint_4 size[BASEARRAY_MAXNDIMS];
|
---|
| 320 | uint_4 step[BASEARRAY_MAXNDIMS];
|
---|
[958] | 321 | for(uint_4 k=0; k<ndim_; k++) {
|
---|
[787] | 322 | if (size_[k] < 2) continue;
|
---|
| 323 | size[ndim] = size_[k];
|
---|
| 324 | step[ndim] = step_[k];
|
---|
| 325 | ndim++;
|
---|
| 326 | }
|
---|
| 327 | if (ndim == 0) {
|
---|
| 328 | size[0] = size_[0];
|
---|
| 329 | step[0] = step_[0];
|
---|
| 330 | ndim = 1;
|
---|
| 331 | }
|
---|
| 332 | string exmsg = "BaseArray::CompactAllDim() ";
|
---|
| 333 | if (!UpdateSizes(ndim, size, step, offset_, exmsg)) throw( ParmError(exmsg) );
|
---|
| 334 | return;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[894] | 337 | //! Change dimension if some trailed size == 1
|
---|
[787] | 338 | void BaseArray::CompactTrailingDim()
|
---|
| 339 | {
|
---|
| 340 | if (ndim_ < 2) return;
|
---|
| 341 | uint_4 ndim = 0;
|
---|
| 342 | uint_4 size[BASEARRAY_MAXNDIMS];
|
---|
| 343 | uint_4 step[BASEARRAY_MAXNDIMS];
|
---|
[958] | 344 | for(uint_4 k=0; k<ndim_; k++) {
|
---|
[787] | 345 | size[ndim] = size_[k];
|
---|
| 346 | step[ndim] = step_[k];
|
---|
| 347 | if (size_[k] > 1) ndim=k;
|
---|
| 348 | }
|
---|
| 349 | if (ndim == 0) ndim = 1;
|
---|
| 350 | string exmsg = "BaseArray::CompactTrailingDim() ";
|
---|
| 351 | if (!UpdateSizes(ndim, size, step, offset_, exmsg)) throw( ParmError(exmsg) );
|
---|
| 352 | return;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[894] | 355 | //! return minimum value for step[ndim]
|
---|
[1099] | 356 | int BaseArray::MinStepKA() const
|
---|
[787] | 357 | {
|
---|
[1103] | 358 | for(uint_4 ka=0; ka<ndim_; ka++)
|
---|
| 359 | if (step_[ka] == minstep_) return((int)ka);
|
---|
[787] | 360 | return(0);
|
---|
| 361 | }
|
---|
| 362 |
|
---|
[894] | 363 | //! return maximum value for step[ndim]
|
---|
[1099] | 364 | int BaseArray::MaxSizeKA() const
|
---|
[787] | 365 | {
|
---|
[1099] | 366 | int ka = 0;
|
---|
[787] | 367 | uint_4 mx = size_[0];
|
---|
[1103] | 368 | for(uint_4 k=1; k<ndim_; k++)
|
---|
[1099] | 369 | if (size_[k] > mx) { ka = k; mx = size_[k]; }
|
---|
[787] | 370 | return(ka);
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 |
|
---|
| 374 | // Acces lineaire aux elements .... Calcul d'offset
|
---|
[813] | 375 | // --------------------------------------------------
|
---|
| 376 | // Position de l'element 0 du vecteur i selon l'axe ka
|
---|
| 377 | // --------------------------------------------------
|
---|
[894] | 378 | //! return position of first element for vector \b i alond \b ka th axe.
|
---|
[813] | 379 | uint_8 BaseArray::Offset(uint_4 ka, uint_8 i) const
|
---|
| 380 | {
|
---|
[787] | 381 |
|
---|
[813] | 382 | if ( (ndim_ < 1) || (i == 0) ) return(offset_);
|
---|
| 383 | //#ifdef SO_BOUNDCHECKING
|
---|
| 384 | if (ka >= ndim_)
|
---|
| 385 | throw RangeCheckError("BaseArray::Offset(uint_4 ka, uint_8 i) Axe KA Error");
|
---|
| 386 | if ( i*size_[ka] >= totsize_ )
|
---|
| 387 | throw RangeCheckError("BaseArray::Offset(uint_4 ka, uint_8 i) Index Error");
|
---|
| 388 | //#endif
|
---|
| 389 | uint_4 idx[BASEARRAY_MAXNDIMS];
|
---|
[958] | 390 | uint_4 k;
|
---|
[813] | 391 | uint_8 rest = i;
|
---|
| 392 | idx[ka] = 0;
|
---|
| 393 | for(k=0; k<ndim_; k++) {
|
---|
| 394 | if (k == ka) continue;
|
---|
| 395 | idx[k] = rest%size_[k]; rest /= size_[k];
|
---|
| 396 | }
|
---|
| 397 | uint_8 off = offset_;
|
---|
| 398 | for(k=0; k<ndim_; k++) off += idx[k]*step_[k];
|
---|
| 399 | return (off);
|
---|
| 400 | }
|
---|
| 401 |
|
---|
[894] | 402 | //! return position of element \b ip.
|
---|
[787] | 403 | uint_8 BaseArray::Offset(uint_8 ip) const
|
---|
| 404 | {
|
---|
[813] | 405 | if ( (ndim_ < 1) || (ip == 0) ) return(offset_);
|
---|
| 406 | //#ifdef SO_BOUNDCHECKING
|
---|
| 407 | if (ip >= totsize_)
|
---|
| 408 | throw RangeCheckError("BaseArray::Offset(uint_8 ip) Out of range index ip");
|
---|
| 409 | //#endif
|
---|
| 410 |
|
---|
| 411 | uint_4 idx[BASEARRAY_MAXNDIMS];
|
---|
[958] | 412 | uint_4 k;
|
---|
[813] | 413 | uint_8 rest = ip;
|
---|
| 414 | for(k=0; k<ndim_; k++) {
|
---|
| 415 | idx[k] = rest%size_[k]; rest /= size_[k];
|
---|
| 416 | }
|
---|
| 417 | //#ifdef SO_BOUNDCHECKING
|
---|
| 418 | if (rest != 0)
|
---|
| 419 | throw PError("BaseArray::Offset(uint_8 ip) GUG !!! rest != 0");
|
---|
| 420 | //#endif
|
---|
| 421 | // if (rest != 0) cerr << " BUG ---- BaseArray::Offset( " << ip << " )" << rest << endl;
|
---|
| 422 | // cerr << " DBG-Offset( " << ip << ")" ;
|
---|
| 423 | // for(k=0; k<ndim_; k++) cerr << idx[k] << "," ;
|
---|
| 424 | // cerr << " ZZZZ " << endl;
|
---|
| 425 | uint_8 off = offset_;
|
---|
| 426 | for(k=0; k<ndim_; k++) off += idx[k]*step_[k];
|
---|
| 427 | return (off);
|
---|
[787] | 428 | }
|
---|
| 429 |
|
---|
[1099] | 430 | //! return various parameters for double loop operations on two arrays.
|
---|
| 431 | void BaseArray::GetOpeParams(const BaseArray& a, bool smo, int& ax, int& axa, uint_8& step,
|
---|
| 432 | uint_8& stepa, uint_8& gpas, uint_8& naxa)
|
---|
| 433 | {
|
---|
| 434 | if (smo) { // Same memory organisation
|
---|
| 435 | ax = axa = MaxSizeKA();
|
---|
| 436 | }
|
---|
| 437 | else {
|
---|
| 438 | if (Size(RowsKA()) >= Size(ColsKA()) ) {
|
---|
| 439 | ax = RowsKA();
|
---|
| 440 | axa = a.RowsKA();
|
---|
| 441 | }
|
---|
| 442 | else {
|
---|
| 443 | ax = ColsKA();
|
---|
| 444 | axa = a.ColsKA();
|
---|
| 445 | }
|
---|
| 446 | }
|
---|
| 447 | step = Step(ax);
|
---|
| 448 | stepa = a.Step(axa);
|
---|
| 449 | gpas = Size(ax)*step;
|
---|
| 450 | naxa = Size()/Size(ax);
|
---|
| 451 | return;
|
---|
| 452 | }
|
---|
[787] | 453 |
|
---|
| 454 | // ----------------------------------------------------
|
---|
| 455 | // Impression, etc ...
|
---|
| 456 | // ----------------------------------------------------
|
---|
| 457 |
|
---|
[894] | 458 | //! Show infos on stream \b os (\b si to display DvList)
|
---|
[787] | 459 | void BaseArray::Show(ostream& os, bool si) const
|
---|
| 460 | {
|
---|
[850] | 461 | if (ndim_ < 1) {
|
---|
| 462 | os << "\n--- " << BaseArray::InfoString() << " Unallocated Array ! " << endl;
|
---|
| 463 | return;
|
---|
| 464 | }
|
---|
[813] | 465 | os << "\n--- " << InfoString() ;
|
---|
| 466 | os << " ND=" << ndim_ << " SizeX*Y*...= " ;
|
---|
[958] | 467 | for(uint_4 k=0; k<ndim_; k++) {
|
---|
[787] | 468 | os << size_[k];
|
---|
[813] | 469 | if (k<ndim_-1) os << "x";
|
---|
[787] | 470 | }
|
---|
[813] | 471 | os << " ---" << endl;
|
---|
| 472 | if (prt_lev_ > 0) {
|
---|
| 473 | os << " TotSize= " << totsize_ << " Step(X Y ...)=" ;
|
---|
[958] | 474 | for(uint_4 k=0; k<ndim_; k++) os << step_[k] << " " ;
|
---|
[813] | 475 | os << " Offset= " << offset_ << endl;
|
---|
| 476 | }
|
---|
| 477 | if (prt_lev_ > 1) {
|
---|
| 478 | os << " MemoryMapping=" << GetMemoryMapping() << " VecType= " << GetVectorType()
|
---|
| 479 | << " RowsKA= " << RowsKA() << " ColsKA= " << ColsKA()
|
---|
[1103] | 480 | << " VectKA=" << VectKA() << " ArrayType=" << arrtype_ << endl;
|
---|
[813] | 481 | }
|
---|
| 482 | if (!si && (prt_lev_ < 2)) return;
|
---|
| 483 | if (mInfo != NULL) os << (*mInfo) << endl;
|
---|
[787] | 484 |
|
---|
| 485 | }
|
---|
| 486 |
|
---|
[894] | 487 | //! Return BaseArray Type
|
---|
[813] | 488 | string BaseArray::InfoString() const
|
---|
| 489 | {
|
---|
| 490 | string rs = "BaseArray Type= ";
|
---|
| 491 | rs += typeid(*this).name() ;
|
---|
| 492 | return rs;
|
---|
| 493 | }
|
---|
[787] | 494 |
|
---|
[894] | 495 | //! Return attached DVList
|
---|
[787] | 496 | DVList& BaseArray::Info()
|
---|
| 497 | {
|
---|
| 498 | if (mInfo == NULL) mInfo = new DVList;
|
---|
| 499 | return(*mInfo);
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[894] | 502 | //! Update sizes and information for array
|
---|
| 503 | /*!
|
---|
| 504 | \param ndim : dimension
|
---|
| 505 | \param siz[ndim] : sizes
|
---|
| 506 | \param step : step (must be the same on all dimensions)
|
---|
| 507 | \param offset : offset of the first element
|
---|
| 508 | \return true if all OK, false if problems appear
|
---|
| 509 | \return string \b exmsg for explanation in case of problems
|
---|
| 510 | */
|
---|
[787] | 511 | bool BaseArray::UpdateSizes(uint_4 ndim, const uint_4 * siz, uint_4 step, uint_8 offset, string & exmsg)
|
---|
| 512 | {
|
---|
| 513 | if (ndim >= BASEARRAY_MAXNDIMS) {
|
---|
| 514 | exmsg += " NDim Error"; return false;
|
---|
| 515 | }
|
---|
| 516 | if (step < 1) {
|
---|
| 517 | exmsg += " Step(=0) Error"; return false;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | minstep_ = moystep_ = step;
|
---|
| 521 |
|
---|
| 522 | // Flagging bad updates ...
|
---|
| 523 | ndim_ = 0;
|
---|
| 524 |
|
---|
| 525 | totsize_ = 1;
|
---|
[958] | 526 | uint_4 k;
|
---|
[787] | 527 | for(k=0; k<BASEARRAY_MAXNDIMS; k++) {
|
---|
| 528 | size_[k] = 1;
|
---|
| 529 | step_[k] = 0;
|
---|
| 530 | }
|
---|
| 531 | for(k=0; k<ndim; k++) {
|
---|
| 532 | size_[k] = siz[k] ;
|
---|
| 533 | step_[k] = totsize_*step;
|
---|
| 534 | totsize_ *= size_[k];
|
---|
| 535 | }
|
---|
| 536 | if (totsize_ < 1) {
|
---|
| 537 | exmsg += " Size Error"; return false;
|
---|
| 538 | }
|
---|
| 539 | offset_ = offset;
|
---|
[813] | 540 | // Default for matrices : Memory organisation and Vector type
|
---|
| 541 | if (default_memory_mapping == CMemoryMapping) {
|
---|
| 542 | marowi_ = 1; macoli_ = 0;
|
---|
| 543 | }
|
---|
| 544 | else {
|
---|
| 545 | marowi_ = 0; macoli_ = 1;
|
---|
| 546 | }
|
---|
| 547 | veceli_ = (default_vector_type == ColumnVector ) ? marowi_ : macoli_;
|
---|
[787] | 548 | // Update OK
|
---|
| 549 | ndim_ = ndim;
|
---|
| 550 | return true;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
[894] | 553 | //! Update sizes and information for array
|
---|
| 554 | /*!
|
---|
| 555 | \param ndim : dimension
|
---|
| 556 | \param siz[ndim] : sizes
|
---|
| 557 | \param step[ndim] : steps
|
---|
| 558 | \param offset : offset of the first element
|
---|
| 559 | \return true if all OK, false if problems appear
|
---|
| 560 | \return string \b exmsg for explanation in case of problems
|
---|
| 561 | */
|
---|
[787] | 562 | bool BaseArray::UpdateSizes(uint_4 ndim, const uint_4 * siz, const uint_4 * step, uint_8 offset, string & exmsg)
|
---|
| 563 | {
|
---|
| 564 | if (ndim >= BASEARRAY_MAXNDIMS) {
|
---|
| 565 | exmsg += " NDim Error"; return false;
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | // Flagging bad updates ...
|
---|
| 569 | ndim_ = 0;
|
---|
| 570 |
|
---|
| 571 | totsize_ = 1;
|
---|
[958] | 572 | uint_4 k;
|
---|
[787] | 573 | for(k=0; k<BASEARRAY_MAXNDIMS; k++) {
|
---|
| 574 | size_[k] = 1;
|
---|
| 575 | step_[k] = 0;
|
---|
| 576 | }
|
---|
| 577 | uint_4 minstep = step[0];
|
---|
| 578 | for(k=0; k<ndim; k++) {
|
---|
| 579 | size_[k] = siz[k] ;
|
---|
| 580 | step_[k] = step[k];
|
---|
| 581 | totsize_ *= size_[k];
|
---|
| 582 | if (step_[k] < minstep) minstep = step_[k];
|
---|
| 583 | }
|
---|
| 584 | if (minstep < 1) {
|
---|
| 585 | exmsg += " Step(=0) Error"; return false;
|
---|
| 586 | }
|
---|
| 587 | if (totsize_ < 1) {
|
---|
| 588 | exmsg += " Size Error"; return false;
|
---|
| 589 | }
|
---|
| 590 | uint_8 plast = 0;
|
---|
| 591 | for(k=0; k<ndim; k++) plast += (siz[k]-1)*step[k];
|
---|
| 592 | if (plast == minstep*totsize_ ) moystep_ = minstep;
|
---|
| 593 | else moystep_ = 0;
|
---|
| 594 | minstep_ = minstep;
|
---|
| 595 | offset_ = offset;
|
---|
[813] | 596 | // Default for matrices : Memory organisation and Vector type
|
---|
| 597 | if (default_memory_mapping == CMemoryMapping) {
|
---|
| 598 | marowi_ = 1; macoli_ = 0;
|
---|
| 599 | }
|
---|
| 600 | else {
|
---|
| 601 | marowi_ = 0; macoli_ = 1;
|
---|
| 602 | }
|
---|
| 603 | veceli_ = (default_vector_type == ColumnVector ) ? marowi_ : macoli_;
|
---|
[787] | 604 | // Update OK
|
---|
| 605 | ndim_ = ndim;
|
---|
| 606 | return true;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[894] | 609 | //! Update sizes and information relative to array \b a
|
---|
| 610 | /*!
|
---|
| 611 | \param a : array to be compare with
|
---|
| 612 | \return true if all OK, false if problems appear
|
---|
| 613 | \return string \b exmsg for explanation in case of problems
|
---|
| 614 | */
|
---|
[787] | 615 | bool BaseArray::UpdateSizes(const BaseArray& a, string & exmsg)
|
---|
| 616 | {
|
---|
| 617 | if (a.ndim_ >= BASEARRAY_MAXNDIMS) {
|
---|
| 618 | exmsg += " NDim Error"; return false;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | // Flagging bad updates ...
|
---|
| 622 | ndim_ = 0;
|
---|
| 623 |
|
---|
| 624 | totsize_ = 1;
|
---|
[958] | 625 | uint_4 k;
|
---|
[787] | 626 | for(k=0; k<BASEARRAY_MAXNDIMS; k++) {
|
---|
| 627 | size_[k] = 1;
|
---|
| 628 | step_[k] = 0;
|
---|
| 629 | }
|
---|
| 630 | uint_4 minstep = a.step_[0];
|
---|
| 631 | for(k=0; k<a.ndim_; k++) {
|
---|
| 632 | size_[k] = a.size_[k] ;
|
---|
| 633 | step_[k] = a.step_[k];
|
---|
| 634 | totsize_ *= size_[k];
|
---|
| 635 | if (step_[k] < minstep) minstep = step_[k];
|
---|
| 636 | }
|
---|
| 637 | if (minstep < 1) {
|
---|
| 638 | exmsg += " Step(=0) Error"; return false;
|
---|
| 639 | }
|
---|
| 640 | if (totsize_ < 1) {
|
---|
| 641 | exmsg += " Size Error"; return false;
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | minstep_ = a.minstep_;
|
---|
| 645 | moystep_ = a.moystep_;
|
---|
| 646 | offset_ = a.offset_;
|
---|
| 647 | macoli_ = a.macoli_;
|
---|
| 648 | marowi_ = a.marowi_;
|
---|
[804] | 649 | veceli_ = a.veceli_;
|
---|
[787] | 650 | // Update OK
|
---|
| 651 | ndim_ = a.ndim_;
|
---|
| 652 | return true;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 |
|
---|
[894] | 656 | //! Update sizes and information relative to array \b a
|
---|
| 657 | /*!
|
---|
| 658 | \param a : array to be compare with
|
---|
| 659 | \param ndim : could be change (but should be less than the ndim of the current class)
|
---|
| 660 | \param siz[ndim],pos[ndim],step[ndim] : could be changed but must be
|
---|
| 661 | compatible within the memory size with those of the current class.
|
---|
| 662 | \return true if all OK, false if problems appear
|
---|
| 663 | \return string \b exmsg for explanation in case of problems
|
---|
| 664 | */
|
---|
[804] | 665 | void BaseArray::UpdateSubArraySizes(BaseArray & ra, uint_4 ndim, uint_4 * siz, uint_4 * pos, uint_4 * step) const
|
---|
[787] | 666 | {
|
---|
[804] | 667 | if ( (ndim > ndim_) || (ndim < 1) )
|
---|
| 668 | throw(SzMismatchError("BaseArray::UpdateSubArraySizes( ... ) NDim Error") );
|
---|
[958] | 669 | uint_4 k;
|
---|
[787] | 670 | for(k=0; k<ndim; k++)
|
---|
| 671 | if ( (siz[k]*step[k]+pos[k]) > size_[k] )
|
---|
[804] | 672 | throw(SzMismatchError("BaseArray::UpdateSubArraySizes( ... ) Size/Pos Error") );
|
---|
[787] | 673 | uint_8 offset = offset_;
|
---|
| 674 | for(k=0; k<ndim_; k++) {
|
---|
| 675 | offset += pos[k]*step_[k];
|
---|
| 676 | step[k] *= step_[k];
|
---|
| 677 | }
|
---|
[804] | 678 | string exm = "BaseArray::UpdateSubArraySizes() ";
|
---|
[787] | 679 | if (!ra.UpdateSizes(ndim, siz, step, offset, exm))
|
---|
| 680 | throw( ParmError(exm) );
|
---|
| 681 | return;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 |
|
---|