| 1 |
/* |
| 2 |
* (C) Copyright 2024- ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "SparseMatrixMultiply_EckitLinalg.h" |
| 12 |
|
| 13 |
#include "atlas/array.h" |
| 14 |
#include "atlas/library/config.h" |
| 15 |
|
| 16 |
#if ATLAS_ECKIT_HAVE_ECKIT_585 |
| 17 |
#include "eckit/linalg/LinearAlgebraSparse.h" |
| 18 |
#else |
| 19 |
#include "eckit/linalg/LinearAlgebra.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "eckit/linalg/Matrix.h" |
| 23 |
#include "eckit/linalg/Vector.h" |
| 24 |
|
| 25 |
#include "atlas/runtime/Exception.h" |
| 26 |
#include "atlas/linalg/sparse/MakeEckitSparseMatrix.h" |
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace linalg { |
| 30 |
namespace sparse { |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
#if ATLAS_ECKIT_HAVE_ECKIT_585 |
| 35 |
const eckit::linalg::LinearAlgebraSparse& eckit_linalg_backend(const Configuration& config) { |
| 36 |
std::string backend = "default"; |
| 37 |
config.get("backend", backend); |
| 38 |
|
| 39 |
if (backend == "default") { |
| 40 |
return eckit::linalg::LinearAlgebraSparse::backend(); |
| 41 |
} |
| 42 |
ATLAS_ASSERT(eckit::linalg::LinearAlgebraSparse::hasBackend(backend)); |
| 43 |
return eckit::linalg::LinearAlgebraSparse::getBackend(backend); |
| 44 |
} |
| 45 |
#else |
| 46 |
const eckit::linalg::LinearAlgebra& eckit_linalg_backend(const Configuration& config) { |
| 47 |
std::string backend = "default"; |
| 48 |
config.get("backend", backend); |
| 49 |
if (backend == "default") { |
| 50 |
return eckit::linalg::LinearAlgebra::backend(); |
| 51 |
} |
| 52 |
ATLAS_ASSERT(eckit::linalg::LinearAlgebra::hasBackend(backend)); |
| 53 |
return eckit::linalg::LinearAlgebra::getBackend(backend); |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
template <typename Value, int Rank> |
| 58 |
auto linalg_make_view(atlas::array::ArrayT<double>& array) { |
| 59 |
auto v_array = array::make_view<Value, Rank>(array); |
| 60 |
return atlas::linalg::make_view(v_array); |
| 61 |
} |
| 62 |
|
| 63 |
} // namespace |
| 64 |
|
| 65 |
void SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 1, double, const double, double>::multiply( |
| 66 |
const SparseMatrixView<double>& W, const View<const double, 1>& src, View<double, 1>& tgt, const Configuration& config) { |
| 67 |
ATLAS_ASSERT(src.contiguous()); |
| 68 |
ATLAS_ASSERT(tgt.contiguous()); |
| 69 |
eckit::linalg::Vector v_src(src.data(), src.size()); |
| 70 |
eckit::linalg::Vector v_tgt(tgt.data(), tgt.size()); |
| 71 |
eckit::linalg::SparseMatrix m_W = make_non_owning_eckit_sparse_matrix(W); |
| 72 |
eckit_linalg_backend(config).spmv(m_W, v_src, v_tgt); |
| 73 |
} |
| 74 |
|
| 75 |
void SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 2, double, const double, double>::multiply( |
| 76 |
const SparseMatrixView<double>& W, const View<const double, 2>& src, View<double, 2>& tgt, const Configuration& config) { |
| 77 |
ATLAS_ASSERT(src.contiguous()); |
| 78 |
ATLAS_ASSERT(tgt.contiguous()); |
| 79 |
ATLAS_ASSERT(src.shape(1) >= W.cols()); |
| 80 |
ATLAS_ASSERT(tgt.shape(1) >= W.rows()); |
| 81 |
eckit::linalg::Matrix m_src(src.data(), src.shape(1), src.shape(0)); |
| 82 |
eckit::linalg::Matrix m_tgt(tgt.data(), tgt.shape(1), tgt.shape(0)); |
| 83 |
eckit::linalg::SparseMatrix m_W = make_non_owning_eckit_sparse_matrix(W); |
| 84 |
eckit_linalg_backend(config).spmm(m_W, m_src, m_tgt); |
| 85 |
} |
| 86 |
|
| 87 |
void SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_left, 1, double, const double, double>::multiply( |
| 88 |
const SparseMatrixView<double>& W, const View<const double, 1>& src, View<double, 1>& tgt, const Configuration& config) { |
| 89 |
SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 1, double, const double, double>::multiply(W, src, tgt, |
| 90 |
config); |
| 91 |
} |
| 92 |
|
| 93 |
void SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 1, double, const double, double>::multiply_add( |
| 94 |
const SparseMatrixView<double>& W, const View<const double, 1>& src, View<double, 1>& tgt, const Configuration& config) { |
| 95 |
|
| 96 |
array::ArrayT<double> tmp(src.shape(0)); |
| 97 |
auto v_tmp = linalg_make_view<double, 1>(tmp); |
| 98 |
v_tmp.assign(0.); |
| 99 |
|
| 100 |
SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 1, double, const double, double>::multiply(W, src, v_tmp, config); |
| 101 |
|
| 102 |
for (idx_t t = 0; t < tmp.shape(0); ++t) { |
| 103 |
tgt(t) += v_tmp(t); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 2, double, const double, double>::multiply_add( |
| 108 |
const SparseMatrixView<double>& W, const View<const double, 2>& src, View<double, 2>& tgt, const Configuration& config) { |
| 109 |
|
| 110 |
array::ArrayT<double> tmp(src.shape(0), src.shape(1)); |
| 111 |
auto v_tmp = linalg_make_view<double, 2>(tmp); |
| 112 |
v_tmp.assign(0.); |
| 113 |
|
| 114 |
SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 2, double, const double, double>::multiply(W, src, v_tmp, config); |
| 115 |
|
| 116 |
for (idx_t t = 0; t < tmp.shape(0); ++t) { |
| 117 |
for (idx_t k = 0; k < tmp.shape(1); ++k) { |
| 118 |
tgt(t, k) += v_tmp(t, k); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_left, 1, double, const double, double>::multiply_add( |
| 124 |
const SparseMatrixView<double>& W, const View<const double, 1>& src, View<double, 1>& tgt, const Configuration& config) { |
| 125 |
SparseMatrixMultiply<backend::eckit_linalg, Indexing::layout_right, 1, double, const double, double>::multiply_add(W, src, tgt, config); |
| 126 |
} |
| 127 |
|
| 128 |
} // namespace sparse |
| 129 |
} // namespace linalg |
| 130 |
} // namespace atlas |