Flow123d  master-1cc3435
vector_mpi.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file vector_mpi.hh
15  * @brief
16  */
17 
18 #ifndef VECTOR_MPI_HH_
19 #define VECTOR_MPI_HH_
20 
21 #include <vector>
22 #include <memory>
23 #include "system/system.hh"
24 #include "system/global_defs.h"
25 #include "system/index_types.hh"
26 
27 #include <petscvec.h>
28 #include <armadillo>
29 
30 /**
31  * Auxiliary class for output elementwise concentration vectors
32  * in convection transport, sorptions, dual porosity etc.
33  *
34  * Stores data in two formats:
35  * - shared pointer to std::vector of double
36  * - pointer to PETSC vector that use same data
37  *
38  * Allows the following functionalities:
39  * - access to local part
40  * - return shared pointer to std::vector of double
41  * - return pointer to PETSC vector
42  */
43 class VectorMPI {
44 public:
45  typedef typename std::vector<double> VectorData;
46  typedef typename std::shared_ptr< VectorData > VectorDataPtr;
47 
48  VectorMPI(MPI_Comm comm = PETSC_COMM_SELF);
49 
50  /// Create shared pointer and PETSC vector with given size. COLLECTIVE.
51  VectorMPI(unsigned int local_size, MPI_Comm comm = PETSC_COMM_WORLD);
52 
53  /// Create PETSc vector with ghost values whose indices are specified in @p ghost_idx.
54  VectorMPI(unsigned int local_size, std::vector<LongIdx> &ghost_idx);
55 
56  /**
57  * Helper method creating VectorMPI of given size with serial Petsc communicator.
58  *
59  * Method is used for better readability of code.
60  */
61  static VectorMPI sequential(unsigned int size);
62 
63  /**
64  * Resize the vector to given local size. Operation is allowed only if this object is
65  * a unique vector object pointing to the actual data.
66  */
67  void resize(unsigned int local_size);
68 
69  /**
70  * Resize the vector to given local size with ghost values. Indices of ghost values are in ghost_idx.
71  */
72  void resize(unsigned int local_size, std::vector<LongIdx> &ghost_idx);
73 
74  /// Getter for shared pointer of output data.
75  ///VectorDataPtr data_ptr()
76  //{ return data_ptr_;}
77 
78  /// Getter for PETSC vector of output data (e.g. can be used by scatters).
79  Vec &petsc_vec()
80  { return data_petsc_;}
81 
82  void zero_entries() {
83  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
84  std::fill(data_ptr_->begin(), data_ptr_->end(), 0.0);
85  chkerr(VecZeroEntries( data_petsc_ ));
86  }
87 
88  //VectorData &data()
89  //{
90  // ASSERT(data_ptr_);
91  // return *data_ptr_;
92  //}
93 
94  //const VectorData &data() const
95  //{
96  // ASSERT(data_ptr_);
97  // return *data_ptr_;
98  //}
99 
100  /// Return size of output data.
101  unsigned int size() const
102  {
103  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
104  return data_ptr_->size();
105  }
106 
107  /// Return value on given position
108  inline double get(unsigned int pos) const {
109  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
110  return (*data_ptr_)[pos];
111  }
112 
113  /// Set value on given position
114  inline void set(unsigned int pos, double val) {
115  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
116  ASSERT_LT(pos, data_ptr_->size()).error("Given 'pos' out of vector size!\n");
117  (*data_ptr_)[pos] = val;
118  }
119 
120  /// Normalize value on given position
121  inline void normalize(unsigned int pos, double divisor) {
122  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
123  ASSERT_LT(pos, data_ptr_->size()).error("Given 'pos' out of vector size!\n");
124  (*data_ptr_)[pos] /= divisor;
125  }
126 
127  /// Add value to item on given position
128  inline void add(unsigned int pos, double val) {
129  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
130  ASSERT_LT(pos, data_ptr_->size()).error("Given 'pos' out of vector size!\n");
131  (*data_ptr_)[pos] += val;
132  }
133 
134  /// Add value to item on given global position
135  inline void add_global(unsigned int pos, double val) {
136  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
137  VecSetValue(data_petsc_, pos, val, ADD_VALUES);
138  }
139 
140 
141  /// For the current vector, it creates the same parallel structure as the @p other vector has.
142  /**
143  * FIXME: it does not take care of ghost values, so it cannot be used in that situation.
144  */
145  void duplicate_from(VectorMPI other);
146 
147  /// Swaps the current vector data with the other vector data.
148  /**
149  * FIXME: it does not take care of ghost values, so it cannot be used
150  */
151  void swap(VectorMPI &other);
152 
153  /// Copies data from the @p other vector.
154  /// Both vector must have the same communicator and distribution.
155  void copy_from(const VectorMPI &other);
156 
157 
158  /// assembly_{begin,end} performs global assembly
160  { VecAssemblyBegin(data_petsc_);}
161 
162  /// assembly_{begin,end} performs global assembly
164  { VecAssemblyEnd(data_petsc_);}
165 
166  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
168  { VecGhostUpdateBegin(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
169 
170  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
172  { VecGhostUpdateEnd(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
173 
174  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
176  { VecGhostUpdateBegin(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
177 
178  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
180  { VecGhostUpdateEnd(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
181 
182  /// Destructor.
183  ~VectorMPI();
184 
185  /**
186  * Access to the vector element on local index @p idx.
187  */
188 // inline double &operator[](unsigned int idx)
189 // {
190 // ASSERT(data_ptr_);
191 // ASSERT(idx < data_ptr_->size()) (idx) (data_ptr_->size());
192 // return (*data_ptr_)[idx];
193 // }
194 
195  /**
196  * Access to the vector element on local index @p idx (const version).
197  */
198 // inline double &operator[](unsigned int idx) const
199 // {
200 // ASSERT(data_ptr_);
201 // ASSERT(idx < data_ptr_->size()) (idx) (data_ptr_->size());
202 // return (*data_ptr_)[idx];
203 // }
204 
205  /**
206  * Access to the vector elements on local indices @p idx.
207  */
208  arma::vec get_subvec(const LocDofVec& loc_indices);
209 
210  /**
211  * Access to the vector elements on local indices @p idx (const version).
212  */
213  arma::vec get_subvec(const LocDofVec& loc_indices) const;
214 
215  /**
216  * Set some vector elements on local indices @p idx.
217  */
218  void set_subvec(const LocDofVec& loc_indices, const arma::vec& values);
219 private:
220 
221  /// shared pointer to vector of data
223  /// stored vector of data in PETSC format
225  /// communicator
227 };
228 
229 
230 #endif /* VECTOR_MPI_HH_ */
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
void add_global(unsigned int pos, double val)
Add value to item on given global position.
Definition: vector_mpi.hh:135
void copy_from(const VectorMPI &other)
Definition: vector_mpi.cc:103
void assembly_end()
assembly_{begin,end} performs global assembly
Definition: vector_mpi.hh:163
void duplicate_from(VectorMPI other)
For the current vector, it creates the same parallel structure as the other vector has.
Definition: vector_mpi.cc:80
arma::vec get_subvec(const LocDofVec &loc_indices)
Definition: vector_mpi.cc:110
double get(unsigned int pos) const
Return value on given position.
Definition: vector_mpi.hh:108
void normalize(unsigned int pos, double divisor)
Normalize value on given position.
Definition: vector_mpi.hh:121
void set(unsigned int pos, double val)
Set value on given position.
Definition: vector_mpi.hh:114
void resize(unsigned int local_size)
Definition: vector_mpi.cc:50
void local_to_ghost_end()
local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
Definition: vector_mpi.hh:171
void set_subvec(const LocDofVec &loc_indices, const arma::vec &values)
Definition: vector_mpi.cc:138
void add(unsigned int pos, double val)
Add value to item on given position.
Definition: vector_mpi.hh:128
~VectorMPI()
Destructor.
Definition: vector_mpi.cc:151
static VectorMPI sequential(unsigned int size)
Definition: vector_mpi.cc:44
void local_to_ghost_begin()
local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
Definition: vector_mpi.hh:167
void zero_entries()
Definition: vector_mpi.hh:82
VectorDataPtr data_ptr_
shared pointer to vector of data
Definition: vector_mpi.hh:222
Vec data_petsc_
stored vector of data in PETSC format
Definition: vector_mpi.hh:224
void ghost_to_local_end()
ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processo...
Definition: vector_mpi.hh:179
std::shared_ptr< VectorData > VectorDataPtr
Definition: vector_mpi.hh:46
void assembly_begin()
assembly_{begin,end} performs global assembly
Definition: vector_mpi.hh:159
Vec & petsc_vec()
Getter for PETSC vector of output data (e.g. can be used by scatters).
Definition: vector_mpi.hh:79
void swap(VectorMPI &other)
Swaps the current vector data with the other vector data.
Definition: vector_mpi.cc:86
MPI_Comm communicator_
communicator
Definition: vector_mpi.hh:226
unsigned int size() const
Return size of output data.
Definition: vector_mpi.hh:101
void ghost_to_local_begin()
ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processo...
Definition: vector_mpi.hh:175
VectorMPI(MPI_Comm comm=PETSC_COMM_SELF)
Definition: vector_mpi.cc:29
std::vector< double > VectorData
Definition: vector_mpi.hh:45
Global macros to enhance readability and debugging, general constants.
arma::Col< IntIdx > LocDofVec
Definition: index_types.hh:28
int MPI_Comm
Definition: mpi.h:141
ArmaVec< double, N > vec
Definition: armor.hh:933
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:142