Flow123d  JS_before_hm-1873-g0cc6cc928
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  { chkerr(VecZeroEntries( data_petsc_ ));}
84 
85  //VectorData &data()
86  //{
87  // ASSERT_DBG(data_ptr_);
88  // return *data_ptr_;
89  //}
90 
91  //const VectorData &data() const
92  //{
93  // ASSERT_DBG(data_ptr_);
94  // return *data_ptr_;
95  //}
96 
97  /// Return size of output data.
98  unsigned int size() const
99  {
100  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
101  return data_ptr_->size();
102  }
103 
104  /// Return value on given position
105  inline double get(unsigned int pos) const {
106  ASSERT_PTR_DBG(data_ptr_).error("Uninitialized data vector.\n");
107  return (*data_ptr_)[pos];
108  }
109 
110  /// Set value on given position
111  inline void set(unsigned int pos, double val) {
112  ASSERT_PTR_DBG(data_ptr_).error("Uninitialized data vector.\n");
113  (*data_ptr_)[pos] = val;
114  }
115 
116  /// Normalize value on given position
117  inline void normalize(unsigned int pos, double divisor) {
118  ASSERT_PTR_DBG(data_ptr_).error("Uninitialized data vector.\n");
119  (*data_ptr_)[pos] /= divisor;
120  }
121 
122  /// Add value to item on given position
123  inline void add(unsigned int pos, double val) {
124  ASSERT_PTR_DBG(data_ptr_).error("Uninitialized data vector.\n");
125  (*data_ptr_)[pos] += val;
126  }
127 
128 
129  /// For the current vector, it creates the same parallel structure as the @p other vector has.
130  /**
131  * FIXME: it does not take care of ghost values, so it cannot be used in that situation.
132  */
133  void duplicate_from(VectorMPI other);
134 
135  /// Swaps the current vector data with the other vector data.
136  /**
137  * FIXME: it does not take care of ghost values, so it cannot be used
138  */
139  void swap(VectorMPI &other);
140 
141  /// Copies data from the @p other vector.
142  /// Both vector must have the same communicator and distribution.
143  void copy_from(VectorMPI &other);
144 
145 
146  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
148  { VecGhostUpdateBegin(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
149 
150  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
152  { VecGhostUpdateEnd(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
153 
154  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
156  { VecGhostUpdateBegin(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
157 
158  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
160  { VecGhostUpdateEnd(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
161 
162  /// Destructor.
163  ~VectorMPI();
164 
165  /**
166  * Access to the vector element on local index @p idx.
167  */
168 // inline double &operator[](unsigned int idx)
169 // {
170 // ASSERT_DBG(data_ptr_);
171 // ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
172 // return (*data_ptr_)[idx];
173 // }
174 
175  /**
176  * Access to the vector element on local index @p idx (const version).
177  */
178 // inline double &operator[](unsigned int idx) const
179 // {
180 // ASSERT_DBG(data_ptr_);
181 // ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
182 // return (*data_ptr_)[idx];
183 // }
184 
185  /**
186  * Access to the vector elements on local indices @p idx.
187  */
188  arma::vec get_subvec(const LocDofVec& loc_indices);
189 
190  /**
191  * Access to the vector elements on local indices @p idx (const version).
192  */
193  arma::vec get_subvec(const LocDofVec& loc_indices) const;
194 
195  /**
196  * Set some vector elements on local indices @p idx.
197  */
198  void set_subvec(const LocDofVec& loc_indices, const arma::vec& values);
199 private:
200 
201  /// shared pointer to vector of data
203  /// stored vector of data in PETSC format
205  /// communicator
207 };
208 
209 
210 #endif /* VECTOR_MPI_HH_ */
LocDofVec
arma::Col< IntIdx > LocDofVec
Definition: index_types.hh:28
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
VectorMPI::data_ptr_
VectorDataPtr data_ptr_
shared pointer to vector of data
Definition: vector_mpi.hh:202
VectorMPI::VectorMPI
VectorMPI(MPI_Comm comm=PETSC_COMM_SELF)
Definition: vector_mpi.cc:29
VectorMPI::ghost_to_local_begin
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:155
VectorMPI::zero_entries
void zero_entries()
Definition: vector_mpi.hh:82
VectorMPI::resize
void resize(unsigned int local_size)
Definition: vector_mpi.cc:50
chkerr
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:142
std::vector< double >
system.hh
VectorMPI::normalize
void normalize(unsigned int pos, double divisor)
Normalize value on given position.
Definition: vector_mpi.hh:117
index_types.hh
ASSERT_PTR_DBG
#define ASSERT_PTR_DBG(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:340
VectorMPI::set
void set(unsigned int pos, double val)
Set value on given position.
Definition: vector_mpi.hh:111
VectorMPI::swap
void swap(VectorMPI &other)
Swaps the current vector data with the other vector data.
Definition: vector_mpi.cc:86
VectorMPI::copy_from
void copy_from(VectorMPI &other)
Definition: vector_mpi.cc:103
VectorMPI::VectorDataPtr
std::shared_ptr< VectorData > VectorDataPtr
Definition: vector_mpi.hh:46
VectorMPI::data_petsc_
Vec data_petsc_
stored vector of data in PETSC format
Definition: vector_mpi.hh:204
VectorMPI::get_subvec
arma::vec get_subvec(const LocDofVec &loc_indices)
Definition: vector_mpi.cc:110
VectorMPI::ghost_to_local_end
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:159
VectorMPI::local_to_ghost_begin
void local_to_ghost_begin()
local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
Definition: vector_mpi.hh:147
VectorMPI::add
void add(unsigned int pos, double val)
Add value to item on given position.
Definition: vector_mpi.hh:123
VectorMPI::~VectorMPI
~VectorMPI()
Destructor.
Definition: vector_mpi.cc:151
MPI_Comm
int MPI_Comm
Definition: mpi.h:141
VectorMPI::communicator_
MPI_Comm communicator_
communicator
Definition: vector_mpi.hh:206
VectorMPI::set_subvec
void set_subvec(const LocDofVec &loc_indices, const arma::vec &values)
Definition: vector_mpi.cc:138
global_defs.h
Global macros to enhance readability and debugging, general constants.
VectorMPI::local_to_ghost_end
void local_to_ghost_end()
local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
Definition: vector_mpi.hh:151
VectorMPI::sequential
static VectorMPI sequential(unsigned int size)
Definition: vector_mpi.cc:44
VectorMPI::duplicate_from
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
VectorMPI::get
double get(unsigned int pos) const
Return value on given position.
Definition: vector_mpi.hh:105
VectorMPI
Definition: vector_mpi.hh:43
VectorMPI::petsc_vec
Vec & petsc_vec()
Getter for PETSC vector of output data (e.g. can be used by scatters).
Definition: vector_mpi.hh:79
ASSERT_PTR
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:336
VectorMPI::size
unsigned int size() const
Return size of output data.
Definition: vector_mpi.hh:98
VectorMPI::VectorData
std::vector< double > VectorData
Definition: vector_mpi.hh:45