Debian Codegraph @

src/atlas/linalg/sparse/MakeEckitSparseMatrix.h

raw
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 #pragma once
12
13 #include <type_traits>
14
15 #include "eckit/linalg/SparseMatrix.h"
16
17 #include "atlas/linalg/sparse/SparseMatrixView.h"
18 #include "atlas/linalg/sparse/SparseMatrixStorage.h"
19
20 namespace atlas {
21 namespace linalg {
22
23 //----------------------------------------------------------------------------------------------------------------------
24
25 class EckitSparseMatrixNonOwningAllocator : public eckit::linalg::SparseMatrix::Allocator {
26 public:
27 EckitSparseMatrixNonOwningAllocator(const SparseMatrixView<eckit::linalg::Scalar, eckit::linalg::Index>& view) : view_{view} {}
28
29 eckit::linalg::SparseMatrix::Layout allocate(eckit::linalg::SparseMatrix::Shape& shape) override {
30 eckit::linalg::SparseMatrix::Layout p;
31 using Index = eckit::linalg::Index;
32 using OuterIndex = std::remove_pointer_t<decltype(p.outer_)>;
33 using InnerIndex = std::remove_pointer_t<decltype(p.inner_)>;
34 static_assert(sizeof(OuterIndex) == sizeof(Index));
35 static_assert(sizeof(InnerIndex) == sizeof(Index));
36 p.data_ = const_cast<eckit::linalg::Scalar*>(view_.value());
37 p.outer_ = reinterpret_cast<OuterIndex*>(const_cast<Index*>(view_.outer()));
38 p.inner_ = reinterpret_cast<InnerIndex*>(const_cast<Index*>(view_.inner()));
39 shape.size_ = view_.nnz();
40 shape.rows_ = view_.rows();
41 shape.cols_ = view_.cols();
42 return p;
43 }
44
45 void deallocate(eckit::linalg::SparseMatrix::Layout p, eckit::linalg::SparseMatrix::Shape) override {
46 /* do nothing */
47 }
48
49 bool inSharedMemory() const override { return false; }
50
51 void print(std::ostream& out) const override { out << "atlas::linalg::EckitSparseMatrixNonOwningAllocator[rows="<<view_.rows()<<",cols="<<view_.cols()<<",nnz="<<view_.nnz()<<"]"; }
52
53 private:
54 SparseMatrixView<eckit::linalg::Scalar, eckit::linalg::Index> view_;
55 };
56
57 //----------------------------------------------------------------------------------------------------------------------
58
59 // Create new eckit sparsematrix from a SparseMatrixView. Warning, this matrix does not own the data!
60 inline eckit::linalg::SparseMatrix make_non_owning_eckit_sparse_matrix(const SparseMatrixView<eckit::linalg::Scalar, eckit::linalg::Index>& view) {
61 eckit::linalg::SparseMatrix m(new EckitSparseMatrixNonOwningAllocator(view));
62 return m;
63 }
64
65 // Create new eckit sparsematrix from a SparseMatrixStorage. Warning, this matrix does not own the data!
66 inline eckit::linalg::SparseMatrix make_non_owning_eckit_sparse_matrix(const SparseMatrixStorage& m) {
67 return make_non_owning_eckit_sparse_matrix(
68 make_host_view<eckit::linalg::Scalar, eckit::linalg::Index>(m) );
69 }
70
71 inline eckit::linalg::SparseMatrix make_eckit_sparse_matrix(const SparseMatrixView<eckit::linalg::Scalar, eckit::linalg::Index>& view) {
72 eckit::linalg::SparseMatrix m(new EckitSparseMatrixNonOwningAllocator(view));
73 return eckit::linalg::SparseMatrix{m}; // makes copy
74 }
75
76 // Create new eckit sparsematrix from a SparseMatrixStorage. Warning, this creates a copy!
77 inline eckit::linalg::SparseMatrix make_eckit_sparse_matrix(const SparseMatrixStorage& m) {
78 return make_eckit_sparse_matrix(
79 make_host_view<eckit::linalg::Scalar, eckit::linalg::Index>(m) );
80 }
81
82 //----------------------------------------------------------------------------------------------------------------------
83
84 } // namespace linalg
85 } // namespace atlas