Vector_petsc.hpp 6.24 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
Pietro Incardona's avatar
Pietro Incardona committed
89
	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
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
	{
Pietro Incardona's avatar
Pietro Incardona committed
103
		PETSC_SAFE_CALL(VecSetFromOptions(v));
Pietro Incardona's avatar
Pietro Incardona committed
104

Pietro Incardona's avatar
Pietro Incardona committed
105

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

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

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


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

public:

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

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

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

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

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

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

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

		PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
Pietro Incardona's avatar
Pietro Incardona committed
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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	}

	/*! \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
237 238
		row_val.last().template get<row_id>() = i;
		return row_val.last().template get<val_id>();
Pietro Incardona's avatar
Pietro Incardona committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	}

	/*! \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
283
	/*! \brief Get the PETSC Vector object
Pietro Incardona's avatar
Pietro Incardona committed
284
	 *
Pietro Incardona's avatar
Pietro Incardona committed
285
	 * \return the PETSC Vector
Pietro Incardona's avatar
Pietro Incardona committed
286 287 288 289 290 291 292 293 294
	 *
	 */
	const Vec & getVec() const
	{
		setPetsc();

		return v;
	}

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

		return v;
	}

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

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

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

		return *this;
	}

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

		return *this;
	}
};


#endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */