Ginkgo Generated from branch based on main. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
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>
37struct block_interleaved_storage_scheme {
38 block_interleaved_storage_scheme() = default;
39
40 block_interleaved_storage_scheme(IndexType block_offset,
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
186template <typename ValueType = default_precision, typename IndexType = int32>
187class Jacobi : public EnableLinOp<Jacobi<ValueType, IndexType>>,
188 public ConvertibleTo<matrix::Dense<ValueType>>,
189 public WritableToMatrixData<ValueType, IndexType>,
190 public Transposable {
191 friend class EnableLinOp<Jacobi>;
192 friend class EnablePolymorphicObject<Jacobi, LinOp>;
193
194public:
195 using EnableLinOp<Jacobi>::convert_to;
196 using EnableLinOp<Jacobi>::move_to;
197 using ConvertibleTo<matrix::Dense<ValueType>>::convert_to;
199 using value_type = ValueType;
200 using index_type = IndexType;
201 using mat_data = matrix_data<ValueType, IndexType>;
202 using transposed_type = Jacobi<ValueType, IndexType>;
203
212 size_type get_num_blocks() const noexcept { return num_blocks_; }
213
223 const noexcept
224 {
225 return storage_scheme_;
226 }
227
239 const value_type* get_blocks() const noexcept
240 {
241 return blocks_.get_const_data();
242 }
243
254 {
255 return conditioning_.get_const_data();
256 }
257
264 {
265 return blocks_.get_size();
266 }
267
268 void convert_to(matrix::Dense<value_type>* result) const override;
269
270 void move_to(matrix::Dense<value_type>* result) override;
271
272 void write(mat_data& data) const override;
273
274 std::unique_ptr<LinOp> transpose() const override;
275
276 std::unique_ptr<LinOp> conj_transpose() const override;
277
282 Jacobi& operator=(const Jacobi& other);
283
290
295 Jacobi(const Jacobi& other);
296
302 Jacobi(Jacobi&& other);
303
305 {
315
326
345
372 nullptr);
373
386
387 private:
388 // See documentation of storage_optimization parameter for details about
389 // this class
390 struct storage_optimization_type {
391 storage_optimization_type(precision_reduction p)
392 : is_block_wise{false}, of_all_blocks{p}
393 {}
394
395 storage_optimization_type(
396 const array<precision_reduction>& block_wise_opt)
397 : is_block_wise{block_wise_opt.get_size() > 0},
398 block_wise{block_wise_opt}
399 {}
400
401 storage_optimization_type(
402 array<precision_reduction>&& block_wise_opt)
403 : is_block_wise{block_wise_opt.get_size() > 0},
404 block_wise{std::move(block_wise_opt)}
405 {}
406
407 operator precision_reduction() { return of_all_blocks; }
408
409 bool is_block_wise;
410 precision_reduction of_all_blocks;
412 };
413
414 public:
482 storage_optimization_type GKO_FACTORY_PARAMETER_VECTOR(
484
513 };
514 GKO_ENABLE_LIN_OP_FACTORY(Jacobi, parameters, Factory);
516
533 static parameters_type parse(
534 const config::pnode& config, const config::registry& context,
535 const config::type_descriptor& td_for_child =
536 config::make_type_descriptor<ValueType, IndexType>());
537
538protected:
544 explicit Jacobi(std::shared_ptr<const Executor> exec)
545 : EnableLinOp<Jacobi>(exec),
546 num_blocks_{},
547 blocks_(exec),
548 conditioning_(exec)
549 {
550 parameters_.block_pointers.set_executor(exec);
551 parameters_.storage_optimization.block_wise.set_executor(exec);
552 }
553
561 explicit Jacobi(const Factory* factory,
562 std::shared_ptr<const LinOp> system_matrix)
563 : EnableLinOp<Jacobi>(factory->get_executor(),
564 gko::transpose(system_matrix->get_size())),
565 parameters_{factory->get_parameters()},
566 storage_scheme_{this->compute_storage_scheme(
567 parameters_.max_block_size, parameters_.max_block_stride)},
568 num_blocks_{parameters_.block_pointers.get_size() - 1},
569 blocks_(factory->get_executor(),
570 storage_scheme_.compute_storage_space(
571 parameters_.block_pointers.get_size() - 1)),
572 conditioning_(factory->get_executor())
573 {
574 parameters_.block_pointers.set_executor(this->get_executor());
575 parameters_.storage_optimization.block_wise.set_executor(
576 this->get_executor());
577 this->generate(system_matrix.get(), parameters_.skip_sorting);
578 }
579
588 block_interleaved_storage_scheme<index_type> compute_storage_scheme(
589 uint32 max_block_size, uint32 param_max_block_stride)
590 {
591 uint32 default_block_stride = 32;
592 // If the executor is hip, the warp size is 32 or 64
593 if (auto hip_exec = std::dynamic_pointer_cast<const gko::HipExecutor>(
594 this->get_executor())) {
595 default_block_stride = hip_exec->get_warp_size();
596 }
597 uint32 max_block_stride = default_block_stride;
598 if (param_max_block_stride != 0) {
599 // if parameter max_block_stride is not zero, set max_block_stride =
600 // param_max_block_stride
601 max_block_stride = param_max_block_stride;
602 if (this->get_executor() != this->get_executor()->get_master() &&
603 max_block_stride != default_block_stride) {
604 // only support the default value on the gpu device
605 GKO_NOT_SUPPORTED(this);
606 }
607 }
608 if (parameters_.max_block_size > max_block_stride ||
609 parameters_.max_block_size < 1) {
610 GKO_NOT_SUPPORTED(this);
611 }
612 const auto group_size = static_cast<uint32>(
613 max_block_stride / get_superior_power(uint32{2}, max_block_size));
614 const auto block_offset = max_block_size;
615 const auto block_stride = group_size * block_offset;
616 const auto group_offset = max_block_size * block_stride;
617 return {static_cast<index_type>(block_offset),
618 static_cast<index_type>(group_offset),
619 get_significant_bit(group_size)};
620 }
621
631 void generate(const LinOp* system_matrix, bool skip_sorting);
632
640 void detect_blocks(const matrix::Csr<ValueType, IndexType>* system_matrix);
641
642 void apply_impl(const LinOp* b, LinOp* x) const override;
643
644 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
645 LinOp* x) const override;
646
647private:
648 block_interleaved_storage_scheme<index_type> storage_scheme_{};
649 size_type num_blocks_;
650 array<value_type> blocks_;
652};
653
654
655} // namespace preconditioner
656} // namespace gko
657
658
659#endif // GKO_PUBLIC_CORE_PRECONDITIONER_JACOBI_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:479
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition lin_op.hpp:210
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:243
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:433
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:660
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
void set_executor(std::shared_ptr< const Executor > exec)
Changes the Executor of the array, moving the allocated data to the new Executor.
Definition array.hpp:714
pnode describes a tree of properties.
Definition property_tree.hpp:28
This class stores additional context for creating Ginkgo objects from configuration files.
Definition registry.hpp:167
This class describes the value and index types to be used when building a Ginkgo type from a configur...
Definition type_descriptor.hpp:39
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
This class is used to encode storage precisions of low precision algorithms.
Definition types.hpp:239
Definition jacobi.hpp:514
A block-Jacobi preconditioner is a block-diagonal linear operator, obtained by inverting the diagonal...
Definition jacobi.hpp:190
const block_interleaved_storage_scheme< index_type > & get_storage_scheme() const noexcept
Returns the storage scheme used for storing Jacobi blocks.
Definition jacobi.hpp:222
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
Jacobi(const Jacobi &other)
Copy-constructs a Jacobi preconditioner.
const remove_complex< value_type > * get_conditioning() const noexcept
Returns an array of 1-norm condition numbers of the blocks.
Definition jacobi.hpp:253
Jacobi(Jacobi &&other)
Move-assigns a Jacobi preconditioner.
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 >())
Create the parameters from the property_tree.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
const value_type * get_blocks() const noexcept
Returns the pointer to the memory used for storing the block data.
Definition jacobi.hpp:239
Jacobi & operator=(Jacobi &&other)
Move-assigns a Jacobi preconditioner.
Jacobi & operator=(const Jacobi &other)
Copy-assigns a Jacobi preconditioner.
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition jacobi.hpp:263
size_type get_num_blocks() const noexcept
Returns the number of blocks of the operator.
Definition jacobi.hpp:212
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
This Macro will generate a new type containing the parameters for the factory _factory_name.
Definition abstract_factory.hpp:280
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Defines a build method for the factory, simplifying its construction by removing the repetitive typin...
Definition abstract_factory.hpp:394
#define GKO_ENABLE_LIN_OP_FACTORY(_lin_op, _parameters_name, _factory_name)
This macro will generate a default implementation of a LinOpFactory for the LinOp subclass it is defi...
Definition lin_op.hpp:1017
#define GKO_FACTORY_PARAMETER_VECTOR(_name,...)
Creates a vector factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:461
The Preconditioner namespace.
Definition gauss_seidel.hpp:19
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:654
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:264
constexpr uint32 get_significant_bit(const T &n, uint32 hint=0u) noexcept
Returns the position of the most significant bit of the number.
Definition math.hpp:1057
std::uint32_t uint32
32-bit unsigned integral type.
Definition types.hpp:130
void write(StreamType &&os, MatrixPtrType &&matrix, layout_type layout=detail::mtx_io_traits< std::remove_cv_t< detail::pointee< MatrixPtrType > > >::default_layout)
Writes a matrix into an output stream in matrix market format.
Definition mtx_io.hpp:295
constexpr int64 ceildiv(int64 num, int64 den)
Performs integer division with rounding up.
Definition math.hpp:614
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition batch_dim.hpp:119
constexpr T get_superior_power(const T &base, const T &limit, const T &hint=T{1}) noexcept
Returns the smallest power of base not smaller than limit.
Definition math.hpp:1075
@ array
The matrix should be written as dense matrix in column-major order.
Definition mtx_io.hpp:96
STL namespace.
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126
uint32 max_block_stride
Stride between two columns of a block (as number of elements).
Definition jacobi.hpp:325
storage_optimization_type storage_optimization
The precisions to use for the blocks of the matrix.
Definition jacobi.hpp:483
uint32 max_block_size
Maximal size of diagonal blocks.
Definition jacobi.hpp:314
remove_complex< value_type > accuracy
The relative accuracy of the adaptive Jacobi variant.
Definition jacobi.hpp:512
gko::array< index_type > block_pointers
Starting (row / column) indexes of individual blocks.
Definition jacobi.hpp:372
bool skip_sorting
true means it is known that the matrix given to this factory will be sorted first by row,...
Definition jacobi.hpp:344
bool aggregate_l1
Use L1 Jacboi, which is introduced in the paper A.
Definition jacobi.hpp:385
Defines the parameters of the interleaved block storage scheme used by block-Jacobi blocks.
Definition jacobi.hpp:37
IndexType get_block_offset(IndexType block_id) const noexcept
Returns the offset of the block with the given ID within its group.
Definition jacobi.hpp:113
IndexType get_group_offset(IndexType block_id) const noexcept
Returns the offset of the group belonging to the block with the given ID.
Definition jacobi.hpp:101
uint32 group_power
Then base 2 power of the group.
Definition jacobi.hpp:62
IndexType get_stride() const noexcept
Returns the stride between columns of the block.
Definition jacobi.hpp:137
IndexType group_offset
The offset between two block groups.
Definition jacobi.hpp:55
IndexType get_global_block_offset(IndexType block_id) const noexcept
Returns the offset of the block with the given ID.
Definition jacobi.hpp:126
IndexType block_offset
The offset between consecutive blocks within the group.
Definition jacobi.hpp:50
size_type compute_storage_space(size_type num_blocks) const noexcept
Computes the storage space required for the requested number of blocks.
Definition jacobi.hpp:87
IndexType get_group_size() const noexcept
Returns the number of elements in the group.
Definition jacobi.hpp:69