| 1 |
/* |
| 2 |
* (C) Copyright 2025- 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 |
* |
| 7 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 8 |
* granted to it by virtue of its status as an intergovernmental organisation nor |
| 9 |
* does it submit to any jurisdiction. |
| 10 |
*/ |
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
#include <numeric> |
| 15 |
|
| 16 |
#include "atlas/linalg/sparse/SparseMatrixStorage.h" |
| 17 |
#include "atlas/linalg/sparse/SparseMatrixView.h" |
| 18 |
#include "atlas/util/DataType.h" |
| 19 |
#include "atlas/runtime/Trace.h" |
| 20 |
|
| 21 |
namespace atlas::linalg { |
| 22 |
|
| 23 |
template<typename ViewIndex, typename ViewValue, typename Index, typename Value> |
| 24 |
void sparse_matrix_to_rows_columns_values(const linalg::SparseMatrixView<ViewValue, ViewIndex>& mat, Index rows[], Index columns[], Value values[]) { |
| 25 |
const auto outer = mat.outer(); |
| 26 |
const auto index = mat.inner(); |
| 27 |
const auto vals = mat.value(); |
| 28 |
|
| 29 |
std::size_t idx = 0; |
| 30 |
for(std::size_t r = 0; r < mat.rows(); ++r) { |
| 31 |
for (auto c = outer[r]; c < outer[r + 1]; ++c) { |
| 32 |
auto col = index[c]; |
| 33 |
rows[idx] = r; |
| 34 |
columns[idx] = col; |
| 35 |
values[idx] = vals[c]; |
| 36 |
++idx; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
template<typename ViewIndex, typename ViewValue, typename Index, typename Value> |
| 43 |
void sparse_matrix_to_rows_columns_values(const linalg::SparseMatrixView<ViewValue, ViewIndex>& mat, std::vector<Index>& rows, std::vector<Index>& columns, std::vector<Value>& values) { |
| 44 |
rows.resize(mat.nnz()); |
| 45 |
columns.resize(mat.nnz()); |
| 46 |
values.resize(mat.nnz()); |
| 47 |
sparse_matrix_to_rows_columns_values(mat, rows.data(), columns.data(), values.data()); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
template<typename Index, typename Value> |
| 52 |
void sparse_matrix_to_rows_columns_values(const SparseMatrixStorage& mat, std::vector<Index>& rows, std::vector<Index>& columns, std::vector<Value>& values) { |
| 53 |
if (mat.inner().datatype().kind() != DataType::kind<Index>() || mat.outer().datatype().kind() != DataType::kind<Index>() ) { |
| 54 |
ATLAS_NOTIMPLEMENTED; |
| 55 |
} |
| 56 |
switch (mat.value().datatype().kind() ) { |
| 57 |
case DataType::kind<double>() : { |
| 58 |
sparse_matrix_to_rows_columns_values( atlas::linalg::make_host_view<double, Index>(mat), rows, columns, values ); |
| 59 |
return; |
| 60 |
} |
| 61 |
case DataType::kind<float>() : { |
| 62 |
sparse_matrix_to_rows_columns_values( atlas::linalg::make_host_view<float, Index>(mat), rows, columns, values ); |
| 63 |
return; |
| 64 |
} |
| 65 |
default: |
| 66 |
ATLAS_NOTIMPLEMENTED; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
template<typename SparseMatrixValue = eckit::linalg::Scalar, typename SparseMatrixIndex = eckit::linalg::Index, typename Value, typename Index, typename IndexBase> |
| 71 |
SparseMatrixStorage make_sparse_matrix_storage_from_rows_columns_values(std::size_t nr, std::size_t nc, std::size_t nnz, const Index rows[], const Index cols[], const Value vals[], const IndexBase index_base = 0, bool is_sorted = true) { |
| 72 |
std::unique_ptr<array::Array> array_value(array::Array::create<SparseMatrixValue>(nnz)); |
| 73 |
std::unique_ptr<array::Array> array_inner(array::Array::create<SparseMatrixIndex>(nnz)); |
| 74 |
std::unique_ptr<array::Array> array_outer(array::Array::create<SparseMatrixIndex>(nr+1)); |
| 75 |
auto* value = array_value->host_data<SparseMatrixValue>(); |
| 76 |
auto* inner = array_inner->host_data<SparseMatrixIndex>(); |
| 77 |
auto* outer = array_outer->host_data<SparseMatrixIndex>(); |
| 78 |
std::fill(outer, outer + nr + 1, 0); |
| 79 |
|
| 80 |
Index base = index_base; |
| 81 |
|
| 82 |
if (not is_sorted) { |
| 83 |
std::vector<size_t> sorted_index(nnz); |
| 84 |
std::iota(sorted_index.begin(), sorted_index.end(), 0); |
| 85 |
std::sort(sorted_index.begin(), sorted_index.end(), [&](auto i, auto j) { |
| 86 |
return rows[i] != rows[j] ? rows[i] < rows[j] : |
| 87 |
cols[i] != cols[j] ? cols[i] < cols[j] : |
| 88 |
i < j; |
| 89 |
}); |
| 90 |
|
| 91 |
for (size_t n = 0; n < nnz; ++n) { |
| 92 |
auto r = rows[sorted_index[n]] - base; |
| 93 |
auto c = cols[sorted_index[n]] - base; |
| 94 |
outer[r + 1]++; |
| 95 |
inner[n] = c; |
| 96 |
} |
| 97 |
for (size_t n = 0; n < nnz; ++n) { |
| 98 |
value[n] = vals[sorted_index[n]]; |
| 99 |
} |
| 100 |
} |
| 101 |
else { |
| 102 |
for (size_t n = 0; n < nnz; ++n) { |
| 103 |
auto r = rows[n] - base; |
| 104 |
auto c = cols[n] - base; |
| 105 |
outer[r + 1]++; |
| 106 |
inner[n] = c; |
| 107 |
} |
| 108 |
for (size_t n = 0; n < nnz; ++n) { |
| 109 |
value[n] = vals[n]; |
| 110 |
} |
| 111 |
|
| 112 |
} |
| 113 |
for (size_t r = 0; r < nr; ++r) { |
| 114 |
outer[r + 1] += outer[r]; |
| 115 |
} |
| 116 |
ATLAS_ASSERT(outer[0] == 0); |
| 117 |
ATLAS_ASSERT(outer[nr] == nnz); |
| 118 |
return SparseMatrixStorage::make(nr, nc, nnz, std::move(array_value), std::move(array_inner), std::move(array_outer), std::any()); |
| 119 |
} |
| 120 |
|
| 121 |
template<typename SparseMatrixValue = eckit::linalg::Scalar, typename SparseMatrixIndex = eckit::linalg::Index, typename Value, typename Index, typename IndexBase> |
| 122 |
SparseMatrixStorage make_sparse_matrix_storage_from_rows_columns_values(std::size_t nr, std::size_t nc, const std::vector<Index>& rows, const std::vector<Index>& cols, const std::vector<Value>& vals, const IndexBase index_base = 0, bool is_sorted = true) { |
| 123 |
ATLAS_TRACE("make_sparse_matrix_storage_from_rows_columns_values partition"); |
| 124 |
std::size_t nnz = vals.size(); |
| 125 |
ATLAS_ASSERT(rows.size() == nnz); |
| 126 |
ATLAS_ASSERT(cols.size() == nnz); |
| 127 |
return make_sparse_matrix_storage_from_rows_columns_values<SparseMatrixValue, SparseMatrixIndex>(nr, nc, nnz, rows.data(), cols.data(), vals.data(), index_base, is_sorted); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
} // namespace atlas::linalg |