1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_DISTRARRAY_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_DISTRARRAY_H
15#include <molpro/linalg/array/Span.h>
16#include <molpro/linalg/array/util/Distribution.h>
30 template<
typename Array>
41 template <
typename OtherArray>
43 return *
this =
static_cast<typename OtherArray::value_type
>(other);
47 m_arr.set(m_idx, std::move(val));
52 operator typename Array::value_type()
const {
53 return m_arr.at(m_idx);
57 std::add_lvalue_reference_t<Array> m_arr;
58 Array::index_type m_idx;
60 friend std::remove_const_t<Array>;
62 ValueProxy(std::add_lvalue_reference_t<Array> arr, Array::index_type idx) : m_arr(arr), m_idx(idx) {}
149 DistrArray(
size_t dimension, MPI_Comm commun);
163 virtual void sync()
const;
192 [[nodiscard]]
virtual std::unique_ptr<const LocalBuffer>
local_buffer()
const = 0;
221 [[nodiscard]]
virtual std::vector<value_type>
gather(
const std::vector<index_type> &indices)
const = 0;
226 virtual void scatter(
const std::vector<index_type> &indices,
const std::vector<value_type> &data) = 0;
232 virtual void scatter_acc(std::vector<index_type> &indices,
const std::vector<value_type> &data) = 0;
237 [[nodiscard]]
virtual std::vector<value_type>
vec()
const = 0;
273 virtual void recip();
303 bool negative =
false) {
304 _divide(y, z, shift, append, negative);
312 [[nodiscard]] std::list<std::pair<index_type, value_type>>
min_n(
int n)
const;
319 [[nodiscard]] std::list<std::pair<index_type, value_type>>
max_n(
int n)
const;
326 [[nodiscard]] std::list<std::pair<index_type, value_type>>
min_abs_n(
int n)
const;
333 [[nodiscard]] std::list<std::pair<index_type, value_type>>
max_abs_n(
int n)
const;
340 [[nodiscard]] std::vector<index_type>
min_loc_n(
int n)
const;
344 [[nodiscard]] std::map<size_t, value_type>
select(
size_t n,
bool max =
false,
bool ignore_sign =
false)
const;
351 virtual void error(
const std::string &message)
const;
358template <
typename T,
class Compare>
360 constexpr bool operator()(
const T &lhs,
const T &rhs)
const {
return Compare()(std::abs(lhs), std::abs(rhs)); }
362template <
class Compare>
363[[nodiscard]] std::list<std::pair<DistrArray::index_type, DistrArray::value_type>>
extrema(
const DistrArray &x,
int n);
365 MPI_Comm communicator);
367template<
bool is_const>
434 std::conditional_t<is_const, const DistrArray, DistrArray> *m_arr =
nullptr;
438static_assert(std::random_access_iterator<DistrArrayIteratorImpl<true>>);
439static_assert(std::random_access_iterator<DistrArrayIteratorImpl<false>>);
Provides access to the local portion of the array.
Definition DistrArray.h:175
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:181
size_type m_start
index of first element of local buffer in the array
Definition DistrArray.h:186
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:183
virtual ~LocalBuffer()=default
Array distributed across many processes supporting remote-memory-access, access to process local buff...
Definition DistrArray.h:137
double value_type
Definition DistrArray.h:140
virtual void _divide(const DistrArray &y, const DistrArray &z, value_type shift, bool append, bool negative)
Definition DistrArray.cpp:241
virtual void zero()
Set all local elements to zero.
Definition DistrArray.cpp:142
virtual void fill(value_type val)
Definition DistrArray.cpp:144
virtual void recip()
Take element-wise reciprocal of this. Local. No checks are made for zero values.
Definition DistrArray.cpp:194
void distributed_array
a compile time tag that this is a distributed array
Definition DistrArray.h:139
virtual void copy(const DistrArray &y)
Definition DistrArray.cpp:507
std::map< size_t, value_type > select_max_dot(size_t n, const DistrArray &y) const
Definition DistrArray.cpp:335
bool compatible(const DistrArray &other) const
Checks that arrays are of the same dimensionality.
Definition DistrArray.cpp:118
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:484
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:150
MPI_Comm m_communicator
Definition DistrArray.h:147
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:488
virtual std::vector< value_type > get(index_type lo, index_type hi) const =0
std::map< index_type, double > SparseArray
Definition DistrArray.h:142
virtual size_t disk_page_size() const
Definition DistrArray.h:197
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:224
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:492
std::map< size_t, value_type > select(size_t n, bool max=false, bool ignore_sign=false) const
Definition DistrArray.cpp:371
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:201
index_type m_dimension
number of elements in the array
Definition DistrArray.h:146
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....
DistrArray(const DistrArray &)=default
virtual ~DistrArray()=default
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:107
virtual void scal(value_type a)
Scale by a constant. Local.
Definition DistrArray.cpp:174
size_t size() const
total number of elements, same as overall dimension of array
Definition DistrArray.h:165
MPI_Comm communicator() const
return a copy of the communicator
Definition DistrArray.h:161
DistrArray(DistrArray &&) noexcept=default
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:302
virtual void error(const std::string &message) const
stops application with an error
Definition DistrArray.cpp:109
value_type operator[](index_type ind) const
Definition DistrArray.cpp:134
virtual void add(const DistrArray &y)
Add another array to this. Local. Throws error if any array is empty.
Definition DistrArray.cpp:181
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:500
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:516
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:141
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:190
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:496
Non-owning container taking a pointer to the data buffer and its size and exposing routines for itera...
Definition Span.h:31
size_t size_type
Definition Span.h:37
size_type size() const
Definition Span.h:77
Definition DistrArray.h:368
DistrArrayIteratorImpl & operator++()
Definition DistrArray.h:397
DistrArrayIteratorImpl()=default
DistrArrayIteratorImpl & operator+=(difference_type val)
Definition DistrArray.h:387
reference operator*() const
Definition DistrArray.h:424
DistrArrayIteratorImpl & operator-=(difference_type val)
Definition DistrArray.h:407
std::strong_ordering operator<=>(const DistrArrayIteratorImpl &) const =default
std::random_access_iterator_tag iterator_category
Definition DistrArray.h:381
friend DistrArrayIteratorImpl operator+(const DistrArrayIteratorImpl &it, difference_type val)
Definition DistrArray.h:391
difference_type operator-(const DistrArrayIteratorImpl &other) const
Definition DistrArray.h:428
friend DistrArrayIteratorImpl operator-(const DistrArrayIteratorImpl &it, difference_type val)
Definition DistrArray.h:411
value_type reference
Definition DistrArray.h:372
DistrArrayIteratorImpl(std::conditional_t< is_const, const DistrArray, DistrArray > &array, DistrArray::index_type pos)
Definition DistrArray.h:384
bool operator==(const DistrArrayIteratorImpl &) const =default
DistrArrayIteratorImpl operator++(int)
Definition DistrArray.h:401
std::conditional_t< is_const, DistrArray::value_type, ValueProxy< DistrArray > > value_type
Definition DistrArray.h:371
reference const_reference
Definition DistrArray.h:373
void pointer
Definition DistrArray.h:380
friend DistrArrayIteratorImpl operator+(difference_type val, const DistrArrayIteratorImpl &it)
Definition DistrArray.h:394
DistrArrayIteratorImpl operator--(int)
Definition DistrArray.h:418
reference operator[](difference_type offset) const
Definition DistrArray.h:426
std::ptrdiff_t difference_type
Definition DistrArray.h:374
DistrArrayIteratorImpl & operator--()
Definition DistrArray.h:414
Specifies distribution of a contiguous array into non-overlapping chunks.
Definition Distribution.h:16
Definition DistrArray.h:31
ValueProxy(ValueProxy &&)=delete
ValueProxy(const ValueProxy &)=delete
ValueProxy & operator=(const ValueProxy &other)
Definition DistrArray.h:39
ValueProxy & operator=(ValueProxy &&other)
Definition DistrArray.h:40
ValueProxy & operator=(const ValueProxy< OtherArray > &other)
Definition DistrArray.h:42
ValueProxy & operator=(Array::value_type val)
Definition DistrArray.h:46
auto begin(Span< T > &x)
Definition Span.h:87
auto end(Span< T > &x)
Definition Span.h:97
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:272
std::list< std::pair< DistrArray::index_type, DistrArray::value_type > > extrema(const DistrArray &x, int n)
Definition DistrArray.cpp:394
Definition ArrayHandler.h:19
DistrArrayConstIterator cbegin(const DistrArray &array)
Definition DistrArray.h:450
util::DistrArrayIteratorImpl< true > DistrArrayConstIterator
Definition DistrArray.h:444
DistrArrayConstIterator cend(const DistrArray &array)
Definition DistrArray.h:451
util::DistrArrayIteratorImpl< false > DistrArrayIterator
Definition DistrArray.h:443
Definition DistrArray.h:359
constexpr bool operator()(const T &lhs, const T &rhs) const
Definition DistrArray.h:360