OpenFPM  5.2.0
Project that contain the implementation of distributed structures
SparseMatrix_Eigen.hpp
1 /*
2  * SparseMatrix_Eigen.hpp
3  *
4  * Created on: Nov 27, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_
9 #define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_
10 
11 #include "Vector/map_vector.hpp"
12 #include <boost/mpl/int.hpp>
13 #include "VCluster/VCluster.hpp"
14 
15 #define EIGEN_TRIPLET 1
16 
23 template<typename T>
24 class triplet<T,EIGEN_TRIPLET>
25 {
26  Eigen::Triplet<T,long int> triplet_ei;
27 
28 public:
29 
30  long int & row()
31  {
32  size_t ptr = (size_t)&triplet_ei.row();
33 
34  return (long int &)*(long int *)ptr;
35  }
36 
37  long int & col()
38  {
39  size_t ptr = (size_t)&triplet_ei.col();
40 
41  return (long int &)*(long int *)ptr;
42  }
43 
44  T & value()
45  {
46  size_t ptr = (size_t)&triplet_ei.value();
47 
48  return (T &)*(T *)ptr;
49  }
50 
58  triplet(long int i, long int j, T val)
59  {
60  row() = i;
61  col() = j;
62  value() = val;
63  }
64 
65  // Default constructor
66  triplet() {};
67 };
68 
69 /* ! \brief Sparse Matrix implementation, that map over Eigen
70  *
71  * \tparam T Type of the sparse Matrix store on each row,colums
72  * \tparam id_t type of id
73  * \tparam impl implementation
74  *
75  */
76 template<typename T, typename id_t>
77 class SparseMatrix<T,id_t,EIGEN_BASE>
78 {
79 public:
80 
82  typedef boost::mpl::int_<EIGEN_BASE> triplet_impl;
83 
86 
87 private:
88 
89  Eigen::SparseMatrix<T,0,id_t> mat;
92 
94  bool m_created = false;
95 
100  void assemble()
101  {
102  Vcluster<> & vcl = create_vcluster();
103 
105  // we assemble the Matrix from the collected data
106  if (vcl.getProcessingUnits() != 1)
107  {
108  collect();
109  // only master assemble the Matrix
110  if (vcl.getProcessUnitID() == 0)
111  mat.setFromTriplets(trpl_recv.begin(),trpl_recv.end());
112  }
113  else
114  mat.setFromTriplets(trpl.begin(),trpl.end());
115 
116  m_created = true;
117  }
118 
122  void collect()
123  {
124  Vcluster<> & vcl = create_vcluster();
125 
126  trpl_recv.clear();
127 
128  // here we collect all the triplet in one array on the root node
129  vcl.SGather(trpl,trpl_recv,0);
130  m_created = false;
131  }
132 
133 public:
134 
135 
136 
143  SparseMatrix(size_t N1, size_t N2)
144  :mat(N1,N2)
145  {
146  }
147 
148 
153 
162  {
163  m_created = false; return this->trpl;
164  }
165 
171  const Eigen::SparseMatrix<T,0,id_t> & getMat() const
172  {
173  // Here we collect the information on master
174  if (m_created == false) assemble();
175 
176  return mat;
177  }
178 
184  Eigen::SparseMatrix<T,0,id_t> & getMat()
185  {
186  if (m_created == false) assemble();
187 
188  return mat;
189  }
190 
197  void resize(size_t row, size_t col,size_t l_row, size_t l_col)
198  {
199  m_created = false; mat.resize(row,col);
200  }
201 
207  T operator()(id_t i, id_t j)
208  {
209  return mat.coeffRef(i,j);
210  }
211 
219  bool save(const std::string & file) const
220  {
221  size_t pap_prp;
222 
223  Packer<decltype(trpl),HeapMemory>::packRequest(trpl,pap_prp);
224 
225  // allocate the memory
226  HeapMemory pmem;
227  pmem.allocate(pap_prp);
228  ExtPreAlloc<HeapMemory> mem(pap_prp,pmem);
229 
230  //Packing
231 
232  Pack_stat sts;
233  Packer<decltype(trpl),HeapMemory>::pack(mem,trpl,sts);
234 
235  // Save into a binary file
236  std::ofstream dump (file, std::ios::out | std::ios::binary);
237  if (dump.is_open() == false)
238  return false;
239  dump.write ((const char *)pmem.getPointer(), pmem.size());
240 
241  return true;
242  }
243 
251  bool load(const std::string & file)
252  {
253  m_created = false;
254  std::ifstream fs (file, std::ios::in | std::ios::binary | std::ios::ate );
255  if (fs.is_open() == false)
256  return false;
257 
258  // take the size of the file
259  size_t sz = fs.tellg();
260 
261  fs.close();
262 
263  // reopen the file without ios::ate to read
264  std::ifstream input (file, std::ios::in | std::ios::binary );
265  if (input.is_open() == false)
266  return false;
267 
268  // Create the HeapMemory and the ExtPreAlloc memory
269  HeapMemory pmem;
270  ExtPreAlloc<HeapMemory> mem(sz,pmem);
271 
272  // read
273  input.read((char *)pmem.getPointer(), sz);
274 
275  //close the file
276  input.close();
277 
278  //Unpacking
279  Unpack_stat ps;
280 
281  Unpacker<decltype(trpl),HeapMemory>::unpack(mem,trpl,ps);
282 
283  return true;
284  }
285 
294  T getValue(size_t r, size_t c)
295  {
296  for (size_t i = 0 ; i < trpl.size() ; i++)
297  {
298  if (r == (size_t)trpl.get(i).row() && c == (size_t)trpl.get(i).col())
299  return trpl.get(i).value();
300  }
301 
302  return 0;
303  }
304 
312  {
313  return m_created;
314  }
315 };
316 
317 
318 
319 #endif /* OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_ */
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:40
virtual bool allocate(size_t sz)
allocate memory
Definition: HeapMemory.cpp:33
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:228
virtual size_t size() const
the the size of the allocated memory
Definition: HeapMemory.cpp:153
Packing status object.
Definition: Pack_stat.hpp:61
Packing class.
Definition: Packer.hpp:50
T operator()(id_t i, id_t j)
Get the row i and the colum j of the Matrix.
bool save(const std::string &file) const
Save this object into file.
bool load(const std::string &file)
Load this object from file.
triplet< T, EIGEN_BASE > triplet_type
Triplet type.
void collect()
Here we collect the full matrix on master.
openfpm::vector< triplet_type > & getMatrixTriplets()
Get the Matrix triplets bugger.
SparseMatrix(size_t N1, size_t N2)
Create an empty Matrix.
bool isMatrixFilled()
Return the state of matrix.
T getValue(size_t r, size_t c)
Get the value from triplet.
void resize(size_t row, size_t col, size_t l_row, size_t l_col)
Resize the Sparse Matrix.
const Eigen::SparseMatrix< T, 0, id_t > & getMat() const
Get the Eigen Matrix object.
Eigen::SparseMatrix< T, 0, id_t > & getMat()
Get the Eigen Matrix object.
boost::mpl::int_< EIGEN_BASE > triplet_impl
Triplet implementation id.
Sparse Matrix implementation.
Unpacking status object.
Definition: Pack_stat.hpp:16
Unpacker class.
Definition: Unpacker.hpp:34
size_t getProcessUnitID()
Get the process unit id.
size_t getProcessingUnits()
Get the total number of processors.
Implementation of VCluster class.
Definition: VCluster.hpp:59
bool SGather(T &send, S &recv, size_t root)
Semantic Gather, gather the data from all processors into one node.
Definition: VCluster.hpp:455
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:204
size_t size()
Stub size.
Definition: map_vector.hpp:212
triplet(long int i, long int j, T val)
Constructor from row, colum and value.
It store the non zero elements of the matrix.