C++ mpi module for stochmagnet_main Package
SM_PackedBlockSymmetricMatrix.h
1 #ifndef SM_PackedBlockSymmetricMatrix_H
2 #define SM_PackedBlockSymmetricMatrix_H
3 
4 //base classes
5 #include "SM_PackedBlockMatrix.h"
6 
7 
17 template<typename T,tUCInt P>
19 
20 public:
21 
22 
23 private :
24 
25  //type class
28 
29  //single attributes
30  //-----------------
31 
32 
33 
34 
35  //multiple attributes
36  //------------------
37 
38  //values of superior blocks of the matrix
39  std::valarray<T> mSupBlocksValues;
40  //diagonal values : Diagonal[i].I_P
41  std::valarray<T> mDiagonalValues;
42 
43 
44  tIndex mWIndex;
45 
46  //single associations
47  //-------------------
48 
49 
50  //multiple associations
51  //---------------------
52 
53 
54 
55 protected:
56  // CONSTRUCTORS
60 
61  }
62 
63  // DESTRUCTORS
67  }
68 
69 
70 public :
71 
72  //Instance building
73  //=================
74 
75 
89  virtual tMemSize getMemorySize() const {
90  return sizeof(*this)+this->getContentsMemorySize();
91  }
92 
101  virtual tMemSize getContentsMemorySize() const {
102  tMemSize mem=SuperClass::getContentsMemorySize();
103  mem+=mSupBlocksValues.size()*sizeof(T);
104  mem+=mDiagonalValues.size()*sizeof(T);
105  return mem;
106  }
107 
111  inline static CORE_UniquePointer<SelfClass> New() {
112  return CORE_UniquePointer<SelfClass>(new SelfClass(),
114  }
115 
116 public:
117 
118  //SET & GET
119  //=========
120 
123  virtual void reset() final {
124  memset(&mSupBlocksValues[0],0,mSupBlocksValues.size()*sizeof(T));
125  memset(&mDiagonalValues[0],0,mDiagonalValues.size()*sizeof(T));
126 
127  }
131  inline void setSize(const tIndex& nRowBlocks) {
132  setSize(nRowBlocks,nRowBlocks);
133  }
134 
139  virtual void setSize(const tIndex& nRowBlocks,const tIndex& nColBlocks) final {
140  ASSERT_IN(nRowBlocks==nColBlocks);
141 
142  SuperClass::setSize(nRowBlocks,nColBlocks);
143 
144  //number of elements of the sup block values
145  tIndex s=GetSupBlocksValuesNumber(nRowBlocks);
146  if (s!=mSupBlocksValues.size()) {
147  try {
148  mSupBlocksValues.resize(s);
149  memset(&mSupBlocksValues[0],0,sizeof(T)*s);
150  } catch (std::exception& e) {
151  throw CORE_Exception("stochmagnet/operators/dipolar",
152  "SM_PackedBlockSymmetricMatrix::setSize("+std::to_string(nRowBlocks)+")",
153  "not enough size to allocate "+std::to_string(s)+" elements for sup matrix : memory size needed: "+std::to_string((sizeof(T)*s)/(1024*1024*1024))+" Go");
154  }
155  }
156  if (nRowBlocks!=mDiagonalValues.size()) {
157  try {
158  mDiagonalValues.resize(nRowBlocks);
159  memset(&mDiagonalValues[0],0,sizeof(T)*nRowBlocks);
160  } catch (std::exception& e) {
161  throw CORE_Exception("stochmagnet/operators/dipolar",
162  "SM_PackedBlockSymmetricMatrix::setSize("+std::to_string(nRowBlocks)+")",
163  "not enough size to allocate "+std::to_string(s)+" elements for diagonla matrix : memory size needed: "+std::to_string((sizeof(T)*s)/(1024*1024*1024))+" Go");
164 
165  }
166  }
167  }
168 
174  virtual void setValue(const tIndex& i,const tIndex& j,const T& v) final{
175  (*this)(i,j)=v;
176  }
177 
183  virtual const T& getValue(const tIndex& i,const tIndex& j) const final {
184  return (*this)(i,j);
185  }
191  virtual T& getValue(const tIndex& i,const tIndex& j) final {
192  return (*this)(i,j);
193  }
194 
195 public:
196 
199  inline std::valarray<T>& getSupBlocksValues() {
200  return mSupBlocksValues;
201  }
204  inline std::valarray<T>& getDiagonalValues(){
205  return mDiagonalValues;
206  }
207 
210  inline const std::valarray<T>& getSupBlocksValues() const {
211  return mSupBlocksValues;
212  }
215  inline const std::valarray<T>& getDiagonalValues() const {
216  return mDiagonalValues;
217  }
218 
219 
220  //OPERATORS
221  //==========
222 public:
228  inline const T& operator()(tIndex i,tIndex j) const {
229  tIndex index=this->GetPackedIndex(i%P,j%P);
230  //index of the block
231  i/=P;
232  j/=P;
233  return (i==j)?mDiagonalValues[i]:mSupBlocksValues[GetSupBlockIndex<P>(i,j)+index];
234  }
240  inline T& operator()(tIndex i,tIndex j) {
241  mWIndex=this->GetPackedIndex(i%P,j%P);
242  i/=P;
243  j/=P;
244  return (i==j)?mDiagonalValues[i]:mSupBlocksValues[GetSupBlockIndex<P>(i,j)+mWIndex];
245  }
246 
247 
248 private:
252  inline static tIndex GetSupBlocksValuesNumber(const tIndex& n) {
253  //number of blocks
254  tIndex p=n;
255  p*=(n-1);
256  p/=2;
257  //number of elements
259  return p;
260  }
261 
262 
263 public:
268  inline static tIndex GetBlockIndex(const tIndex& ib ,const tIndex& jb) {
269  return GetSupBlockIndex<SM_PackedBlockMatrix<T,P>::BLOCK_SIZE>(ib,jb);
270  }
271 private:
278  template<tUCInt s>
279  inline static tIndex GetBlockIndex(const tIndex& i ,const tIndex& j) {
280  ASSERT_IN(i<=j);
281  tIndex index=j;
282  index++;
283  index*=j;
284  index/=2;
285  index+=i;
286  index*=s;
287  return index;
288  }
295  template<tUCInt s>
296  inline static tIndex GetSupBlockIndex(const tIndex& ib ,const tIndex& jb) {
297  ASSERT_IN(ib<jb);
298  tIndex index=jb;
299  index--;
300  index*=jb;
301  index/=2;
302  index+=ib;
303  index*=s;
304  return index;
305  }
306 
307 
308 public:
309 
314  virtual void vectorProduct(const T* vX,T* vY) const;
315 
316 public:
317 
320  virtual tString toString() const override {
321  std::stringstream ret;
322  ret<<SuperClass::toString()<<"\n";
323  //print matrix 6 first blocks
324  ret<<"sup blocks values:"<<functions_array::toString(mSupBlocksValues)<<"\n";
325  ret<<"diagonal values:"<<functions_array::toString(mDiagonalValues)<<"\n";
326  return ret.str();
327  }
328 
329 };
330 
331 #include "SM_PackedBlockSymmetricMatrix.hpp"
332 
333 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
abstract base class for most classes.
Definition: CORE_Object.h:65
This class described a matrix by packed block of size in a row storage.
Definition: SM_PackedBlockMatrix.h:18
virtual tString toString() const override
return string representaton of the operator
Definition: SM_PackedBlockMatrix.h:236
virtual void setSize(const tInteger &nRowBlocks, const tInteger &nColBlocks)
set the number or rows & columns of the blocks matrix
Definition: SM_PackedBlockMatrix.h:112
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_PackedBlockMatrix.h:94
static tIndex GetPackedIndex(const tIndex &i, const tIndex &j)
return the pack index of size Q in a column storage
Definition: SM_PackedBlockMatrix.h:165
This class described a symmetric matrix by block of size PxP in a packed storage.
Definition: SM_PackedBlockSymmetricMatrix.h:18
static tIndex GetBlockIndex(const tIndex &ib, const tIndex &jb)
get the value of the sub pack block at index
Definition: SM_PackedBlockSymmetricMatrix.h:268
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_PackedBlockSymmetricMatrix.h:101
SM_PackedBlockSymmetricMatrix(void)
create a matrix of size 0
Definition: SM_PackedBlockSymmetricMatrix.h:59
virtual void setSize(const tIndex &nRowBlocks, const tIndex &nColBlocks) final
set the number or rows & columns of the blocks matrix
Definition: SM_PackedBlockSymmetricMatrix.h:139
const T & operator()(tIndex i, tIndex j) const
get values at row i and column j for reading
Definition: SM_PackedBlockSymmetricMatrix.h:228
virtual tString toString() const override
return string representaton of the operator
Definition: SM_PackedBlockSymmetricMatrix.h:320
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_PackedBlockSymmetricMatrix.h:89
std::valarray< T > & getDiagonalValues()
get the diagonal values : Diagonal[i].I_P
Definition: SM_PackedBlockSymmetricMatrix.h:204
virtual void vectorProduct(const T *vX, T *vY) const
vector product
Definition: SM_PackedBlockSymmetricMatrix.hpp:5
T & operator()(tIndex i, tIndex j)
get values at row i and column j for writing
Definition: SM_PackedBlockSymmetricMatrix.h:240
virtual void reset() final
reset the values
Definition: SM_PackedBlockSymmetricMatrix.h:123
virtual ~SM_PackedBlockSymmetricMatrix(void)
destroy
Definition: SM_PackedBlockSymmetricMatrix.h:66
std::valarray< T > & getSupBlocksValues()
get the values of superior blocks of the matrix
Definition: SM_PackedBlockSymmetricMatrix.h:199
virtual void setValue(const tIndex &i, const tIndex &j, const T &v) final
set values at row i and column j
Definition: SM_PackedBlockSymmetricMatrix.h:174
virtual const T & getValue(const tIndex &i, const tIndex &j) const final
get values at row i and column j for reading
Definition: SM_PackedBlockSymmetricMatrix.h:183
virtual T & getValue(const tIndex &i, const tIndex &j) final
get values at row i and column j for writing
Definition: SM_PackedBlockSymmetricMatrix.h:191
void setSize(const tIndex &nRowBlocks)
set the number or rows & columns of the blocks matrix
Definition: SM_PackedBlockSymmetricMatrix.h:131
static CORE_UniquePointer< SelfClass > New()
build a new instance of the operator
Definition: SM_PackedBlockSymmetricMatrix.h:111
const std::valarray< T > & getDiagonalValues() const
get the diagonal values : Diagonal[i].I_P
Definition: SM_PackedBlockSymmetricMatrix.h:215
const std::valarray< T > & getSupBlocksValues() const
get the values of superior blocks of the matrix
Definition: SM_PackedBlockSymmetricMatrix.h:210