Vector_petsc.hpp 7.05 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
	 *
	 * \param i row
	 * \param val value
	 *
	 */
	rval(long int i, T val)
	{
		rw() = i;
		val() = val;
	}
incardon's avatar
incardon committed
81 82 83 84 85 86 87 88 89 90

	/*! \brief Indicate that the structure has no pointer
	 *
	 * \return true
	 *
	 */
	static inline bool noPointers()
	{
		return true;
	}
Pietro Incardona's avatar
Pietro Incardona committed
91 92 93 94 95
};

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

96 97 98 99 100 101

/*! \brief PETSC vector for linear algebra
 *
 * This vector wrap the PETSC vector for solving linear systems
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
102
template<typename T>
Pietro Incardona's avatar
Pietro Incardona committed
103
class Vector<T,PETSC_BASE>
Pietro Incardona's avatar
Pietro Incardona committed
104
{
105
	//! Number of row the petsc vector has
Pietro Incardona's avatar
Pietro Incardona committed
106 107
	size_t n_row;

108
	//! Number of local rows
Pietro Incardona's avatar
Pietro Incardona committed
109 110
	size_t n_row_local;

111 112
	//! Indicate if v has been allocated
	mutable bool v_created = false;
Pietro Incardona's avatar
Pietro Incardona committed
113

114
	//! Mutable vector
Pietro Incardona's avatar
Pietro Incardona committed
115 116
	mutable Vec v;

117
	//! Mutable row value vector
incardon's avatar
incardon committed
118
	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
119

120
	//! Global to local map
Pietro Incardona's avatar
Pietro Incardona committed
121 122
	mutable std::unordered_map<size_t,size_t> map;

123
	//! invalid
Pietro Incardona's avatar
Pietro Incardona committed
124 125 126 127 128 129 130 131
	T invalid;

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

Pietro Incardona's avatar
Pietro Incardona committed
135

Pietro Incardona's avatar
Pietro Incardona committed
136 137
		// set the vector

Pietro Incardona's avatar
Pietro Incardona committed
138 139
		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
140

Pietro Incardona's avatar
Pietro Incardona committed
141 142 143
		PETSC_SAFE_CALL(VecAssemblyBegin(v));
		PETSC_SAFE_CALL(VecAssemblyEnd(v));

144
		v_created = true;
Pietro Incardona's avatar
Pietro Incardona committed
145 146 147 148 149 150 151 152 153
	}

public:

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

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
164
	Vector(Vector<T,PETSC_BASE> && v)
165
	:n_row(0),n_row_local(0),invalid(0)
Pietro Incardona's avatar
Pietro Incardona committed
166 167 168 169
	{
		this->operator=(v);
	}

170
	/*! \brief Destroy the vector
Pietro Incardona's avatar
Pietro Incardona committed
171 172 173 174 175
	 *
	 *
	 */
	~Vector()
	{
176 177
		if (is_openfpm_init() == true)
		{PETSC_SAFE_CALL(VecDestroy(&v));}
Pietro Incardona's avatar
Pietro Incardona committed
178 179
	}

Pietro Incardona's avatar
Pietro Incardona committed
180 181
	/*! \brief Create a vector with n elements
	 *
incardon's avatar
incardon committed
182 183
	 * \param n global number of elements in the vector
	 * \param n_row_local number
Pietro Incardona's avatar
Pietro Incardona committed
184 185
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
186
	Vector(size_t n, size_t n_row_local)
incardon's avatar
incardon committed
187
	:n_row_local(n_row_local),v(NULL),invalid(0)
Pietro Incardona's avatar
Pietro Incardona committed
188
	{
Pietro Incardona's avatar
Pietro Incardona committed
189 190 191
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));

Pietro Incardona's avatar
Pietro Incardona committed
192
		resize(n,n_row_local);
Pietro Incardona's avatar
Pietro Incardona committed
193 194 195 196 197 198
	}

	/*! \brief Create a vector with 0 elements
	 *
	 */
	Vector()
199
	:n_row(0),n_row_local(0),invalid(0)
Pietro Incardona's avatar
Pietro Incardona committed
200
	{
Pietro Incardona's avatar
Pietro Incardona committed
201 202
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
Pietro Incardona's avatar
Pietro Incardona committed
203 204 205 206 207
	}

	/*! \brief Resize the Vector
	 *
	 * \param row numbers of row
Pietro Incardona's avatar
Pietro Incardona committed
208
	 * \param l_row number of local row
Pietro Incardona's avatar
Pietro Incardona committed
209 210
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
211
	void resize(size_t row, size_t l_row)
Pietro Incardona's avatar
Pietro Incardona committed
212 213
	{
		n_row = row;
Pietro Incardona's avatar
Pietro Incardona committed
214
		n_row_local = l_row;
Pietro Incardona's avatar
Pietro Incardona committed
215 216

		PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
Pietro Incardona's avatar
Pietro Incardona committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	}

	/*! \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
243
	inline PetscScalar & insert(size_t i)
Pietro Incardona's avatar
Pietro Incardona committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	{
		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
261
	inline const PetscScalar & insert(size_t i) const
Pietro Incardona's avatar
Pietro Incardona committed
262 263 264 265 266 267
	{
		row_val.add();

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

Pietro Incardona's avatar
Pietro Incardona committed
268 269
		row_val.last().template get<row_id>() = i;
		return row_val.last().template get<val_id>();
Pietro Incardona's avatar
Pietro Incardona committed
270 271 272 273 274 275 276 277 278 279 280
	}

	/*! \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
281
	const PetscScalar & operator()(size_t i) const
Pietro Incardona's avatar
Pietro Incardona committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	{
		// 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
302
	PetscScalar & operator()(size_t i)
Pietro Incardona's avatar
Pietro Incardona committed
303 304 305 306 307 308 309 310 311 312 313
	{
		// 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
314
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
315
	 *
Pietro Incardona's avatar
Pietro Incardona committed
316
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
317 318 319 320 321 322 323 324 325
	 *
	 */
	const Vec & getVec() const
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
326
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
327
	 *
Pietro Incardona's avatar
Pietro Incardona committed
328
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
329 330 331 332 333 334 335 336 337
	 *
	 */
	Vec & getVec()
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
338
	/*! \brief Update the Vector with the PETSC object
Pietro Incardona's avatar
Pietro Incardona committed
339 340
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
341
	void update()
Pietro Incardona's avatar
Pietro Incardona committed
342
	{
Pietro Incardona's avatar
Pietro Incardona committed
343 344
		PetscInt n_row;
		PetscInt n_row_local;
Pietro Incardona's avatar
Pietro Incardona committed
345

Pietro Incardona's avatar
Pietro Incardona committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
		// 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;
365
		for (size_t i = low ; i < (size_t)high ; i++)
Pietro Incardona's avatar
Pietro Incardona committed
366 367 368 369 370 371 372
		{
			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
373 374 375 376 377 378 379
	}

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

		return *this;
	}

Pietro Incardona's avatar
Pietro Incardona committed
388
	/*! \brief Copy the vector
Pietro Incardona's avatar
Pietro Incardona committed
389
	 *
Pietro Incardona's avatar
Pietro Incardona committed
390
	 * \param v vector to copy
Pietro Incardona's avatar
Pietro Incardona committed
391 392
	 *
	 */
393
	Vector<T,PETSC_BASE> & operator=(Vector<T,PETSC_BASE> && v)
Pietro Incardona's avatar
Pietro Incardona committed
394
	{
Pietro Incardona's avatar
Pietro Incardona committed
395 396
		map.swap(v.map);
		row_val.swap(v.row_val);
Pietro Incardona's avatar
Pietro Incardona committed
397 398 399

		return *this;
	}
incardon's avatar
incardon committed
400 401 402 403 404 405 406 407 408 409 410 411

	/*! \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
412 413 414 415 416
};


#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */