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