C++ mpi module for stochmagnet_main Package
SM_PackedBlockGeneralMatrix.h
1 #ifndef SM_PackedBlockGeneralMatrix_H
2 #define SM_PackedBlockGeneralMatrix_H
3 
4 //base classes
5 #include "SM_PackedBlockMatrix.h"
6 
7 
16 template<typename T,tUCInt P>
18 
19 public:
20 
21 
22 private :
23 
24  //type class
27 
28  //single attributes
29  //-----------------
30 
31 
32  //multiple attributes
33  //------------------
34 
35  //values of superior blocks of the matrix
36  std::valarray<T> mBlocksValues;
37 
38 
39 
40  //single associations
41  //-------------------
42 
43 
44  //multiple associations
45  //---------------------
46 
47 
48 
49 protected:
50  // CONSTRUCTORS
54 
55  }
56 
57  // DESTRUCTORS
61  }
62 
63 
64 public :
65 
66  //Instance building
67  //=================
68 
69 
83  virtual tMemSize getMemorySize() const {
84  return sizeof(*this)+this->getContentsMemorySize();
85  }
86 
95  virtual tMemSize getContentsMemorySize() const {
96  tMemSize mem=SuperClass::getContentsMemorySize();
97  mem+=mBlocksValues.size()*sizeof(T);
98  return mem;
99  }
100 
101 
105  inline static CORE_UniquePointer<SelfClass> New() {
106  return CORE_UniquePointer<SelfClass>(new SelfClass(),
108  }
109 
110 
111 public:
112 
113  //SET & GET
114  //=========
115 
118  virtual void reset() final{
119  memset(&mBlocksValues[0],0,mBlocksValues.size()*sizeof(T));
120 
121  }
125  virtual void setSize(const tIndex& nRowBlocks,const tIndex& nColBlocks) final {
126  SuperClass::setSize(nRowBlocks,nColBlocks);
127 
128  //number of elements of the sup block values
129  tIndex s=nRowBlocks*nColBlocks*SM_PackedBlockMatrix<T,P>::BLOCK_SIZE;
130  if (s!=mBlocksValues.size()) {
131  try {
132  mBlocksValues.resize(s);
133  memset(&mBlocksValues[0],0,sizeof(T)*s);
134  } catch (std::exception& e) {
135  throw CORE_Exception("stochmagnet/operators/dipolar",
136  "SM_PackedBlockGeneralMatrix::setSize("+std::to_string(nRowBlocks)+","+std::to_string(nColBlocks)+")",
137  "not enough size to allocate "+std::to_string(s)+" elements for matrix : memory size needed: "+std::to_string((sizeof(T)*s)/(1024*1024*1024))+" Go");
138  }
139  }
140 
141  }
142 
148  virtual void setValue(const tIndex& i,const tIndex& j,const T& v) final {
149  (*this)(i,j)=v;
150  }
151 
157  virtual const T& getValue(const tIndex& i,const tIndex& j) const final {
158  return (*this)(i,j);
159  }
165  virtual T& getValue(const tIndex& i,const tIndex& j) final {
166  return (*this)(i,j);
167  }
168 
169 public:
170 
173  inline std::valarray<T>& getBlocksValues() {
174  return mBlocksValues;
175  }
176 
179  inline const std::valarray<T>& getBlocksValues() const {
180  return mBlocksValues;
181  }
182 
183 
184 
185  //OPERATORS
186  //==========
187 public:
193  inline const T& operator()(tIndex i,tIndex j) const {
194  //indices of the block : ib=i/P jb=j/P
195  //index of the block index= ib x nColBlocks + jb
196  //index of the element (ib x nColBlocks + jb) + getPackedIndex(i%P,j%P)
197  return mBlocksValues[SM_PackedBlockMatrix<T,P>::GetPackedIndex(i%P,j%P)+(i/P)*this->getColumnBlocksNumber()+(j/P)];
198  }
199 
205  inline T& operator()(tIndex i,tIndex j) {
206  return mBlocksValues[SM_PackedBlockMatrix<T,P>::GetPackedIndex(i%P,j%P)+(i/P)*this->getColumnBlocksNumber()+(j/P)];
207  }
208 
209 
210 
211 
212 private:
213 
214 
215 
216 public:
217 
222  virtual void vectorProduct(const T* vX,T* vY) const;
223 
224 public:
225 
228  virtual tString toString() const override {
229  std::stringstream ret;
230  ret<<SuperClass::toString()<<"\n";
231  //print matrix 6 first blocks
232  ret<<"blocks values:"<<functions_array::toString(mBlocksValues)<<"\n";
233  return ret.str();
234  }
235 
236 };
237 
238 #include "SM_PackedBlockGeneralMatrix.hpp"
239 
240 #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
This class described a matrix by packed symmetric block of size NxP in a row storage.
Definition: SM_PackedBlockGeneralMatrix.h:17
static CORE_UniquePointer< SelfClass > New()
build a new instance of the operator
Definition: SM_PackedBlockGeneralMatrix.h:105
virtual void reset() final
reset the values
Definition: SM_PackedBlockGeneralMatrix.h:118
virtual ~SM_PackedBlockGeneralMatrix(void)
destroy
Definition: SM_PackedBlockGeneralMatrix.h:60
virtual const T & getValue(const tIndex &i, const tIndex &j) const final
get values at row i and column j for reading
Definition: SM_PackedBlockGeneralMatrix.h:157
virtual void vectorProduct(const T *vX, T *vY) const
vector product
Definition: SM_PackedBlockGeneralMatrix.hpp:5
virtual void setSize(const tIndex &nRowBlocks, const tIndex &nColBlocks) final
set the number or rows & columns of the blocks matrix
Definition: SM_PackedBlockGeneralMatrix.h:125
T & operator()(tIndex i, tIndex j)
get values at row i and column j for writing
Definition: SM_PackedBlockGeneralMatrix.h:205
SM_PackedBlockGeneralMatrix(void)
create a matrix of size 0
Definition: SM_PackedBlockGeneralMatrix.h:53
const std::valarray< T > & getBlocksValues() const
get the values of superior blocks of the matrix
Definition: SM_PackedBlockGeneralMatrix.h:179
virtual T & getValue(const tIndex &i, const tIndex &j) final
get values at row i and column j for writing
Definition: SM_PackedBlockGeneralMatrix.h:165
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_PackedBlockGeneralMatrix.h:83
virtual void setValue(const tIndex &i, const tIndex &j, const T &v) final
set values at row i and column j
Definition: SM_PackedBlockGeneralMatrix.h:148
const T & operator()(tIndex i, tIndex j) const
get values at row i and column j for reading
Definition: SM_PackedBlockGeneralMatrix.h:193
std::valarray< T > & getBlocksValues()
get the values of superior blocks of the matrix
Definition: SM_PackedBlockGeneralMatrix.h:173
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_PackedBlockGeneralMatrix.h:95
virtual tString toString() const override
return string representaton of the operator
Definition: SM_PackedBlockGeneralMatrix.h:228
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
const tInteger & getColumnBlocksNumber() const
get the number of column blocks
Definition: SM_PackedBlockMatrix.h:124
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