/* * (C) Copyright 2024- ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ #pragma once #include #include #include #include #include "atlas/library/defines.h" #if ATLAS_HAVE_EIGEN ATLAS_SUPPRESS_WARNINGS_PUSH ATLAS_SUPPRESS_WARNINGS_INTEGER_SIGN_CHANGE ATLAS_SUPPRESS_WARNINGS_CODE_IS_UNREACHABLE ATLAS_SUPPRESS_WARNINGS_UNUSED_BUT_SET_VARIABLE #include ATLAS_SUPPRESS_WARNINGS_POP #endif #include "atlas/array.h" #include "atlas/linalg/sparse/SparseMatrixStorage.h" namespace atlas { namespace linalg { //---------------------------------------------------------------------------------------------------------------------- #if ATLAS_HAVE_EIGEN template void host_copy_eigen (const InputT* input_data, atlas::array::Array& output) { auto size = output.size(); OutputT* output_data = output.host_data(); std::copy( input_data, input_data + size, output_data ); } template SparseMatrixStorage make_sparse_matrix_storage(Eigen::SparseMatrix&& m) { std::size_t rows = m.rows(); std::size_t cols = m.cols(); std::size_t nnz = m.nonZeros(); array::Array* outer; array::Array* inner; array::Array* value; { array::label label{"sparse_matrix.outer"}; outer = atlas::array::Array::wrap(const_cast(m.outerIndexPtr()), atlas::array::make_shape(rows+1)); } { array::label label{"sparse_matrix.inner"}; inner = atlas::array::Array::wrap(const_cast(m.innerIndexPtr()), atlas::array::make_shape(nnz)); } { array::label label{"sparse_matrix.value"}; value = atlas::array::Array::wrap(const_cast(m.valuePtr()), atlas::array::make_shape(nnz)); } using EigenMatrix = Eigen::SparseMatrix; auto m_ptr = std::make_shared(); m_ptr->swap(m); auto storage = std::make_any>(std::move(m_ptr)); return SparseMatrixStorage::make( rows, cols, nnz, std::unique_ptr(value), std::unique_ptr(inner), std::unique_ptr(outer), std::move(storage) ); } template< typename value_type, typename index_type = eckit::linalg::Index, typename Value, typename Index, typename = std::enable_if_t < !std::is_same_v>> SparseMatrixStorage make_sparse_matrix_storage(Eigen::SparseMatrix&& m) { if (std::is_same_v && std::is_same_v) { return make_sparse_matrix_storage(std::move(m)); } std::size_t rows = m.rows(); std::size_t cols = m.cols(); std::size_t nnz = m.nonZeros(); array::Array* outer; array::Array* inner; array::Array* value; { array::label label{"sparse_matrix.outer"}; outer = atlas::array::Array::create(rows+1); } { array::label label{"sparse_matrix.inner"}; inner = atlas::array::Array::create(nnz); } { array::label label{"sparse_matrix.value"}; value = atlas::array::Array::create(nnz); } host_copy_eigen(m.outerIndexPtr(), *outer); host_copy_eigen(m.innerIndexPtr(), *inner); host_copy_eigen(m.valuePtr(), *value); return SparseMatrixStorage::make( rows, cols, nnz, std::unique_ptr(value), std::unique_ptr(inner), std::unique_ptr(outer), std::any() ); } template SparseMatrixStorage make_sparse_matrix_storage(const Eigen::SparseMatrix& m) { std::size_t rows = m.rows(); std::size_t cols = m.cols(); std::size_t nnz = m.nonZeros(); array::Array* outer; array::Array* inner; array::Array* value; { array::label label{"sparse_matrix.outer"}; outer = atlas::array::Array::create(rows+1); } { array::label label{"sparse_matrix.inner"}; inner = atlas::array::Array::create(nnz); } { array::label label{"sparse_matrix.value"}; value = atlas::array::Array::create(nnz); } host_copy_eigen(m.outerIndexPtr(), *outer); host_copy_eigen(m.innerIndexPtr(), *inner); host_copy_eigen(m.valuePtr(), *value); return SparseMatrixStorage::make( rows, cols, nnz, std::unique_ptr(value), std::unique_ptr(inner), std::unique_ptr(outer), std::any() ); } template< typename value_type, typename index_type = eckit::linalg::Index, typename Value, typename Index, typename = std::enable_if_t < !std::is_same_v>> SparseMatrixStorage make_sparse_matrix_storage(const Eigen::SparseMatrix& m) { std::size_t rows = m.rows(); std::size_t cols = m.cols(); std::size_t nnz = m.nonZeros(); array::Array* outer; array::Array* inner; array::Array* value; { array::label label{"sparse_matrix.outer"}; outer = atlas::array::Array::create(rows+1); } { array::label label{"sparse_matrix.inner"}; inner = atlas::array::Array::create(nnz); } { array::label label{"sparse_matrix.value"}; value = atlas::array::Array::create(nnz); } host_copy_eigen(m.outerIndexPtr(), *outer); host_copy_eigen(m.innerIndexPtr(), *inner); host_copy_eigen(m.valuePtr(), *value); return SparseMatrixStorage::make( rows, cols, nnz, std::unique_ptr(value), std::unique_ptr(inner), std::unique_ptr(outer), std::any() ); } #endif //---------------------------------------------------------------------------------------------------------------------- } // namespace linalg } // namespace atlas