iterative-solver 0.0
DistrArray.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_DISTRARRAY_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_DISTRARRAY_H
3#include <cmath>
4#include <cstdlib>
5#include <list>
6#include <map>
7#include <memory>
8#include <molpro/mpi.h>
9#include <vector>
10
11#include <molpro/linalg/array/Span.h>
12
13namespace molpro::linalg::array {
14namespace util {
15template <typename Ind>
16class Distribution;
17}
91public:
92 using distributed_array = void;
93 using value_type = double;
94 using index_type = size_t;
95 using SparseArray = std::map<index_type, double>;
97
98protected:
100 MPI_Comm m_communicator;
102 DistrArray(size_t dimension, MPI_Comm commun);
103 DistrArray() = default;
104
105public:
106 virtual ~DistrArray() = default;
107
109 MPI_Comm communicator() const { return m_communicator; }
111 virtual void sync() const;
113 size_t size() const { return m_dimension; };
115 bool compatible(const DistrArray &other) const;
116
121protected:
123 class LocalBuffer : public span::Span<value_type> {
124 public:
125 virtual ~LocalBuffer() = default;
127 friend void swap(LocalBuffer &, LocalBuffer &) = delete;
129 bool compatible(const LocalBuffer &other) const { return start() == other.start() && size() == other.size(); };
131 size_type start() const { return m_start; }
132
133 protected:
135 };
136
137public:
139 [[nodiscard]] virtual std::unique_ptr<LocalBuffer> local_buffer() = 0;
140 [[nodiscard]] virtual std::unique_ptr<const LocalBuffer> local_buffer() const = 0;
142 [[nodiscard]] virtual const Distribution &distribution() const = 0;
144
150 [[nodiscard]] virtual value_type at(index_type ind) const = 0;
152 virtual void set(index_type ind, value_type val) = 0;
154 virtual void get(index_type lo, index_type hi, value_type *buf) const = 0;
155 [[nodiscard]] virtual std::vector<value_type> get(index_type lo, index_type hi) const = 0;
157 virtual void put(index_type lo, index_type hi, const value_type *data) = 0;
159 virtual void acc(index_type lo, index_type hi, const value_type *data) = 0;
164 [[nodiscard]] virtual std::vector<value_type> gather(const std::vector<index_type> &indices) const = 0;
169 virtual void scatter(const std::vector<index_type> &indices, const std::vector<value_type> &data) = 0;
175 virtual void scatter_acc(std::vector<index_type> &indices, const std::vector<value_type> &data) = 0;
180 [[nodiscard]] virtual std::vector<value_type> vec() const = 0;
182
187 virtual void fill(value_type val);
190 virtual void copy(const DistrArray &y);
198 virtual void copy_patch(const DistrArray &y, index_type start, index_type end);
203 virtual void axpy(value_type a, const DistrArray &y);
204 virtual void axpy(value_type a, const SparseArray &y);
206 virtual void scal(value_type a);
208 virtual void add(const DistrArray &y);
210 virtual void add(value_type a);
212 virtual void sub(const DistrArray &y);
214 virtual void sub(value_type a);
216 virtual void recip();
218 virtual void times(const DistrArray &y);
220 virtual void times(const DistrArray &y, const DistrArray &z);
222
226
231 [[nodiscard]] virtual value_type dot(const DistrArray &y) const;
232 [[nodiscard]] virtual value_type dot(const SparseArray &y) const;
233
245 void divide(const DistrArray &y, const DistrArray &z, value_type shift = 0, bool append = false,
246 bool negative = false) {
247 _divide(y, z, shift, append, negative);
248 }
249
255 [[nodiscard]] std::list<std::pair<index_type, value_type>> min_n(int n) const;
256
262 [[nodiscard]] std::list<std::pair<index_type, value_type>> max_n(int n) const;
263
269 [[nodiscard]] std::list<std::pair<index_type, value_type>> min_abs_n(int n) const;
270
276 [[nodiscard]] std::list<std::pair<index_type, value_type>> max_abs_n(int n) const;
277
283 [[nodiscard]] std::vector<index_type> min_loc_n(int n) const;
284
285 [[nodiscard]] std::map<size_t, value_type> select_max_dot(size_t n, const DistrArray &y) const;
286 [[nodiscard]] std::map<size_t, value_type> select_max_dot(size_t n, const SparseArray &y) const;
287 [[nodiscard]] std::map<size_t, value_type> select(size_t n, bool max = false, bool ignore_sign = false) const;
289
291 virtual void zero();
292
294 virtual void error(const std::string &message) const;
295
296 value_type operator[](size_t index) { return (*this->local_buffer())[index]; };
297
298protected:
299 virtual void _divide(const DistrArray &y, const DistrArray &z, value_type shift, bool append, bool negative);
300};
301
302namespace util {
303template <typename T, class Compare>
305 constexpr bool operator()(const T &lhs, const T &rhs) const { return Compare()(std::abs(lhs), std::abs(rhs)); }
306};
307template <class Compare>
308[[nodiscard]] std::list<std::pair<DistrArray::index_type, DistrArray::value_type>> extrema(const DistrArray &x, int n);
309std::map<size_t, double> select_max_dot_broadcast(size_t n, std::map<size_t, double> &local_selection,
310 MPI_Comm communicator);
311} // namespace util
312} // namespace molpro::linalg::array
313
314#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_DISTRARRAY_H
Provides access to the local portion of the array.
Definition: DistrArray.h:123
bool compatible(const LocalBuffer &other) const
Checks that the current and the other buffers correspond to the same section of their respective arra...
Definition: DistrArray.h:129
size_type m_start
index of first element of local buffer in the array
Definition: DistrArray.h:134
friend void swap(LocalBuffer &, LocalBuffer &)=delete
size_type start() const
Return index to the start of the local buffer section in the distributed array.
Definition: DistrArray.h:131
Array distributed across many processes supporting remote-memory-access, access to process local buff...
Definition: DistrArray.h:90
double value_type
Definition: DistrArray.h:93
virtual void _divide(const DistrArray &y, const DistrArray &z, value_type shift, bool append, bool negative)
Definition: DistrArray.cpp:140
virtual void zero()
Set all local elements to zero.
Definition: DistrArray.cpp:41
virtual void fill(value_type val)
Definition: DistrArray.cpp:43
virtual void recip()
Take element-wise reciprocal of this. Local. No checks are made for zero values.
Definition: DistrArray.cpp:91
void distributed_array
a compile time tag that this is a distributed array
Definition: DistrArray.h:92
virtual void copy(const DistrArray &y)
Definition: DistrArray.cpp:391
std::map< size_t, value_type > select_max_dot(size_t n, const DistrArray &y) const
Definition: DistrArray.cpp:233
bool compatible(const DistrArray &other) const
Checks that arrays are of the same dimensionality.
Definition: DistrArray.cpp:25
std::list< std::pair< index_type, value_type > > min_n(int n) const
returns n smallest elements in array x Collective operation, must be called by all processes in the g...
Definition: DistrArray.cpp:368
virtual void axpy(value_type a, const DistrArray &y)
this[:] += a * y[:]. Throws an error if any array is empty. Add a multiple of another array to this o...
Definition: DistrArray.cpp:49
MPI_Comm m_communicator
Definition: DistrArray.h:100
virtual std::vector< value_type > vec() const =0
Copies the whole buffer into a vector. Blocking.
virtual void get(index_type lo, index_type hi, value_type *buf) const =0
Gets buffer[lo:hi) from global array (hi is past-the-end). Blocking.
virtual void put(index_type lo, index_type hi, const value_type *data)=0
array[lo:hi) = data[:] (hi is past-the-end). Blocking
std::list< std::pair< index_type, value_type > > max_n(int n) const
returns n largest elements in array x Collective operation, must be called by all processes in the gr...
Definition: DistrArray.cpp:372
virtual std::vector< value_type > get(index_type lo, index_type hi) const =0
std::map< index_type, double > SparseArray
Definition: DistrArray.h:95
virtual void set(index_type ind, value_type val)=0
Set one element to a scalar. Global operation.
virtual value_type dot(const DistrArray &y) const
Scalar product of two arrays. Collective. Throws error if any array is empty. Both arrays should be p...
Definition: DistrArray.cpp:124
std::list< std::pair< index_type, value_type > > min_abs_n(int n) const
returns n elements that are largest by absolute value in array x Collective operation,...
Definition: DistrArray.cpp:376
std::map< size_t, value_type > select(size_t n, bool max=false, bool ignore_sign=false) const
Definition: DistrArray.cpp:263
virtual const Distribution & distribution() const =0
Access distribution of the array among processes.
virtual std::unique_ptr< LocalBuffer > local_buffer()=0
Access the buffer local to this process.
virtual void times(const DistrArray &y)
this[i] *= y[i]. Throws error if any array is empty.
Definition: DistrArray.cpp:97
index_type m_dimension
number of elements in the array
Definition: DistrArray.h:99
virtual void scatter_acc(std::vector< index_type > &indices, const std::vector< value_type > &data)=0
array[indices[i]] += vals[i] Accumulates vals of elements into discontinuous indices of array....
virtual std::vector< value_type > gather(const std::vector< index_type > &indices) const =0
gets elements with discontinuous indices from array. Blocking
virtual void sync() const
Synchronizes all process in this group and ensures any outstanding operations on the array have compl...
Definition: DistrArray.cpp:14
virtual void scal(value_type a)
Scale by a constant. Local.
Definition: DistrArray.cpp:73
size_t size() const
total number of elements, same as overall dimension of array
Definition: DistrArray.h:113
MPI_Comm communicator() const
return a copy of the communicator
Definition: DistrArray.h:109
void divide(const DistrArray &y, const DistrArray &z, value_type shift=0, bool append=false, bool negative=false)
this[i] = y[i]/(z[i]+shift). Collective. Throws error if any array is empty.
Definition: DistrArray.h:245
value_type operator[](size_t index)
Definition: DistrArray.h:296
virtual void error(const std::string &message) const
stops application with an error
Definition: DistrArray.cpp:16
virtual void add(const DistrArray &y)
Add another array to this. Local. Throws error if any array is empty.
Definition: DistrArray.cpp:79
std::vector< index_type > min_loc_n(int n) const
find the index of n smallest components in array x Collective operation, must be called by all proces...
Definition: DistrArray.cpp:384
virtual std::unique_ptr< const LocalBuffer > local_buffer() const =0
virtual value_type at(index_type ind) const =0
virtual void copy_patch(const DistrArray &y, index_type start, index_type end)
Copies elements in a patch of y. If both arrays are empty than does nothing. If only one is empty,...
Definition: DistrArray.cpp:403
virtual void acc(index_type lo, index_type hi, const value_type *data)=0
array[lo:hi) += scaling_constant * data[:] (hi is past-the-end). Blocking
size_t index_type
Definition: DistrArray.h:94
virtual void scatter(const std::vector< index_type > &indices, const std::vector< value_type > &data)=0
array[indices[i]] = data[i] Puts vals of elements with discontinuous indices of array....
virtual void sub(const DistrArray &y)
Subtract another array from this. Local. Throws error if any array is empty.
Definition: DistrArray.cpp:87
std::list< std::pair< index_type, value_type > > max_abs_n(int n) const
returns n elements that are largest by absolute value in array x Collective operation,...
Definition: DistrArray.cpp:380
Non-owning container taking a pointer to the data buffer and its size and exposing routines for itera...
Definition: Span.h:28
size_type size() const
Definition: Span.h:74
Specifies distribution of a contiguous array into non-overlapping chunks.
Definition: Distribution.h:16
auto end(Span< T > &x)
Definition: Span.h:94
std::map< size_t, double > select_max_dot_broadcast(size_t n, std::map< size_t, double > &local_selection, MPI_Comm communicator)
Definition: DistrArray.cpp:170
std::list< std::pair< DistrArray::index_type, DistrArray::value_type > > extrema(const DistrArray &x, int n)
Definition: DistrArray.cpp:280
Definition: ArrayHandler.h:22
Definition: DistrArray.h:304
constexpr bool operator()(const T &lhs, const T &rhs) const
Definition: DistrArray.h:305