Debian Codegraph @

src/atlas/linalg/sparse/MakeSparseMatrixStorageEigen.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 <any>
14 #include <memory>
15 #include <algorithm>
16 #include <type_traits>
17
18 #include "atlas/library/defines.h"
19 #if ATLAS_HAVE_EIGEN
20 ATLAS_SUPPRESS_WARNINGS_PUSH
21 ATLAS_SUPPRESS_WARNINGS_INTEGER_SIGN_CHANGE
22 ATLAS_SUPPRESS_WARNINGS_CODE_IS_UNREACHABLE
23 ATLAS_SUPPRESS_WARNINGS_UNUSED_BUT_SET_VARIABLE
24 #include <Eigen/Sparse>
25 ATLAS_SUPPRESS_WARNINGS_POP
26 #endif
27
28 #include "atlas/array.h"
29 #include "atlas/linalg/sparse/SparseMatrixStorage.h"
30
31 namespace atlas {
32 namespace linalg {
33
34 //----------------------------------------------------------------------------------------------------------------------
35
36 #if ATLAS_HAVE_EIGEN
37
38 template<typename OutputT, typename InputT>
39 void host_copy_eigen (const InputT* input_data, atlas::array::Array& output) {
40 auto size = output.size();
41 OutputT* output_data = output.host_data<OutputT>();
42 std::copy( input_data, input_data + size, output_data );
43 }
44
45 template <typename Value, typename Index>
46 SparseMatrixStorage make_sparse_matrix_storage(Eigen::SparseMatrix<Value, Eigen::RowMajor, Index>&& m) {
47
48 std::size_t rows = m.rows();
49 std::size_t cols = m.cols();
50 std::size_t nnz = m.nonZeros();
51 array::Array* outer;
52 array::Array* inner;
53 array::Array* value;
54 {
55 array::label label{"sparse_matrix.outer"};
56 outer = atlas::array::Array::wrap(const_cast<Index*>(m.outerIndexPtr()), atlas::array::make_shape(rows+1));
57 }
58 {
59 array::label label{"sparse_matrix.inner"};
60 inner = atlas::array::Array::wrap(const_cast<Index*>(m.innerIndexPtr()), atlas::array::make_shape(nnz));
61 }
62 {
63 array::label label{"sparse_matrix.value"};
64 value = atlas::array::Array::wrap(const_cast<Value*>(m.valuePtr()), atlas::array::make_shape(nnz));
65 }
66
67 using EigenMatrix = Eigen::SparseMatrix<Value, Eigen::RowMajor, Index>;
68 auto m_ptr = std::make_shared<EigenMatrix>();
69 m_ptr->swap(m);
70 auto storage = std::make_any<std::shared_ptr<EigenMatrix>>(std::move(m_ptr));
71
72 return SparseMatrixStorage::make( rows, cols, nnz,
73 std::unique_ptr<atlas::array::Array>(value),
74 std::unique_ptr<atlas::array::Array>(inner),
75 std::unique_ptr<atlas::array::Array>(outer),
76 std::move(storage)
77 );
78 }
79
80 template< typename value_type, typename index_type = eckit::linalg::Index, typename Value, typename Index,
81 typename = std::enable_if_t < !std::is_same_v<value_type,Value>>>
82 SparseMatrixStorage make_sparse_matrix_storage(Eigen::SparseMatrix<Value, Eigen::RowMajor, Index>&& m) {
83
84 if (std::is_same_v<Value, value_type> && std::is_same_v<Index, index_type>) {
85 return make_sparse_matrix_storage(std::move(m));
86 }
87 std::size_t rows = m.rows();
88 std::size_t cols = m.cols();
89 std::size_t nnz = m.nonZeros();
90 array::Array* outer;
91 array::Array* inner;
92 array::Array* value;
93 {
94 array::label label{"sparse_matrix.outer"};
95 outer = atlas::array::Array::create<index_type>(rows+1);
96 }
97 {
98 array::label label{"sparse_matrix.inner"};
99 inner = atlas::array::Array::create<index_type>(nnz);
100 }
101 {
102 array::label label{"sparse_matrix.value"};
103 value = atlas::array::Array::create<value_type>(nnz);
104 }
105
106 host_copy_eigen<index_type>(m.outerIndexPtr(), *outer);
107 host_copy_eigen<index_type>(m.innerIndexPtr(), *inner);
108 host_copy_eigen<value_type>(m.valuePtr(), *value);
109
110 return SparseMatrixStorage::make( rows, cols, nnz,
111 std::unique_ptr<atlas::array::Array>(value),
112 std::unique_ptr<atlas::array::Array>(inner),
113 std::unique_ptr<atlas::array::Array>(outer),
114 std::any()
115 );
116 }
117
118
119 template <typename Value, typename Index>
120 SparseMatrixStorage make_sparse_matrix_storage(const Eigen::SparseMatrix<Value, Eigen::RowMajor, Index>& m) {
121 std::size_t rows = m.rows();
122 std::size_t cols = m.cols();
123 std::size_t nnz = m.nonZeros();
124 array::Array* outer;
125 array::Array* inner;
126 array::Array* value;
127 {
128 array::label label{"sparse_matrix.outer"};
129 outer = atlas::array::Array::create<Index>(rows+1);
130 }
131 {
132 array::label label{"sparse_matrix.inner"};
133 inner = atlas::array::Array::create<Index>(nnz);
134 }
135 {
136 array::label label{"sparse_matrix.value"};
137 value = atlas::array::Array::create<Value>(nnz);
138 }
139
140 host_copy_eigen<Index>(m.outerIndexPtr(), *outer);
141 host_copy_eigen<Index>(m.innerIndexPtr(), *inner);
142 host_copy_eigen<Value>(m.valuePtr(), *value);
143
144 return SparseMatrixStorage::make( rows, cols, nnz,
145 std::unique_ptr<atlas::array::Array>(value),
146 std::unique_ptr<atlas::array::Array>(inner),
147 std::unique_ptr<atlas::array::Array>(outer),
148 std::any()
149 );
150 }
151
152 template< typename value_type, typename index_type = eckit::linalg::Index, typename Value, typename Index,
153 typename = std::enable_if_t < !std::is_same_v<value_type,Value>>>
154 SparseMatrixStorage make_sparse_matrix_storage(const Eigen::SparseMatrix<Value, Eigen::RowMajor, Index>& m) {
155
156 std::size_t rows = m.rows();
157 std::size_t cols = m.cols();
158 std::size_t nnz = m.nonZeros();
159 array::Array* outer;
160 array::Array* inner;
161 array::Array* value;
162 {
163 array::label label{"sparse_matrix.outer"};
164 outer = atlas::array::Array::create<index_type>(rows+1);
165 }
166 {
167 array::label label{"sparse_matrix.inner"};
168 inner = atlas::array::Array::create<index_type>(nnz);
169 }
170 {
171 array::label label{"sparse_matrix.value"};
172 value = atlas::array::Array::create<value_type>(nnz);
173 }
174 host_copy_eigen<index_type>(m.outerIndexPtr(), *outer);
175 host_copy_eigen<index_type>(m.innerIndexPtr(), *inner);
176 host_copy_eigen<value_type>(m.valuePtr(), *value);
177
178 return SparseMatrixStorage::make( rows, cols, nnz,
179 std::unique_ptr<atlas::array::Array>(value),
180 std::unique_ptr<atlas::array::Array>(inner),
181 std::unique_ptr<atlas::array::Array>(outer),
182 std::any()
183 );
184 }
185
186 #endif
187
188 //----------------------------------------------------------------------------------------------------------------------
189
190 } // namespace linalg
191 } // namespace atlas