Vector_petsc.hpp 6.92 KB
Newer Older
Pietro Incardona's avatar
Pietro Incardona committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * 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"
12
#include "Vector/vector_def.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#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;

36
	//! Property id for the row
Pietro Incardona's avatar
Pietro Incardona committed
37
	static const unsigned int row = 0;
38 39

	//! Property id for the value
Pietro Incardona's avatar
Pietro Incardona committed
40
	static const unsigned int value = 1;
41 42

	//! This object has 2 properties
Pietro Incardona's avatar
Pietro Incardona committed
43 44
	static const unsigned int max_prop = 2;

45 46 47 48 49
	/*! \brief Get the row
	 *
	 * \return the row
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
50 51 52 53 54
	long int & rw()
	{
		return boost::fusion::at_c<row>(data);
	}

55 56 57 58 59
	/*! \brief Get the value
	 *
	 * \return the value
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
60 61 62 63 64 65 66 67 68 69
	T & val()
	{
		return boost::fusion::at_c<value>(data);
	}

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

70
	/*! \brief Constructor from row, column and value
Pietro Incardona's avatar
Pietro Incardona committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	 *
	 * \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;

86 87 88 89 90 91

/*! \brief PETSC vector for linear algebra
 *
 * This vector wrap the PETSC vector for solving linear systems
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
92
template<typename T>
Pietro Incardona's avatar
Pietro Incardona committed
93
class Vector<T,PETSC_BASE>
Pietro Incardona's avatar
Pietro Incardona committed
94
{
95
	//! Number of row the petsc vector has
Pietro Incardona's avatar
Pietro Incardona committed
96 97
	size_t n_row;

98
	//! Number of local rows
Pietro Incardona's avatar
Pietro Incardona committed
99 100
	size_t n_row_local;

101 102
	//! Indicate if v has been allocated
	mutable bool v_created = false;
Pietro Incardona's avatar
Pietro Incardona committed
103

104
	//! Mutable vector
Pietro Incardona's avatar
Pietro Incardona committed
105 106
	mutable Vec v;

107
	//! Mutable row value vector
incardon's avatar
incardon committed
108
	mutable openfpm::vector<rval<PetscScalar,PETSC_RVAL>,HeapMemory,typename memory_traits_inte<rval<PetscScalar,PETSC_RVAL>>::type > row_val;
Pietro Incardona's avatar
Pietro Incardona committed
109

110
	//! Global to local map
Pietro Incardona's avatar
Pietro Incardona committed
111 112
	mutable std::unordered_map<size_t,size_t> map;

113
	//! invalid
Pietro Incardona's avatar
Pietro Incardona committed
114 115 116 117 118 119 120 121
	T invalid;

	/*! \brief Set the Eigen internal vector
	 *
	 *
	 */
	void setPetsc() const
	{
incardon's avatar
incardon committed
122 123
		if (v_created == false)
		{PETSC_SAFE_CALL(VecSetType(v,VECMPI));}
Pietro Incardona's avatar
Pietro Incardona committed
124

Pietro Incardona's avatar
Pietro Incardona committed
125

Pietro Incardona's avatar
Pietro Incardona committed
126 127
		// set the vector

Pietro Incardona's avatar
Pietro Incardona committed
128 129
		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
130

Pietro Incardona's avatar
Pietro Incardona committed
131 132 133
		PETSC_SAFE_CALL(VecAssemblyBegin(v));
		PETSC_SAFE_CALL(VecAssemblyEnd(v));

134
		v_created = true;
Pietro Incardona's avatar
Pietro Incardona committed
135 136 137 138 139 140 141 142 143
	}

public:

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
144
	Vector(const Vector<T,PETSC_BASE> & v)
Pietro Incardona's avatar
Pietro Incardona committed
145 146 147 148 149 150 151 152 153
	{
		this->operator=(v);
	}

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
154
	Vector(Vector<T,PETSC_BASE> && v)
155
	:n_row(0),n_row_local(0),invalid(0)
Pietro Incardona's avatar
Pietro Incardona committed
156 157 158 159
	{
		this->operator=(v);
	}

160
	/*! \brief Destroy the vector
Pietro Incardona's avatar
Pietro Incardona committed
161 162 163 164 165
	 *
	 *
	 */
	~Vector()
	{
166 167
		if (is_openfpm_init() == true)
		{PETSC_SAFE_CALL(VecDestroy(&v));}
Pietro Incardona's avatar
Pietro Incardona committed
168 169
	}

Pietro Incardona's avatar
Pietro Incardona committed
170 171
	/*! \brief Create a vector with n elements
	 *
incardon's avatar
incardon committed
172 173
	 * \param n global number of elements in the vector
	 * \param n_row_local number
Pietro Incardona's avatar
Pietro Incardona committed
174 175
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
176
	Vector(size_t n, size_t n_row_local)
incardon's avatar
incardon committed
177
	:n_row_local(n_row_local),v(NULL),invalid(0)
Pietro Incardona's avatar
Pietro Incardona committed
178
	{
Pietro Incardona's avatar
Pietro Incardona committed
179 180 181
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));

Pietro Incardona's avatar
Pietro Incardona committed
182
		resize(n,n_row_local);
Pietro Incardona's avatar
Pietro Incardona committed
183 184 185 186 187 188
	}

	/*! \brief Create a vector with 0 elements
	 *
	 */
	Vector()
189
	:n_row(0),n_row_local(0),invalid(0)
Pietro Incardona's avatar
Pietro Incardona committed
190
	{
Pietro Incardona's avatar
Pietro Incardona committed
191 192
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
Pietro Incardona's avatar
Pietro Incardona committed
193 194 195 196 197
	}

	/*! \brief Resize the Vector
	 *
	 * \param row numbers of row
Pietro Incardona's avatar
Pietro Incardona committed
198
	 * \param l_row number of local row
Pietro Incardona's avatar
Pietro Incardona committed
199 200
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
201
	void resize(size_t row, size_t l_row)
Pietro Incardona's avatar
Pietro Incardona committed
202 203
	{
		n_row = row;
Pietro Incardona's avatar
Pietro Incardona committed
204
		n_row_local = l_row;
Pietro Incardona's avatar
Pietro Incardona committed
205 206

		PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
Pietro Incardona's avatar
Pietro Incardona committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	}

	/*! \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
	 *
	 */
incardon's avatar
incardon committed
233
	inline PetscScalar & insert(size_t i)
Pietro Incardona's avatar
Pietro Incardona committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	{
		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
	 *
	 */
incardon's avatar
incardon committed
251
	inline const PetscScalar & insert(size_t i) const
Pietro Incardona's avatar
Pietro Incardona committed
252 253 254 255 256 257
	{
		row_val.add();

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

Pietro Incardona's avatar
Pietro Incardona committed
258 259
		row_val.last().template get<row_id>() = i;
		return row_val.last().template get<val_id>();
Pietro Incardona's avatar
Pietro Incardona committed
260 261 262 263 264 265 266 267 268 269 270
	}

	/*! \brief Return a reference to the vector element
	 *
	 * \warning The element must exist
	 *
	 * \param i element
	 *
	 * \return reference to the element vector
	 *
	 */
incardon's avatar
incardon committed
271
	const PetscScalar & operator()(size_t i) const
Pietro Incardona's avatar
Pietro Incardona committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	{
		// 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
	 *
	 */
incardon's avatar
incardon committed
292
	PetscScalar & operator()(size_t i)
Pietro Incardona's avatar
Pietro Incardona committed
293 294 295 296 297 298 299 300 301 302 303
	{
		// 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
304
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
305
	 *
Pietro Incardona's avatar
Pietro Incardona committed
306
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
307 308 309 310 311 312 313 314 315
	 *
	 */
	const Vec & getVec() const
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
316
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
317
	 *
Pietro Incardona's avatar
Pietro Incardona committed
318
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
319 320 321 322 323 324 325 326 327
	 *
	 */
	Vec & getVec()
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
328
	/*! \brief Update the Vector with the PETSC object
Pietro Incardona's avatar
Pietro Incardona committed
329 330
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
331
	void update()
Pietro Incardona's avatar
Pietro Incardona committed
332
	{
Pietro Incardona's avatar
Pietro Incardona committed
333 334
		PetscInt n_row;
		PetscInt n_row_local;
Pietro Incardona's avatar
Pietro Incardona committed
335

Pietro Incardona's avatar
Pietro Incardona committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		// 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;
355
		for (size_t i = low ; i < (size_t)high ; i++)
Pietro Incardona's avatar
Pietro Incardona committed
356 357 358 359 360 361 362
		{
			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
363 364 365 366 367 368 369
	}

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
370
	Vector<T,PETSC_BASE> & operator=(const Vector<T,PETSC_BASE> & v)
Pietro Incardona's avatar
Pietro Incardona committed
371 372 373 374 375 376 377
	{
		map = v.map;
		row_val = v.row_val;

		return *this;
	}

Pietro Incardona's avatar
Pietro Incardona committed
378
	/*! \brief Copy the vector
Pietro Incardona's avatar
Pietro Incardona committed
379
	 *
Pietro Incardona's avatar
Pietro Incardona committed
380
	 * \param v vector to copy
Pietro Incardona's avatar
Pietro Incardona committed
381 382
	 *
	 */
383
	Vector<T,PETSC_BASE> & operator=(Vector<T,PETSC_BASE> && v)
Pietro Incardona's avatar
Pietro Incardona committed
384
	{
Pietro Incardona's avatar
Pietro Incardona committed
385 386
		map.swap(v.map);
		row_val.swap(v.row_val);
Pietro Incardona's avatar
Pietro Incardona committed
387 388 389

		return *this;
	}
incardon's avatar
incardon committed
390 391 392 393 394 395 396 397 398 399 400 401

	/*! \brief Set to zero all the entries
	 *
	 *
	 */
	void setZero()
	{
		if (v_created == false)
		{PETSC_SAFE_CALL(VecSetType(v,VECMPI));}

		v_created = true;
	}
Pietro Incardona's avatar
Pietro Incardona committed
402 403 404 405 406
};


#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */