|
| 1 | +// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +#include "core/components/format_conversion_kernels.hpp" |
| 6 | + |
| 7 | +#include <thrust/for_each.h> |
| 8 | +#include <thrust/iterator/counting_iterator.h> |
| 9 | +#include <thrust/iterator/transform_iterator.h> |
| 10 | + |
| 11 | +#include <ginkgo/core/base/types.hpp> |
| 12 | + |
| 13 | +#include "common/cuda_hip/base/thrust.hpp" |
| 14 | +#include "common/cuda_hip/components/bitvector.hpp" |
| 15 | + |
| 16 | + |
| 17 | +namespace gko { |
| 18 | +namespace kernels { |
| 19 | +namespace GKO_DEVICE_NAMESPACE { |
| 20 | +namespace components { |
| 21 | + |
| 22 | + |
| 23 | +template <typename IndexType, typename RowPtrType> |
| 24 | +void convert_ptrs_to_idxs(std::shared_ptr<const DefaultExecutor> exec, |
| 25 | + const RowPtrType* ptrs, size_type num_blocks, |
| 26 | + IndexType* idxs) |
| 27 | +{ |
| 28 | + const auto policy = thrust_policy(exec); |
| 29 | + const auto num_elements = exec->copy_val_to_host(ptrs + num_blocks); |
| 30 | + // transform the ptrs to a bitvector in unary delta encoding, i.e. |
| 31 | + // every row with n elements is encoded as 1 0 ... n times ... 0 |
| 32 | + auto it = thrust::make_transform_iterator( |
| 33 | + thrust::make_counting_iterator(IndexType{}), |
| 34 | + [ptrs] __device__(IndexType i) -> RowPtrType { return ptrs[i] + i; }); |
| 35 | + auto bv = bitvector::from_sorted_indices(exec, it, num_blocks, |
| 36 | + num_blocks + num_elements); |
| 37 | + auto device_bv = bv.device_view(); |
| 38 | + thrust::for_each_n(policy, thrust::make_counting_iterator(IndexType{}), |
| 39 | + num_blocks + num_elements, |
| 40 | + [device_bv, idxs] __device__(RowPtrType i) { |
| 41 | + if (!device_bv.get(i)) { |
| 42 | + auto rank = device_bv.rank(i); |
| 43 | + idxs[i - rank] = rank - 1; |
| 44 | + } |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_CONVERT_PTRS_TO_IDXS32); |
| 49 | +GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_DECLARE_CONVERT_PTRS_TO_IDXS64); |
| 50 | + |
| 51 | + |
| 52 | +} // namespace components |
| 53 | +} // namespace GKO_DEVICE_NAMESPACE |
| 54 | +} // namespace kernels |
| 55 | +} // namespace gko |
0 commit comments