Flow123d  JS_before_hm-927-g0a4a2b5
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  {
88  return *data_ptr_;
89  }
90 
91  const VectorData &data() const
92  {
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 
105  /// For the current vector, it creates the same parallel structure as the @p other vector has.
106  /**
107  * FIXME: it does not take care of ghost values, so it cannot be used in that situation.
108  */
109  void duplicate_from(VectorMPI other);
110 
111  /// Swaps the current vector data with the other vector data.
112  /**
113  * FIXME: it does not take care of ghost values, so it cannot be used
114  */
115  void swap(VectorMPI &other);
116 
117  /// Copies data from the @p other vector.
118  /// Both vector must have the same communicator and distribution.
119  void copy_from(VectorMPI &other);
120 
121 
122  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
124  { VecGhostUpdateBegin(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
125 
126  /// local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values
128  { VecGhostUpdateEnd(data_petsc_, INSERT_VALUES, SCATTER_FORWARD);}
129 
130  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
132  { VecGhostUpdateBegin(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
133 
134  /// ghost_to_local_{begin,end} updates the local values by adding ghost values from neighbouring processors
136  { VecGhostUpdateEnd(data_petsc_, ADD_VALUES, SCATTER_REVERSE);}
137 
138  /// Destructor.
139  ~VectorMPI();
140 
141  /**
142  * Access to the vector element on local index @p idx.
143  */
144  inline double &operator[](unsigned int idx)
145  {
147  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
148  return (*data_ptr_)[idx];
149  }
150 
151  /**
152  * Access to the vector element on local index @p idx (const version).
153  */
154  inline double &operator[](unsigned int idx) const
155  {
157  ASSERT_DBG(idx < data_ptr_->size()) (idx) (data_ptr_->size());
158  return (*data_ptr_)[idx];
159  }
160 
161  /**
162  * Access to the vector elements on local indices @p idx.
163  */
164  arma::vec get_subvec(const LocDofVec& loc_indices);
165 
166  /**
167  * Access to the vector elements on local indices @p idx (const version).
168  */
169  arma::vec get_subvec(const LocDofVec& loc_indices) const;
170 
171  /**
172  * Set some vector elements on local indices @p idx.
173  */
174  void set_subvec(const LocDofVec& loc_indices, const arma::vec& values);
175 private:
176 
177  /// shared pointer to vector of data
178  VectorDataPtr data_ptr_;
179  /// stored vector of data in PETSC format
181  /// communicator
183 };
184 
185 
186 #endif /* VECTOR_MPI_HH_ */
unsigned int size() const
Return size of output data.
Definition: vector_mpi.hh:98
MPI_Comm communicator_
communicator
Definition: vector_mpi.hh:182
arma::Col< IntIdx > LocDofVec
Definition: index_types.hh:28
ArmaVec< double, N > vec
Definition: armor.hh:861
int MPI_Comm
Definition: mpi.h:141
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:135
void local_to_ghost_begin()
local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values ...
Definition: vector_mpi.hh:123
void zero_entries()
Definition: vector_mpi.hh:82
std::vector< double > VectorData
Definition: vector_mpi.hh:45
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:148
VectorDataPtr data_ptr()
Getter for shared pointer of output data.
Definition: vector_mpi.hh:75
double & operator[](unsigned int idx)
Definition: vector_mpi.hh:144
double & operator[](unsigned int idx) const
Definition: vector_mpi.hh:154
~VectorMPI()
Destructor.
Definition: vector_mpi.cc:151
const VectorData & data() const
Definition: vector_mpi.hh:91
VectorMPI(MPI_Comm comm=PETSC_COMM_SELF)
Definition: vector_mpi.cc:29
void copy_from(VectorMPI &other)
Definition: vector_mpi.cc:103
Global macros to enhance readability and debugging, general constants.
VectorDataPtr data_ptr_
shared pointer to vector of data
Definition: vector_mpi.hh:178
Vec data_petsc_
stored vector of data in PETSC format
Definition: vector_mpi.hh:180
void resize(unsigned int local_size)
Definition: vector_mpi.cc:50
Vec & petsc_vec()
Getter for PETSC vector of output data (e.g. can be used by scatters).
Definition: vector_mpi.hh:79
void local_to_ghost_end()
local_to_ghost_{begin,end} updates the ghost values on neighbouring processors from local values ...
Definition: vector_mpi.hh:127
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR)
Definition: asserts.hh:336
void swap(VectorMPI &other)
Swaps the current vector data with the other vector data.
Definition: vector_mpi.cc:86
#define ASSERT_DBG(expr)
void set_subvec(const LocDofVec &loc_indices, const arma::vec &values)
Definition: vector_mpi.cc:138
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
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:131
std::shared_ptr< VectorData > VectorDataPtr
Definition: vector_mpi.hh:46
VectorData & data()
Definition: vector_mpi.hh:85
static VectorMPI sequential(unsigned int size)
Definition: vector_mpi.cc:44