SparseMatrix_Eigen.hpp 5.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * SparseMatrix_Eigen.hpp
 *
 *  Created on: Nov 27, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_
#define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_

#include "Vector/map_vector.hpp"
#include <boost/mpl/int.hpp>
incardon's avatar
incardon committed
13
#include "VCluster/VCluster.hpp"
14 15 16

#define EIGEN_TRIPLET 1

Pietro Incardona's avatar
Pietro Incardona committed
17 18 19 20 21 22
/*! \brief It store one non-zero element in the sparse matrix
 *
 * Given a row, and a column, store a value
 *
 *
 */
23
template<typename T>
24
class triplet<T,EIGEN_TRIPLET>
25
{
26 27 28
	Eigen::Triplet<T,long int> triplet_ei;

public:
29 30 31

	long int & row()
	{
32
		size_t ptr = (size_t)&triplet_ei.row();
33 34 35 36 37 38

		return (long int &)*(long int *)ptr;
	}

	long int & col()
	{
39
		size_t ptr = (size_t)&triplet_ei.col();
40 41 42 43 44 45

		return (long int &)*(long int *)ptr;
	}

	T & value()
	{
46
		size_t ptr = (size_t)&triplet_ei.value();
47 48 49

		return (T &)*(T *)ptr;
	}
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

	/*! \brief Constructor from row, colum and value
	 *
	 * \param i row
	 * \param j colum
	 * \param val value
	 *
	 */
	triplet(long int i, long int j, T val)
	{
		row() = i;
		col() = j;
		value() = val;
	}

	// Default constructor
	triplet()	{};
67 68
};

Pietro Incardona's avatar
Pietro Incardona committed
69 70 71 72 73 74 75
/* ! \brief Sparse Matrix implementation, that map over Eigen
 *
 * \tparam T Type of the sparse Matrix store on each row,colums
 * \tparam id_t type of id
 * \tparam impl implementation
 *
 */
76
template<typename T, typename id_t>
Pietro Incardona's avatar
Pietro Incardona committed
77
class SparseMatrix<T,id_t,EIGEN_BASE>
78 79 80
{
public:

81
	//! Triplet implementation id
Pietro Incardona's avatar
Pietro Incardona committed
82
	typedef boost::mpl::int_<EIGEN_BASE> triplet_impl;
83

84
	//! Triplet type
Pietro Incardona's avatar
Pietro Incardona committed
85
	typedef triplet<T,EIGEN_BASE> triplet_type;
86

87 88 89 90
private:

	Eigen::SparseMatrix<T,0,id_t> mat;
	openfpm::vector<triplet_type> trpl;
91
	openfpm::vector<triplet_type> trpl_recv;
92 93 94 95 96

	/*! \brief Assemble the matrix
	 *
	 *
	 */
97
	void assemble()
98
	{
99
		Vcluster & vcl = create_vcluster();
100 101 102 103 104

		////// On Master and only here
		// we assemble the Matrix from the collected data
		if (vcl.getProcessingUnits() != 1)
		{
105 106 107 108
			collect();
			// only master assemble the Matrix
			if (vcl.getProcessUnitID() == 0)
				mat.setFromTriplets(trpl_recv.begin(),trpl_recv.end());
109 110
		}
		else
111
			mat.setFromTriplets(trpl.begin(),trpl.end());
112 113 114 115 116 117 118
	}

	/*! \brief Here we collect the full matrix on master
	 *
	 */
	void collect()
	{
119
		Vcluster & vcl = create_vcluster();
120

121
		trpl_recv.clear();
122

123 124
		// here we collect all the triplet in one array on the root node
		vcl.SGather(trpl,trpl_recv,0);
125 126 127 128 129 130
	}

public:



131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	/*! \brief Create an empty Matrix
	 *
	 * \param N1 number of row
	 * \param N2 number of colums
	 *
	 */
	SparseMatrix(size_t N1, size_t N2)
	:mat(N1,N2)
	{
	}


	/*! \brief Create an empty Matrix
	 *
	 */
	SparseMatrix()	{}

148 149 150
	/*! \brief Get the Matrix triplets bugger
	 *
	 * It return a buffer that can be filled with triplets
151
	 *
152
	 * \return triplet buffer
153 154
	 *
	 */
155
	openfpm::vector<triplet_type> & getMatrixTriplets()
156
	{
157
		return this->trpl;
158 159 160 161 162 163 164 165 166
	}

	/*! \brief Get the Eigen Matrix object
	 *
	 * \return the Eigen Matrix
	 *
	 */
	const Eigen::SparseMatrix<T,0,id_t> & getMat() const
	{
167 168 169
		// Here we collect the information on master
		assemble();

170 171 172 173 174 175 176 177 178 179
		return mat;
	}

	/*! \brief Get the Eigen Matrix object
	 *
	 * \return the Eigen Matrix
	 *
	 */
	Eigen::SparseMatrix<T,0,id_t> & getMat()
	{
180 181
		assemble();

182 183 184 185 186 187 188 189 190
		return mat;
	}

	/*! \brief Resize the Sparse Matrix
	 *
	 * \param row number for row
	 * \param col number of colums
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
191
	void resize(size_t row, size_t col,size_t l_row, size_t l_col)
192 193 194
	{
		mat.resize(row,col);
	}
195 196 197 198 199 200 201 202 203 204

	/*! \brief Get the row i and the colum j of the Matrix
	 *
	 * \return the value of the matrix at row i colum j
	 *
	 */
	T operator()(id_t i, id_t j)
	{
		return mat.coeffRef(i,j);
	}
Pietro Incardona's avatar
Pietro Incardona committed
205 206 207 208 209 210 211 212 213 214

	/*! \brief Save this object into file
	 *
	 * \param file filename
	 *
	 * \return true if succed
	 *
	 */
	bool save(const std::string & file) const
	{
215
		size_t pap_prp;
Pietro Incardona's avatar
Pietro Incardona committed
216 217 218 219 220

		Packer<decltype(trpl),HeapMemory>::packRequest(trpl,pap_prp);

		// allocate the memory
		HeapMemory pmem;
221
		pmem.allocate(pap_prp);
Pietro Incardona's avatar
Pietro Incardona committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
		ExtPreAlloc<HeapMemory> mem(pap_prp,pmem);

		//Packing

		Pack_stat sts;
		Packer<decltype(trpl),HeapMemory>::pack(mem,trpl,sts);

		// Save into a binary file
	    std::ofstream dump (file, std::ios::out | std::ios::binary);
	    if (dump.is_open() == false)
	    	return false;
	    dump.write ((const char *)pmem.getPointer(), pmem.size());

	    return true;
	}

	/*! \brief Load this object from file
	 *
	 * \param file filename
	 *
	 * \return true if succed
	 *
	 */
	bool load(const std::string & file)
	{
	    std::ifstream fs (file, std::ios::in | std::ios::binary | std::ios::ate );
	    if (fs.is_open() == false)
	    	return false;

	    // take the size of the file
	    size_t sz = fs.tellg();

	    fs.close();

	    // reopen the file without ios::ate to read
	    std::ifstream input (file, std::ios::in | std::ios::binary );
	    if (input.is_open() == false)
	    	return false;

	    // Create the HeapMemory and the ExtPreAlloc memory
	    HeapMemory pmem;
263
		ExtPreAlloc<HeapMemory> mem(sz,pmem);
Pietro Incardona's avatar
Pietro Incardona committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

		// read
	    input.read((char *)pmem.getPointer(), sz);

	    //close the file
	    input.close();

		//Unpacking
		Unpack_stat ps;

	 	Unpacker<decltype(trpl),HeapMemory>::unpack(mem,trpl,ps);

	 	return true;
	}

	/*! \brief Get the value from triplet
	 *
	 * \warning It is extremly slow because it do a full search across the triplets elements
	 *
	 * \param r row
	 * \param c colum
	 *
	 */
	T getValue(size_t r, size_t c)
	{
		for (size_t i = 0 ; i < trpl.size() ; i++)
		{
			if (r == (size_t)trpl.get(i).row() && c == (size_t)trpl.get(i).col())
				return trpl.get(i).value();
		}

		return 0;
	}
297 298 299 300 301
};



#endif /* OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_EIGEN_HPP_ */