Vector_petsc.hpp 6.49 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
	size_t n_row_local;

Pietro Incardona's avatar
Pietro Incardona committed
82 83 84
	// Indicate if v has been allocated
	bool v_created = false;

Pietro Incardona's avatar
Pietro Incardona committed
85 86 87 88
	// Mutable vector
	mutable Vec v;

	// Mutable row value vector
incardon's avatar
incardon committed
89
	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
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
	{
incardon's avatar
incardon committed
103 104
		if (v_created == false)
		{PETSC_SAFE_CALL(VecSetType(v,VECMPI));}
Pietro Incardona's avatar
Pietro Incardona committed
105

Pietro Incardona's avatar
Pietro Incardona committed
106

Pietro Incardona's avatar
Pietro Incardona committed
107 108
		// set the vector

Pietro Incardona's avatar
Pietro Incardona committed
109 110
		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
111

Pietro Incardona's avatar
Pietro Incardona committed
112 113 114 115
		PETSC_SAFE_CALL(VecAssemblyBegin(v));
		PETSC_SAFE_CALL(VecAssemblyEnd(v));


Pietro Incardona's avatar
Pietro Incardona committed
116 117 118 119 120 121 122 123 124
	}

public:

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

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

140
	/*! \brief Destroy the vector
Pietro Incardona's avatar
Pietro Incardona committed
141 142 143 144 145
	 *
	 *
	 */
	~Vector()
	{
146 147
		if (is_openfpm_init() == true)
		{PETSC_SAFE_CALL(VecDestroy(&v));}
Pietro Incardona's avatar
Pietro Incardona committed
148 149
	}

Pietro Incardona's avatar
Pietro Incardona committed
150 151 152
	/*! \brief Create a vector with n elements
	 *
	 * \param n number of elements in the vector
Pietro Incardona's avatar
Pietro Incardona committed
153
	 * \param n_row_loc number
Pietro Incardona's avatar
Pietro Incardona committed
154 155
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
156 157
	Vector(size_t n, size_t n_row_local)
	:n_row_local(n_row_local)
Pietro Incardona's avatar
Pietro Incardona committed
158
	{
Pietro Incardona's avatar
Pietro Incardona committed
159 160 161
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));

Pietro Incardona's avatar
Pietro Incardona committed
162
		resize(n,n_row_local);
Pietro Incardona's avatar
Pietro Incardona committed
163 164 165 166 167 168
	}

	/*! \brief Create a vector with 0 elements
	 *
	 */
	Vector()
Pietro Incardona's avatar
Pietro Incardona committed
169
	:n_row(0),n_row_local(0)
Pietro Incardona's avatar
Pietro Incardona committed
170
	{
Pietro Incardona's avatar
Pietro Incardona committed
171 172
		// Create the vector
		PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
Pietro Incardona's avatar
Pietro Incardona committed
173 174 175 176 177
	}

	/*! \brief Resize the Vector
	 *
	 * \param row numbers of row
Pietro Incardona's avatar
Pietro Incardona committed
178
	 * \param l_row number of local row
Pietro Incardona's avatar
Pietro Incardona committed
179 180
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
181
	void resize(size_t row, size_t l_row)
Pietro Incardona's avatar
Pietro Incardona committed
182 183
	{
		n_row = row;
Pietro Incardona's avatar
Pietro Incardona committed
184
		n_row_local = l_row;
Pietro Incardona's avatar
Pietro Incardona committed
185 186

		PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
Pietro Incardona's avatar
Pietro Incardona committed
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
	}

	/*! \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
213
	inline PetscScalar & insert(size_t i)
Pietro Incardona's avatar
Pietro Incardona committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	{
		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
231
	inline const PetscScalar & insert(size_t i) const
Pietro Incardona's avatar
Pietro Incardona committed
232 233 234 235 236 237
	{
		row_val.add();

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

Pietro Incardona's avatar
Pietro Incardona committed
238 239
		row_val.last().template get<row_id>() = i;
		return row_val.last().template get<val_id>();
Pietro Incardona's avatar
Pietro Incardona committed
240 241 242 243 244 245 246 247 248 249 250
	}

	/*! \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
251
	const PetscScalar & operator()(size_t i) const
Pietro Incardona's avatar
Pietro Incardona committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	{
		// 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
272
	PetscScalar & operator()(size_t i)
Pietro Incardona's avatar
Pietro Incardona committed
273 274 275 276 277 278 279 280 281 282 283
	{
		// 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
284
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
285
	 *
Pietro Incardona's avatar
Pietro Incardona committed
286
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
287 288 289 290 291 292 293 294 295
	 *
	 */
	const Vec & getVec() const
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
296
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
297
	 *
Pietro Incardona's avatar
Pietro Incardona committed
298
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
299 300 301 302 303 304 305 306 307
	 *
	 */
	Vec & getVec()
	{
		setPetsc();

		return v;
	}

Pietro Incardona's avatar
Pietro Incardona committed
308
	/*! \brief Update the Vector with the PETSC object
Pietro Incardona's avatar
Pietro Incardona committed
309 310
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
311
	void update()
Pietro Incardona's avatar
Pietro Incardona committed
312
	{
Pietro Incardona's avatar
Pietro Incardona committed
313 314
		PetscInt n_row;
		PetscInt n_row_local;
Pietro Incardona's avatar
Pietro Incardona committed
315

Pietro Incardona's avatar
Pietro Incardona committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
		// 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;
335
		for (size_t i = low ; i < (size_t)high ; i++)
Pietro Incardona's avatar
Pietro Incardona committed
336 337 338 339 340 341 342
		{
			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
343 344 345 346 347 348 349
	}

	/*! \brief Copy the vector
	 *
	 * \param v vector to copy
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
350
	Vector<T,PETSC_BASE> & operator=(const Vector<T,PETSC_BASE> & v)
Pietro Incardona's avatar
Pietro Incardona committed
351 352 353 354 355 356 357
	{
		map = v.map;
		row_val = v.row_val;

		return *this;
	}

Pietro Incardona's avatar
Pietro Incardona committed
358
	/*! \brief Copy the vector
Pietro Incardona's avatar
Pietro Incardona committed
359
	 *
Pietro Incardona's avatar
Pietro Incardona committed
360
	 * \param v vector to copy
Pietro Incardona's avatar
Pietro Incardona committed
361 362
	 *
	 */
363
	Vector<T,PETSC_BASE> & operator=(Vector<T,PETSC_BASE> && v)
Pietro Incardona's avatar
Pietro Incardona committed
364
	{
Pietro Incardona's avatar
Pietro Incardona committed
365 366
		map.swap(v.map);
		row_val.swap(v.row_val);
Pietro Incardona's avatar
Pietro Incardona committed
367 368 369

		return *this;
	}
incardon's avatar
incardon committed
370 371 372 373 374 375 376 377 378 379 380 381

	/*! \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
382 383 384 385 386
};


#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */