Vector_petsc.hpp 5.94 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*
 * Vector_petsc.hpp
 *
 *  Created on: Apr 29, 2016
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_PETSC_HPP_
#define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_PETSC_HPP_

#include "Vector/map_vector.hpp"
#include <boost/mpl/int.hpp>
#include <petscvec.h>
#include "util/petsc_util.hpp"

#define PETSC_RVAL 2

/*! \brief It store one row value of a vector
 *
 * Given a row, store a value
 *
 *
 */
template<typename T>
class rval<T,PETSC_RVAL>
{
public:

	//! boost fusion that store the point
	typedef boost::fusion::vector<PetscInt,T> type;

	//! structure that store the data of the point
	type data;

	//! Property id of the point
	static const unsigned int row = 0;
	static const unsigned int value = 1;
	static const unsigned int max_prop = 2;

	// Get the row
	long int & rw()
	{
		return boost::fusion::at_c<row>(data);
	}

	// Get the value
	T & val()
	{
		return boost::fusion::at_c<value>(data);
	}

	/*! \brief Default constructor
	 *
	 */
	rval()	{}

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

constexpr unsigned int row_id = 0;
constexpr unsigned int val_id = 1;

template<typename T>
Pietro Incardona's avatar
Pietro Incardona committed
74
class Vector<T,PETSC_BASE>
Pietro Incardona's avatar
Pietro Incardona committed
75 76 77 78
{
	// n_row
	size_t n_row;

Pietro Incardona's avatar
Pietro Incardona committed
79
	// Number of local rows
Pietro Incardona's avatar
Pietro Incardona committed
80 81 82 83 84 85
	size_t n_row_local;

	// Mutable vector
	mutable Vec v;

	// Mutable row value vector
Pietro Incardona's avatar
Pietro Incardona committed
86
	mutable openfpm::vector<rval<T,PETSC_RVAL>,HeapMemory,typename memory_traits_inte<rval<T,PETSC_RVAL>>::type > row_val;
Pietro Incardona's avatar
Pietro Incardona committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	// Global to local map
	mutable std::unordered_map<size_t,size_t> map;

	// invalid
	T invalid;

	/*! \brief Set the Eigen internal vector
	 *
	 *
	 */
	void setPetsc() const
	{
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
		PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
Pietro Incardona's avatar
Pietro Incardona committed
103
		PETSC_SAFE_CALL(VecSetFromOptions(v));
Pietro Incardona's avatar
Pietro Incardona committed
104 105 106

		// set the vector

Pietro Incardona's avatar
Pietro Incardona committed
107 108
		if (row_val.size() != 0)
			PETSC_SAFE_CALL(VecSetValues(v,row_val.size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0),INSERT_VALUES))
Pietro Incardona's avatar
Pietro Incardona committed
109

Pietro Incardona's avatar
Pietro Incardona committed
110 111
		VecAssemblyBegin(v);
		VecAssemblyEnd(v);
Pietro Incardona's avatar
Pietro Incardona committed
112 113 114 115 116 117 118 119 120
	}

public:

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
121
	Vector(const Vector<T,PETSC_BASE> & v)
Pietro Incardona's avatar
Pietro Incardona committed
122 123 124 125 126 127 128 129 130
	{
		this->operator=(v);
	}

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
131
	Vector(const Vector<T,PETSC_BASE> && v)
Pietro Incardona's avatar
Pietro Incardona committed
132 133 134 135 136 137 138
	{
		this->operator=(v);
	}

	/*! \brief Create a vector with n elements
	 *
	 * \param n number of elements in the vector
Pietro Incardona's avatar
Pietro Incardona committed
139
	 * \param n_row_loc number
Pietro Incardona's avatar
Pietro Incardona committed
140 141
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
142 143
	Vector(size_t n, size_t n_row_local)
	:n_row_local(n_row_local)
Pietro Incardona's avatar
Pietro Incardona committed
144
	{
Pietro Incardona's avatar
Pietro Incardona committed
145
		resize(n,n_row_local);
Pietro Incardona's avatar
Pietro Incardona committed
146 147 148 149 150 151
	}

	/*! \brief Create a vector with 0 elements
	 *
	 */
	Vector()
Pietro Incardona's avatar
Pietro Incardona committed
152
	:n_row(0),n_row_local(0)
Pietro Incardona's avatar
Pietro Incardona committed
153 154 155 156 157 158
	{
	}

	/*! \brief Resize the Vector
	 *
	 * \param row numbers of row
Pietro Incardona's avatar
Pietro Incardona committed
159
	 * \param l_row number of local row
Pietro Incardona's avatar
Pietro Incardona committed
160 161
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
162
	void resize(size_t row, size_t l_row)
Pietro Incardona's avatar
Pietro Incardona committed
163 164
	{
		n_row = row;
Pietro Incardona's avatar
Pietro Incardona committed
165
		n_row_local = l_row;
Pietro Incardona's avatar
Pietro Incardona committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \param i element
	 * \param val value
	 *
	 */
	void insert(size_t i, T val)
	{
		row_val.add();

		// Map
		map[i] = row_val.size()-1;

		row_val.last().template get<row_id>() = i;
		row_val.last().template get<val_id>() = val;
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \param i element
	 *
	 * \return reference to the element vector
	 *
	 */
	inline T & insert(size_t i)
	{
		row_val.add();

		// Map
		map[i] = row_val.size()-1;

		row_val.last().template get<row_id>() = i;
		return row_val.last().template get<val_id>();
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \param i element
	 *
	 * \return reference to the element vector
	 *
	 */
	inline const T & insert(size_t i) const
	{
		row_val.add();

		// Map
		map[i] = row_val.size()-1;

Pietro Incardona's avatar
Pietro Incardona committed
217 218
		row_val.last().template get<row_id>() = i;
		return row_val.last().template get<val_id>();
Pietro Incardona's avatar
Pietro Incardona committed
219 220 221 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
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \warning The element must exist
	 *
	 * \param i element
	 *
	 * \return reference to the element vector
	 *
	 */
	const T & operator()(size_t i) const
	{
		// Search if exist

		std::unordered_map<size_t,size_t>::iterator it = map.find(i);

		if ( it != map.end() )
			return row_val.template get<val_id>(it->second);

		return insert(i);
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \warning The element must exist
	 *
	 * \param i element
	 *
	 * \return reference to the element vector
	 *
	 */
	T & operator()(size_t i)
	{
		// Search if exist

		std::unordered_map<size_t,size_t>::iterator it = map.find(i);

		if ( it != map.end() )
			return row_val.template get<val_id>(it->second);

		return insert(i);
	}

Pietro Incardona's avatar
Pietro Incardona committed
263
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
264
	 *
Pietro Incardona's avatar
Pietro Incardona committed
265
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
266 267 268 269 270 271 272 273 274
	 *
	 */
	const Vec & getVec() const
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
275
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
276
	 *
Pietro Incardona's avatar
Pietro Incardona committed
277
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
278 279 280 281 282 283 284 285 286
	 *
	 */
	Vec & getVec()
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
287
	/*! \brief Update the Vector with the PETSC object
Pietro Incardona's avatar
Pietro Incardona committed
288 289
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
290
	void update()
Pietro Incardona's avatar
Pietro Incardona committed
291
	{
Pietro Incardona's avatar
Pietro Incardona committed
292 293
		PetscInt n_row;
		PetscInt n_row_local;
Pietro Incardona's avatar
Pietro Incardona committed
294

Pietro Incardona's avatar
Pietro Incardona committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		// Get the size of the vector from PETSC
		VecGetSize(v,&n_row);
		VecGetLocalSize(v,&n_row_local);

		this->n_row = n_row;
		this->n_row_local = n_row_local;

		row_val.resize(n_row_local);

		//

		PetscInt low;
		PetscInt high;

		VecGetOwnershipRange(v,&low,&high);

		// Fill the index and construct the map

		size_t k = 0;
		for (size_t i = low ; i < high ; i++)
		{
			row_val.template get<row_id>(k) = i;
			map[i] = k;
			k++;
		}

		PETSC_SAFE_CALL(VecGetValues(v,row_val.size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0)))
Pietro Incardona's avatar
Pietro Incardona committed
322 323 324 325 326 327 328
	}

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
329
	Vector<T,PETSC_BASE> & operator=(const Vector<T,PETSC_BASE> & v)
Pietro Incardona's avatar
Pietro Incardona committed
330 331 332 333 334 335 336
	{
		map = v.map;
		row_val = v.row_val;

		return *this;
	}

Pietro Incardona's avatar
Pietro Incardona committed
337
	/*! \brief Copy the vector
Pietro Incardona's avatar
Pietro Incardona committed
338
	 *
Pietro Incardona's avatar
Pietro Incardona committed
339
	 * \param v vector to copy
Pietro Incardona's avatar
Pietro Incardona committed
340 341
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
342
	Vector<T,PETSC_BASE> & operator=(const Vector<T,PETSC_BASE> && v)
Pietro Incardona's avatar
Pietro Incardona committed
343
	{
Pietro Incardona's avatar
Pietro Incardona committed
344 345
		map.swap(v.map);
		row_val.swap(v.row_val);
Pietro Incardona's avatar
Pietro Incardona committed
346 347 348 349 350 351 352 353

		return *this;
	}
};


#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */