Chi-Tech
ParallelSTLVector.h
Go to the documentation of this file.
1#ifndef PARALLEL_VECTOR_H
2#define PARALLEL_VECTOR_H
3
4#include "ParallelVector.h"
5#include "math/chi_math.h"
6
7#include <vector>
8#include <cstdint>
9#include <map>
10
11#include <mpi.h>
12
13namespace chi_math
14{
15
16/**
17 * An implementation of a parallel vector.
18 */
20{
21public:
22 /**
23 * Initialize a parallel vector with the given local and global sizes with
24 * the given communicator whose entries are set to zero.
25 */
26 ParallelSTLVector(uint64_t local_size,
27 uint64_t global_size,
28 MPI_Comm communicator);
29
30 /**Copy constructor.*/
32
33 /**Move constructor.*/
34 ParallelSTLVector(ParallelSTLVector&& other) noexcept;
35
36 std::unique_ptr<ParallelVector> MakeCopy() const override;
37 std::unique_ptr<ParallelVector> MakeClone() const override;
38
39 /** Returns a direct pointer to the memory array used internally by the
40 * vector to store its owned elements.*/
41 double* Data() override;
42
43 /** Returns a direct const pointer to the memory array used internally by the
44 * vector to store its owned elements*/
45 const double* Data() const override;
46
47 /**Returns a constant reference to the local data of the vector.*/
48 const std::vector<double>& LocalSTLData() const;
49 /**Returns a reference to the local data of the vector.*/
50 std::vector<double>& LocalSTLData();
51
52 /**
53 * Read only accessor to the entry at the given local index of
54 * the local vector.
55 *
56 * \note This accessor allows access to all locally stored elements,
57 * including any data beyond local_size_ that may exist in derived
58 * classes.
59 */
60 double operator[](int64_t local_id) const override;
61
62 /**
63 * Read/write accessor to the entry at the given local index of
64 * the local vector.
65 *
66 * \note This accessor allows access to all locally stored elements,
67 * including any data beyond local_size_ that may exist in derived
68 * classes.
69 */
70 double& operator[](int64_t local_id) override;
71
72 /// Return a vector containing the locally owned data.
73 std::vector<double> MakeLocalVector() override;
74
75 /**
76 * Set the entries of the locally owned portion of the parallel vector
77 * to the given value.
78 */
79 void Set(double value) override;
80
81 /**
82 * Set the entries of the locally owned portion of the parallel vector
83 * to the given STL vector.
84 */
85 void Set(const std::vector<double>& local_vector) override;
86
87 /**Copies a contiguous block of data from the source STL vector to the
88 * current vector starting at local_offset. The input STL vector must have
89 * exactly num_values entries.
90 * */
91 void BlockSet(const std::vector<double>& y,
92 int64_t local_offset,
93 int64_t num_values) override;
94
95 /**Sets the local values of one vector equal to another. The sizes must be
96 * compatible.*/
97 void CopyLocalValues(const ParallelVector& y) override;
98
99 /**Sets the local values of the vector equal to that of the PETSc vector.
100 * The sizes must be compatible.*/
101 void CopyLocalValues(Vec y) override;
102
103 /**Copies a contiguous block of local data (num_values entries) from the
104 * source vector (starting at y_offset) to the
105 * current vector starting at local_offset. */
107 int64_t y_offset,
108 int64_t local_offset,
109 int64_t num_values) override;
110
111 /**Copies a contiguous block of local data (num_values entries) from the
112 * source vector (starting at y_offset) to the
113 * current vector starting at local_offset. PETSc flavor.*/
115 int64_t y_offset,
116 int64_t local_offset,
117 int64_t num_values) override;
118
119 /**
120 * Define a set or add operation for the given global id-value pair
121 *
122 * This routine adds the global id-value pair to the set operation cache,
123 * which upon execution of Assemble, communicates the operations to the
124 * appropriate process.
125 */
126 void SetValue(int64_t global_id, double value, VecOpType op_type) override;
127
128 /**
129 * Group multiple operations into a single call.
130 *
131 * This routine goes through the given global id-value pairs and calls
132 * SetValue for each.
133 */
134 void SetValues(const std::vector<int64_t>& global_ids,
135 const std::vector<double>& values,
136 VecOpType op_type) override;
137
138 /**In place adding of vectors. The sizes must be compatible.*/
139 void operator+=(const ParallelVector& y) override;
140
141 /**Adds a vector multiplied by scalar a, `x = x + a*y`. Optimized for a=1.0
142 * and -1.0*/
143 void PlusAY(const ParallelVector& y, double a) override;
144
145 /**Performs x = a*x + y with the current vector being x.*/
146 void AXPlusY(double a, const ParallelVector& y) override;
147
148 /**Adds a constant scalar value to all the entries of the vector*/
149 void Scale(double a) override;
150
151 /**Returns the specified norm of the vector.*/
152 void Shift(double a) override;
153
154 /**Returns the specified norm of the vector.*/
155 double ComputeNorm(chi_math::NormType norm_type) const override;
156
157 /**
158 * Communicate all operations stored within the operation cache to the
159 * corresponding processes that own the respective global indices, and
160 * apply the operations.
161 *
162 * This routine clears the respective operation cache once completed.
163 */
164 void Assemble() override;
165
166 /// Print the local vectors to stings.
167 std::string PrintStr() const override;
168
169protected:
170 const std::vector<uint64_t> extents_;
171
172 std::vector<double> values_;
173
174 using Operation = std::pair<int64_t, double>;
175 std::vector<Operation> set_cache_;
176 std::vector<Operation> add_cache_;
177
178private:
179 static std::vector<uint64_t>
180 DefineExtents(uint64_t local_size, int comm_size, MPI_Comm communicator);
181 int FindOwnerPID(uint64_t global_id) const;
182};
183
184} // namespace chi_math
185
186#endif // PARALLEL_VECTOR_H
const std::vector< uint64_t > extents_
const std::vector< double > & LocalSTLData() const
void SetValue(int64_t global_id, double value, VecOpType op_type) override
ParallelSTLVector(uint64_t local_size, uint64_t global_size, MPI_Comm communicator)
double ComputeNorm(chi_math::NormType norm_type) const override
std::unique_ptr< ParallelVector > MakeClone() const override
void BlockSet(const std::vector< double > &y, int64_t local_offset, int64_t num_values) override
void SetValues(const std::vector< int64_t > &global_ids, const std::vector< double > &values, VecOpType op_type) override
void PlusAY(const ParallelVector &y, double a) override
void AXPlusY(double a, const ParallelVector &y) override
int FindOwnerPID(uint64_t global_id) const
void operator+=(const ParallelVector &y) override
void Shift(double a) override
std::vector< double > values_
void Scale(double a) override
void BlockCopyLocalValues(const ParallelVector &y, int64_t y_offset, int64_t local_offset, int64_t num_values) override
static std::vector< uint64_t > DefineExtents(uint64_t local_size, int comm_size, MPI_Comm communicator)
void Set(double value) override
double operator[](int64_t local_id) const override
std::vector< Operation > add_cache_
std::string PrintStr() const override
Print the local vectors to stings.
std::vector< double > MakeLocalVector() override
Return a vector containing the locally owned data.
std::vector< Operation > set_cache_
std::pair< int64_t, double > Operation
void CopyLocalValues(const ParallelVector &y) override
std::unique_ptr< ParallelVector > MakeCopy() const override
struct _p_Vec * Vec