iterative-solver 0.0
 
Loading...
Searching...
No Matches
ArrayHandlerDistrSparse.h
1#ifndef LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERDISTRSPARSE_H
2#define LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERDISTRSPARSE_H
3#include "molpro/linalg/array/ArrayHandler.h"
4#include <molpro/linalg/array/util/gemm.h>
5#include <stdexcept>
6
7namespace molpro::linalg::array {
10template <typename AL, typename AR, bool = has_mapped_type_v<AR>>
11class ArrayHandlerDistrSparse : public ArrayHandler<AL, AR> {};
12
16template <typename AL, typename AR>
17class ArrayHandlerDistrSparse<AL, AR, true> : public ArrayHandler<AL, AR> {
18public:
24
25 AL copy(const AR &source) override {
26 throw std::logic_error("General copy from sparse to dense is ill-defined");
27 };
28
29 void copy(AL &x, const AR &y) override {
30 this->m_counter->copy++;
31 // checked before x is overwritten, and on every process, which each hold all of y
32 if (not y.empty() and y.rbegin()->first >= x.size())
33 error("ArrayHandlerDistrSparse::copy() sparse array y is incompatible");
34 x.copy(y);
35 };
36
37 void scal(value_type alpha, AL &x) override { throw std::logic_error("Use ArrayHandlerDistr for unary operations"); };
38
39 void fill(value_type alpha, AL &x) override { throw std::logic_error("Use ArrayHandlerDistr for unary operations"); };
40
41 // TODO For now these functions are in DistrArray, but they should be moved into the handler.
42 void axpy(value_type alpha, const AR &x, AL &y) override {
43 this->m_counter->axpy++;
44 y.axpy(alpha, x);
45 };
46
47 value_type dot(const AL &x, const AR &y) override {
48 this->m_counter->dot++;
49 return x.dot(y);
50 };
51
52 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
53 this->m_counter->gemm_outer++;
54 gemm_outer_distr_sparse(alphas, xx, yy);
55 }
56
57 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
58 this->m_counter->gemm_inner++;
59 return gemm_inner_distr_sparse(xx, yy);
60 }
61
62 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
63 return x.select_max_dot(n, y);
64 }
65
66 std::map<size_t, value_type_abs> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
67 return x.select(n, max, ignore_sign);
68 }
69
70 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
71
72protected:
73 using ArrayHandler<AL, AR>::error;
74 using ArrayHandler<AL, AR>::lazy_handle;
75 using ArrayHandler<AL, AR>::m_lazy_handles;
76};
77
78} // namespace molpro::linalg::array
79
80#endif // LINEARALGEBRA_SRC_MOLPRO_LINALG_ARRAY_ARRAYHANDLERDISTRSPARSE_H
ProxyHandle lazy_handle() override
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
Definition ArrayHandlerDistrSparse.h:70
void fill(value_type alpha, AL &x) override
Definition ArrayHandlerDistrSparse.h:39
void scal(value_type alpha, AL &x) override
Definition ArrayHandlerDistrSparse.h:37
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition ArrayHandlerDistrSparse.h:29
value_type dot(const AL &x, const AR &y) override
The hermitian inner product <x|y>: conjugate-linear in x, linear in y.
Definition ArrayHandlerDistrSparse.h:47
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition ArrayHandlerDistrSparse.h:52
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 ArrayHandlerDistrSparse.h:62
void axpy(value_type alpha, const AR &x, AL &y) override
Definition ArrayHandlerDistrSparse.h:42
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition ArrayHandlerDistrSparse.h:57
std::map< size_t, value_type_abs > 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 ArrayHandlerDistrSparse.h:66
AL copy(const AR &source) override
Definition ArrayHandlerDistrSparse.h:25
Definition ArrayHandlerDistrSparse.h:11
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
std::vector< std::weak_ptr< LazyHandle > > m_lazy_handles
keeps track of all created lazy handles
Definition ArrayHandler.h:424
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
virtual ProxyHandle lazy_handle()=0
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
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
Matrix< typename array::mapped_or_value_type_t< AL > > gemm_inner_distr_sparse(const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition gemm.h:486
void gemm_outer_distr_sparse(const Matrix< typename array::mapped_or_value_type_t< AL > > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy)
Definition gemm.h:473
Definition ArrayHandler.h:19