Flow123d  3.9.0-454df51df
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(data_ptr_);
88  // return *data_ptr_;
89  //}
90 
91  //const VectorData &data() const
92  //{
93  // ASSERT(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(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(data_ptr_).error("Uninitialized data vector.\n");
113  ASSERT_LT(pos, data_ptr_->size()).error("Given 'pos' out of vector size!\n");
114  (*data_ptr_)[pos] = val;
115  }
116 
117  /// Normalize value on given position
118  inline void normalize(unsigned int pos, double divisor) {
119  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
120  ASSERT_LT(pos, data_ptr_->size()).error("Given 'pos' out of vector size!\n");
121  (*data_ptr_)[pos] /= divisor;
122  }
123 
124  /// Add value to item on given position
125  inline void add(unsigned int pos, double val) {
126  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
127  ASSERT_LT(pos, data_ptr_->size()).error("Given 'pos' out of vector size!\n");
128  (*data_ptr_)[pos] += val;
129  }
130 
131  /// Add value to item on given global position
132  inline void add_global(unsigned int pos, double val) {
133  ASSERT_PTR(data_ptr_).error("Uninitialized data vector.\n");
134  VecSetValue(data_petsc_, pos, val, ADD_VALUES);
135  }
136 
137 
138  /// For the current vector, it creates the same parallel structure as the @p other vector has.
139  /**
140  * FIXME: it does not take care of ghost values, so it cannot be used in that situation.
141  */
142  void duplicate_from(VectorMPI other);
143 
144  /// Swaps the current vector data with the other vector data.
145  /**
146  * FIXME: it does not take care of ghost values, so it cannot be used
147  */
148  void swap(VectorMPI &other);
149 
150  /// Copies data from the @p other vector.
151  /// Both vector must have the same communicator and distribution.
152  void copy_from(VectorMPI &other);
153 
154 
155  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
157  { VecGhostUpdateBegin(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
158 
159  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
161  { VecGhostUpdateEnd(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
162 
163  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
165  { VecGhostUpdateBegin(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
166 
167  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
169  { VecGhostUpdateEnd(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
170 
171  /// Destructor.
172  ~VectorMPI();
173 
174  /**
175  * Access to the vector element on local index @p idx.
176  */
177 // inline double &operator[](unsigned int idx)
178 // {
179 // ASSERT(data_ptr_);
180 // ASSERT(idx < data_ptr_->size()) (idx) (data_ptr_->size());
181 // return (*data_ptr_)[idx];
182 // }
183 
184  /**
185  * Access to the vector element on local index @p idx (const version).
186  */
187 // inline double &operator[](unsigned int idx) const
188 // {
189 // ASSERT(data_ptr_);
190 // ASSERT(idx < data_ptr_->size()) (idx) (data_ptr_->size());
191 // return (*data_ptr_)[idx];
192 // }
193 
194  /**
195  * Access to the vector elements on local indices @p idx.
196  */
197  arma::vec get_subvec(const LocDofVec& loc_indices);
198 
199  /**
200  * Access to the vector elements on local indices @p idx (const version).
201  */
202  arma::vec get_subvec(const LocDofVec& loc_indices) const;
203 
204  /**
205  * Set some vector elements on local indices @p idx.
206  */
207  void set_subvec(const LocDofVec& loc_indices, const arma::vec& values);
208 private:
209 
210  /// shared pointer to vector of data
212  /// stored vector of data in PETSC format
214  /// communicator
216 };
217 
218 
219 #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:211
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:164
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:118
index_types.hh
ASSERT_LT
#define ASSERT_LT(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:301
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:213
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:168
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:156
VectorMPI::add
void add(unsigned int pos, double val)
Add value to item on given position.
Definition: vector_mpi.hh:125
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:215
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:160
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::add_global
void add_global(unsigned int pos, double val)
Add value to item on given global position.
Definition: vector_mpi.hh:132
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) only for debug mode.
Definition: asserts.hh:341
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