/* * (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 "eckit/linalg/SparseMatrix.h" #include "atlas/array.h" #include "atlas/linalg/sparse/SparseMatrixStorage.h" namespace atlas { namespace linalg { //---------------------------------------------------------------------------------------------------------------------- template void host_copy_eckit (const InputT* input_data, array::Array& output) { auto size = output.size(); OutputT* output_data = output.host_data(); std::copy( input_data, input_data + size, output_data ); } inline SparseMatrixStorage make_sparse_matrix_storage(eckit::linalg::SparseMatrix&& m) { using Index = eckit::linalg::Index; using Value = eckit::linalg::Scalar; 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 = array::Array::wrap(const_cast(m.outer()), array::make_shape(rows+1)); } { array::label label{"sparse_matrix.inner"}; inner = array::Array::wrap(const_cast(m.inner()), array::make_shape(nnz)); } { array::label label{"sparse_matrix.value"}; value = array::Array::wrap(const_cast(m.data()), array::make_shape(nnz)); } // We now move the eckit::linalg::SparseMatrix into a generic storage so // the wrapped array data does not go out of scope auto storage = std::make_any(std::move(m)); 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> SparseMatrixStorage make_sparse_matrix_storage(eckit::linalg::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 = array::Array::create(rows+1); } { array::label label{"sparse_matrix.inner"}; inner = array::Array::create(nnz); } { array::label label{"sparse_matrix.value"}; value = array::Array::create(nnz); } host_copy_eckit(m.outer(), *outer); host_copy_eckit(m.inner(), *inner); host_copy_eckit(m.data(), *value); return SparseMatrixStorage::make( rows, cols, nnz, std::unique_ptr(value), std::unique_ptr(inner), std::unique_ptr(outer), std::any() ); } inline SparseMatrixStorage make_sparse_matrix_storage(const eckit::linalg::SparseMatrix& m) { using Index = eckit::linalg::Index; using Value = eckit::linalg::Scalar; 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 = array::Array::create(rows+1); } { array::label label{"sparse_matrix.inner"}; inner = array::Array::create(nnz); } { array::label label{"sparse_matrix.value"}; value = array::Array::create(nnz); } host_copy_eckit(m.outer(), *outer); host_copy_eckit(m.inner(), *inner); host_copy_eckit(m.data(), *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> SparseMatrixStorage make_sparse_matrix_storage(const eckit::linalg::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 = array::Array::create(rows+1); } { array::label label{"sparse_matrix.inner"}; inner = array::Array::create(nnz); } { array::label label{"sparse_matrix.value"}; value = array::Array::create(nnz); } host_copy_eckit(m.outer(), *outer); host_copy_eckit(m.inner(), *inner); host_copy_eckit(m.data(), *value); return SparseMatrixStorage::make( rows, cols, nnz, std::unique_ptr(value), std::unique_ptr(inner), std::unique_ptr(outer), std::any() ); } //---------------------------------------------------------------------------------------------------------------------- } // namespace linalg } // namespace atlas