source: Sophya/trunk/SophyaLib/TArray/basarr.h@ 882

Last change on this file since 882 was 813, checked in by ansari, 25 years ago

Correction bug/amelioarions TArray,TMatrix,TVector - Reza 5/4/2000

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