Ginkgo  Generated from pipelines/1556235455 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
matrix_data.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
6 #define GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
7 
8 
9 #include <algorithm>
10 #include <numeric>
11 #include <tuple>
12 #include <vector>
13 
14 #include <ginkgo/core/base/dim.hpp>
15 #include <ginkgo/core/base/math.hpp>
16 #include <ginkgo/core/base/range.hpp>
17 #include <ginkgo/core/base/range_accessors.hpp>
18 #include <ginkgo/core/base/types.hpp>
19 #include <ginkgo/core/base/utils.hpp>
20 
21 
22 namespace gko {
23 
24 
25 namespace detail {
26 
27 
28 // internal structure used to get around explicit constructors in std::tuple
29 template <typename ValueType, typename IndexType>
30 struct input_triple {
31  IndexType row;
32  IndexType col;
33  ValueType val;
34 };
35 
36 
37 template <typename ValueType, typename Distribution, typename Generator>
38 typename std::enable_if<!is_complex_s<ValueType>::value, ValueType>::type
39 get_rand_value(Distribution&& dist, Generator&& gen)
40 {
41  return dist(gen);
42 }
43 
44 
45 template <typename ValueType, typename Distribution, typename Generator>
46 typename std::enable_if<is_complex_s<ValueType>::value, ValueType>::type
47 get_rand_value(Distribution&& dist, Generator&& gen)
48 {
49  return ValueType(dist(gen), dist(gen));
50 }
51 
52 
53 } // namespace detail
54 
55 
59 template <typename ValueType, typename IndexType>
61  using value_type = ValueType;
62  using index_type = IndexType;
63  matrix_data_entry() = default;
64 
65  GKO_ATTRIBUTES matrix_data_entry(index_type r, index_type c, value_type v)
66  : row(r), column(c), value(v)
67  {}
68 
69  bool operator==(const matrix_data_entry& other) const
70  {
71  return std::tie(this->row, this->column, this->value) ==
72  std::tie(other.row, other.column, other.value);
73  };
74  bool operator!=(const matrix_data_entry& other) const
75  {
76  return std::tie(this->row, this->column, this->value) !=
77  std::tie(other.row, other.column, other.value);
78  };
79 
80 #define GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(_op) \
81  bool operator _op(const matrix_data_entry& other) const \
82  { \
83  return std::tie(this->row, this->column) \
84  _op std::tie(other.row, other.column); \
85  }
86 
87  GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(<);
88  GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(>);
89  GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(<=);
90  GKO_DEFINE_DEFAULT_COMPARE_OPERATOR(>=);
91 
92 #undef GKO_DEFINE_DEFAULT_COMPARE_OPERATOR
93 
94  friend std::ostream& operator<<(std::ostream& os,
95  const matrix_data_entry& x)
96  {
97  os << '(' << x.row << ',' << x.column << ',' << x.value << ')';
98  return os;
99  }
100 
101  index_type row;
102  index_type column;
103  value_type value;
104 };
105 
106 
125 template <typename ValueType = default_precision, typename IndexType = int32>
126 struct matrix_data {
127  using value_type = ValueType;
128  using index_type = IndexType;
130 
137  matrix_data(dim<2> size_ = dim<2>{}, ValueType value = zero<ValueType>())
138  : size{size_}
139  {
140  if (is_zero(value)) {
141  return;
142  }
143  nonzeros.reserve(size[0] * size[1]);
144  for (size_type row = 0; row < size[0]; ++row) {
145  for (size_type col = 0; col < size[1]; ++col) {
146  nonzeros.emplace_back(row, col, value);
147  }
148  }
149  }
150 
161  template <typename RandomDistribution, typename RandomEngine>
162  matrix_data(dim<2> size_, RandomDistribution&& dist, RandomEngine&& engine)
163  : size{size_}
164  {
165  nonzeros.reserve(size[0] * size[1]);
166  for (size_type row = 0; row < size[0]; ++row) {
167  for (size_type col = 0; col < size[1]; ++col) {
168  const auto value =
169  detail::get_rand_value<ValueType>(dist, engine);
170  if (is_nonzero(value)) {
171  nonzeros.emplace_back(row, col, value);
172  }
173  }
174  }
175  }
176 
182  matrix_data(std::initializer_list<std::initializer_list<ValueType>> values)
183  : size{values.size(), 0}
184  {
185  for (size_type row = 0; row < values.size(); ++row) {
186  const auto row_data = begin(values)[row];
187  size[1] = std::max(size[1], row_data.size());
188  for (size_type col = 0; col < row_data.size(); ++col) {
189  const auto& val = begin(row_data)[col];
190  if (is_nonzero(val)) {
191  nonzeros.emplace_back(row, col, val);
192  }
193  }
194  }
195  }
196 
204  dim<2> size_,
205  std::initializer_list<detail::input_triple<ValueType, IndexType>>
206  nonzeros_)
207  : size{size_}, nonzeros()
208  {
209  nonzeros.reserve(nonzeros_.size());
210  for (const auto& elem : nonzeros_) {
211  nonzeros.emplace_back(elem.row, elem.col, elem.val);
212  }
213  }
214 
221  matrix_data(dim<2> size_, const matrix_data& block)
222  : size{size_ * block.size}
223  {
224  nonzeros.reserve(size_[0] * size_[1] * block.nonzeros.size());
225  for (size_type row = 0; row < size_[0]; ++row) {
226  for (size_type col = 0; col < size_[1]; ++col) {
227  for (const auto& elem : block.nonzeros) {
228  nonzeros.emplace_back(row * block.size[0] + elem.row,
229  col * block.size[1] + elem.column,
230  elem.value);
231  }
232  }
233  }
234  this->sort_row_major();
235  }
236 
244  template <typename Accessor>
246  : size{data.length(0), data.length(1)}
247  {
248  for (gko::size_type row = 0; row < size[0]; ++row) {
249  for (gko::size_type col = 0; col < size[1]; ++col) {
250  if (is_nonzero(data(row, col))) {
251  nonzeros.emplace_back(row, col, data(row, col));
252  }
253  }
254  }
255  }
256 
265  static matrix_data diag(dim<2> size_, ValueType value)
266  {
267  matrix_data res(size_);
268  if (is_nonzero(value)) {
269  const auto num_nnz = std::min(size_[0], size_[1]);
270  res.nonzeros.reserve(num_nnz);
271  for (size_type i = 0; i < num_nnz; ++i) {
272  res.nonzeros.emplace_back(i, i, value);
273  }
274  }
275  return res;
276  }
277 
286  static matrix_data diag(dim<2> size_,
287  std::initializer_list<ValueType> nonzeros_)
288  {
289  matrix_data res(size_);
290  res.nonzeros.reserve(nonzeros_.size());
291  int pos = 0;
292  for (auto value : nonzeros_) {
293  res.nonzeros.emplace_back(pos, pos, value);
294  ++pos;
295  }
296  return res;
297  }
298 
307  static matrix_data diag(dim<2> size_, const matrix_data& block)
308  {
309  matrix_data res(size_ * block.size);
310  const auto num_blocks = std::min(size_[0], size_[1]);
311  res.nonzeros.reserve(num_blocks * block.nonzeros.size());
312  for (size_type b = 0; b < num_blocks; ++b) {
313  for (const auto& elem : block.nonzeros) {
314  res.nonzeros.emplace_back(b * block.size[0] + elem.row,
315  b * block.size[1] + elem.column,
316  elem.value);
317  }
318  }
319  return res;
320  }
321 
333  template <typename ForwardIterator>
334  static matrix_data diag(ForwardIterator begin, ForwardIterator end)
335  {
336  matrix_data res(std::accumulate(
337  begin, end, dim<2>{}, [](dim<2> s, const matrix_data& d) {
338  return dim<2>{s[0] + d.size[0], s[1] + d.size[1]};
339  }));
340 
341  size_type row_offset{};
342  size_type col_offset{};
343  for (auto it = begin; it != end; ++it) {
344  for (const auto& elem : it->nonzeros) {
345  res.nonzeros.emplace_back(row_offset + elem.row,
346  col_offset + elem.column, elem.value);
347  }
348  row_offset += it->size[0];
349  col_offset += it->size[1];
350  }
351 
352  return res;
353  }
354 
363  static matrix_data diag(std::initializer_list<matrix_data> blocks)
364  {
365  return diag(begin(blocks), end(blocks));
366  }
367 
387  template <typename RandomDistribution, typename RandomEngine>
389  remove_complex<ValueType> condition_number,
390  RandomDistribution&& dist, RandomEngine&& engine,
391  size_type num_reflectors)
392  {
394  std::vector<ValueType> mtx_data(size * size, zero<ValueType>());
395  std::vector<ValueType> ref_data(size);
396  std::vector<ValueType> work(size);
397  range matrix(mtx_data.data(), size, size, size);
398  range reflector(ref_data.data(), size, 1u, 1u);
399 
400  initialize_diag_with_cond(condition_number, matrix);
401  for (size_type i = 0; i < num_reflectors; ++i) {
402  generate_random_reflector(dist, engine, reflector);
403  reflect_domain(reflector, matrix, work.data());
404  generate_random_reflector(dist, engine, reflector);
405  reflect_range(reflector, matrix, work.data());
406  }
407  return matrix;
408  }
409 
431  template <typename RandomDistribution, typename RandomEngine>
433  remove_complex<ValueType> condition_number,
434  RandomDistribution&& dist, RandomEngine&& engine)
435  {
436  return cond(size, condition_number,
437  std::forward<RandomDistribution>(dist),
438  std::forward<RandomEngine>(engine), size - 1);
439  }
440 
445 
453  std::vector<nonzero_type> nonzeros;
454 
459  {
460  std::sort(
461  begin(nonzeros), end(nonzeros), [](nonzero_type x, nonzero_type y) {
462  return std::tie(x.row, x.column) < std::tie(y.row, y.column);
463  });
464  }
465 
469  GKO_DEPRECATED("Use sort_row_major() instead") void ensure_row_major_order()
470  {
471  this->sort_row_major();
472  }
473 
478  {
479  nonzeros.erase(
480  std::remove_if(begin(nonzeros), end(nonzeros),
481  [](nonzero_type nz) { return is_zero(nz.value); }),
482  end(nonzeros));
483  }
484 
490  {
491  this->sort_row_major();
492  std::vector<nonzero_type> new_nonzeros;
493  if (!nonzeros.empty()) {
494  new_nonzeros.emplace_back(nonzeros.front().row,
495  nonzeros.front().column,
496  zero<ValueType>());
497  for (auto entry : nonzeros) {
498  if (entry.row != new_nonzeros.back().row ||
499  entry.column != new_nonzeros.back().column) {
500  new_nonzeros.emplace_back(entry.row, entry.column,
501  zero<ValueType>());
502  }
503  new_nonzeros.back().value += entry.value;
504  }
505  nonzeros = std::move(new_nonzeros);
506  }
507  }
508 
509 private:
510  template <typename Accessor>
511  static void initialize_diag_with_cond(
512  remove_complex<ValueType> condition_number,
513  const range<Accessor>& matrix)
514  {
515  using sigma_type = remove_complex<ValueType>;
516  const auto size = matrix.length(0);
517  const auto min_sigma = one(condition_number) / sqrt(condition_number);
518  const auto max_sigma = sqrt(condition_number);
519 
520  matrix = zero(matrix);
521  for (gko::size_type i = 0; i < size; ++i) {
522  matrix(i, i) = max_sigma * static_cast<sigma_type>(size - i - 1) /
523  static_cast<sigma_type>(size - 1) +
524  min_sigma * static_cast<sigma_type>(i) /
525  static_cast<sigma_type>(size - 1);
526  }
527  }
528 
529  template <typename RandomDistribution, typename RandomEngine,
530  typename Accessor>
531  static void generate_random_reflector(RandomDistribution&& dist,
532  RandomEngine&& engine,
533  const range<Accessor>& reflector)
534  {
535  for (gko::size_type i = 0; i < reflector.length(0); ++i) {
536  reflector(i, 0) = detail::get_rand_value<ValueType>(dist, engine);
537  }
538  }
539 
540  template <typename Accessor>
541  static void reflect_domain(const range<Accessor>& reflector,
542  const range<Accessor>& matrix,
543  ValueType* work_data)
544  {
545  const auto two = one<ValueType>() + one<ValueType>();
546  range<accessor::row_major<ValueType, 2>> work(work_data,
547  matrix.length(0), 1u, 1u);
548  work = mmul(matrix, reflector);
549  const auto ct_reflector = conj(transpose(reflector));
550  const auto scale = two / mmul(ct_reflector, reflector)(0, 0);
551  matrix = matrix - scale * mmul(work, ct_reflector);
552  }
553 
554  template <typename Accessor>
555  static void reflect_range(const range<Accessor>& reflector,
556  const range<Accessor>& matrix,
557  ValueType* work_data)
558  {
559  const auto two = one<ValueType>() + one<ValueType>();
560  range<accessor::row_major<ValueType, 2>> work(
561  work_data, 1u, matrix.length(0), matrix.length(0));
562  const auto ct_reflector = conj(transpose(reflector));
563  work = mmul(ct_reflector, matrix);
564  const auto scale = two / mmul(ct_reflector, reflector)(0, 0);
565  matrix = matrix - scale * mmul(reflector, work);
566  }
567 };
568 
569 
570 } // namespace gko
571 
572 
573 #endif // GKO_PUBLIC_CORE_BASE_MATRIX_DATA_HPP_
gko::is_zero
constexpr bool is_zero(T value)
Returns true if and only if the given value is zero.
Definition: math.hpp:683
gko::matrix_data::nonzeros
std::vector< nonzero_type > nonzeros
A vector of tuples storing the non-zeros of the matrix.
Definition: matrix_data.hpp:453
gko::matrix_data::matrix_data
matrix_data(dim< 2 > size_, std::initializer_list< detail::input_triple< ValueType, IndexType >> nonzeros_)
Initializes the structure from a list of nonzeros.
Definition: matrix_data.hpp:203
gko::matrix_data_entry
Type used to store nonzeros.
Definition: matrix_data.hpp:60
gko::matrix_data::cond
static matrix_data cond(size_type size, remove_complex< ValueType > condition_number, RandomDistribution &&dist, RandomEngine &&engine, size_type num_reflectors)
Initializes a random dense matrix with a specific condition number.
Definition: matrix_data.hpp:388
gko::matrix_data::diag
static matrix_data diag(ForwardIterator begin, ForwardIterator end)
Initializes a block-diagonal matrix from a list of diagonal blocks.
Definition: matrix_data.hpp:334
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:86
gko::matrix_data::matrix_data
matrix_data(dim< 2 > size_=dim< 2 >{}, ValueType value=zero< ValueType >())
Initializes a matrix filled with the specified value.
Definition: matrix_data.hpp:137
gko::is_nonzero
constexpr bool is_nonzero(T value)
Returns true if and only if the given value is not zero.
Definition: math.hpp:698
gko::matrix_data::diag
static matrix_data diag(dim< 2 > size_, std::initializer_list< ValueType > nonzeros_)
Initializes a diagonal matrix using a list of diagonal elements.
Definition: matrix_data.hpp:286
gko::matrix_data::remove_zeros
void remove_zeros()
Remove entries with value zero from the matrix data.
Definition: matrix_data.hpp:477
gko::range
A range is a multidimensional view of the memory.
Definition: range.hpp:297
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix_data::matrix_data
matrix_data(dim< 2 > size_, const matrix_data &block)
Initializes a matrix out of a matrix block via duplication.
Definition: matrix_data.hpp:221
gko::matrix_data::matrix_data
matrix_data(std::initializer_list< std::initializer_list< ValueType >> values)
List-initializes the structure from a matrix of values.
Definition: matrix_data.hpp:182
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_data::size
dim< 2 > size
Size of the matrix.
Definition: matrix_data.hpp:444
gko::conj
constexpr auto conj(const T &x)
Returns the conjugate of an object.
Definition: math.hpp:914
gko::matrix_data::ensure_row_major_order
void ensure_row_major_order()
Sorts the nonzero vector so the values follow row-major order.
Definition: matrix_data.hpp:469
gko::transpose
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
gko::range::length
constexpr size_type length(size_type dimension) const
Returns the length of the specified dimension of the range.
Definition: range.hpp:400
gko::matrix_data::sum_duplicates
void sum_duplicates()
Sum up all values that refer to the same matrix entry.
Definition: matrix_data.hpp:489
gko::matrix_data::sort_row_major
void sort_row_major()
Sorts the nonzero vector so the values follow row-major order.
Definition: matrix_data.hpp:458
gko::matrix_data::matrix_data
matrix_data(const range< Accessor > &data)
Initializes a matrix from a range.
Definition: matrix_data.hpp:245
gko::matrix_data::matrix_data
matrix_data(dim< 2 > size_, RandomDistribution &&dist, RandomEngine &&engine)
Initializes a matrix with random values from the specified distribution.
Definition: matrix_data.hpp:162
gko::matrix_data::diag
static matrix_data diag(dim< 2 > size_, ValueType value)
Initializes a diagonal matrix.
Definition: matrix_data.hpp:265
gko::matrix_data::cond
static matrix_data cond(size_type size, remove_complex< ValueType > condition_number, RandomDistribution &&dist, RandomEngine &&engine)
Initializes a random dense matrix with a specific condition number.
Definition: matrix_data.hpp:432
gko::matrix_data::diag
static matrix_data diag(std::initializer_list< matrix_data > blocks)
Initializes a block-diagonal matrix from a list of diagonal blocks.
Definition: matrix_data.hpp:363
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:325
gko::matrix_data::diag
static matrix_data diag(dim< 2 > size_, const matrix_data &block)
Initializes a block-diagonal matrix.
Definition: matrix_data.hpp:307
gko::zero
constexpr T zero()
Returns the additive identity for T.
Definition: math.hpp:624
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:652