Ginkgo  Generated from pipelines/1868155508 branch based on main. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
fbcsr.hpp
1 // SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_FBCSR_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_FBCSR_HPP_
7 
8 
9 #include <ginkgo/core/base/array.hpp>
10 #include <ginkgo/core/base/lin_op.hpp>
11 #include <ginkgo/core/base/math.hpp>
12 
13 
14 namespace gko {
15 namespace matrix {
16 
17 
18 template <typename ValueType>
19 class Dense;
20 
21 template <typename ValueType, typename IndexType>
22 class Csr;
23 
24 template <typename ValueType, typename IndexType>
25 class SparsityCsr;
26 
27 template <typename ValueType, typename IndexType>
28 class Fbcsr;
29 
30 template <typename ValueType, typename IndexType>
32 
33 
34 namespace detail {
35 
36 
47 template <typename IndexType>
48 inline IndexType get_num_blocks(const int block_size, const IndexType size)
49 {
50  GKO_ASSERT_BLOCK_SIZE_CONFORMANT(size, block_size);
51  return size / block_size;
52 }
53 
54 
55 } // namespace detail
56 
57 
98 template <typename ValueType = default_precision, typename IndexType = int32>
99 class Fbcsr
100  : public EnableLinOp<Fbcsr<ValueType, IndexType>>,
101  public ConvertibleTo<Fbcsr<next_precision<ValueType>, IndexType>>,
102 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
103  public ConvertibleTo<Fbcsr<next_precision<ValueType, 2>, IndexType>>,
104 #endif
105 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
106  public ConvertibleTo<Fbcsr<next_precision<ValueType, 3>, IndexType>>,
107 #endif
108  public ConvertibleTo<Dense<ValueType>>,
109  public ConvertibleTo<Csr<ValueType, IndexType>>,
110  public ConvertibleTo<SparsityCsr<ValueType, IndexType>>,
111  public DiagonalExtractable<ValueType>,
112  public ReadableFromMatrixData<ValueType, IndexType>,
113  public WritableToMatrixData<ValueType, IndexType>,
114  public Transposable,
116  remove_complex<Fbcsr<ValueType, IndexType>>> {
117  friend class EnablePolymorphicObject<Fbcsr, LinOp>;
118  friend class Csr<ValueType, IndexType>;
119  friend class Dense<ValueType>;
120  friend class SparsityCsr<ValueType, IndexType>;
121  friend class FbcsrBuilder<ValueType, IndexType>;
122  friend class Fbcsr<to_complex<ValueType>, IndexType>;
123 
124 public:
125  using value_type = ValueType;
126  using index_type = IndexType;
127  using transposed_type = Fbcsr<ValueType, IndexType>;
128  using mat_data = matrix_data<ValueType, IndexType>;
129  using device_mat_data = device_matrix_data<ValueType, IndexType>;
130  using absolute_type = remove_complex<Fbcsr>;
131 
138 
143  using EnableLinOp<Fbcsr<ValueType, IndexType>>::convert_to;
144 
145  using ConvertibleTo<
146  Fbcsr<next_precision<ValueType>, IndexType>>::convert_to;
147  using ConvertibleTo<Fbcsr<next_precision<ValueType>, IndexType>>::move_to;
148  using ConvertibleTo<Dense<ValueType>>::convert_to;
149  using ConvertibleTo<Dense<ValueType>>::move_to;
150  using ConvertibleTo<Csr<ValueType, IndexType>>::convert_to;
154 
155  friend class Fbcsr<previous_precision<ValueType>, IndexType>;
156 
157  void convert_to(
158  Fbcsr<next_precision<ValueType>, IndexType>* result) const override;
159 
160  void move_to(Fbcsr<next_precision<ValueType>, IndexType>* result) override;
161 
162 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
163  friend class Fbcsr<previous_precision<ValueType, 2>, IndexType>;
164  using ConvertibleTo<
165  Fbcsr<next_precision<ValueType, 2>, IndexType>>::convert_to;
166  using ConvertibleTo<
167  Fbcsr<next_precision<ValueType, 2>, IndexType>>::move_to;
168 
169  void convert_to(
170  Fbcsr<next_precision<ValueType, 2>, IndexType>* result) const override;
171 
172  void move_to(
173  Fbcsr<next_precision<ValueType, 2>, IndexType>* result) override;
174 #endif
175 
176 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
177  friend class Fbcsr<previous_precision<ValueType, 3>, IndexType>;
178  using ConvertibleTo<
179  Fbcsr<next_precision<ValueType, 3>, IndexType>>::convert_to;
180  using ConvertibleTo<
181  Fbcsr<next_precision<ValueType, 3>, IndexType>>::move_to;
182 
183  void convert_to(
184  Fbcsr<next_precision<ValueType, 3>, IndexType>* result) const override;
185 
186  void move_to(
187  Fbcsr<next_precision<ValueType, 3>, IndexType>* result) override;
188 #endif
189 
190  void convert_to(Dense<ValueType>* other) const override;
191 
192  void move_to(Dense<ValueType>* other) override;
193 
200  void convert_to(Csr<ValueType, IndexType>* result) const override;
201 
202  void move_to(Csr<ValueType, IndexType>* result) override;
203 
210  void convert_to(SparsityCsr<ValueType, IndexType>* result) const override;
211 
212  void move_to(SparsityCsr<ValueType, IndexType>* result) override;
213 
220  void read(const mat_data& data) override;
221 
222  void read(const device_mat_data& data) override;
223 
224  void read(device_mat_data&& data) override;
225 
226  void write(mat_data& data) const override;
227 
228  std::unique_ptr<LinOp> transpose() const override;
229 
230  std::unique_ptr<LinOp> conj_transpose() const override;
231 
232  std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
233 
234  std::unique_ptr<absolute_type> compute_absolute() const override;
235 
236  void compute_absolute_inplace() override;
237 
242  void sort_by_column_index();
243 
250  bool is_sorted_by_column_index() const;
251 
255  value_type* get_values() noexcept { return values_.get_data(); }
256 
264  const value_type* get_const_values() const noexcept
265  {
266  return values_.get_const_data();
267  }
268 
272  index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
273 
281  const index_type* get_const_col_idxs() const noexcept
282  {
283  return col_idxs_.get_const_data();
284  }
285 
289  index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
290 
298  const index_type* get_const_row_ptrs() const noexcept
299  {
300  return row_ptrs_.get_const_data();
301  }
302 
307  {
308  return values_.get_size();
309  }
310 
315  {
316  return col_idxs_.get_size();
317  }
318 
322  int get_block_size() const noexcept { return bs_; }
323 
327  index_type get_num_block_rows() const noexcept
328  {
329  return this->get_size()[0] / bs_;
330  }
331 
335  index_type get_num_block_cols() const noexcept
336  {
337  return this->get_size()[1] / bs_;
338  }
339 
349  static std::unique_ptr<Fbcsr> create(std::shared_ptr<const Executor> exec,
350  int block_size = 1);
351 
363  static std::unique_ptr<Fbcsr> create(std::shared_ptr<const Executor> exec,
364  const dim<2>& size,
365  size_type num_nonzeros,
366  int block_size);
367 
388  static std::unique_ptr<Fbcsr> create(std::shared_ptr<const Executor> exec,
389  const dim<2>& size, int block_size,
390  array<value_type> values,
391  array<index_type> col_idxs,
392  array<index_type> row_ptrs);
393 
399  template <typename InputValueType, typename InputColumnIndexType,
400  typename InputRowPtrType>
401  GKO_DEPRECATED(
402  "explicitly construct the gko::array argument instead of passing "
403  "initializer lists")
404  static std::unique_ptr<Fbcsr> create(
405  std::shared_ptr<const Executor> exec, const dim<2>& size,
406  int block_size, std::initializer_list<InputValueType> values,
407  std::initializer_list<InputColumnIndexType> col_idxs,
408  std::initializer_list<InputRowPtrType> row_ptrs)
409  {
410  return create(exec, size, block_size,
411  array<value_type>{exec, std::move(values)},
412  array<index_type>{exec, std::move(col_idxs)},
413  array<index_type>{exec, std::move(row_ptrs)});
414  }
415 
430  static std::unique_ptr<const Fbcsr> create_const(
431  std::shared_ptr<const Executor> exec, const dim<2>& size, int blocksize,
432  gko::detail::const_array_view<ValueType>&& values,
433  gko::detail::const_array_view<IndexType>&& col_idxs,
434  gko::detail::const_array_view<IndexType>&& row_ptrs);
435 
440  Fbcsr& operator=(const Fbcsr&);
441 
447  Fbcsr& operator=(Fbcsr&&);
448 
452  Fbcsr(const Fbcsr&);
453 
459  Fbcsr(Fbcsr&&);
460 
461 protected:
462  Fbcsr(std::shared_ptr<const Executor> exec, int block_size = 1);
463 
464  Fbcsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
465  size_type num_nonzeros, int block_size);
466 
467  Fbcsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
468  int block_size, array<value_type> values, array<index_type> col_idxs,
469  array<index_type> row_ptrs);
470 
471  void apply_impl(const LinOp* b, LinOp* x) const override;
472 
473  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
474  LinOp* x) const override;
475 
476 private:
477  int bs_;
478  array<value_type> values_;
479  array<index_type> col_idxs_;
480  array<index_type> row_ptrs_;
481 };
482 
483 
484 } // namespace matrix
485 } // namespace gko
486 
487 
488 #endif // GKO_PUBLIC_CORE_MATRIX_FBCSR_HPP_
gko::matrix::Fbcsr
Fixed-block compressed sparse row storage matrix format.
Definition: csr.hpp:46
gko::matrix::Fbcsr::Fbcsr
Fbcsr(const Fbcsr &)
Copy-constructs an Ell matrix.
gko::matrix::Fbcsr::get_block_size
int get_block_size() const noexcept
Definition: fbcsr.hpp:322
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Fbcsr::get_const_values
const value_type * get_const_values() const noexcept
Definition: fbcsr.hpp:264
gko::DiagonalExtractable
The diagonal of a LinOp implementing this interface can be extracted.
Definition: lin_op.hpp:743
gko::matrix::Fbcsr::extract_diagonal
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
gko::matrix::Fbcsr::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Definition: fbcsr.hpp:306
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:433
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::matrix::Fbcsr::is_sorted_by_column_index
bool is_sorted_by_column_index() const
Tests if all row entry pairs (value, col_idx) are sorted by column index.
gko::matrix::Fbcsr::get_values
value_type * get_values() noexcept
Definition: fbcsr.hpp:255
gko::matrix::Fbcsr::get_row_ptrs
index_type * get_row_ptrs() noexcept
Definition: fbcsr.hpp:289
gko::matrix::Fbcsr::get_num_block_cols
index_type get_num_block_cols() const noexcept
Definition: fbcsr.hpp:335
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix::Fbcsr::get_const_col_idxs
const index_type * get_const_col_idxs() const noexcept
Definition: fbcsr.hpp:281
gko::array< value_type >
gko::dim< 2 >
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
gko::matrix::Fbcsr::sort_by_column_index
void sort_by_column_index()
Sorts the values blocks and block-column indices in each row by column index.
gko::matrix::Fbcsr::get_const_row_ptrs
const index_type * get_const_row_ptrs() const noexcept
Definition: fbcsr.hpp:298
gko::array::get_data
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:687
gko::ReadableFromMatrixData
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition: lin_op.hpp:605
gko::matrix::Fbcsr::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::WritableToMatrixData
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:660
gko::matrix::Fbcsr::compute_absolute
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
gko::matrix::Fbcsr::get_num_block_rows
index_type get_num_block_rows() const noexcept
Definition: fbcsr.hpp:327
gko::matrix::Fbcsr::create
static std::unique_ptr< Fbcsr > create(std::shared_ptr< const Executor > exec, int block_size=1)
Creates an uninitialized FBCSR matrix with the given block size.
gko::next_precision
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition: math.hpp:466
gko::matrix::Fbcsr::operator=
Fbcsr & operator=(const Fbcsr &)
Copy-assigns an Fbcsr matrix.
gko::matrix::Fbcsr::get_num_stored_blocks
size_type get_num_stored_blocks() const noexcept
Definition: fbcsr.hpp:314
gko::previous_precision
typename detail::find_precision_impl< T, -step >::type previous_precision
Obtains the previous move type of T in the singly-linked precision corresponding bfloat16/half.
Definition: math.hpp:473
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:479
gko::matrix::Fbcsr::get_col_idxs
index_type * get_col_idxs() noexcept
Definition: fbcsr.hpp:272
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:615
gko::matrix::FbcsrBuilder
Definition: fbcsr.hpp:31
gko::array::get_const_data
const value_type * get_const_data() const noexcept
Returns a constant pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:696
gko::matrix::Fbcsr::compute_absolute_inplace
void compute_absolute_inplace() override
Compute absolute inplace on each element.
gko::EnableAbsoluteComputation
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition: lin_op.hpp:794
gko::matrix::Fbcsr::create_const
static std::unique_ptr< const Fbcsr > create_const(std::shared_ptr< const Executor > exec, const dim< 2 > &size, int blocksize, gko::detail::const_array_view< ValueType > &&values, gko::detail::const_array_view< IndexType > &&col_idxs, gko::detail::const_array_view< IndexType > &&row_ptrs)
Creates a constant (immutable) Fbcsr matrix from a constant array.
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:670
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:210
gko::matrix::Fbcsr::write
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
gko::matrix::Fbcsr::read
void read(const mat_data &data) override
Reads a matrix_data into Fbcsr format.
gko::remove_complex
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
gko::device_matrix_data
This type is a device-side equivalent to matrix_data.
Definition: device_matrix_data.hpp:36
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:877
gko::matrix::Fbcsr::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::to_complex
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition: math.hpp:283
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:667