OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
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
23template<typename T>
24class triplet<T,EIGEN_TRIPLET>
25{
26 Eigen::Triplet<T,long int> triplet_ei;
27
28public:
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 */
76template<typename T, typename id_t>
77class SparseMatrix<T,id_t,EIGEN_BASE>
78{
79public:
80
82 typedef boost::mpl::int_<EIGEN_BASE> triplet_impl;
83
86
87private:
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
127public:
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
217 Packer<decltype(trpl),HeapMemory>::packRequest(trpl,pap_prp);
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;
227 Packer<decltype(trpl),HeapMemory>::pack(mem,trpl,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
274 Unpacker<decltype(trpl),HeapMemory>::unpack(mem,trpl,ps);
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_ */
This class allocate, and destroy CPU memory.
virtual bool allocate(size_t sz)
allocate memory
virtual void * getPointer()
get a readable pointer with the data
virtual size_t size() const
the the size of the allocated memory
Packing status object.
Definition Pack_stat.hpp:61
Packing class.
Definition Packer.hpp:50
Eigen::SparseMatrix< T, 0, id_t > & getMat()
Get the Eigen Matrix object.
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.
const Eigen::SparseMatrix< T, 0, id_t > & getMat() const
Get the Eigen Matrix object.
SparseMatrix(size_t N1, size_t N2)
Create an empty Matrix.
T getValue(size_t r, size_t c)
Get the value from triplet.
openfpm::vector< triplet_type > & getMatrixTriplets()
Get the Matrix triplets bugger.
void resize(size_t row, size_t col, size_t l_row, size_t l_col)
Resize the Sparse Matrix.
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:450
Implementation of 1-D std::vector like structure.
size_t size()
Stub size.
triplet(long int i, long int j, T val)
Constructor from row, colum and value.
It store the non zero elements of the matrix.