Ginkgo  Generated from pipelines/2017069469 branch based on develop. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
dense_cache.hpp
1 // SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_DENSE_CACHE_HPP_
6 #define GKO_PUBLIC_CORE_BASE_DENSE_CACHE_HPP_
7 
8 
9 #include <map>
10 #include <memory>
11 #include <string>
12 
13 #include <ginkgo/core/base/array.hpp>
14 #include <ginkgo/core/base/dim.hpp>
15 #include <ginkgo/core/base/executor.hpp>
16 
17 
18 namespace gko {
19 
20 
21 class LinOp;
22 
23 
24 namespace matrix {
25 
26 
27 template <typename ValueType>
28 class Dense;
29 
30 
31 }
32 
33 
34 namespace detail {
35 
36 
46 template <typename ValueType>
47 struct DenseCache {
48  DenseCache() = default;
49  ~DenseCache() = default;
50  DenseCache(const DenseCache&) {}
51  DenseCache(DenseCache&&) noexcept {}
52  DenseCache& operator=(const DenseCache&) { return *this; }
53  DenseCache& operator=(DenseCache&&) noexcept { return *this; }
54  mutable std::unique_ptr<matrix::Dense<ValueType>> vec{};
55 
56 
69  void init_from(const matrix::Dense<ValueType>* template_vec) const;
70 
80  void init(std::shared_ptr<const Executor> exec, dim<2> size) const;
81 
86  matrix::Dense<ValueType>& operator*() const { return *vec; }
87 
92  matrix::Dense<ValueType>* operator->() const { return vec.get(); }
93 
98  matrix::Dense<ValueType>* get() const { return vec.get(); }
99 };
100 
101 
102 // helper to access private member for testing
103 class GenericDenseCacheAccessor;
104 
105 
116 struct GenericDenseCache {
117  friend class GenericDenseCacheAccessor;
118 
119  GenericDenseCache() = default;
120  ~GenericDenseCache() = default;
121  GenericDenseCache(const GenericDenseCache&);
122  GenericDenseCache(GenericDenseCache&&) noexcept;
123  GenericDenseCache& operator=(const GenericDenseCache&);
124  GenericDenseCache& operator=(GenericDenseCache&&) noexcept;
125 
131  template <typename ValueType>
132  std::shared_ptr<matrix::Dense<ValueType>> get(
133  std::shared_ptr<const Executor> exec, dim<2> size) const;
134 
135 private:
136  mutable array<char> workspace;
137 };
138 
139 
140 // helper to access private member for testing.
141 class ScalarCacheAccessor;
142 
143 
154 struct ScalarCache {
155  friend class ScalarCacheAccessor;
156 
157  ScalarCache(std::shared_ptr<const Executor> executor, double scalar_value);
158  ~ScalarCache() = default;
159  ScalarCache(const ScalarCache& other);
160  ScalarCache(ScalarCache&& other) noexcept;
161  ScalarCache& operator=(const ScalarCache& other);
162  ScalarCache& operator=(ScalarCache&& other) noexcept;
163 
169  template <typename ValueType>
170  std::shared_ptr<const matrix::Dense<ValueType>> get() const;
171 
172 private:
173  std::shared_ptr<const Executor> exec;
174  double value;
175  mutable std::map<std::string, std::shared_ptr<const gko::LinOp>> scalars;
176 };
177 
178 
179 } // namespace detail
180 } // namespace gko
181 
182 
183 #endif // GKO_PUBLIC_CORE_BASE_DENSE_CACHE_HPP_
gko::layout_type::array
The matrix should be written as dense matrix in column-major order.
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20