iterative-solver 0.0
 
Loading...
Searching...
No Matches
ArrayHandlerDDiskIterable.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERDDISKITERABLE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERDDISKITERABLE_H
3#include <molpro/linalg/array/ArrayHandlerIterableDDisk.h>
4#include <molpro/linalg/array/ArrayHandlerWithCopyFunc.h>
5#include <molpro/linalg/array/util/gemm.h>
6#include <molpro/mpi.h>
7#include <stdexcept>
8#include <string>
9#include <type_traits>
10
11namespace molpro::linalg::array {
12
23template <class AL, class AR>
25public:
31 static_assert(is_contiguous_v<AR>, "ArrayHandlerDDiskIterable: the right array must be contiguous");
32 static_assert(std::is_same_v<value_type, typename AL::value_type>,
33 "ArrayHandlerDDiskIterable: the arrays must have the same value type");
34
38 [](const AR &source) { return AL::CreateTempCopy(util::contiguous_span(source), comm_self()); }) {}
39
41 explicit ArrayHandlerDDiskIterable(const std::string &directory)
42 : ArrayHandlerWithCopyFunc<AL, AR>([directory](const AR &source) {
43 return AL::CreateTempCopy(util::contiguous_span(source), comm_self(), directory);
44 }) {}
45
47 ArrayHandlerDDiskIterable(std::function<AL(const AR &)> copy_func)
48 : ArrayHandlerWithCopyFunc<AL, AR>(std::move(copy_func)) {}
49
50 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
51
52 using ArrayHandler<AL, AR>::lazy_handle;
53 using ArrayHandler<AL, AR>::error;
55
56 void copy(AL &x, const AR &y) override {
57 this->m_counter->copy++;
58 x.copy(util::contiguous_span(y));
59 };
60
61 void scal(value_type alpha, AL &x) override {
62 this->m_counter->scal++;
63 x.scal(alpha);
64 }
65
66 void fill(value_type alpha, AL &x) override { x.fill(alpha); }
67
68 void axpy(value_type alpha, const AR &x, AL &y) override {
69 this->m_counter->axpy++;
70 y.axpy(alpha, util::contiguous_span(x));
71 }
72
73 value_type dot(const AL &x, const AR &y) override {
74 this->m_counter->dot++;
75 return x.dot(util::contiguous_span(y));
76 }
77
78 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
79 this->m_counter->gemm_outer++;
81 }
82
83 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
84 this->m_counter->gemm_inner++;
86 }
87
88 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
89 return x.select_max_dot(n, util::contiguous_span(y));
90 }
91
92 std::map<size_t, value_type> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
93 return x.select(n, max, ignore_sign);
94 }
95
96private:
103 static MPI_Comm comm_self() {
104 const auto comm = molpro::mpi::comm_self();
105#ifdef HAVE_MPI_H
106 if (comm == MPI_COMM_NULL)
107 throw std::logic_error("ArrayHandlerDDiskIterable: MPI must be initialised before a distributed array is "
108 "created as a copy of a contiguous one");
109#endif
110 return comm;
111 }
112};
113
114} // namespace molpro::linalg::array
115
116#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERDDISKITERABLE_H
Array handler between a distributed array (left), typically one held on disk, and a contiguous array ...
Definition ArrayHandlerDDiskIterable.h:24
void scal(value_type alpha, AL &x) override
Definition ArrayHandlerDDiskIterable.h:61
value_type dot(const AL &x, const AR &y) override
The hermitian inner product <x|y>: conjugate-linear in x, linear in y.
Definition ArrayHandlerDDiskIterable.h:73
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition ArrayHandlerDDiskIterable.h:83
ArrayHandlerDDiskIterable()
New arrays are created by AL::CreateTempCopy() on molpro::mpi::comm_self(), with any file in its defa...
Definition ArrayHandlerDDiskIterable.h:36
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition ArrayHandlerDDiskIterable.h:50
std::map< size_t, value_type > select(size_t n, const AL &x, bool max=false, bool ignore_sign=false) override
Select n indices with largest (or smallest) actual (or absolute) value.
Definition ArrayHandlerDDiskIterable.h:92
ArrayHandlerDDiskIterable(const std::string &directory)
New arrays are created by AL::CreateTempCopy() on molpro::mpi::comm_self(), with any file in director...
Definition ArrayHandlerDDiskIterable.h:41
ArrayHandlerDDiskIterable(std::function< AL(const AR &)> copy_func)
Constructor taking a copy function, which returns a new distributed array as a copy of a contiguous o...
Definition ArrayHandlerDDiskIterable.h:47
void fill(value_type alpha, AL &x) override
Definition ArrayHandlerDDiskIterable.h:66
void axpy(value_type alpha, const AR &x, AL &y) override
Definition ArrayHandlerDDiskIterable.h:68
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition ArrayHandlerDDiskIterable.h:56
std::map< size_t, value_type_abs > select_max_dot(size_t n, const AL &x, const AR &y) override
Select n indices with largest by absolute value contributions to the dot product.
Definition ArrayHandlerDDiskIterable.h:88
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition ArrayHandlerDDiskIterable.h:78
Base for handlers whose left arrays are created by a function given by the user.
Definition ArrayHandlerWithCopyFunc.h:16
A convenience wrapper around a pointer to the LazyHandle.
Definition ArrayHandler.h:392
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition ArrayHandler.h:163
std::unique_ptr< Counter > m_counter
Definition ArrayHandler.h:177
typename array::mapped_or_value_type_t< AR > value_type_R
Definition ArrayHandler.h:181
typename array::mapped_or_value_type_t< AL > value_type_L
Definition ArrayHandler.h:180
decltype(value_type_L{} *value_type_R{}) value_type
Definition ArrayHandler.h:182
decltype(check_abs< value_type >()) value_type_abs
Definition ArrayHandler.h:183
virtual void error(const std::string &message)
Throws an error.
Definition ArrayHandler.h:276
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition Matrix.h:32
auto contiguous_span(R &r)
A view of all the elements of a contiguous array.
Definition util.h:93
void gemm_outer_contiguous_distr(const Matrix< typename AD::value_type > &alphas, const CVecRef< R > &rr, const VecRef< AD > &qq)
qq += alphas^T rr for contiguous arrays rr and distributed arrays qq. Local.
Definition gemm.h:410
Matrix< typename AD::value_type > gemm_inner_distr_contiguous(const CVecRef< AD > &qq, const CVecRef< R > &rr)
The overlap matrix (qq.size(), rr.size()) between distributed arrays qq and contiguous arrays rr....
Definition gemm.h:383
Definition ArrayHandler.h:19