ginkgo/core/preconditioner/jacobi.hpp Source File

ginkgo/core/preconditioner/jacobi.hpp Source File#

Reference API: ginkgo/core/preconditioner/jacobi.hpp Source File
Reference API
jacobi.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_PRECONDITIONER_JACOBI_HPP_
6#define GKO_PUBLIC_CORE_PRECONDITIONER_JACOBI_HPP_
7
8
9#include <ginkgo/core/base/array.hpp>
10#include <ginkgo/core/base/lin_op.hpp>
11#include <ginkgo/core/config/config.hpp>
12#include <ginkgo/core/config/registry.hpp>
13#include <ginkgo/core/matrix/csr.hpp>
14#include <ginkgo/core/matrix/dense.hpp>
15#include <ginkgo/core/matrix/diagonal.hpp>
16
17
18namespace gko {
24namespace preconditioner {
25
26
27// TODO: replace this with a custom accessor
36template <typename IndexType>
39
45 {}
46
50 IndexType block_offset;
51
55 IndexType group_offset;
56
63
69 GKO_ATTRIBUTES IndexType get_group_size() const noexcept
70 {
71 return one<IndexType>() << group_power;
72 }
73
86 GKO_ATTRIBUTES size_type
87 compute_storage_space(size_type num_blocks) const noexcept
88 {
89 return (num_blocks + 1 == size_type{0})
90 ? size_type{0}
91 : ceildiv(num_blocks, this->get_group_size()) * group_offset;
92 }
93
101 GKO_ATTRIBUTES IndexType get_group_offset(IndexType block_id) const noexcept
102 {
103 return group_offset * (block_id >> group_power);
104 }
105
113 GKO_ATTRIBUTES IndexType get_block_offset(IndexType block_id) const noexcept
114 {
115 return block_offset * (block_id & (this->get_group_size() - 1));
116 }
117
125 GKO_ATTRIBUTES IndexType
126 get_global_block_offset(IndexType block_id) const noexcept
127 {
128 return this->get_group_offset(block_id) +
129 this->get_block_offset(block_id);
130 }
131
137 GKO_ATTRIBUTES IndexType get_stride() const noexcept
138 {
139 return block_offset << group_power;
140 }
141};
142
143
185template <typename ValueType = default_precision, typename IndexType = int32>
186class Jacobi : public EnableLinOp<Jacobi<ValueType, IndexType>>,
187 public ConvertibleTo<matrix::Dense<ValueType>>,
188 public WritableToMatrixData<ValueType, IndexType>,
189 public Transposable {
190 friend class EnableLinOp<Jacobi>;
191 friend class EnablePolymorphicObject<Jacobi, LinOp>;
192
193public:
194 using EnableLinOp<Jacobi>::convert_to;
195 using EnableLinOp<Jacobi>::move_to;
196 using ConvertibleTo<matrix::Dense<ValueType>>::convert_to;
198 using value_type = ValueType;
199 using index_type = IndexType;
202
211 size_type get_num_blocks() const noexcept { return num_blocks_; }
212
222 const noexcept
223 {
224 return storage_scheme_;
225 }
226
238 const value_type* get_blocks() const noexcept
239 {
240 return blocks_.get_const_data();
241 }
242
253 {
254 return conditioning_.get_const_data();
255 }
256
263 {
264 return blocks_.get_size();
265 }
266
267 void convert_to(matrix::Dense<value_type>* result) const override;
268
269 void move_to(matrix::Dense<value_type>* result) override;
270
271 void write(mat_data& data) const override;
272
273 std::unique_ptr<LinOp> transpose() const override;
274
275 std::unique_ptr<LinOp> conj_transpose() const override;
276
281 Jacobi& operator=(const Jacobi& other);
282
289
294 Jacobi(const Jacobi& other);
295
301 Jacobi(Jacobi&& other);
302
304 {
314
325
343 bool GKO_FACTORY_PARAMETER_SCALAR(skip_sorting, false);
344
371 nullptr);
372
384 bool GKO_FACTORY_PARAMETER_SCALAR(aggregate_l1, false);
385
386 private:
387 // See documentation of storage_optimization parameter for details about
388 // this class
389 struct storage_optimization_type {
390 storage_optimization_type(precision_reduction p)
391 : is_block_wise{false}, of_all_blocks{p}
392 {}
393
394 storage_optimization_type(
395 const array<precision_reduction>& block_wise_opt)
396 : is_block_wise{block_wise_opt.get_size() > 0},
397 block_wise{block_wise_opt}
398 {}
399
400 storage_optimization_type(
401 array<precision_reduction>&& block_wise_opt)
402 : is_block_wise{block_wise_opt.get_size() > 0},
403 block_wise{std::move(block_wise_opt)}
404 {}
405
406 operator precision_reduction() { return of_all_blocks; }
407
408 bool is_block_wise;
409 precision_reduction of_all_blocks;
411 };
412
413 public:
481 storage_optimization_type GKO_FACTORY_PARAMETER_VECTOR(
482 storage_optimization, precision_reduction(0, 0));
483
511 accuracy, static_cast<remove_complex<value_type>>(1e-1));
512 };
515
533 const config::pnode& config, const config::registry& context,
534 const config::type_descriptor& td_for_child =
535 config::make_type_descriptor<ValueType, IndexType>());
536
537protected:
543 explicit Jacobi(std::shared_ptr<const Executor> exec)
544 : EnableLinOp<Jacobi>(exec),
545 num_blocks_{},
546 blocks_(exec),
547 conditioning_(exec)
548 {
549 parameters_.block_pointers.set_executor(exec);
550 parameters_.storage_optimization.block_wise.set_executor(exec);
551 }
552
560 explicit Jacobi(const Factory* factory,
561 std::shared_ptr<const LinOp> system_matrix)
562 : EnableLinOp<Jacobi>(factory->get_executor(),
563 gko::transpose(system_matrix->get_size())),
564 parameters_{factory->get_parameters()},
565 storage_scheme_{this->compute_storage_scheme(
566 parameters_.max_block_size, parameters_.max_block_stride)},
567 num_blocks_{parameters_.block_pointers.get_size() - 1},
568 blocks_(factory->get_executor(),
569 storage_scheme_.compute_storage_space(
570 parameters_.block_pointers.get_size() - 1)),
571 conditioning_(factory->get_executor())
572 {
573 parameters_.block_pointers.set_executor(this->get_executor());
574 parameters_.storage_optimization.block_wise.set_executor(
575 this->get_executor());
576 this->generate(system_matrix.get(), parameters_.skip_sorting);
577 }
578
588 uint32 max_block_size, uint32 param_max_block_stride)
589 {
590 uint32 default_block_stride = 32;
591 // If the executor is hip, the warp size is 32 or 64
592 if (auto hip_exec = std::dynamic_pointer_cast<const gko::HipExecutor>(
593 this->get_executor())) {
594 default_block_stride = hip_exec->get_warp_size();
595 }
596 uint32 max_block_stride = default_block_stride;
597 if (param_max_block_stride != 0) {
598 // if parameter max_block_stride is not zero, set max_block_stride =
599 // param_max_block_stride
600 max_block_stride = param_max_block_stride;
601 if (this->get_executor() != this->get_executor()->get_master() &&
602 max_block_stride != default_block_stride) {
603 // only support the default value on the gpu device
604 GKO_NOT_SUPPORTED(this);
605 }
606 }
607 if (parameters_.max_block_size > max_block_stride ||
608 parameters_.max_block_size < 1) {
609 GKO_NOT_SUPPORTED(this);
610 }
611 const auto group_size = static_cast<uint32>(
612 max_block_stride / get_superior_power(uint32{2}, max_block_size));
613 const auto block_offset = max_block_size;
614 const auto block_stride = group_size * block_offset;
615 const auto group_offset = max_block_size * block_stride;
616 return {static_cast<index_type>(block_offset),
617 static_cast<index_type>(group_offset),
618 get_significant_bit(group_size)};
619 }
620
630 void generate(const LinOp* system_matrix, bool skip_sorting);
631
640
641 void apply_impl(const LinOp* b, LinOp* x) const override;
642
643 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
644 LinOp* x) const override;
645
646private:
648 size_type num_blocks_;
649 array<value_type> blocks_;
651};
652
653
654} // namespace preconditioner
655} // namespace gko
656
657
658#endif // GKO_PUBLIC_CORE_PRECONDITIONER_JACOBI_HPP_
Definition polymorphic_object.hpp:479
Definition lin_op.hpp:878
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
std::shared_ptr< const Executor > get_executor() const noexcept
Definition polymorphic_object.hpp:243
Definition lin_op.hpp:433
Definition lin_op.hpp:660
Definition array.hpp:166
const value_type * get_const_data() const noexcept
Definition array.hpp:682
void set_executor(std::shared_ptr< const Executor > exec)
Definition array.hpp:700
size_type get_size() const noexcept
Definition array.hpp:656
Definition property_tree.hpp:28
Definition registry.hpp:167
Definition type_descriptor.hpp:39
Definition csr.hpp:123
Definition dense.hpp:116
Definition types.hpp:238
Definition jacobi.hpp:513
Definition jacobi.hpp:189
const block_interleaved_storage_scheme< index_type > & get_storage_scheme() const noexcept
Definition jacobi.hpp:221
Jacobi(const Factory *factory, std::shared_ptr< const LinOp > system_matrix)
Definition jacobi.hpp:560
std::unique_ptr< LinOp > conj_transpose() const override
void generate(const LinOp *system_matrix, bool skip_sorting)
Jacobi(std::shared_ptr< const Executor > exec)
Definition jacobi.hpp:543
block_interleaved_storage_scheme< index_type > compute_storage_scheme(uint32 max_block_size, uint32 param_max_block_stride)
Definition jacobi.hpp:587
Jacobi(const Jacobi &other)
const remove_complex< value_type > * get_conditioning() const noexcept
Definition jacobi.hpp:252
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< ValueType, IndexType >())
std::unique_ptr< LinOp > transpose() const override
const value_type * get_blocks() const noexcept
Definition jacobi.hpp:238
void write(mat_data &data) const override
Jacobi & operator=(Jacobi &&other)
Jacobi & operator=(const Jacobi &other)
void detect_blocks(const matrix::Csr< ValueType, IndexType > *system_matrix)
size_type get_num_stored_elements() const noexcept
Definition jacobi.hpp:262
size_type get_num_blocks() const noexcept
Definition jacobi.hpp:211
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
Definition abstract_factory.hpp:280
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Definition abstract_factory.hpp:445
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Definition abstract_factory.hpp:394
#define GKO_ENABLE_LIN_OP_FACTORY(_lin_op, _parameters_name, _factory_name)
Definition lin_op.hpp:1016
#define GKO_FACTORY_PARAMETER_VECTOR(_name,...)
Definition abstract_factory.hpp:461
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::uint32_t uint32
Definition types.hpp:129
constexpr uint32 get_significant_bit(const T &n, uint32 hint=0u) noexcept
Definition math.hpp:1009
std::size_t size_type
Definition types.hpp:89
constexpr int64 ceildiv(int64 num, int64 den)
Definition math.hpp:590
constexpr T get_superior_power(const T &base, const T &limit, const T &hint=T{1}) noexcept
Definition math.hpp:1027
typename detail::remove_complex_s< T >::type remove_complex
Definition math.hpp:260
STL namespace.
Definition matrix_data.hpp:126
storage_optimization_type storage_optimization
Definition jacobi.hpp:482
uint32 max_block_size
Definition jacobi.hpp:313
gko::array< index_type > block_pointers
Definition jacobi.hpp:371
bool skip_sorting
true means it is known that the matrix given to this factory will be sorted first by row,...
Definition jacobi.hpp:343
IndexType get_block_offset(IndexType block_id) const noexcept
Definition jacobi.hpp:113
IndexType get_group_offset(IndexType block_id) const noexcept
Definition jacobi.hpp:101
IndexType get_stride() const noexcept
Definition jacobi.hpp:137
IndexType group_offset
Definition jacobi.hpp:55
IndexType get_global_block_offset(IndexType block_id) const noexcept
Definition jacobi.hpp:126
IndexType block_offset
Definition jacobi.hpp:50
size_type compute_storage_space(size_type num_blocks) const noexcept
Definition jacobi.hpp:87
IndexType get_group_size() const noexcept
Definition jacobi.hpp:69