iterative-solver 0.0
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
9
10namespace molpro::linalg::array {
11template <typename AL, typename AR, bool = has_mapped_type_v<AR>>
12class ArrayHandlerDistrSparse : public ArrayHandler<AL, AR> {};
13
17template <typename AL, typename AR>
18class ArrayHandlerDistrSparse<AL, AR, true> : public ArrayHandler<AL, AR> {
19public:
25
26 AL copy(const AR &source) override {
27 throw std::logic_error("General construction of sparse from dense is ill-defined");
28 };
29
30 void copy(AL &x, const AR &y) override {
31 // static_assert(true, "General copy from sparse to dense is ill-defined");
32 x.fill(0);
33 auto lb = x.local_buffer();
34 auto start = lb->start();
35 for (const auto &v : y)
36 if (v.first >= start and v.first < start + lb->size())
37 (*lb)[v.first - start] = v.second;
38 };
39
40 void scal(value_type alpha, AL &x) override { static_assert(true, "Use ArrayHandlerDistr for unary operations"); };
41
42 void fill(value_type alpha, AL &x) override { static_assert(true, "Use ArrayHandlerDistr for unary operations"); };
43
44 // TODO For now these functions are in DistrArray, but they should be moved into the handler.
45 void axpy(value_type alpha, const AR &x, AL &y) override {
46 this->m_counter->axpy++;
47 y.axpy(alpha, x);
48 };
49
50 value_type dot(const AL &x, const AR &y) override {
51 this->m_counter->dot++;
52 return x.dot(y);
53 };
54
55 void gemm_outer(const Matrix<value_type> alphas, const CVecRef<AR> &xx, const VecRef<AL> &yy) override {
56 this->m_counter->gemm_outer++;
57 gemm_outer_distr_sparse(alphas, xx, yy);
58 }
59
60 Matrix<value_type> gemm_inner(const CVecRef<AL> &xx, const CVecRef<AR> &yy) override {
61 this->m_counter->gemm_inner++;
62 return gemm_inner_distr_sparse(xx, yy);
63 }
64
65 std::map<size_t, value_type_abs> select_max_dot(size_t n, const AL &x, const AR &y) override {
66 return x.select_max_dot(n, y);
67 }
68
69 std::map<size_t, value_type_abs> select(size_t n, const AL &x, bool max = false, bool ignore_sign = false) override {
70 return x.select(n, max, ignore_sign);
71 }
72
73 ProxyHandle lazy_handle() override { return this->lazy_handle(*this); };
74
75protected:
76 using ArrayHandler<AL, AR>::error;
77 using ArrayHandler<AL, AR>::lazy_handle;
78 using ArrayHandler<AL, AR>::m_lazy_handles;
79};
80
81} // namespace molpro::linalg::array
82
83#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:73
void fill(value_type alpha, AL &x) override
Definition: ArrayHandlerDistrSparse.h:42
void scal(value_type alpha, AL &x) override
Definition: ArrayHandlerDistrSparse.h:40
void copy(AL &x, const AR &y) override
Copy content of y into x.
Definition: ArrayHandlerDistrSparse.h:30
value_type dot(const AL &x, const AR &y) override
Definition: ArrayHandlerDistrSparse.h:50
void gemm_outer(const Matrix< value_type > alphas, const CVecRef< AR > &xx, const VecRef< AL > &yy) override
Definition: ArrayHandlerDistrSparse.h:55
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:65
void axpy(value_type alpha, const AR &x, AL &y) override
Definition: ArrayHandlerDistrSparse.h:45
Matrix< value_type > gemm_inner(const CVecRef< AL > &xx, const CVecRef< AR > &yy) override
Definition: ArrayHandlerDistrSparse.h:60
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:69
AL copy(const AR &source) override
Definition: ArrayHandlerDistrSparse.h:26
Definition: ArrayHandlerDistrSparse.h:12
Enhances various operations between pairs of arrays and allows dynamic code injection with uniform in...
Definition: ArrayHandler.h:162
std::unique_ptr< Counter > m_counter
Definition: ArrayHandler.h:176
std::vector< std::weak_ptr< LazyHandle > > m_lazy_handles
keeps track of all created lazy handles
Definition: ArrayHandler.h:416
decltype(value_type_L{} *value_type_R{}) value_type
Definition: ArrayHandler.h:181
virtual ProxyHandle lazy_handle()=0
Returns a lazy handle. Most implementations simply need to call the overload: return lazy_handle(*thi...
virtual void error(const std::string &message)
Throws an error.
Definition: ArrayHandler.h:268
Matrix container that allows simple data access, slicing, copying and resizing without loosing data.
Definition: Matrix.h:28
Matrix< typename array::mapped_or_value_type_t< AL > > gemm_inner_distr_sparse(const CVecRef< AL > &xx, const CVecRef< AR > &yy)
Definition: gemm.h:227
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:208
Definition: ArrayHandler.h:22