OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
Vector_petsc.hpp
1 /*
2  * Vector_petsc.hpp
3  *
4  * Created on: Apr 29, 2016
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_PETSC_HPP_
9 #define OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_PETSC_HPP_
10 
11 #include "Vector/map_vector.hpp"
12 #include "Vector/vector_def.hpp"
13 #include <boost/mpl/int.hpp>
14 #include <petscvec.h>
15 #include "util/petsc_util.hpp"
16 #include <unordered_map>
17 
18 #define PETSC_RVAL 2
19 
26 template<typename T>
27 class rval<T,PETSC_RVAL>
28 {
29 public:
30 
32  typedef boost::fusion::vector<PetscInt,T> type;
33 
36 
38  static const unsigned int row = 0;
39 
41  static const unsigned int value = 1;
42 
44  static const unsigned int max_prop = 2;
45 
51  long int & rw()
52  {
53  return boost::fusion::at_c<row>(data);
54  }
55 
61  T & val()
62  {
63  return boost::fusion::at_c<value>(data);
64  }
65 
69  rval() {}
70 
77  rval(long int i, T val)
78  {
79  rw() = i;
80  val() = val;
81  }
82 
88  static inline bool noPointers()
89  {
90  return true;
91  }
92 };
93 
94 constexpr unsigned int row_id = 0;
95 constexpr unsigned int val_id = 1;
96 
97 
103 template<typename T>
104 class Vector<T,PETSC_BASE>
105 {
107  size_t n_row;
108 
110  size_t n_row_local;
111 
113  mutable bool v_created = false;
114 
116  mutable Vec v;
117 
120 
122  mutable std::unordered_map<size_t,size_t> map;
123 
126 
131  void setPetsc() const
132  {
133  if (v_created == false)
134  {PETSC_SAFE_CALL(VecSetType(v,VECMPI));}
135 
136 
137  // set the vector
138 
139  if (row_val.size() != 0)
140  PETSC_SAFE_CALL(VecSetValues(v,row_val.size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0),INSERT_VALUES))
141 
142  PETSC_SAFE_CALL(VecAssemblyBegin(v));
143  PETSC_SAFE_CALL(VecAssemblyEnd(v));
144 
145  v_created = true;
146  }
147 
148 public:
149 
156  {
157  this->operator=(v);
158  }
159 
166  :n_row(0),n_row_local(0),invalid(0)
167  {
168  this->operator=(v);
169  }
170 
176  {
177  if (is_openfpm_init() == true)
178  {PETSC_SAFE_CALL(VecDestroy(&v));}
179  }
180 
187  Vector(size_t n, size_t n_row_local)
188  :n_row_local(n_row_local),v(NULL),invalid(0)
189  {
190  // Create the vector
191  PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
192 
193  resize(n,n_row_local);
194  }
195 
200  :n_row(0),n_row_local(0),invalid(0)
201  {
202  // Create the vector
203  PETSC_SAFE_CALL(VecCreate(PETSC_COMM_WORLD,&v));
204  }
205 
212  void resize(size_t row, size_t l_row)
213  {
214  n_row = row;
215  n_row_local = l_row;
216 
217  PETSC_SAFE_CALL(VecSetSizes(v,n_row_local,n_row));
218  }
219 
226  void insert(size_t i, T val)
227  {
228  row_val.add();
229 
230  // Map
231  map[i] = row_val.size()-1;
232 
233  row_val.last().template get<row_id>() = i;
234  row_val.last().template get<val_id>() = val;
235  }
236 
244  inline PetscScalar & insert(size_t i)
245  {
246  row_val.add();
247 
248  // Map
249  map[i] = row_val.size()-1;
250 
251  row_val.last().template get<row_id>() = i;
252  return row_val.last().template get<val_id>();
253  }
254 
262  inline const PetscScalar & insert(size_t i) const
263  {
264  row_val.add();
265 
266  // Map
267  map[i] = row_val.size()-1;
268 
269  row_val.last().template get<row_id>() = i;
270  return row_val.last().template get<val_id>();
271  }
272 
282  const PetscScalar & operator()(size_t i) const
283  {
284  // Search if exist
285 
286  std::unordered_map<size_t,size_t>::iterator it = map.find(i);
287 
288  if ( it != map.end() )
289  return row_val.template get<val_id>(it->second);
290 
291  return insert(i);
292  }
293 
303  PetscScalar & operator()(size_t i)
304  {
305  // Search if exist
306 
307  std::unordered_map<size_t,size_t>::iterator it = map.find(i);
308 
309  if ( it != map.end() )
310  return row_val.template get<val_id>(it->second);
311 
312  return insert(i);
313  }
314 
320  const Vec & getVec() const
321  {
322  setPetsc();
323 
324  return v;
325  }
326 
332  Vec & getVec()
333  {
334  setPetsc();
335 
336  return v;
337  }
338 
342  void update()
343  {
344  PetscInt n_row;
345  PetscInt n_row_local;
346 
347  // Get the size of the vector from PETSC
348  VecGetSize(v,&n_row);
349  VecGetLocalSize(v,&n_row_local);
350 
351  this->n_row = n_row;
352  this->n_row_local = n_row_local;
353 
354  row_val.resize(n_row_local);
355 
356  //
357 
358  PetscInt low;
359  PetscInt high;
360 
361  VecGetOwnershipRange(v,&low,&high);
362 
363  // Fill the index and construct the map
364 
365  size_t k = 0;
366  for (size_t i = low ; i < (size_t)high ; i++)
367  {
368  row_val.template get<row_id>(k) = i;
369  map[i] = k;
370  k++;
371  }
372 
373  PETSC_SAFE_CALL(VecGetValues(v,row_val.size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0)))
374  }
375 
382  {
383  map = v.map;
384  row_val = v.row_val;
385 
386  return *this;
387  }
388 
395  {
396  map.swap(v.map);
397  row_val.swap(v.row_val);
398 
399  return *this;
400  }
401 
406  void setZero()
407  {
408  if (v_created == false)
409  {PETSC_SAFE_CALL(VecSetType(v,VECMPI));}
410 
411  v_created = true;
412  }
413 };
414 
415 
416 #endif /* OPENFPM_NUMERICS_SRC_VECTOR_VECTOR_EIGEN_HPP_ */
417 
PetscScalar & operator()(size_t i)
Return a reference to the vector element.
void insert(size_t i, T val)
stub insert
Definition: Vector.hpp:105
void update()
Update the Vector with the PETSC object.
size_t n_row
Number of row the petsc vector has.
rval(long int i, T val)
Constructor from row, column and value.
Vector(const Vector< T, PETSC_BASE > &v)
Copy the vector.
std::unordered_map< size_t, size_t > map
Global to local map.
static bool noPointers()
Indicate that the structure has no pointer.
void insert(size_t i, T val)
Return a reference to the vector element.
void resize(size_t row, size_t l_row)
Resize the Vector.
T & val()
Get the value.
Sparse Matrix implementation stub object when OpenFPM is compiled with no linear algebra support...
Definition: Vector.hpp:39
PETSC vector for linear algebra.
Vector(size_t n, size_t n_row_local)
Create a vector with n elements.
Vec & getVec()
Get the PETSC Vector object.
openfpm::vector< rval< PetscScalar, PETSC_RVAL >, HeapMemory, typename memory_traits_inte< rval< PetscScalar, PETSC_RVAL > >::type, memory_traits_inte > row_val
Mutable row value vector.
PetscScalar & insert(size_t i)
Return a reference to the vector element.
~Vector()
Destroy the vector.
This class allocate, and destroy CPU memory.
Definition: HeapMemory.hpp:39
const PetscScalar & insert(size_t i) const
Return a reference to the vector element.
Transform the boost::fusion::vector into memory specification (memory_traits)
Definition: memory_conf.hpp:52
const Vec & getVec() const
Get the PETSC Vector object.
Vector< T > & operator=(const Vector< T > &v)
stub operator=
Definition: Vector.hpp:161
long int & rw()
Get the row.
type data
structure that store the data of the point
void resize(size_t row, size_t row_n)
stub resize
Definition: Vector.hpp:97
Vector(Vector< T, PETSC_BASE > &&v)
Copy the vector.
It store one row value of a vector.
Definition: Vector.hpp:21
const PetscScalar & operator()(size_t i) const
Return a reference to the vector element.
size_t n_row_local
Number of local rows.
boost::fusion::vector< PetscInt, T > type
boost fusion that store the point
void setPetsc() const
Set the Eigen internal vector.
rval()
Default constructor.
Vector()
Create a vector with 0 elements.
Vec v
Mutable vector.
Vector< T, PETSC_BASE > & operator=(const Vector< T, PETSC_BASE > &v)
Copy the vector.
Implementation of 1-D std::vector like structure.
Definition: map_vector.hpp:61
Vector< T, PETSC_BASE > & operator=(Vector< T, PETSC_BASE > &&v)
Copy the vector.
void setZero()
Set to zero all the entries.