|
| 1 | +// SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: BSD-3-Clause |
| 4 | + |
| 5 | +#include <ginkgo/core/base/precision_dispatch.hpp> |
| 6 | +#include <ginkgo/core/matrix/dense.hpp> |
| 7 | +#include <ginkgo/core/sketch/sparse_stack.hpp> |
| 8 | + |
| 9 | +#include "core/sketch/sparse_stack_kernels.hpp" |
| 10 | + |
| 11 | + |
| 12 | +namespace gko { |
| 13 | +namespace sketch { |
| 14 | +namespace sparse_stack { |
| 15 | +namespace { |
| 16 | + |
| 17 | + |
| 18 | +GKO_REGISTER_OPERATION(generate, sparse_stack::generate); |
| 19 | +GKO_REGISTER_OPERATION(apply, sparse_stack::apply); |
| 20 | +GKO_REGISTER_OPERATION(rapply, sparse_stack::rapply); |
| 21 | + |
| 22 | + |
| 23 | +} // anonymous namespace |
| 24 | +} // namespace sparse_stack |
| 25 | + |
| 26 | + |
| 27 | +template <typename ValueType, typename IndexType> |
| 28 | +SparseStack<ValueType, IndexType>::SparseStack( |
| 29 | + std::shared_ptr<const Executor> exec, size_type sketch_size, |
| 30 | + size_type input_size, size_type zeta, uint64 seed) |
| 31 | + : EnableLinOp<SparseStack, SketchOperator<ValueType>>( |
| 32 | + exec, dim<2>{sketch_size, input_size}), |
| 33 | + zeta_{zeta}, |
| 34 | + hash_map_{exec, input_size * zeta}, |
| 35 | + signs_{exec, input_size * zeta}, |
| 36 | + seed_{seed} |
| 37 | +{ |
| 38 | + exec->run(sparse_stack::make_generate(sketch_size, input_size, zeta_, |
| 39 | + hash_map_, signs_, seed_)); |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +template <typename ValueType, typename IndexType> |
| 44 | +std::unique_ptr<SparseStack<ValueType, IndexType>> |
| 45 | +SparseStack<ValueType, IndexType>::create(std::shared_ptr<const Executor> exec, |
| 46 | + size_type sketch_size, |
| 47 | + size_type input_size, size_type zeta, |
| 48 | + uint64 seed) |
| 49 | +{ |
| 50 | + return std::unique_ptr<SparseStack>{ |
| 51 | + new SparseStack(exec, sketch_size, input_size, zeta, seed)}; |
| 52 | +} |
| 53 | + |
| 54 | +template <typename ValueType, typename IndexType> |
| 55 | +std::unique_ptr<SparseStack<ValueType, IndexType>> |
| 56 | +SparseStack<ValueType, IndexType>::create(std::shared_ptr<const Executor> exec) |
| 57 | +{ |
| 58 | + return std::unique_ptr<SparseStack>{new SparseStack(exec)}; |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +template <typename ValueType, typename IndexType> |
| 63 | +void SparseStack<ValueType, IndexType>::apply_sketch_impl( |
| 64 | + const matrix::Dense<ValueType>* b, matrix::Dense<ValueType>* x) const |
| 65 | +{ |
| 66 | + this->get_executor()->run(sparse_stack::make_apply( |
| 67 | + zeta_, hash_map_, signs_, b->get_const_device_view(), |
| 68 | + x->get_device_view())); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +template <typename ValueType, typename IndexType> |
| 73 | +void SparseStack<ValueType, IndexType>::rapply_sketch_impl( |
| 74 | + const matrix::Dense<ValueType>* b, matrix::Dense<ValueType>* x) const |
| 75 | +{ |
| 76 | + this->get_executor()->run(sparse_stack::make_rapply( |
| 77 | + zeta_, hash_map_, signs_, b->get_const_device_view(), |
| 78 | + x->get_device_view())); |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +#define GKO_DECLARE_SPARSE_STACK(ValueType, IndexType) \ |
| 83 | + class SparseStack<ValueType, IndexType> |
| 84 | +GKO_INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(GKO_DECLARE_SPARSE_STACK); |
| 85 | + |
| 86 | + |
| 87 | +} // namespace sketch |
| 88 | +} // namespace gko |
0 commit comments