| 1 |
/* |
| 2 |
* (C) Copyright 2013 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 |
#pragma once |
| 12 |
|
| 13 |
#include "eckit/config/Configuration.h" |
| 14 |
|
| 15 |
#include "atlas/linalg/Indexing.h" |
| 16 |
#include "atlas/linalg/View.h" |
| 17 |
#include "atlas/linalg/sparse/Backend.h" |
| 18 |
#include "atlas/linalg/sparse/SparseMatrixView.h" |
| 19 |
#include "atlas/runtime/Exception.h" |
| 20 |
#include "atlas/util/Config.h" |
| 21 |
|
| 22 |
namespace atlas { |
| 23 |
namespace linalg { |
| 24 |
|
| 25 |
using Configuration = eckit::Configuration; |
| 26 |
|
| 27 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 28 |
void sparse_matrix_multiply(const Matrix& matrix, const SourceView& src, TargetView& tgt); |
| 29 |
|
| 30 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 31 |
void sparse_matrix_multiply(const Matrix& matrix, const SourceView& src, TargetView&& tgt); |
| 32 |
|
| 33 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 34 |
void sparse_matrix_multiply(const Matrix& matrix, const SourceView& src, TargetView& tgt, const Configuration& config); |
| 35 |
|
| 36 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 37 |
void sparse_matrix_multiply(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing); |
| 38 |
|
| 39 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 40 |
void sparse_matrix_multiply(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing, |
| 41 |
const Configuration& config); |
| 42 |
|
| 43 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 44 |
void sparse_matrix_multiply_add(const Matrix& matrix, const SourceView& src, TargetView& tgt); |
| 45 |
|
| 46 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 47 |
void sparse_matrix_multiply_add(const Matrix& matrix, const SourceView& src, TargetView& tgt, const Configuration& config); |
| 48 |
|
| 49 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 50 |
void sparse_matrix_multiply_add(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing); |
| 51 |
|
| 52 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 53 |
void sparse_matrix_multiply_add(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing, |
| 54 |
const Configuration& config); |
| 55 |
|
| 56 |
class SparseMatrixMultiply { |
| 57 |
public: |
| 58 |
SparseMatrixMultiply() = default; |
| 59 |
SparseMatrixMultiply(const std::string& backend): backend_{backend} {} |
| 60 |
SparseMatrixMultiply(const sparse::Backend& backend): backend_(backend) {} |
| 61 |
|
| 62 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 63 |
void operator()(const Matrix& matrix, const SourceView& src, TargetView& tgt) const { |
| 64 |
multiply(matrix, src, tgt); |
| 65 |
} |
| 66 |
|
| 67 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 68 |
void operator()(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing indexing) const { |
| 69 |
multiply(matrix, src, tgt, indexing); |
| 70 |
} |
| 71 |
|
| 72 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 73 |
void multiply(const Matrix& matrix, const SourceView& src, TargetView& tgt) const { |
| 74 |
sparse_matrix_multiply(matrix, src, tgt, backend()); |
| 75 |
} |
| 76 |
|
| 77 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 78 |
void multiply(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing indexing) const { |
| 79 |
sparse_matrix_multiply(matrix, src, tgt, indexing, backend()); |
| 80 |
} |
| 81 |
|
| 82 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 83 |
void multiply_add(const Matrix& matrix, const SourceView& src, TargetView& tgt) const { |
| 84 |
sparse_matrix_multiply_add(matrix, src, tgt, backend()); |
| 85 |
} |
| 86 |
|
| 87 |
template <typename Matrix, typename SourceView, typename TargetView> |
| 88 |
void multiply_add(const Matrix& matrix, const SourceView& src, TargetView& tgt, Indexing indexing) const { |
| 89 |
sparse_matrix_multiply_add(matrix, src, tgt, indexing, backend()); |
| 90 |
} |
| 91 |
|
| 92 |
const sparse::Backend& backend() const { return backend_; } |
| 93 |
|
| 94 |
private: |
| 95 |
sparse::Backend backend_; |
| 96 |
}; |
| 97 |
|
| 98 |
namespace sparse { |
| 99 |
|
| 100 |
// Template class which needs (full or partial) specialization for concrete template parameters |
| 101 |
template <typename Backend, Indexing, int Rank, typename MatrixValue, typename SourceValue, typename TargetValue> |
| 102 |
struct SparseMatrixMultiply { |
| 103 |
static void multiply(const SparseMatrixView<MatrixValue>&, const View<SourceValue, Rank>&, View<TargetValue, Rank>&, |
| 104 |
const Configuration&) { |
| 105 |
throw_NotImplemented("SparseMatrixMultiply needs a template specialization with the implementation", Here()); |
| 106 |
} |
| 107 |
static void multiply_add(const SparseMatrixView<MatrixValue>&, const View<SourceValue, Rank>&, View<TargetValue, Rank>&, |
| 108 |
const Configuration&) { |
| 109 |
throw_NotImplemented("SparseMatrixMultiply needs a template specialization with the implementation", Here()); |
| 110 |
} |
| 111 |
}; |
| 112 |
} // namespace sparse |
| 113 |
|
| 114 |
} // namespace linalg |
| 115 |
} // namespace atlas |
| 116 |
|
| 117 |
#include "SparseMatrixMultiply.tcc" |
| 118 |
#include "SparseMatrixMultiply_EckitLinalg.h" |
| 119 |
#include "SparseMatrixMultiply_OpenMP.h" |
| 120 |
#include "SparseMatrixMultiply_HicSparse.h" |