version GIT relicensing-7278-g091132d5eb 2026-12-02 03:10:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
solver_cg.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 1998 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_solver_cg_h
14#define dealii_solver_cg_h
15
16
17#include <deal.II/base/config.h>
18
24
25#include <deal.II/lac/solver.h>
28
29#include <boost/signals2.hpp>
30
31#include <cmath>
32
34
35// forward declarations
36#ifndef DOXYGEN
37
39
40template <typename VectorType>
41class DiagonalMatrix;
42
43namespace LinearAlgebra
44{
45 namespace distributed
46 {
47 template <typename, typename>
48 class Vector;
49 }
50} // namespace LinearAlgebra
51
52#endif
53
54
217template <typename VectorType = Vector<double>>
219class SolverCG : public SolverBase<VectorType>
220{
221public:
226
227
232 {
247 explicit AdditionalData(const bool use_default_residual = true);
248
253 };
254
255
262
268
272 virtual ~SolverCG() override = default;
273
277 template <typename MatrixType, typename PreconditionerType>
281 void solve(const MatrixType &A,
282 VectorType &x,
283 const VectorType &b,
284 const PreconditionerType &preconditioner);
285
292 boost::signals2::connection
293 connect_coefficients_slot(
294 const std::function<void(typename VectorType::value_type,
295 typename VectorType::value_type)> &slot);
296
303 boost::signals2::connection
304 connect_condition_number_slot(const std::function<void(double)> &slot,
305 const bool every_iteration = false);
306
313 boost::signals2::connection
314 connect_eigenvalues_slot(
315 const std::function<void(const std::vector<double> &)> &slot,
316 const bool every_iteration = false);
317
318protected:
324 virtual void
325 print_vectors(const unsigned int step,
326 const VectorType &x,
327 const VectorType &r,
328 const VectorType &d) const;
329
335 static void
336 compute_eigs_and_cond(
337 const std::vector<typename VectorType::value_type> &diagonal,
338 const std::vector<typename VectorType::value_type> &offdiagonal,
339 const boost::signals2::signal<void(const std::vector<double> &)>
340 &eigenvalues_signal,
341 const boost::signals2::signal<void(double)> &cond_signal);
342
346 AdditionalData additional_data;
347
351 boost::signals2::signal<void(typename VectorType::value_type,
352 typename VectorType::value_type)>
353 coefficients_signal;
354
359 boost::signals2::signal<void(double)> condition_number_signal;
360
365 boost::signals2::signal<void(double)> all_condition_numbers_signal;
366
371 boost::signals2::signal<void(const std::vector<double> &)> eigenvalues_signal;
372
377 boost::signals2::signal<void(const std::vector<double> &)>
378 all_eigenvalues_signal;
379
388 bool determine_beta_by_flexible_formula;
389};
390
391
392
418template <typename VectorType = Vector<double>>
419DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>)
420class SolverFlexibleCG : public SolverCG<VectorType>
421{
422public:
427
428
433 {
448 explicit AdditionalData(const bool use_default_residual = true);
449
454 };
455
456
463
470
471protected:
476};
477
478
481/*------------------------- Implementation ----------------------------*/
482
483#ifndef DOXYGEN
484
485
486template <typename VectorType>
489 const bool use_default_residual)
490 : use_default_residual(use_default_residual)
491{}
492
493template <typename VectorType>
497 const AdditionalData &data)
498 : SolverBase<VectorType>(cn, mem)
499 , additional_data(data)
500 , determine_beta_by_flexible_formula(false)
501{}
502
503
504
505template <typename VectorType>
507SolverCG<VectorType>::SolverCG(SolverControl &cn, const AdditionalData &data)
509 , additional_data(data)
510 , determine_beta_by_flexible_formula(false)
511{}
512
513
514
515template <typename VectorType>
517void SolverCG<VectorType>::print_vectors(const unsigned int,
518 const VectorType &,
519 const VectorType &,
520 const VectorType &) const
521{}
522
523
524
525template <typename VectorType>
528 const std::vector<typename VectorType::value_type> &diagonal,
529 const std::vector<typename VectorType::value_type> &offdiagonal,
530 const boost::signals2::signal<void(const std::vector<double> &)>
531 &eigenvalues_signal,
532 const boost::signals2::signal<void(double)> &cond_signal)
533{
534 // Avoid computing eigenvalues unless they are needed.
535 if (!cond_signal.empty() || !eigenvalues_signal.empty())
536 {
538 true);
539 for (size_type i = 0; i < diagonal.size(); ++i)
540 {
541 T(i, i) = diagonal[i];
542 if (i < diagonal.size() - 1)
543 T(i, i + 1) = offdiagonal[i];
544 }
545 T.compute_eigenvalues();
546 // Need two eigenvalues to estimate the condition number.
547 if (diagonal.size() > 1)
548 {
549 auto condition_number = T.eigenvalue(T.n() - 1) / T.eigenvalue(0);
550 // Condition number is real valued and nonnegative; simply take
551 // the absolute value:
552 cond_signal(std::abs(condition_number));
553 }
554 // Avoid copying the eigenvalues of T to a vector unless a signal is
555 // connected.
556 if (!eigenvalues_signal.empty())
557 {
558 std::vector<double> eigenvalues(T.n());
559 for (unsigned int j = 0; j < T.n(); ++j)
560 {
561 // for a hermitian matrix, all eigenvalues are real-valued
562 // and non-negative, simply return the absolute value:
563 eigenvalues[j] = std::abs(T.eigenvalue(j));
564 }
565 eigenvalues_signal(eigenvalues);
566 }
567 }
568}
569
570
571
572namespace internal
573{
574
575 namespace SolverCG
576 {
577 // This base class is used to select different variants of the conjugate
578 // gradient solver. The default variant is used for standard matrix and
579 // preconditioner arguments, as provided by the derived class
580 // IterationWork below, but there is also a specialized variant further
581 // down that uses SFINAE to identify whether matrices and preconditioners
582 // support special operations on sub-ranges of the vectors.
583 template <typename VectorType,
584 typename MatrixType,
585 typename PreconditionerType>
586 struct IterationWorkerBase
587 {
588 using Number = typename VectorType::value_type;
589
590 const MatrixType &A;
591 const PreconditionerType &preconditioner;
592 const bool flexible;
593 VectorType &x;
594 const VectorType &b;
595
596 typename VectorMemory<VectorType>::Pointer r_pointer;
597 typename VectorMemory<VectorType>::Pointer p_pointer;
598 typename VectorMemory<VectorType>::Pointer v_pointer;
599 typename VectorMemory<VectorType>::Pointer z_pointer;
600 typename VectorMemory<VectorType>::Pointer explicit_r_pointer;
601
602 // Define some aliases for simpler access, using the variables 'r' for
603 // the residual b - A*x, 'p' for the search direction, and 'v' for the
604 // auxiliary vector. This naming convention is used e.g. by the
605 // description on
606 // https://en.wikipedia.org/wiki/Conjugate_gradient_method. The variable
607 // 'z' gets only used for the flexible variant of the CG method.
608 VectorType &r;
609 VectorType &p;
610 VectorType &v;
611 VectorType &z;
612 VectorType &explicit_r;
613
614 Number r_dot_preconditioner_dot_r;
615 Number alpha;
616 Number beta;
617 double residual_norm;
618 Number previous_alpha;
619 const bool use_default_residual;
620
621
622 IterationWorkerBase(const MatrixType &A,
623 const PreconditionerType &preconditioner,
624 const bool flexible,
626 VectorType &x,
627 const VectorType &b,
628 const bool &use_default_residual)
629 : A(A)
630 , preconditioner(preconditioner)
631 , flexible(flexible)
632 , x(x)
633 , b(b)
634 , r_pointer(memory)
635 , p_pointer(memory)
636 , v_pointer(memory)
637 , z_pointer(memory)
638 , explicit_r_pointer(memory)
639 , r(*r_pointer)
640 , p(*p_pointer)
641 , v(*v_pointer)
642 , z(*z_pointer)
643 , explicit_r(*explicit_r_pointer)
644 , r_dot_preconditioner_dot_r(Number())
645 , alpha(Number())
646 , beta(Number())
647 , residual_norm(0.0)
648 , previous_alpha(Number())
649 , use_default_residual(use_default_residual)
650 {}
651
652 void
653 startup()
654 {
655 // Initialize without setting the vector entries, as those would soon
656 // be overwritten anyway
657 r.reinit(x, true);
658 p.reinit(x, true);
659 v.reinit(x, true);
660 if (flexible)
661 z.reinit(x, true);
662 if (!use_default_residual)
663 explicit_r.reinit(x, true);
664
665 // compute residual. if vector is zero, then short-circuit the full
666 // computation
667 if (!x.all_zero())
668 {
669 A.vmult(r, x);
670 r.sadd(-1., 1., b);
671 }
672 else
673 r.equ(1., b);
674
675 residual_norm = r.l2_norm();
676 }
677 };
678
679
680
681 // Implementation of a conjugate gradient operation with matrices and
682 // preconditioners without special capabilities
683 template <typename VectorType,
684 typename MatrixType,
685 typename PreconditionerType,
686 typename = int>
687 struct IterationWorker
688 : public IterationWorkerBase<VectorType, MatrixType, PreconditionerType>
689 {
690 using BaseClass =
691 IterationWorkerBase<VectorType, MatrixType, PreconditionerType>;
692
693
694 IterationWorker(const MatrixType &A,
695 const PreconditionerType &preconditioner,
696 const bool flexible,
698 VectorType &x,
699 const VectorType &b,
700 const bool &use_default_residual)
701 : BaseClass(A,
702 preconditioner,
703 flexible,
704 memory,
705 x,
706 b,
707 use_default_residual)
708 {}
709
710 using BaseClass::A;
711 using BaseClass::alpha;
712 using BaseClass::b;
713 using BaseClass::beta;
714 using BaseClass::explicit_r;
715 using BaseClass::p;
716 using BaseClass::preconditioner;
717 using BaseClass::r;
718 using BaseClass::r_dot_preconditioner_dot_r;
719 using BaseClass::residual_norm;
720 using BaseClass::use_default_residual;
721 using BaseClass::v;
722 using BaseClass::x;
723 using BaseClass::z;
724
725 void
726 do_iteration(const unsigned int iteration_index)
727 {
728 using Number = typename VectorType::value_type;
729
730 const Number previous_r_dot_preconditioner_dot_r =
731 r_dot_preconditioner_dot_r;
732
733 if (std::is_same_v<PreconditionerType, PreconditionIdentity> == false)
734 {
735 preconditioner.vmult(v, r);
736 r_dot_preconditioner_dot_r = r * v;
737 }
738 else
739 r_dot_preconditioner_dot_r = residual_norm * residual_norm;
740
741 const VectorType &direction =
742 std::is_same_v<PreconditionerType, PreconditionIdentity> ? r : v;
743
744 if (iteration_index > 1)
745 {
746 Assert(std::abs(previous_r_dot_preconditioner_dot_r) != 0.,
748 beta =
749 r_dot_preconditioner_dot_r / previous_r_dot_preconditioner_dot_r;
750 if (this->flexible)
751 beta -= (r * z) / previous_r_dot_preconditioner_dot_r;
752 p.sadd(beta, 1., direction);
753 }
754 else
755 p.equ(1., direction);
756
757 if (this->flexible)
758 z.swap(v);
759
760 A.vmult(v, p);
761
762 const Number p_dot_A_dot_p = p * v;
763 Assert(std::abs(p_dot_A_dot_p) != 0., ExcDivideByZero());
764
765 this->previous_alpha = alpha;
766 alpha = r_dot_preconditioner_dot_r / p_dot_A_dot_p;
767
768 x.add(alpha, p);
769
770 // compute the residual norm with implicit residual
771 if (use_default_residual)
772 {
773 residual_norm = std::sqrt(std::abs(r.add_and_dot(-alpha, v, r)));
774 }
775 // compute the residual norm with the explicit residual, i.e.
776 // compute l2 norm of Ax - b.
777 else
778 {
779 // compute the residual conjugate gradient update
780 r.add(-alpha, v);
781 // compute explicit residual
782 A.vmult(explicit_r, x);
783 explicit_r.add(-1, b);
784 residual_norm = explicit_r.l2_norm();
785 }
786 }
787
788 void
789 finalize_after_convergence(const unsigned int)
790 {}
791 };
792
793
794 // In the following, we provide a specialization of the above
795 // IterationWorker class that picks up particular features in the matrix
796 // and preconditioners.
797
798 // a helper type-trait that leverage SFINAE to figure out if MatrixType has
799 // ... MatrixType::vmult(VectorType &, const VectorType&,
800 // std::function<...>, std::function<...>) const
801 template <typename MatrixType, typename VectorType>
802 using vmult_functions_t = decltype(std::declval<const MatrixType>().vmult(
803 std::declval<VectorType &>(),
804 std::declval<const VectorType &>(),
805 std::declval<
806 const std::function<void(const unsigned int, const unsigned int)> &>(),
807 std::declval<const std::function<void(const unsigned int,
808 const unsigned int)> &>()));
809
810 template <typename MatrixType, typename VectorType>
811 constexpr bool has_vmult_functions =
812 is_supported_operation<vmult_functions_t, MatrixType, VectorType>;
813
814 // a helper type-trait that leverage SFINAE to figure out if
815 // PreconditionerType has ... PreconditionerType::apply_to_subrange(const
816 // unsigned int, const unsigned int, const Number*, Number*) const
817 template <typename PreconditionerType>
818 using apply_to_subrange_t =
819 decltype(std::declval<const PreconditionerType>()
820 .apply_to_subrange(0U, 0U, nullptr, nullptr));
821
822 template <typename PreconditionerType>
823 constexpr bool has_apply_to_subrange =
824 is_supported_operation<apply_to_subrange_t, PreconditionerType>;
825
826 // a helper type-trait that leverage SFINAE to figure out if
827 // PreconditionerType has ... PreconditionerType::apply(const
828 // unsigned int, const Number) const
829 template <typename PreconditionerType>
830 using apply_t =
831 decltype(std::declval<const PreconditionerType>().apply(0U, 0.0));
832
833 template <typename PreconditionerType>
834 constexpr bool has_apply =
835 is_supported_operation<apply_t, PreconditionerType>;
836
837
838 // Base class for the variants of the conjugate gradient method that merge
839 // the vector updates of the CG iteration into the matrix-vector product
840 // (on the host) or into a single kernel before and after the
841 // matrix-vector product (on devices) in the form of a pipelined
842 // operation. This requires the preconditioner to act point-wise on the
843 // vector entries, i.e., to be a diagonal matrix. The algorithm works with
844 // the following steps:
845 //
846 // - Before the matrix-vector product, the residual vector r gets updated
847 // by the matrix-vector product computed during the previous iteration as
848 // r <- r - alpha * v, and the new search direction is computed as
849 // p <- beta * p + P^{-1} r. In order to save some memory transfer, the
850 // solution vector x is only updated in every other iteration, where the
851 // previous search direction can be re-constructed from the current one
852 // and the preconditioned residual.
853 //
854 // - After the matrix-vector product v = A p, seven inner products are
855 // computed in a single sweep through the vectors, from which we can
856 // compute the step length alpha and the residual norm of the next
857 // iteration as well as the value of r^T P^{-1} r needed for the next
858 // beta (by using the update formula r <- r - alpha * v, which will only
859 // be applied in the next iteration). This approach needs a single
860 // global reduction per iteration.
861 //
862 // This class collects the scalar part of the algorithm that is shared
863 // by the implementations on the host and the device, while the derived
864 // classes implement the operations on the vectors.
865 template <typename VectorType,
866 typename MatrixType,
867 typename PreconditionerType>
868 struct PipelinedIterationWorkerBase
869 : public IterationWorkerBase<VectorType, MatrixType, PreconditionerType>
870 {
871 using Number = typename VectorType::value_type;
872
873 Number next_r_dot_preconditioner_dot_r;
874 Number previous_beta;
875
876 PipelinedIterationWorkerBase(const MatrixType &A,
877 const PreconditionerType &preconditioner,
878 const bool flexible,
880 VectorType &x,
881 const VectorType &b,
882 const bool &use_default_residual)
883 : IterationWorkerBase<VectorType, MatrixType, PreconditionerType>(
884 A,
885 preconditioner,
886 flexible,
887 memory,
888 x,
889 b,
890 use_default_residual)
891 , next_r_dot_preconditioner_dot_r(0.)
892 , previous_beta(0.)
893 {}
894
895 // Compute the coefficient beta for the update of the search direction
896 // from the quantities computed in the previous iteration.
897 void
898 start_iteration(const unsigned int iteration_index)
899 {
900 if (iteration_index > 1)
901 {
902 previous_beta = this->beta;
903 this->beta = next_r_dot_preconditioner_dot_r /
904 this->r_dot_preconditioner_dot_r;
905 }
906 }
907
908 // Sum the seven local inner products over all MPI ranks and compute the
909 // step length alpha, the residual norm, and the value of r^T P^{-1} r
910 // for the next iteration. The entries of @p sums contain the following
911 // quantities:
912 // [0]: p^T v, [1]: v^T v, [2]: r^T v, [3]: r^T r, [4]: r^T P^{-1} v,
913 // [5]: v^T P^{-1} v, [6]: r^T P^{-1} r.
914 void
915 finish_iteration(std::array<Number, 7> &sums)
916 {
918 this->r.get_mpi_communicator(),
919 ::ArrayView<Number>(sums.data(), 7));
920
921 this->r_dot_preconditioner_dot_r = sums[6];
922
923 const Number p_dot_A_dot_p = sums[0];
924 Assert(std::abs(p_dot_A_dot_p) != 0., ExcDivideByZero());
925
926 this->previous_alpha = this->alpha;
927 this->alpha = this->r_dot_preconditioner_dot_r / p_dot_A_dot_p;
928
929 // Round-off errors near zero might yield negative values, so take
930 // the absolute value in the next two formulas
931 this->residual_norm = std::sqrt(std::abs(
932 sums[3] + this->alpha * (-2. * sums[2] + this->alpha * sums[1])));
933
934 next_r_dot_preconditioner_dot_r =
935 std::abs(this->r_dot_preconditioner_dot_r +
936 this->alpha * (-2. * sums[4] + this->alpha * sums[5]));
937 }
938 };
939
940
941
942 // Internal function to run one iteration of the pipelined conjugate
943 // gradient solver for matrices and preconditioners that support
944 // interleaving the vector updates with the matrix-vector product.
945 template <typename VectorType,
946 typename MatrixType,
947 typename PreconditionerType>
948 struct IterationWorker<
951 PreconditionerType,
952 std::enable_if_t<has_vmult_functions<MatrixType, VectorType> &&
953 (has_apply_to_subrange<PreconditionerType> ||
954 has_apply<PreconditionerType>) &&
955 std::is_same_v<VectorType,
956 LinearAlgebra::distributed::Vector<
957 typename VectorType::value_type,
958 MemorySpace::Host>>,
959 int>>
960 : public PipelinedIterationWorkerBase<VectorType,
961 MatrixType,
962 PreconditionerType>
963 {
964 using Number = typename VectorType::value_type;
965
966 IterationWorker(const MatrixType &A,
967 const PreconditionerType &preconditioner,
968 const bool flexible,
970 VectorType &x,
971 const VectorType &b,
972 const bool &use_default_residual)
973 : PipelinedIterationWorkerBase<VectorType,
975 PreconditionerType>(A,
976 preconditioner,
977 flexible,
978 memory,
979 x,
980 b,
981 use_default_residual)
982 {}
983
984 // This is the main iteration function, that will use some of the
985 // specialized functions below
986 void
987 do_iteration(const unsigned int iteration_index)
988 {
989 this->start_iteration(iteration_index);
990
991 std::array<VectorizedArray<Number>, 7> vectorized_sums = {};
992
993 this->A.vmult(
994 this->v,
995 this->p,
996 [&](const unsigned int begin, const unsigned int end) {
997 operation_before_loop(iteration_index, begin, end);
998 },
999 [&](const unsigned int begin, const unsigned int end) {
1000 operation_after_loop(begin, end, vectorized_sums);
1001 });
1002
1003 std::array<Number, 7> scalar_sums;
1004 for (unsigned int i = 0; i < 7; ++i)
1005 scalar_sums[i] = vectorized_sums[i][0];
1006 for (unsigned int l = 1; l < VectorizedArray<Number>::size(); ++l)
1007 for (unsigned int i = 0; i < 7; ++i)
1008 scalar_sums[i] += vectorized_sums[i][l];
1009
1010 this->finish_iteration(scalar_sums);
1011 }
1012
1015 apply_preconditioner(const ::DiagonalMatrix<VectorType> &diagonal,
1016 const unsigned int j,
1017 const VectorizedArray<Number> value)
1018 {
1020 diag.load(diagonal.get_vector().get_values() + j);
1021 return diag * value;
1022 }
1023
1024 template <typename PreconditionerTypeSelected>
1026 apply_preconditioner(const PreconditionerTypeSelected &preconditioner,
1027 const unsigned int j,
1028 const VectorizedArray<Number> value)
1029 {
1032 for (unsigned int l = 0; l < VectorizedArray<Number>::size(); ++l)
1033 result[l] = preconditioner.apply(j + l, value[l]);
1034 return result;
1035 }
1036
1037 // Function that we use if the PreconditionerType implements an apply()
1038 // function
1039 template <typename U = void>
1040 std::enable_if_t<has_apply<PreconditionerType>, U>
1041 operation_before_loop(const unsigned int iteration_index,
1042 const unsigned int start_range,
1043 const unsigned int end_range) const
1044 {
1045 Number *x = this->x.begin();
1046 Number *r = this->r.begin();
1047 Number *p = this->p.begin();
1048 Number *v = this->v.begin();
1049 const Number alpha = this->alpha;
1050 const Number beta = this->beta;
1051 constexpr unsigned int n_lanes = VectorizedArray<Number>::size();
1052 const unsigned int end_regular =
1053 start_range + (end_range - start_range) / n_lanes * n_lanes;
1054 if (iteration_index == 1)
1055 {
1056 // Vectorize by hand since compilers are often pretty bad at
1057 // doing these steps automatically even with
1058 // DEAL_II_OPENMP_SIMD_PRAGMA
1059 for (unsigned int j = start_range; j < end_regular; j += n_lanes)
1060 {
1062 rj.load(r + j);
1063 const VectorizedArray<Number> pj =
1064 apply_preconditioner(this->preconditioner, j, rj);
1065 pj.store(p + j);
1067 rj.store(v + j);
1068 }
1069 for (unsigned int j = end_regular; j < end_range; ++j)
1070 {
1071 p[j] = this->preconditioner.apply(j, r[j]);
1072 v[j] = Number();
1073 }
1074 }
1075 else if (iteration_index % 2 == 0 && beta != Number())
1076 {
1077 for (unsigned int j = start_range; j < end_regular; j += n_lanes)
1078 {
1079 VectorizedArray<Number> rj, vj, pj;
1080 rj.load(r + j);
1081 vj.load(v + j);
1082 rj -= alpha * vj;
1083 rj.store(r + j);
1084 const VectorizedArray<Number> prec_rj =
1085 apply_preconditioner(this->preconditioner, j, rj);
1086 pj.load(p + j);
1087 pj = beta * pj + prec_rj;
1088 pj.store(p + j);
1090 rj.store(v + j);
1091 }
1092 for (unsigned int j = end_regular; j < end_range; ++j)
1093 {
1094 r[j] -= alpha * v[j];
1095 p[j] = beta * p[j] + this->preconditioner.apply(j, r[j]);
1096 v[j] = Number();
1097 }
1098 }
1099 else if (iteration_index % 2 == 0 && beta == Number())
1100 {
1101 // Case where beta is zero: we cannot reconstruct p_{j-1} in the
1102 // next iteration, and must hence compute x here. This can happen
1103 // before termination
1104 for (unsigned int j = start_range; j < end_regular; j += n_lanes)
1105 {
1106 VectorizedArray<Number> rj, xj, vj, pj;
1107 rj.load(r + j);
1108 vj.load(v + j);
1109 xj.load(x + j);
1110 pj.load(p + j);
1111 xj += alpha * pj;
1112 xj.store(x + j);
1113 rj -= alpha * vj;
1114 rj.store(r + j);
1115 const VectorizedArray<Number> prec_rj =
1116 apply_preconditioner(this->preconditioner, j, rj);
1117 prec_rj.store(p + j);
1119 rj.store(v + j);
1120 }
1121 for (unsigned int j = end_regular; j < end_range; ++j)
1122 {
1123 r[j] -= alpha * v[j];
1124 x[j] += alpha * p[j];
1125 p[j] = this->preconditioner.apply(j, r[j]);
1126 v[j] = Number();
1127 }
1128 }
1129 else
1130 {
1131 const Number alpha_plus_previous_alpha_over_beta =
1132 alpha + this->previous_alpha / this->previous_beta;
1133 const Number previous_alpha_over_beta =
1134 this->previous_alpha / this->previous_beta;
1135 for (unsigned int j = start_range; j < end_regular; j += n_lanes)
1136 {
1137 VectorizedArray<Number> rj, vj, pj, xj;
1138 xj.load(x + j);
1139 pj.load(p + j);
1140 xj += alpha_plus_previous_alpha_over_beta * pj;
1141 rj.load(r + j);
1142 vj.load(v + j);
1143 VectorizedArray<Number> prec_rj =
1144 apply_preconditioner(this->preconditioner, j, rj);
1145 const VectorizedArray<Number> prec_vj =
1146 apply_preconditioner(this->preconditioner, j, vj);
1147 xj -= previous_alpha_over_beta * prec_rj;
1148 xj.store(x + j);
1149 rj -= alpha * vj;
1150 rj.store(r + j);
1151 prec_rj -= alpha * prec_vj;
1152 pj = beta * pj + prec_rj;
1153 pj.store(p + j);
1155 rj.store(v + j);
1156 }
1157 for (unsigned int j = end_regular; j < end_range; ++j)
1158 {
1159 x[j] += alpha_plus_previous_alpha_over_beta * p[j];
1160 x[j] -= previous_alpha_over_beta *
1161 this->preconditioner.apply(j, r[j]);
1162 r[j] -= alpha * v[j];
1163 p[j] = beta * p[j] + this->preconditioner.apply(j, r[j]);
1164 v[j] = Number();
1165 }
1166 }
1167 }
1168
1169 // Function that we use if the PreconditionerType implements an apply()
1170 // function
1171 template <typename U = void>
1172 std::enable_if_t<has_apply<PreconditionerType>, U>
1173 operation_after_loop(
1174 const unsigned int start_range,
1175 const unsigned int end_range,
1176 std::array<VectorizedArray<Number>, 7> &vectorized_sums) const
1177 {
1178 const Number *r = this->r.begin();
1179 const Number *p = this->p.begin();
1180 const Number *v = this->v.begin();
1181 std::array<VectorizedArray<Number>, 7> my_sums = {};
1182 constexpr unsigned int n_lanes = VectorizedArray<Number>::size();
1183 const unsigned int end_regular =
1184 start_range + (end_range - start_range) / n_lanes * n_lanes;
1185 for (unsigned int j = start_range; j < end_regular; j += n_lanes)
1186 {
1187 VectorizedArray<Number> pj, vj, rj;
1188 pj.load(p + j);
1189 vj.load(v + j);
1190 rj.load(r + j);
1191 const VectorizedArray<Number> prec_rj =
1192 apply_preconditioner(this->preconditioner, j, rj);
1193 const VectorizedArray<Number> prec_vj =
1194 apply_preconditioner(this->preconditioner, j, vj);
1195 my_sums[0] += pj * vj;
1196 my_sums[1] += vj * vj;
1197 my_sums[2] += rj * vj;
1198 my_sums[3] += rj * rj;
1199 my_sums[4] += rj * prec_vj;
1200 my_sums[5] += vj * prec_vj;
1201 my_sums[6] += rj * prec_rj;
1202 }
1203 for (unsigned int j = end_regular; j < end_range; ++j)
1204 {
1205 const Number prec_v = this->preconditioner.apply(j, v[j]);
1206 const Number prec_r = this->preconditioner.apply(j, r[j]);
1207 my_sums[0][0] += p[j] * v[j];
1208 my_sums[1][0] += v[j] * v[j];
1209 my_sums[2][0] += r[j] * v[j];
1210 my_sums[3][0] += r[j] * r[j];
1211 my_sums[4][0] += r[j] * prec_v;
1212 my_sums[5][0] += v[j] * prec_v;
1213 my_sums[6][0] += r[j] * prec_r;
1214 }
1215 for (unsigned int i = 0; i < vectorized_sums.size(); ++i)
1216 vectorized_sums[i] += my_sums[i];
1217 }
1218
1219 // Function that we use if the PreconditionerType implements an apply()
1220 // function
1221 template <typename U = void>
1222 std::enable_if_t<has_apply<PreconditionerType>, U>
1223 finalize_after_convergence(const unsigned int iteration_index)
1224 {
1225 if (iteration_index % 2 == 1 || this->beta == Number())
1226 this->x.add(this->alpha, this->p);
1227 else
1228 {
1229 using Number = typename VectorType::value_type;
1230 const unsigned int end_range = this->x.locally_owned_size();
1231
1232 Number *x = this->x.begin();
1233 Number *r = this->r.begin();
1234 Number *p = this->p.begin();
1235
1236 // Note that we use 'beta' here rather than 'previous_beta' in the
1237 // formula above, which is because the shift in beta ->
1238 // previous_beta has not been applied at this stage, allowing us
1239 // to recover the previous search direction
1240 const Number alpha_plus_previous_alpha_over_beta =
1241 this->alpha + this->previous_alpha / this->beta;
1242 const Number previous_alpha_over_beta =
1243 this->previous_alpha / this->beta;
1244
1246 for (unsigned int j = 0; j < end_range; ++j)
1247 {
1248 x[j] += alpha_plus_previous_alpha_over_beta * p[j] -
1249 previous_alpha_over_beta *
1250 this->preconditioner.apply(j, r[j]);
1251 }
1252 }
1253 }
1254
1255 // Function that we use if the PreconditionerType does not implement an
1256 // apply() function, where we instead need to choose the
1257 // apply_to_subrange function
1258 template <typename U = void>
1259 std::enable_if_t<!has_apply<PreconditionerType>, U>
1260 operation_before_loop(const unsigned int iteration_index,
1261 const unsigned int start_range,
1262 const unsigned int end_range) const
1263 {
1264 Number *x = this->x.begin() + start_range;
1265 Number *r = this->r.begin() + start_range;
1266 Number *p = this->p.begin() + start_range;
1267 Number *v = this->v.begin() + start_range;
1268 const Number alpha = this->alpha;
1269 const Number beta = this->beta;
1270 constexpr unsigned int grain_size = 128;
1271 std::array<Number, grain_size> prec_r;
1272 if (iteration_index == 1)
1273 {
1274 for (unsigned int j = start_range; j < end_range; j += grain_size)
1275 {
1276 const unsigned int length = std::min(grain_size, end_range - j);
1277 this->preconditioner.apply_to_subrange(j,
1278 j + length,
1279 r,
1280 prec_r.data());
1282 for (unsigned int i = 0; i < length; ++i)
1283 {
1284 p[i] = prec_r[i];
1285 v[i] = Number();
1286 }
1287 p += length;
1288 r += length;
1289 v += length;
1290 }
1291 }
1292 else if (iteration_index % 2 == 0 && beta != Number())
1293 {
1294 for (unsigned int j = start_range; j < end_range; j += grain_size)
1295 {
1296 const unsigned int length = std::min(grain_size, end_range - j);
1298 for (unsigned int i = 0; i < length; ++i)
1299 r[i] -= this->alpha * v[i];
1300 this->preconditioner.apply_to_subrange(j,
1301 j + length,
1302 r,
1303 prec_r.data());
1305 for (unsigned int i = 0; i < length; ++i)
1306 {
1307 p[i] = this->beta * p[i] + prec_r[i];
1308 v[i] = Number();
1309 }
1310 p += length;
1311 r += length;
1312 v += length;
1313 }
1314 }
1315 else if (iteration_index % 2 == 0 && beta == Number())
1316 {
1317 // Case where beta is zero: We cannot reconstruct p_{j-1} in the
1318 // next iteration, and must hence compute x here. This can happen
1319 // before termination
1320 for (unsigned int j = start_range; j < end_range; j += grain_size)
1321 {
1322 const unsigned int length = std::min(grain_size, end_range - j);
1324 for (unsigned int i = 0; i < length; ++i)
1325 r[i] -= this->alpha * v[i];
1326 this->preconditioner.apply_to_subrange(j,
1327 j + length,
1328 r,
1329 prec_r.data());
1331 for (unsigned int i = 0; i < length; ++i)
1332 {
1333 x[i] += this->alpha * p[i];
1334 p[i] = prec_r[i];
1335 v[i] = Number();
1336 }
1337 p += length;
1338 r += length;
1339 v += length;
1340 }
1341 }
1342 else
1343 {
1344 const Number alpha_plus_previous_alpha_over_beta =
1345 this->alpha + this->previous_alpha / this->previous_beta;
1346 const Number previous_alpha_over_beta =
1347 this->previous_alpha / this->previous_beta;
1348 for (unsigned int j = start_range; j < end_range; j += grain_size)
1349 {
1350 const unsigned int length = std::min(grain_size, end_range - j);
1351 this->preconditioner.apply_to_subrange(j,
1352 j + length,
1353 r,
1354 prec_r.data());
1356 for (unsigned int i = 0; i < length; ++i)
1357 {
1358 x[i] += alpha_plus_previous_alpha_over_beta * p[i] -
1359 previous_alpha_over_beta * prec_r[i];
1360 r[i] -= this->alpha * v[i];
1361 }
1362 this->preconditioner.apply_to_subrange(j,
1363 j + length,
1364 r,
1365 prec_r.data());
1367 for (unsigned int i = 0; i < length; ++i)
1368 {
1369 p[i] = this->beta * p[i] + prec_r[i];
1370 v[i] = Number();
1371 }
1372 p += length;
1373 r += length;
1374 v += length;
1375 x += length;
1376 }
1377 }
1378 }
1379
1380 // Function that we use if the PreconditionerType does not implement an
1381 // apply() function and where we instead need to use the
1382 // apply_to_subrange function
1383 template <typename U = void>
1384 std::enable_if_t<!has_apply<PreconditionerType>, U>
1385 operation_after_loop(
1386 const unsigned int start_range,
1387 const unsigned int end_range,
1388 std::array<VectorizedArray<Number>, 7> &vectorized_sums) const
1389 {
1390 const Number *r = this->r.begin();
1391 const Number *p = this->p.begin();
1392 const Number *v = this->v.begin();
1393 std::array<VectorizedArray<Number>, 7> my_sums = {};
1394 constexpr unsigned int grain_size = 128;
1395 Assert(grain_size % VectorizedArray<Number>::size() == 0,
1397 const unsigned int end_regular =
1398 start_range + (end_range - start_range) / grain_size * grain_size;
1399 std::array<Number, grain_size> prec_r;
1400 std::array<Number, grain_size> prec_v;
1401 for (unsigned int j = start_range; j < end_regular; j += grain_size)
1402 {
1403 this->preconditioner.apply_to_subrange(j,
1404 j + grain_size,
1405 r + j,
1406 prec_r.data());
1407 this->preconditioner.apply_to_subrange(j,
1408 j + grain_size,
1409 v + j,
1410 prec_v.data());
1411 VectorizedArray<Number> pj, vj, rj, prec_vj, prec_rj;
1412 for (unsigned int i = 0; i < grain_size;
1414 {
1415 pj.load(p + j + i);
1416 vj.load(v + j + i);
1417 rj.load(r + j + i);
1418 prec_rj.load(prec_r.data() + i);
1419 prec_vj.load(prec_v.data() + i);
1420
1421 my_sums[0] += pj * vj;
1422 my_sums[1] += vj * vj;
1423 my_sums[2] += rj * vj;
1424 my_sums[3] += rj * rj;
1425 my_sums[4] += rj * prec_vj;
1426 my_sums[5] += vj * prec_vj;
1427 my_sums[6] += rj * prec_rj;
1428 }
1429 }
1430 const unsigned int length = end_range - end_regular;
1431 AssertIndexRange(length, grain_size);
1432 this->preconditioner.apply_to_subrange(end_regular,
1433 end_regular + length,
1434 r + end_regular,
1435 prec_r.data());
1436 this->preconditioner.apply_to_subrange(end_regular,
1437 end_regular + length,
1438 v + end_regular,
1439 prec_v.data());
1440 for (unsigned int j = end_regular; j < end_range; ++j)
1441 {
1442 my_sums[0][0] += p[j] * v[j];
1443 my_sums[1][0] += v[j] * v[j];
1444 my_sums[2][0] += r[j] * v[j];
1445 my_sums[3][0] += r[j] * r[j];
1446 my_sums[4][0] += r[j] * prec_v[j - end_regular];
1447 my_sums[5][0] += v[j] * prec_v[j - end_regular];
1448 my_sums[6][0] += r[j] * prec_r[j - end_regular];
1449 }
1450 for (unsigned int i = 0; i < vectorized_sums.size(); ++i)
1451 vectorized_sums[i] += my_sums[i];
1452 }
1453
1454 // Function that we use if the PreconditionerType does not implement an
1455 // apply() function, where we instead need to choose the
1456 // apply_to_subrange function
1457 template <typename U = void>
1458 std::enable_if_t<!has_apply<PreconditionerType>, U>
1459 finalize_after_convergence(const unsigned int iteration_index)
1460 {
1461 if (iteration_index % 2 == 1 || this->beta == Number())
1462 this->x.add(this->alpha, this->p);
1463 else
1464 {
1465 const unsigned int end_range = this->x.locally_owned_size();
1466
1467 Number *x = this->x.begin();
1468 Number *r = this->r.begin();
1469 Number *p = this->p.begin();
1470 const Number alpha_plus_previous_alpha_over_beta =
1471 this->alpha + this->previous_alpha / this->beta;
1472 const Number previous_alpha_over_beta =
1473 this->previous_alpha / this->beta;
1474
1475 constexpr unsigned int grain_size = 128;
1476 std::array<Number, grain_size> prec_r;
1477 for (unsigned int j = 0; j < end_range; j += grain_size)
1478 {
1479 const unsigned int length = std::min(grain_size, end_range - j);
1480 this->preconditioner.apply_to_subrange(j,
1481 j + length,
1482 r,
1483 prec_r.data());
1485 for (unsigned int i = 0; i < length; ++i)
1486 x[i] += alpha_plus_previous_alpha_over_beta * p[i] -
1487 previous_alpha_over_beta * prec_r[i];
1488
1489 x += length;
1490 r += length;
1491 p += length;
1492 }
1493 }
1494 }
1495 };
1496
1497
1498
1499 // Device kernel for the vector updates of the pipelined CG variant
1500 // before the matrix-vector product, see the function
1501 // operation_before_loop() of the host variant for the operations. The
1502 // variable @p diagonal contains the diagonal of the preconditioner, or
1503 // nullptr for the identity matrix. Implemented in solver_cg.cc.
1504 template <typename Number>
1505 void
1506 do_vector_updates_device(const unsigned int iteration_index,
1507 const std::size_t locally_owned_size,
1508 const Number alpha,
1509 const Number beta,
1510 const Number previous_alpha,
1511 const Number previous_beta,
1512 const Number *diagonal,
1513 Number *x,
1514 Number *r,
1515 Number *p,
1516 const Number *v);
1517
1518
1519
1520 // Device kernel for the seven inner products of the pipelined CG
1521 // variant after the matrix-vector product, see
1522 // PipelinedIterationWorkerBase::finish_iteration() for the meaning of
1523 // the entries of the returned array. The result only contains the
1524 // contributions of the locally owned entries and still needs to be
1525 // summed over all MPI ranks. Implemented in solver_cg.cc.
1526 template <typename Number>
1527 std::array<Number, 7>
1528 do_reductions_device(const std::size_t locally_owned_size,
1529 const Number *diagonal,
1530 const Number *p,
1531 const Number *v,
1532 const Number *r);
1533
1534
1535
1536 // Device kernel for the final update of the solution vector after
1537 // convergence in case the update of the last but one iteration has not
1538 // been applied yet. Implemented in solver_cg.cc.
1539 template <typename Number>
1540 void
1541 do_finalize_device(const std::size_t locally_owned_size,
1542 const Number alpha_plus_previous_alpha_over_beta,
1543 const Number previous_alpha_over_beta,
1544 const Number *diagonal,
1545 Number *x,
1546 const Number *r,
1547 const Number *p);
1548
1549
1550
1551 // Implementation of the pipelined conjugate gradient method for vectors
1552 // in MemorySpace::Default with a point-wise preconditioner. Since the
1553 // parallelization model of devices does not allow to interleave the
1554 // vector updates with the matrix-vector product, we instead run one
1555 // kernel for all vector updates before the matrix-vector product and
1556 // another one for all inner products after it. This approach can be
1557 // used with any matrix type that provides a vmult() function.
1558 template <typename VectorType,
1559 typename MatrixType,
1560 typename PreconditionerType>
1561 struct IterationWorker<
1562 VectorType,
1563 MatrixType,
1564 PreconditionerType,
1565 std::enable_if_t<
1566 (std::is_same_v<PreconditionerType,
1567 ::DiagonalMatrix<VectorType>> ||
1568 std::is_same_v<PreconditionerType, PreconditionIdentity>) &&
1569 std::is_same_v<
1570 VectorType,
1571 LinearAlgebra::distributed::Vector<typename VectorType::value_type,
1572 MemorySpace::Default>>,
1573 int>> : public PipelinedIterationWorkerBase<VectorType,
1574 MatrixType,
1575 PreconditionerType>
1576 {
1577 using Number = typename VectorType::value_type;
1578
1579 const Number *diagonal;
1580
1581 static constexpr Number *
1582 get_diagonal(const ::DiagonalMatrix<VectorType> &preconditioner,
1583 const VectorType &vector)
1584 {
1585 AssertDimension(preconditioner.get_vector().locally_owned_size(),
1586 vector.locally_owned_size());
1587 return preconditioner.get_vector().get_values();
1588 }
1589
1590 static constexpr Number *
1591 get_diagonal(const ::PreconditionIdentity &, const VectorType &)
1592 {
1593 return nullptr;
1594 }
1595
1596 IterationWorker(const MatrixType &A,
1597 const PreconditionerType &preconditioner,
1598 const bool flexible,
1600 VectorType &x,
1601 const VectorType &b,
1602 const bool &use_default_residual)
1603 : PipelinedIterationWorkerBase<VectorType,
1604 MatrixType,
1605 PreconditionerType>(A,
1606 preconditioner,
1607 flexible,
1608 memory,
1609 x,
1610 b,
1611 use_default_residual)
1612 , diagonal(get_diagonal(preconditioner, x))
1613 {}
1614
1615 void
1616 do_iteration(const unsigned int iteration_index)
1617 {
1618 this->start_iteration(iteration_index);
1619
1620 const std::size_t size = this->x.locally_owned_size();
1621 do_vector_updates_device(iteration_index,
1622 size,
1623 this->alpha,
1624 this->beta,
1625 this->previous_alpha,
1626 this->previous_beta,
1627 diagonal,
1628 this->x.get_values(),
1629 this->r.get_values(),
1630 this->p.get_values(),
1631 this->v.get_values());
1632
1633 this->A.vmult(this->v, this->p);
1634
1635 std::array<Number, 7> sums = do_reductions_device(size,
1636 diagonal,
1637 this->p.get_values(),
1638 this->v.get_values(),
1639 this->r.get_values());
1640
1641 this->finish_iteration(sums);
1642 }
1643
1644 void
1645 finalize_after_convergence(const unsigned int iteration_index)
1646 {
1647 if (iteration_index % 2 == 1 || this->beta == Number())
1648 this->x.add(this->alpha, this->p);
1649 else
1650 // Note that we use 'beta' here rather than 'previous_beta', see
1651 // the host variant above
1652 do_finalize_device(this->x.locally_owned_size(),
1653 this->alpha + this->previous_alpha / this->beta,
1654 this->previous_alpha / this->beta,
1655 diagonal,
1656 this->x.get_values(),
1657 this->r.get_values(),
1658 this->p.get_values());
1659 }
1660 };
1661 } // namespace SolverCG
1662} // namespace internal
1663
1664
1665
1666template <typename VectorType>
1668template <typename MatrixType, typename PreconditionerType>
1672void SolverCG<VectorType>::solve(const MatrixType &A,
1673 VectorType &x,
1674 const VectorType &b,
1675 const PreconditionerType &preconditioner)
1676{
1677 using number = typename VectorType::value_type;
1678
1680
1681 LogStream::Prefix prefix("cg");
1682
1683 // Should we build the matrix for eigenvalue computations?
1684 const bool do_eigenvalues =
1685 !condition_number_signal.empty() || !all_condition_numbers_signal.empty() ||
1686 !eigenvalues_signal.empty() || !all_eigenvalues_signal.empty();
1687
1688 // vectors used for eigenvalue computations
1689 std::vector<typename VectorType::value_type> diagonal;
1690 std::vector<typename VectorType::value_type> offdiagonal;
1691
1692 typename VectorType::value_type eigen_beta_alpha = 0;
1693
1694 int it = 0;
1695
1696 internal::SolverCG::
1697 IterationWorker<VectorType, MatrixType, PreconditionerType>
1698 worker(A,
1699 preconditioner,
1700 determine_beta_by_flexible_formula,
1701 this->memory,
1702 x,
1703 b,
1704 additional_data.use_default_residual);
1705
1706 worker.startup();
1707
1708 solver_state = this->iteration_status(0, worker.residual_norm, x);
1709 if (solver_state != SolverControl::iterate)
1710 return;
1711
1712 while (solver_state == SolverControl::iterate)
1713 {
1714 ++it;
1715
1716 worker.do_iteration(it);
1717
1718 print_vectors(it, x, worker.r, worker.p);
1719
1720 if (it > 1)
1721 {
1722 this->coefficients_signal(worker.previous_alpha, worker.beta);
1723 // set up the vectors containing the diagonal and the off diagonal
1724 // of the projected matrix.
1725 if (do_eigenvalues)
1726 {
1727 diagonal.push_back(number(1.) / worker.previous_alpha +
1728 eigen_beta_alpha);
1729 eigen_beta_alpha = worker.beta / worker.previous_alpha;
1730 offdiagonal.push_back(std::sqrt(worker.beta) /
1731 worker.previous_alpha);
1732 }
1733 compute_eigs_and_cond(diagonal,
1734 offdiagonal,
1735 all_eigenvalues_signal,
1736 all_condition_numbers_signal);
1737 }
1738
1739 solver_state = this->iteration_status(it, worker.residual_norm, x);
1740 }
1741
1742 worker.finalize_after_convergence(it);
1743
1744 compute_eigs_and_cond(diagonal,
1745 offdiagonal,
1746 eigenvalues_signal,
1747 condition_number_signal);
1748
1749 AssertThrow(solver_state == SolverControl::success,
1750 SolverControl::NoConvergence(it, worker.residual_norm));
1751}
1752
1753
1754
1755template <typename VectorType>
1757boost::signals2::connection SolverCG<VectorType>::connect_coefficients_slot(
1758 const std::function<void(typename VectorType::value_type,
1759 typename VectorType::value_type)> &slot)
1760{
1761 return coefficients_signal.connect(slot);
1762}
1763
1764
1765
1766template <typename VectorType>
1768boost::signals2::connection SolverCG<VectorType>::connect_condition_number_slot(
1769 const std::function<void(double)> &slot,
1770 const bool every_iteration)
1771{
1772 if (every_iteration)
1773 {
1774 return all_condition_numbers_signal.connect(slot);
1775 }
1776 else
1777 {
1778 return condition_number_signal.connect(slot);
1779 }
1780}
1781
1782
1783
1784template <typename VectorType>
1786boost::signals2::connection SolverCG<VectorType>::connect_eigenvalues_slot(
1787 const std::function<void(const std::vector<double> &)> &slot,
1788 const bool every_iteration)
1789{
1790 if (every_iteration)
1791 {
1792 return all_eigenvalues_signal.connect(slot);
1793 }
1794 else
1795 {
1796 return eigenvalues_signal.connect(slot);
1797 }
1798}
1799
1800
1801
1802template <typename VectorType>
1805 const bool use_default_residual)
1806 : use_default_residual(use_default_residual)
1807{}
1808
1809
1810template <typename VectorType>
1814 const AdditionalData &data)
1815 : SolverCG<VectorType>(cn, mem)
1816{
1817 this->determine_beta_by_flexible_formula = true;
1818 this->additional_data = data;
1819}
1820
1821
1822
1823template <typename VectorType>
1826 const AdditionalData &data)
1827 : SolverCG<VectorType>(cn)
1828{
1829 this->determine_beta_by_flexible_formula = true;
1830 this->additional_data = data;
1831}
1832
1833
1834#endif // DOXYGEN
1835
1837
1838#endif
*  iterator end()
*  *  iterator begin()
SolverCG(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
virtual void print_vectors(const unsigned int step, const VectorType &x, const VectorType &r, const VectorType &d) const
boost::signals2::connection connect_condition_number_slot(const std::function< void(double)> &slot, const bool every_iteration=false)
boost::signals2::connection connect_eigenvalues_slot(const std::function< void(const std::vector< double > &)> &slot, const bool every_iteration=false)
boost::signals2::connection connect_coefficients_slot(const std::function< void(typename VectorType::value_type, typename VectorType::value_type)> &slot)
virtual ~SolverCG() override=default
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverCG(SolverControl &cn, const AdditionalData &data=AdditionalData())
static void compute_eigs_and_cond(const std::vector< typename VectorType::value_type > &diagonal, const std::vector< typename VectorType::value_type > &offdiagonal, const boost::signals2::signal< void(const std::vector< double > &)> &eigenvalues_signal, const boost::signals2::signal< void(double)> &cond_signal)
@ iterate
Continue iteration.
@ success
Stop iteration, goal reached.
AdditionalData additional_data
Definition solver_cg.h:475
SolverFlexibleCG(SolverControl &cn, const AdditionalData &data=AdditionalData())
SolverFlexibleCG(SolverControl &cn, VectorMemory< VectorType > &mem, const AdditionalData &data=AdditionalData())
void store(OtherNumber *ptr) const
void load(const OtherNumber *ptr)
#define DEAL_II_ALWAYS_INLINE
Definition config.h:166
#define DEAL_II_OPENMP_SIMD_PRAGMA
Definition config.h:214
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcDivideByZero()
#define AssertThrow(cond, exc)
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733
types::global_dof_index locally_owned_size
Definition mpi.cc:821
@ diagonal
Matrix is diagonal.
constexpr char U
constexpr char T
constexpr char A
Tpetra::Vector< Number, LO, GO, NodeType< MemorySpace > > VectorType
Tpetra::CrsMatrix< Number, LO, GO, NodeType< MemorySpace > > MatrixType
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
*  *  *  RotationFunction< dim, Number >::RotationFunction Number(dim)
T sum(const T &t, const MPI_Comm mpi_communicator)
void do_finalize_device(const std::size_t size, const Number alpha_plus_previous_alpha_over_beta, const Number previous_alpha_over_beta, const Number *diagonal, Number *x, const Number *r, const Number *p)
Definition solver_cg.cc:348
KOKKOS_FORCEINLINE_FUNCTION Number apply_preconditioner(const Number *diagonal, const std::size_t j, const Number value)
Definition solver_cg.cc:123
void do_vector_updates_device(const unsigned int iteration_index, const std::size_t size, const Number alpha, const Number beta, const Number previous_alpha, const Number previous_beta, const Number *diagonal, Number *x, Number *r, Number *p, const Number *v)
Definition solver_cg.cc:141
std::array< Number, 7 > do_reductions_device(const std::size_t size, const Number *p, const Number *v, const Number *r)
Definition solver_cg.cc:266
STL namespace.
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index
Definition types.h:92
AdditionalData(const bool use_default_residual=true)
AdditionalData(const bool use_default_residual=true)
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)