Ginkgo  Generated from pipelines/3133041809 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
multivector_mixin.hpp
1 // SPDX-FileCopyrightText: 2025 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #pragma once
6 
7 #include <tuple>
8 #include <variant>
9 
10 #include <ginkgo/config.hpp>
11 #include <ginkgo/core/base/multivector.hpp>
12 
13 
14 namespace gko {
15 namespace matrix {
16 
17 
18 template <typename ValueType>
19 class MultiVector;
20 
21 
22 }
23 
24 
42 template <typename ConcreteType>
44 
54 template <template <typename...> typename ConcreteType, typename ValueType,
55  typename... Args>
56 struct vector_traits<ConcreteType<ValueType, Args...>> {
57  using value_type = ValueType;
58  using absolute_value_type = remove_complex<value_type>;
59  using absolute_type = ConcreteType<absolute_value_type, Args...>;
60  using real_type = absolute_type;
61  using complex_value_type = to_complex<value_type>;
62  using complex_type = ConcreteType<complex_value_type, Args...>;
63 };
64 
65 
78 template <typename ValueType>
79 struct scaling_param {
80  using variant_type = std::variant<const matrix::MultiVector<ValueType>*>;
81 
83  variant_type variant;
84 };
85 
90 template <typename ValueType>
91 struct scaling_param<std::complex<ValueType>> {
92  using variant_type =
93  std::variant<const matrix::MultiVector<ValueType>*,
95 
97  variant_type variant;
98 };
99 
100 
151 template <typename ConcreteType>
153  public ConvertibleTo<ConcreteType> {
154 public:
158  using value_type = typename traits::value_type;
160  using absolute_value_type = typename traits::absolute_value_type;
162  using absolute_type = typename traits::absolute_type;
164  using real_type = typename traits::real_type;
166  using complex_type = typename traits::complex_type;
168  using result_type = ConcreteType;
174  using const_device_view =
176 
181 
188  [[nodiscard]] static std::unique_ptr<ConcreteType> create_with_config_of(
190 
197  [[nodiscard]] static std::unique_ptr<ConcreteType> create_with_type_of(
199  std::shared_ptr<const Executor> exec);
200 
208  [[nodiscard]] static std::unique_ptr<ConcreteType> create_with_type_of(
210  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
211  const dim<2>& local_size);
212 
220  [[nodiscard]] static std::unique_ptr<ConcreteType> create_with_type_of(
222  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
223  const dim<2>& local_size, size_type stride);
224 
230  [[nodiscard]] std::unique_ptr<ConcreteType> clone(
231  std::shared_ptr<const Executor> exec) const;
232 
238  [[nodiscard]] std::unique_ptr<ConcreteType> clone() const;
239 
246  ConcreteType* copy_from(ptr_param<const ConcreteType> other);
247 
248 
254  ConcreteType* move_from(ptr_param<ConcreteType> other);
255 
261  [[nodiscard]] std::unique_ptr<ConcreteType> create_default() const;
262 
269  [[nodiscard]] std::unique_ptr<ConcreteType> create_default(
270  std::shared_ptr<const Executor> exec) const;
271 
277  [[nodiscard]] std::unique_ptr<ConcreteType> create_subview(
278  local_span rows, local_span columns);
279 
285  [[nodiscard]] std::unique_ptr<const ConcreteType> create_subview(
286  local_span rows, local_span columns) const;
287 
294  [[nodiscard]] std::unique_ptr<ConcreteType> create_subview(
295  local_span rows, local_span columns, dim<2> global_size);
296 
303  [[nodiscard]] std::unique_ptr<const ConcreteType> create_subview(
304  local_span rows, local_span columns, dim<2> global_size) const;
305 
311  [[nodiscard]] std::unique_ptr<const real_type> create_real_view() const;
312 
318  [[nodiscard]] std::unique_ptr<real_type> create_real_view();
319 
325  [[nodiscard]] std::unique_ptr<absolute_type> compute_absolute() const;
326 
334  void compute_absolute(ptr_param<absolute_type> output) const;
335 
341  [[nodiscard]] std::unique_ptr<complex_type> make_complex() const;
342 
350  void make_complex(ptr_param<complex_type> output) const;
351 
357  [[nodiscard]] std::unique_ptr<real_type> get_real() const;
358 
365  void get_real(ptr_param<real_type> output) const;
366 
372  [[nodiscard]] std::unique_ptr<real_type> get_imag() const;
373 
380  void get_imag(ptr_param<real_type> output) const;
381 
387  void convert_to(result_type* result) const override;
388 
394  void move_to(result_type* result) override;
395 
405  [[nodiscard]] device_view get_local_device_view();
406 
408  [[nodiscard]] const_device_view get_const_local_device_view() const;
409 
410 protected:
418  EnableMultiVector(std::shared_ptr<const Executor> exec)
419  : AbstractMultiVector(exec, {}, {}, precision_v<value_type>)
420  {}
421 
432  EnableMultiVector(std::shared_ptr<const Executor> exec, dim<2> size,
433  dim<2> local_size)
434  : AbstractMultiVector(exec, size, local_size, precision_v<value_type>)
435  {}
436 
437  Cloneable* copy_from_impl(const Cloneable* other) override;
438 
439  Cloneable* move_from_impl(Cloneable* other) override;
440 
441  [[nodiscard]] std::unique_ptr<Cloneable> clone_impl(
442  std::shared_ptr<const Executor> exec) const override;
443 
444  [[nodiscard]] std::unique_ptr<Cloneable> clone_impl() const override;
445 
446  [[nodiscard]] std::unique_ptr<Cloneable> create_default_impl()
447  const override;
448 
449  [[nodiscard]] std::unique_ptr<Cloneable> create_default_impl(
450  std::shared_ptr<const Executor> exec) const override;
451 
452  // Concretized virtual function calls
453  //
454  // These are the functions that classes using this mixin have to implement.
455  // All arguments passed to them are already concretized, i.e. they are
456  // stored on the executor of this vector, their dimensions have been
457  // checked, and their precision matches the one required by the operation.
458  // Thus, implementations don't have to handle any conversion themselves.
459 
460  [[nodiscard]] virtual std::unique_ptr<ConcreteType>
461  create_with_same_config_impl() const = 0;
462 
463  [[nodiscard]] virtual std::unique_ptr<ConcreteType>
464  create_with_type_of_impl(std::shared_ptr<const Executor> exec,
465  const dim<2>& global_size,
466  const dim<2>& local_size,
467  size_type stride) const = 0;
468 
469  [[nodiscard]] virtual std::unique_ptr<ConcreteType> create_subview_impl(
470  local_span rows, local_span columns) = 0;
471 
472  [[nodiscard]] virtual std::unique_ptr<const ConcreteType>
473  create_subview_impl(local_span rows, local_span columns) const = 0;
474 
475  [[nodiscard]] virtual std::unique_ptr<ConcreteType> create_subview_impl(
476  local_span rows, local_span columns, dim<2> global_size) = 0;
477 
478  [[nodiscard]] virtual std::unique_ptr<const ConcreteType>
479  create_subview_impl(local_span rows, local_span columns,
480  dim<2> global_size) const = 0;
481 
482  [[nodiscard]] virtual std::unique_ptr<const real_type>
483  create_real_view_impl() const = 0;
484 
485  [[nodiscard]] virtual std::unique_ptr<real_type>
486  create_real_view_impl() = 0;
487 
488  [[nodiscard]] virtual std::unique_ptr<absolute_type> compute_absolute_impl()
489  const = 0;
490 
491  virtual void compute_absolute_impl(absolute_type* result) const = 0;
492 
493  [[nodiscard]] virtual std::unique_ptr<complex_type> make_complex_impl()
494  const = 0;
495 
496  [[nodiscard]] virtual std::unique_ptr<real_type> get_real_impl() const = 0;
497 
498  [[nodiscard]] virtual std::unique_ptr<real_type> get_imag_impl() const = 0;
499 
500  virtual void make_complex_impl(complex_type* result) const = 0;
501 
502  virtual void get_real_impl(real_type* result) const = 0;
503 
504  virtual void get_imag_impl(real_type* result) const = 0;
505 
506  virtual void fill_impl(value_type value) = 0;
507 
508  virtual void scale_impl(scaling_param<value_type> alpha) = 0;
509 
510  virtual void inv_scale_impl(scaling_param<value_type> alpha) = 0;
511 
512  virtual void add_scaled_impl(scaling_param<value_type> alpha,
513  const ConcreteType* b) = 0;
514 
515  virtual void sub_scaled_impl(scaling_param<value_type> alpha,
516  const ConcreteType* b) = 0;
517 
518  virtual void compute_dot_impl(const ConcreteType* b,
519  matrix::MultiVector<value_type>* result,
520  array<char>& tmp) const = 0;
521 
522  virtual void compute_conj_dot_impl(const ConcreteType* b,
523  matrix::MultiVector<value_type>* result,
524  array<char>& tmp) const = 0;
525 
526  virtual void compute_norm2_impl(norm_type* result,
527  array<char>& tmp) const = 0;
528 
529  virtual void compute_squared_norm2_impl(norm_type* result,
530  array<char>& tmp) const = 0;
531 
532  virtual void compute_norm1_impl(norm_type* result,
533  array<char>& tmp) const = 0;
534 
535  [[nodiscard]] temporary_conversion<AbstractMultiVector> as_precision_impl(
536  precision p) override;
537 
538  [[nodiscard]] temporary_conversion<const AbstractMultiVector>
539  as_precision_impl(precision p) const override;
540 
541  virtual AbstractMultiVector::device_view<value_type>
542  get_local_device_view_impl() = 0;
543 
544  virtual AbstractMultiVector::device_view<const value_type>
545  get_const_local_device_view_impl() const = 0;
546 
547  [[nodiscard]] std::variant<
548 #if GINKGO_ENABLE_HALF
549  AbstractMultiVector::device_view<half>,
550  AbstractMultiVector::device_view<std::complex<half>>,
551 #endif
552 #if GINKGO_ENABLE_BFLOAT16
553  AbstractMultiVector::device_view<bfloat16>,
554  AbstractMultiVector::device_view<std::complex<bfloat16>>,
555 #endif
556  AbstractMultiVector::device_view<float>,
557  AbstractMultiVector::device_view<std::complex<float>>,
558  AbstractMultiVector::device_view<double>,
559  AbstractMultiVector::device_view<std::complex<double>>>
560  get_local_device_view_generic_impl() override;
561 
562  [[nodiscard]] std::variant<
563 #if GINKGO_ENABLE_HALF
564  AbstractMultiVector::device_view<const half>,
565  AbstractMultiVector::device_view<const std::complex<half>>,
566 #endif
567 #if GINKGO_ENABLE_BFLOAT16
568  AbstractMultiVector::device_view<const bfloat16>,
569  AbstractMultiVector::device_view<const std::complex<bfloat16>>,
570 #endif
571  AbstractMultiVector::device_view<const float>,
572  AbstractMultiVector::device_view<const std::complex<float>>,
573  AbstractMultiVector::device_view<const double>,
574  AbstractMultiVector::device_view<const std::complex<double>>>
575  get_const_local_device_view_generic_impl() const override;
576 
577  GKO_ENABLE_SELF(ConcreteType);
578 
579  // Overridden generic functions
580  //
581  // These implement the type-erased interface of AbstractMultiVector by
582  // dispatching to the concretized functions above, see the class
583  // documentation. They are final, so classes using this mixin can't
584  // override them and have to implement the concretized functions instead.
585  // Precision combinations that can't be dispatched, e.g. scaling a real
586  // vector by a complex factor, throw NotImplemented.
587 
588  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
589  create_generic_with_same_config_impl() const final;
590 
591  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
592  create_generic_with_type_of_impl(std::shared_ptr<const Executor> exec,
593  const dim<2>& global_size,
594  const dim<2>& local_size,
595  size_type stride) const final;
596 
597  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
598  create_subview_generic_impl(local_span rows, local_span columns) final;
599 
600  [[nodiscard]] std::unique_ptr<const AbstractMultiVector>
601  create_subview_generic_impl(local_span rows,
602  local_span columns) const final;
603 
604  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
605  create_subview_generic_impl(local_span rows, local_span columns,
606  dim<2> global_size) final;
607 
608  [[nodiscard]] std::unique_ptr<const AbstractMultiVector>
609  create_subview_generic_impl(local_span rows, local_span columns,
610  dim<2> global_size) const final;
611 
612  [[nodiscard]] std::unique_ptr<const AbstractMultiVector>
613  create_real_view_generic_impl() const final;
614 
615  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
616  create_real_view_generic_impl() final;
617 
618  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
619  compute_absolute_generic_impl() const final;
620 
621  void compute_absolute_generic_impl(AbstractMultiVector* result) const final;
622 
623  [[nodiscard]] std::unique_ptr<AbstractMultiVector>
624  make_complex_generic_impl() const final;
625 
626  void make_complex_generic_impl(AbstractMultiVector* result) const final;
627 
628  [[nodiscard]] std::unique_ptr<AbstractMultiVector> get_real_generic_impl()
629  const final;
630 
631  void get_real_generic_impl(AbstractMultiVector* result) const final;
632 
633  [[nodiscard]] std::unique_ptr<AbstractMultiVector> get_imag_generic_impl()
634  const final;
635 
636  void get_imag_generic_impl(AbstractMultiVector* result) const final;
637 
638  void fill_impl(any_scalar value) final;
639 
640  void scale_impl(const AbstractMultiVector* alpha) final;
641 
642  void inv_scale_impl(const AbstractMultiVector* alpha) final;
643 
644  void add_scaled_impl(const AbstractMultiVector* alpha,
645  const AbstractMultiVector* b) final;
646 
647  void sub_scaled_impl(const AbstractMultiVector* alpha,
648  const AbstractMultiVector* b) final;
649 
650  void compute_dot_impl(const AbstractMultiVector* b,
651  AbstractMultiVector* result,
652  array<char>& tmp) const final;
653 
654  void compute_conj_dot_impl(const AbstractMultiVector* b,
655  AbstractMultiVector* result,
656  array<char>& tmp) const final;
657 
658  void compute_norm2_impl(AbstractMultiVector* result,
659  array<char>& tmp) const final;
660 
661  void compute_squared_norm2_impl(AbstractMultiVector* result,
662  array<char>& tmp) const final;
663 
664  void compute_norm1_impl(AbstractMultiVector* result,
665  array<char>& tmp) const final;
666 };
667 
668 
669 template <typename ConcreteType>
670 std::unique_ptr<ConcreteType>
672  ptr_param<const ConcreteType> other)
673 {
674  return static_cast<const EnableMultiVector*>(other.get())
675  ->create_with_same_config_impl();
676 }
677 
678 
679 template <typename ConcreteType>
680 std::unique_ptr<ConcreteType>
682  ptr_param<const ConcreteType> other, std::shared_ptr<const Executor> exec)
683 {
684  return static_cast<const EnableMultiVector*>(other.get())
685  ->create_with_type_of_impl(std::move(exec), {}, {}, 0);
686 }
687 
688 
689 template <typename ConcreteType>
690 std::unique_ptr<ConcreteType>
692  ptr_param<const ConcreteType> other, std::shared_ptr<const Executor> exec,
693  const dim<2>& global_size, const dim<2>& local_size)
694 {
695  GKO_ASSERT_EQUAL_COLS(global_size, local_size);
696  return static_cast<const EnableMultiVector*>(other.get())
697  ->create_with_type_of_impl(std::move(exec), global_size, local_size,
698  local_size[1]);
699 }
700 
701 
702 template <typename ConcreteType>
703 std::unique_ptr<ConcreteType>
705  ptr_param<const ConcreteType> other, std::shared_ptr<const Executor> exec,
706  const dim<2>& global_size, const dim<2>& local_size, size_type stride)
707 {
708  return static_cast<const EnableMultiVector*>(other.get())
709  ->create_with_type_of_impl(std::move(exec), global_size, local_size,
710  stride);
711 }
712 
713 
714 template <typename ConcreteType>
715 std::unique_ptr<ConcreteType> EnableMultiVector<ConcreteType>::clone(
716  std::shared_ptr<const Executor> exec) const
717 {
718  return as<ConcreteType>(this->clone_impl(std::move(exec)));
719 }
720 
721 
722 template <typename ConcreteType>
723 std::unique_ptr<ConcreteType> EnableMultiVector<ConcreteType>::clone() const
724 {
725  return as<ConcreteType>(this->clone_impl());
726 }
727 
728 
729 template <typename ConcreteType>
732 {
733  return as<ConcreteType>(this->copy_from_impl(other.get()));
734 }
735 
736 
737 template <typename ConcreteType>
740 {
741  return as<ConcreteType>(this->move_from_impl(other.get()));
742 }
743 
744 
745 template <typename ConcreteType>
746 std::unique_ptr<ConcreteType> EnableMultiVector<ConcreteType>::create_default()
747  const
748 {
749  return as<ConcreteType>(this->create_default_impl());
750 }
751 
752 template <typename ConcreteType>
754  std::shared_ptr<const Executor> exec) const
755 {
756  return as<ConcreteType>(this->create_default_impl(std::move(exec)));
757 }
758 
759 
760 template <typename ConcreteType>
762  local_span rows, local_span columns)
763 {
764  return this->create_subview_impl(rows, columns);
765 }
766 
767 
768 template <typename ConcreteType>
769 std::unique_ptr<const ConcreteType>
771  local_span columns) const
772 {
773  return this->create_subview_impl(rows, columns);
774 }
775 
776 
777 template <typename ConcreteType>
779  local_span rows, local_span columns, dim<2> global_size)
780 {
781  return this->create_subview_impl(rows, columns, global_size);
782 }
783 
784 
785 template <typename ConcreteType>
786 std::unique_ptr<const ConcreteType>
788  local_span columns,
789  dim<2> global_size) const
790 {
791  return this->create_subview_impl(rows, columns, global_size);
792 }
793 
794 
795 template <typename ConcreteType>
796 std::unique_ptr<const typename EnableMultiVector<ConcreteType>::real_type>
798 {
799  return this->create_real_view_impl();
800 }
801 
802 
803 template <typename ConcreteType>
804 std::unique_ptr<typename EnableMultiVector<ConcreteType>::real_type>
806 {
807  return this->create_real_view_impl();
808 }
809 
810 
811 template <typename ConcreteType>
812 std::unique_ptr<typename EnableMultiVector<ConcreteType>::absolute_type>
814 {
815  return this->compute_absolute_impl();
816 }
817 
818 
819 template <typename ConcreteType>
821  ptr_param<absolute_type> output) const
822 {
823  GKO_ASSERT_EQUAL_DIMENSIONS(this, output);
824  auto exec = this->get_executor();
825  this->compute_absolute_impl(
826  make_temporary_output_clone(exec, output.get()).get());
827 }
828 
829 
830 template <typename ConcreteType>
831 std::unique_ptr<typename EnableMultiVector<ConcreteType>::complex_type>
833 {
834  return this->make_complex_impl();
835 }
836 
837 template <typename ConcreteType>
839  ptr_param<complex_type> output) const
840 {
841  GKO_ASSERT_EQUAL_DIMENSIONS(this, output);
842  auto exec = this->get_executor();
843  this->make_complex_impl(
844  make_temporary_output_clone(exec, output.get()).get());
845 }
846 
847 
848 template <typename ConcreteType>
849 std::unique_ptr<typename EnableMultiVector<ConcreteType>::real_type>
851 {
852  return this->get_real_impl();
853 }
854 
855 
856 template <typename ConcreteType>
858  ptr_param<real_type> output) const
859 {
860  GKO_ASSERT_EQUAL_DIMENSIONS(this, output);
861  auto exec = this->get_executor();
862  this->get_real_impl(make_temporary_output_clone(exec, output.get()).get());
863 }
864 
865 
866 template <typename ConcreteType>
867 std::unique_ptr<typename EnableMultiVector<ConcreteType>::real_type>
869 {
870  return this->get_imag_impl();
871 }
872 
873 
874 template <typename ConcreteType>
876  ptr_param<real_type> output) const
877 {
878  GKO_ASSERT_EQUAL_DIMENSIONS(this, output);
879  auto exec = this->get_executor();
880  this->get_imag_impl(make_temporary_output_clone(exec, output.get()).get());
881 }
882 
883 
884 template <typename ConcreteType>
886 {
887  *result = *self();
888 }
889 
890 
891 template <typename ConcreteType>
893 {
894  *result = std::move(*self());
895 }
896 
897 
898 template <typename ConcreteType>
901 {
902  return this->get_local_device_view_impl();
903 }
904 
905 
906 template <typename ConcreteType>
909 {
910  return this->get_const_local_device_view_impl();
911 }
912 
913 
914 template <typename ConcreteType>
916  const Cloneable* other)
917 {
918  self()->template log<log::Logger::polymorphic_object_copy_started>(
919  self()->get_executor().get(),
920  dynamic_cast<const PolymorphicObject*>(other),
921  dynamic_cast<const PolymorphicObject*>(this));
922  as<ConvertibleTo<ConcreteType>>(other)->convert_to(self());
923  self()->template log<log::Logger::polymorphic_object_copy_completed>(
924  self()->get_executor().get(),
925  dynamic_cast<const PolymorphicObject*>(other),
926  dynamic_cast<const PolymorphicObject*>(this));
927  return this;
928 }
929 
930 
931 template <typename ConcreteType>
932 Cloneable* EnableMultiVector<ConcreteType>::move_from_impl(Cloneable* other)
933 {
934  self()->template log<log::Logger::polymorphic_object_move_started>(
935  self()->get_executor().get(),
936  dynamic_cast<const PolymorphicObject*>(other),
937  dynamic_cast<const PolymorphicObject*>(this));
938  as<ConvertibleTo<ConcreteType>>(other)->move_to(self());
939  self()->template log<log::Logger::polymorphic_object_move_completed>(
940  self()->get_executor().get(),
941  dynamic_cast<const PolymorphicObject*>(other),
942  dynamic_cast<const PolymorphicObject*>(this));
943  return this;
944 }
945 
946 
947 template <typename ConcreteType>
948 std::unique_ptr<Cloneable> EnableMultiVector<ConcreteType>::clone_impl(
949  std::shared_ptr<const Executor> exec) const
950 {
951  auto result = this->create_default_impl(exec);
952  result->copy_from(this);
953  return result;
954 }
955 
956 
957 template <typename ConcreteType>
958 std::unique_ptr<Cloneable> EnableMultiVector<ConcreteType>::clone_impl() const
959 {
960  auto result = this->create_default_impl();
961  result->copy_from(this);
962  return result;
963 }
964 
965 
966 template <typename ConcreteType>
967 std::unique_ptr<Cloneable>
968 EnableMultiVector<ConcreteType>::create_default_impl() const
969 {
970  return this->create_with_type_of_impl(this->get_executor(), {}, {}, 0);
971 }
972 
973 
974 template <typename ConcreteType>
975 std::unique_ptr<Cloneable> EnableMultiVector<ConcreteType>::create_default_impl(
976  std::shared_ptr<const Executor> exec) const
977 {
978  return this->create_with_type_of_impl(exec, {}, {}, 0);
979 }
980 
981 
982 template <typename ConcreteType>
983 std::unique_ptr<AbstractMultiVector>
984 EnableMultiVector<ConcreteType>::create_generic_with_same_config_impl() const
985 {
986  return this->create_with_same_config_impl();
987 }
988 
989 
990 template <typename ConcreteType>
991 std::unique_ptr<AbstractMultiVector>
992 EnableMultiVector<ConcreteType>::create_generic_with_type_of_impl(
993  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
994  const dim<2>& local_size, size_type stride) const
995 {
996  return this->create_with_type_of_impl(std::move(exec), global_size,
997  local_size, stride);
998 }
999 
1000 
1001 template <typename ConcreteType>
1002 std::unique_ptr<AbstractMultiVector>
1003 EnableMultiVector<ConcreteType>::create_subview_generic_impl(local_span rows,
1004  local_span columns)
1005 {
1006  return this->create_subview_impl(rows, columns);
1007 }
1008 
1009 
1010 template <typename ConcreteType>
1011 std::unique_ptr<const AbstractMultiVector>
1012 EnableMultiVector<ConcreteType>::create_subview_generic_impl(
1013  local_span rows, local_span columns) const
1014 {
1015  return this->create_subview_impl(rows, columns);
1016 }
1017 
1018 
1019 template <typename ConcreteType>
1020 std::unique_ptr<AbstractMultiVector>
1021 EnableMultiVector<ConcreteType>::create_subview_generic_impl(local_span rows,
1022  local_span columns,
1023  dim<2> global_size)
1024 {
1025  return this->create_subview_impl(rows, columns, global_size);
1026 }
1027 
1028 
1029 template <typename ConcreteType>
1030 std::unique_ptr<const AbstractMultiVector>
1031 EnableMultiVector<ConcreteType>::create_subview_generic_impl(
1032  local_span rows, local_span columns, dim<2> global_size) const
1033 {
1034  return this->create_subview_impl(rows, columns, global_size);
1035 }
1036 
1037 
1038 template <typename ConcreteType>
1039 std::unique_ptr<const AbstractMultiVector>
1040 EnableMultiVector<ConcreteType>::create_real_view_generic_impl() const
1041 {
1042  return this->create_real_view_impl();
1043 }
1044 
1045 
1046 template <typename ConcreteType>
1047 std::unique_ptr<AbstractMultiVector>
1048 EnableMultiVector<ConcreteType>::create_real_view_generic_impl()
1049 {
1050  return this->create_real_view_impl();
1051 }
1052 
1053 
1054 template <typename ConcreteType>
1055 std::unique_ptr<AbstractMultiVector>
1056 EnableMultiVector<ConcreteType>::compute_absolute_generic_impl() const
1057 {
1058  return this->compute_absolute_impl();
1059 }
1060 
1061 
1062 template <typename ConcreteType>
1063 void EnableMultiVector<ConcreteType>::compute_absolute_generic_impl(
1064  AbstractMultiVector* result) const
1065 {
1066  this->compute_absolute_impl(as<absolute_type>(result));
1067 }
1068 
1069 template <typename ConcreteType>
1070 std::unique_ptr<AbstractMultiVector>
1071 EnableMultiVector<ConcreteType>::make_complex_generic_impl() const
1072 {
1073  return this->make_complex_impl();
1074 }
1075 
1076 
1077 template <typename ConcreteType>
1078 void EnableMultiVector<ConcreteType>::make_complex_generic_impl(
1079  AbstractMultiVector* result) const
1080 {
1081  this->make_complex_impl(as<complex_type>(result));
1082 }
1083 
1084 
1085 template <typename ConcreteType>
1086 std::unique_ptr<AbstractMultiVector>
1087 EnableMultiVector<ConcreteType>::get_real_generic_impl() const
1088 {
1089  return this->get_real_impl();
1090 }
1091 
1092 
1093 template <typename ConcreteType>
1094 void EnableMultiVector<ConcreteType>::get_real_generic_impl(
1095  AbstractMultiVector* result) const
1096 {
1097  this->get_real_impl(as<absolute_type>(result));
1098 }
1099 
1100 
1101 template <typename ConcreteType>
1102 std::unique_ptr<AbstractMultiVector>
1103 EnableMultiVector<ConcreteType>::get_imag_generic_impl() const
1104 {
1105  return this->get_imag_impl();
1106 }
1107 
1108 
1109 template <typename ConcreteType>
1110 void EnableMultiVector<ConcreteType>::get_imag_generic_impl(
1111  AbstractMultiVector* result) const
1112 {
1113  this->get_imag_impl(as<absolute_type>(result));
1114 }
1115 
1116 
1117 template <typename ConcreteType>
1118 void EnableMultiVector<ConcreteType>::fill_impl(any_scalar value)
1119 {
1120  std::visit(
1121  [this](auto value_v) {
1122  using snd_value_type = std::decay_t<decltype(value_v)>;
1123  if constexpr (std::is_constructible_v<value_type, snd_value_type>) {
1124  this->fill_impl(static_cast<value_type>(value_v));
1125  } else {
1126  GKO_NOT_IMPLEMENTED;
1127  }
1128  },
1129  value.variant);
1130 }
1131 
1132 
1133 namespace detail {
1134 
1135 
1143 template <typename ValueType, typename AlphaValueType>
1144 struct scaling_factor_target {
1145  using type = std::conditional_t<is_complex<ValueType>() &&
1146  !is_complex<AlphaValueType>(),
1147  remove_complex<ValueType>, ValueType>;
1148 };
1149 
1150 template <typename ValueType, typename AlphaValueType>
1151 using scaling_factor_target_type =
1152  typename scaling_factor_target<ValueType, AlphaValueType>::type;
1153 
1154 
1155 } // namespace detail
1156 
1157 
1158 template <typename ConcreteType>
1159 void EnableMultiVector<ConcreteType>::scale_impl(
1160  const AbstractMultiVector* alpha)
1161 {
1162  std::visit(
1163  [this, alpha](auto p) {
1164  using alpha_value_type = std::decay_t<decltype(p)>;
1165  if constexpr (is_complex<alpha_value_type>() &&
1166  !is_complex<value_type>()) {
1167  // can't scale a real vector with a complex scalar
1168  GKO_NOT_IMPLEMENTED;
1169  } else {
1170  auto alpha_v = as<matrix::MultiVector<alpha_value_type>>(alpha);
1171  this->scale_impl(scaling_param<value_type>{
1172  alpha_v
1173  ->template as_precision<
1174  detail::scaling_factor_target_type<
1175  value_type, alpha_value_type>>()
1176  .get()});
1177  }
1178  },
1179  precision_to_variant(alpha->get_precision()));
1180 }
1181 
1182 
1183 template <typename ConcreteType>
1184 void EnableMultiVector<ConcreteType>::inv_scale_impl(
1185  const AbstractMultiVector* alpha)
1186 {
1187  std::visit(
1188  [this, alpha](auto p) {
1189  using alpha_value_type = std::decay_t<decltype(p)>;
1190  if constexpr (is_complex<alpha_value_type>() &&
1191  !is_complex<value_type>()) {
1192  // can't scale a real vector with a complex scalar
1193  GKO_NOT_IMPLEMENTED;
1194  } else {
1195  auto alpha_v = as<matrix::MultiVector<alpha_value_type>>(alpha);
1196  this->inv_scale_impl(scaling_param<value_type>{
1197  alpha_v
1198  ->template as_precision<
1199  detail::scaling_factor_target_type<
1200  value_type, alpha_value_type>>()
1201  .get()});
1202  }
1203  },
1204  precision_to_variant(alpha->get_precision()));
1205 }
1206 
1207 template <typename ConcreteType>
1208 void EnableMultiVector<ConcreteType>::add_scaled_impl(
1209  const AbstractMultiVector* alpha, const AbstractMultiVector* b)
1210 {
1211  std::visit(
1212  [this, alpha, b](auto p) {
1213  using alpha_value_type = std::decay_t<decltype(p)>;
1214  if constexpr (is_complex<alpha_value_type>() &&
1215  !is_complex<value_type>()) {
1216  // can't scale a real vector with a complex scalar
1217  GKO_NOT_IMPLEMENTED;
1218  } else {
1219  auto alpha_v = as<matrix::MultiVector<alpha_value_type>>(alpha);
1220  this->add_scaled_impl(
1221  scaling_param<value_type>{
1222  alpha_v
1223  ->template as_precision<
1224  detail::scaling_factor_target_type<
1225  value_type, alpha_value_type>>()
1226  .get()},
1227  as<const ConcreteType>(
1228  b->as_precision(this->get_precision()).get()));
1229  }
1230  },
1231  precision_to_variant(alpha->get_precision()));
1232 }
1233 
1234 
1235 template <typename ConcreteType>
1236 void EnableMultiVector<ConcreteType>::sub_scaled_impl(
1237  const AbstractMultiVector* alpha, const AbstractMultiVector* b)
1238 {
1239  std::visit(
1240  [this, alpha, b](auto p) {
1241  using alpha_value_type = std::decay_t<decltype(p)>;
1242  if constexpr (is_complex<alpha_value_type>() &&
1243  !is_complex<value_type>()) {
1244  // can't scale a real vector with a complex scalar
1245  GKO_NOT_IMPLEMENTED;
1246  } else {
1247  auto alpha_v = as<matrix::MultiVector<alpha_value_type>>(alpha);
1248  this->sub_scaled_impl(
1249  scaling_param<value_type>{
1250  alpha_v
1251  ->template as_precision<
1252  detail::scaling_factor_target_type<
1253  value_type, alpha_value_type>>()
1254  .get()},
1255  as<const ConcreteType>(
1256  b->as_precision(this->get_precision()).get()));
1257  }
1258  },
1259  precision_to_variant(alpha->get_precision()));
1260 }
1261 
1262 
1263 template <typename ConcreteType>
1264 void EnableMultiVector<ConcreteType>::compute_dot_impl(
1265  const AbstractMultiVector* b, AbstractMultiVector* result,
1266  array<char>& tmp) const
1267 {
1268  this->compute_dot_impl(
1269  as<const ConcreteType>(b->as_precision(this->get_precision()).get()),
1270  as<matrix::MultiVector<value_type>>(
1271  result->as_precision(this->get_precision()).get()),
1272  tmp);
1273 }
1274 
1275 
1276 template <typename ConcreteType>
1277 void EnableMultiVector<ConcreteType>::compute_conj_dot_impl(
1278  const AbstractMultiVector* b, AbstractMultiVector* result,
1279  array<char>& tmp) const
1280 {
1281  this->compute_conj_dot_impl(
1282  as<const ConcreteType>(b->as_precision(this->get_precision()).get()),
1283  as<matrix::MultiVector<value_type>>(
1284  result->as_precision(this->get_precision()).get()),
1285  tmp);
1286 }
1287 
1288 
1289 template <typename ConcreteType>
1290 void EnableMultiVector<ConcreteType>::compute_norm2_impl(
1291  AbstractMultiVector* result, array<char>& tmp) const
1292 {
1293  this->compute_norm2_impl(
1294  as<norm_type>(
1295  result->as_precision(as_real(this->get_precision())).get()),
1296  tmp);
1297 }
1298 
1299 
1300 template <typename ConcreteType>
1301 void EnableMultiVector<ConcreteType>::compute_squared_norm2_impl(
1302  AbstractMultiVector* result, array<char>& tmp) const
1303 {
1304  this->compute_squared_norm2_impl(
1305  as<norm_type>(
1306  result->as_precision(as_real(this->get_precision())).get()),
1307  tmp);
1308 }
1309 
1310 
1311 template <typename ConcreteType>
1312 void EnableMultiVector<ConcreteType>::compute_norm1_impl(
1313  AbstractMultiVector* result, array<char>& tmp) const
1314 {
1315  this->compute_norm1_impl(
1316  as<norm_type>(
1317  result->as_precision(as_real(this->get_precision())).get()),
1318  tmp);
1319 }
1320 
1321 
1322 template <typename ConcreteType>
1323 temporary_conversion<AbstractMultiVector>
1324 EnableMultiVector<ConcreteType>::as_precision_impl(precision p)
1325 {
1326  return std::visit(
1327  [this](auto v) -> temporary_conversion<AbstractMultiVector> {
1328  using target_value_type = std::decay_t<decltype(v)>;
1329  if constexpr (is_complex<value_type>() ==
1330  is_complex<target_value_type>()) {
1333  self()->template as_precision<target_value_type>());
1334  } else {
1335  GKO_NOT_IMPLEMENTED;
1336  }
1337  },
1339 }
1340 
1341 
1342 template <typename ConcreteType>
1343 temporary_conversion<const AbstractMultiVector>
1344 EnableMultiVector<ConcreteType>::as_precision_impl(precision p) const
1345 {
1346  return std::visit(
1347  [this](auto v) -> temporary_conversion<const AbstractMultiVector> {
1348  using target_value_type = std::decay_t<decltype(v)>;
1349  if constexpr (is_complex<value_type>() ==
1350  is_complex<target_value_type>()) {
1353  self()->template as_precision<target_value_type>());
1354  } else {
1355  GKO_NOT_IMPLEMENTED;
1356  }
1357  },
1359 }
1360 
1361 
1362 template <typename ConcreteType>
1363 std::variant<
1364 #if GINKGO_ENABLE_HALF
1365  AbstractMultiVector::device_view<half>,
1366  AbstractMultiVector::device_view<std::complex<half>>,
1367 #endif
1368 #if GINKGO_ENABLE_BFLOAT16
1369  AbstractMultiVector::device_view<bfloat16>,
1370  AbstractMultiVector::device_view<std::complex<bfloat16>>,
1371 #endif
1372  AbstractMultiVector::device_view<float>,
1373  AbstractMultiVector::device_view<std::complex<float>>,
1374  AbstractMultiVector::device_view<double>,
1375  AbstractMultiVector::device_view<std::complex<double>>>
1376 EnableMultiVector<ConcreteType>::get_local_device_view_generic_impl()
1377 {
1378  return this->get_local_device_view_impl();
1379 }
1380 
1381 
1382 template <typename ConcreteType>
1383 std::variant<
1384 #if GINKGO_ENABLE_HALF
1385  AbstractMultiVector::device_view<const half>,
1386  AbstractMultiVector::device_view<const std::complex<half>>,
1387 #endif
1388 #if GINKGO_ENABLE_BFLOAT16
1389  AbstractMultiVector::device_view<const bfloat16>,
1390  AbstractMultiVector::device_view<const std::complex<bfloat16>>,
1391 #endif
1392  AbstractMultiVector::device_view<const float>,
1393  AbstractMultiVector::device_view<const std::complex<float>>,
1394  AbstractMultiVector::device_view<const double>,
1395  AbstractMultiVector::device_view<const std::complex<double>>>
1396 EnableMultiVector<ConcreteType>::get_const_local_device_view_generic_impl()
1397  const
1398 {
1399  return this->get_const_local_device_view_impl();
1400 }
1401 
1402 
1403 } // namespace gko
gko::matrix::view::dense
Non-owning view of a matrix::MultiVector to be used inside device kernels.
Definition: device_views.hpp:27
gko::EnableMultiVector::clone
std::unique_ptr< ConcreteType > clone() const
Creates a copy of this vector on the given executor.
Definition: multivector_mixin.hpp:723
gko::EnableMultiVector
Mixin class to simplify the implementation of the AbstractMultiVector interface.
Definition: multivector_mixin.hpp:152
gko::vector_traits
Trait class containing type aliases required by EnableMultiVector.
Definition: multivector_mixin.hpp:43
gko::EnableMultiVector::make_complex
std::unique_ptr< complex_type > make_complex() const
Creates a complex copy of this vector.
Definition: multivector_mixin.hpp:832
gko::layout_type::array
The matrix should be written as dense matrix in column-major order.
gko::EnableMultiVector< MultiVector< value_type > >::absolute_value_type
typename traits::absolute_value_type absolute_value_type
The real counterpart of value_type.
Definition: multivector_mixin.hpp:160
gko::AbstractMultiVector::get_const_local_device_view
device_view< const ValueType > get_const_local_device_view() const
Gets a local device view of this vector for the specified value type.
gko::EnableMultiVector::norm_type
matrix::MultiVector< absolute_value_type > norm_type
The vector type used to store the results of reductions to norms.
Definition: multivector_mixin.hpp:170
gko::EnableMultiVector::get_const_local_device_view
const_device_view get_const_local_device_view() const
Gets a local device view of this vector.
Definition: multivector_mixin.hpp:908
gko::make_temporary_output_clone
detail::temporary_clone< detail::pointee< Ptr > > make_temporary_output_clone(std::shared_ptr< const Executor > exec, Ptr &&ptr)
Creates a uninitialized temporary_clone that will be copied back to the input afterwards.
Definition: temporary_clone.hpp:231
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::ptr_param::get
T * get() const
Definition: utils_helper.hpp:77
gko::EnableMultiVector::move_to
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition: multivector_mixin.hpp:892
gko::AbstractMultiVector::as_precision
temporary_conversion< AbstractMultiVector > as_precision(precision p)
Creates a temporary conversion of this vector with another precision.
gko::precision
precision
An enum to specify the precision of stored data.
Definition: precision.hpp:19
gko::EnableMultiVector< MultiVector< value_type > >::real_type
typename traits::real_type real_type
The vector type holding real or imaginary parts of this vector.
Definition: multivector_mixin.hpp:164
gko::EnableMultiVector::create_with_config_of
static std::unique_ptr< ConcreteType > create_with_config_of(ptr_param< const ConcreteType > other)
Definition: multivector_mixin.hpp:671
gko::scaling_param
The allowed matrix::MultiVector<T> type to be used in scaling operations, e.g.
Definition: multivector_mixin.hpp:79
gko::AbstractMultiVector::get_local_device_view
device_view< ValueType > get_local_device_view()
Gets a local device view of this vector for the specified value type.
gko::as
temporary_conversion< T > as(temporary_conversion< U > &&obj)
Performs polymorphic type conversion of a temporary_conversion.
Definition: temporary_conversion.hpp:376
gko::EnableMultiVector::convert_to
void convert_to(result_type *result) const override
Definition: multivector_mixin.hpp:885
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::EnableMultiVector::move_from
ConcreteType * move_from(ptr_param< ConcreteType > other)
Moves the data of other into this vector.
Definition: multivector_mixin.hpp:738
gko::dim< 2 >
gko::EnableMultiVector::create_default
std::unique_ptr< ConcreteType > create_default() const
Creates an empty vector with the same type as this vector on the given executor.
Definition: multivector_mixin.hpp:746
gko::EnableMultiVector< MultiVector< value_type > >::result_type
MultiVector< value_type > result_type
The target type of the conversions provided by this mixin.
Definition: multivector_mixin.hpp:168
gko::precision_v
constexpr precision precision_v
Map from compile time type to runtime precision.
Definition: precision.hpp:83
gko::EnableMultiVector::compute_absolute
std::unique_ptr< absolute_type > compute_absolute() const
Creates a new vector with the element-wise absolute values of this vector.
Definition: multivector_mixin.hpp:813
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::scaling_param::variant
variant_type variant
The scaling factor with one of the allowed precisions.
Definition: multivector_mixin.hpp:83
gko::EnableMultiVector< MultiVector< value_type > >::absolute_type
typename traits::absolute_type absolute_type
The vector type holding absolute values of this vector.
Definition: multivector_mixin.hpp:162
gko::matrix::MultiVector
MultiVector is a multivector format which explicitly stores all values of the vector.
Definition: dense_cache.hpp:28
gko::EnableMultiVector< MultiVector< value_type > >::value_type
typename traits::value_type value_type
The value type of the stored elements.
Definition: multivector_mixin.hpp:158
gko::EnableMultiVector::copy_from
ConcreteType * copy_from(ptr_param< const ConcreteType > other)
Definition: multivector_mixin.hpp:730
gko::precision_to_variant
syn::variant_from_list< supported_value_types > precision_to_variant(precision p)
Create a variant from a precision.
Definition: precision.hpp:240
gko::Cloneable
Interface for objects that can be cloned.
Definition: polymorphic_object.hpp:305
gko::EnableMultiVector::create_subview
std::unique_ptr< ConcreteType > create_subview(local_span rows, local_span columns)
Creates a view containing the selected local rows and columns.
Definition: multivector_mixin.hpp:761
gko::EnableMultiVector::get_imag
std::unique_ptr< real_type > get_imag() const
Creates a new real vector with the imaginary parts of this vector.
Definition: multivector_mixin.hpp:868
gko::EnableMultiVector::get_real
std::unique_ptr< real_type > get_real() const
Creates a new real vector with the real parts of this vector.
Definition: multivector_mixin.hpp:850
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:147
gko::EnableMultiVector< MultiVector< value_type > >::complex_type
typename traits::complex_type complex_type
The vector type holding a complex version of this vector.
Definition: multivector_mixin.hpp:166
gko::EnableMultiVector::create_real_view
std::unique_ptr< const real_type > create_real_view() const
Create a real view of the (potentially) complex original multi-vector.
Definition: multivector_mixin.hpp:797
gko::temporary_conversion::create_from_derived
static temporary_conversion create_from_derived(temporary_conversion< Derived > &&derived)
Create a temporary conversion for a base type T from an object of a derived type.
Definition: temporary_conversion.hpp:302
gko::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
gko::EnableMultiVector::get_local_device_view
device_view get_local_device_view()
Gets a local device view of this vector.
Definition: multivector_mixin.hpp:900
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:69
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:263
gko::local_span
Same as span, except that it only represents a range of local rows/columns, and that it is copyable/m...
Definition: range.hpp:138
gko::as_real
constexpr precision as_real(precision p)
Maps a precision to its corresponding real precision.
Definition: precision.hpp:129
gko::AbstractMultiVector::get_precision
precision get_precision() const noexcept
Gets the precision of this vector.
gko::EnableMultiVector::create_with_type_of
static std::unique_ptr< ConcreteType > create_with_type_of(ptr_param< const ConcreteType > other, std::shared_ptr< const Executor > exec)
Definition: multivector_mixin.hpp:681
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:282