OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
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 
97  void assemble()
98  {
99  Vcluster & vcl = create_vcluster();
100 
102  // we assemble the Matrix from the collected data
103  if (vcl.getProcessingUnits() != 1)
104  {
105  collect();
106  // only master assemble the Matrix
107  if (vcl.getProcessUnitID() == 0)
108  mat.setFromTriplets(trpl_recv.begin(),trpl_recv.end());
109  }
110  else
111  mat.setFromTriplets(trpl.begin(),trpl.end());
112  }
113 
117  void collect()
118  {
119  Vcluster & vcl = create_vcluster();
120 
121  trpl_recv.clear();
122 
123  // here we collect all the triplet in one array on the root node
124  vcl.SGather(trpl,trpl_recv,0);
125  }
126 
127 public:
128 
129 
130 
137  SparseMatrix(size_t N1, size_t N2)
138  :mat(N1,N2)
139  {
140  }
141 
142 
147 
156  {
157  return this->trpl;
158  }
159 
165  const Eigen::SparseMatrix<T,0,id_t> & getMat() const
166  {
167  // Here we collect the information on master
168  assemble();
169 
170  return mat;
171  }
172 
178  Eigen::SparseMatrix<T,0,id_t> & getMat()
179  {
180  assemble();
181 
182  return mat;
183  }
184 
191  void resize(size_t row, size_t col,size_t l_row, size_t l_col)
192  {
193  mat.resize(row,col);
194  }
195 
201  T operator()(id_t i, id_t j)
202  {
203  return mat.coeffRef(i,j);
204  }
205 
213  bool save(const std::string & file) const
214  {
215  size_t pap_prp;
216 
218 
219  // allocate the memory
220  HeapMemory pmem;
221  pmem.allocate(pap_prp);
222  ExtPreAlloc<HeapMemory> mem(pap_prp,pmem);
223 
224  //Packing
225 
226  Pack_stat sts;
228 
229  // Save into a binary file
230  std::ofstream dump (file, std::ios::out | std::ios::binary);
231  if (dump.is_open() == false)
232  return false;
233  dump.write ((const char *)pmem.getPointer(), pmem.size());
234 
235  return true;
236  }
237 
245  bool load(const std::string & file)
246  {
247  std::ifstream fs (file, std::ios::in | std::ios::binary | std::ios::ate );
248  if (fs.is_open() == false)
249  return false;
250 
251  // take the size of the file
252  size_t sz = fs.tellg();
253 
254  fs.close();
255 
256  // reopen the file without ios::ate to read
257  std::ifstream input (file, std::ios::in | std::ios::binary );
258  if (input.is_open() == false)
259  return false;
260 
261  // Create the HeapMemory and the ExtPreAlloc memory
262  HeapMemory pmem;
263  ExtPreAlloc<HeapMemory> mem(sz,pmem);
264 
265  // read
266  input.read((char *)pmem.getPointer(), sz);
267 
268  //close the file
269  input.close();
270 
271  //Unpacking
272  Unpack_stat ps;
273 
275 
276  return true;
277  }
278 
287  T getValue(size_t r, size_t c)
288  {
289  for (size_t i = 0 ; i < trpl.size() ; i++)
290  {
291  if (r == (size_t)trpl.get(i).row() && c == (size_t)trpl.get(i).col())
292  return trpl.get(i).value();
293  }
294 
295  return 0;
296  }
297 };
298 
299 
300 
301 #endif /* OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_ */
void assemble()
Assemble the matrix.
void collect()
Here we collect the full matrix on master.
boost::mpl::int_< EIGEN_BASE > triplet_impl
Triplet implementation id.
Unpacker class.
Definition: Packer_util.hpp:20
size_t getProcessUnitID()
Get the process unit id.
It store the non zero elements of the matrix.
Eigen::SparseMatrix< T, 0, id_t > & getMat()
Get the Eigen Matrix object.
triplet< T, EIGEN_BASE > triplet_type
Triplet type.
SparseMatrix(size_t N1, size_t N2)
Create an empty Matrix.
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
void resize(size_t row, size_t col, size_t l_row, size_t l_col)
Resize the Sparse Matrix.
Implementation of VCluster class.
Definition: VCluster.hpp:36
virtual void * getPointer()
get a readable pointer with the data
Definition: HeapMemory.cpp:237
SparseMatrix()
Create an empty Matrix.
const Eigen::SparseMatrix< T, 0, id_t > & getMat() const
Get the Eigen Matrix object.
Sparse Matrix implementation.
triplet(long int i, long int j, T val)
Constructor from row, colum and value.
Packing class.
Definition: Packer.hpp:44
Unpacking status object.
Definition: Pack_stat.hpp:15
bool SGather(T &send, S &recv, size_t root)
Semantic Gather, gather the data from all processors into one node.
Definition: VCluster.hpp:330
bool load(const std::string &file)
Load this object from file.
T getValue(size_t r, size_t c)
Get the value from triplet.
bool save(const std::string &file) const
Save this object into file.
openfpm::vector< triplet_type > & getMatrixTriplets()
Get the Matrix triplets bugger.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
Packing status object.
Definition: Pack_stat.hpp:51
size_t getProcessingUnits()
Get the total number of processors.
T operator()(id_t i, id_t j)
Get the row i and the colum j of the Matrix.