Debian Codegraph @

packages/trilinoscouplings/examples/fenl/CGSolve.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOS_EXAMPLE_CG_SOLVE
18 #define KOKKOS_EXAMPLE_CG_SOLVE
19
20 #include <cmath>
21 #include <limits>
22 #include <Kokkos_Core.hpp>
23 #include <KokkosSparse_CrsMatrix.hpp>
24 #include <Kokkos_Timer.hpp>
25 #include <Kokkos_ArithTraits.hpp>
26
27 #include <Teuchos_CommHelpers.hpp>
28 #include <Tpetra_CrsMatrix.hpp>
29 //----------------------------------------------------------------------------
30 //----------------------------------------------------------------------------
31
32 namespace Kokkos {
33 namespace Example {
34
35 template <typename Scalar>
36 inline
37 Scalar all_reduce( Scalar local , const Teuchos::RCP<const Teuchos::Comm<int> >& comm )
38 {
39 Scalar global = 0 ;
40 Teuchos::reduceAll( *comm , Teuchos::REDUCE_SUM , 1 , & local , & global );
41 return global ;
42 }
43
44 template <typename Scalar>
45 inline
46 Teuchos::Array<Scalar>
47 all_reduce( Teuchos::Array<Scalar> local ,
48 const Teuchos::RCP<const Teuchos::Comm<int> >& comm )
49 {
50 const int sz = local.size();
51 Teuchos::Array<Scalar> global(sz);
52 Teuchos::reduceAll( *comm , Teuchos::REDUCE_SUM , sz , local.getRawPtr() , global.getRawPtr() );
53 return global ;
54 }
55
56 template <typename Scalar>
57 inline
58 Scalar all_reduce_max( Scalar local , const Teuchos::RCP<const Teuchos::Comm<int> >& comm )
59 {
60 Scalar global = 0 ;
61 Teuchos::reduceAll( *comm , Teuchos::REDUCE_MAX , 1 , & local , & global );
62 return global ;
63 }
64
65 struct result_struct {
66 double add_time, dot_time, matvec_time, iter_time, prec_setup_time,
67 prec_apply_time, total_time;
68 int iteration;
69 std::vector<int> ensemble_its;
70 double norm_res;
71 result_struct() :
72 add_time(0), dot_time(0), matvec_time(0), iter_time(0), prec_setup_time(0),
73 prec_apply_time(0), total_time(0), iteration(0), norm_res(0) {}
74 result_struct(double add, double dot, double matvec, double prec_setup,
75 double prec_apply, double total, int niter, double res) :
76 add_time(add),
77 dot_time(dot),
78 matvec_time(matvec),
79 iter_time(add+dot+matvec),
80 prec_setup_time(prec_setup),
81 prec_apply_time(prec_apply),
82 total_time(total),
83 iteration(niter),
84 norm_res(res) {};
85 };
86
87 template<class CrsMatrix, class Vector>
88 result_struct cg_solve(
89 Teuchos::RCP<CrsMatrix> A, Teuchos::RCP<Vector> b, Teuchos::RCP<Vector> x,
90 int max_iter = 200,
91 typename Kokkos::ArithTraits<typename CrsMatrix::scalar_type>::mag_type tolerance =
92 Kokkos::ArithTraits<typename CrsMatrix::scalar_type>::epsilon(),
93 int print = 0)
94 {
95 Kokkos::Timer total_timer;
96
97 typedef typename CrsMatrix::scalar_type ScalarType;
98 typedef Kokkos::ArithTraits<ScalarType> KAT;
99 typedef typename KAT::mag_type MagnitudeType;
100 typedef typename CrsMatrix::local_ordinal_type LocalOrdinalType;
101 Teuchos::RCP<Vector> r,p,Ap;
102
103 // create temporary Vectors
104 r = Tpetra::createVector<ScalarType>(A->getRangeMap());
105 p = Tpetra::createVector<ScalarType>(A->getRangeMap());
106 Ap = Tpetra::createVector<ScalarType>(A->getRangeMap());
107
108 // fill with initial Values (make this a functor call or something)
109 x->putScalar(0.0);
110
111 MagnitudeType normr = 0;
112 MagnitudeType rtrans = 0;
113 MagnitudeType oldrtrans = 0;
114
115 LocalOrdinalType print_freq = max_iter/10;
116 if (print_freq>50) print_freq = 50;
117 if (print_freq<1) print_freq = 1;
118
119 double dottime = 0;
120 double addtime = 0;
121 double matvectime = 0;
122
123 Kokkos::Timer timer;
124 p->update(1.0,*x,0.0,*x,0.0);
125 addtime += timer.seconds(); timer.reset();
126
127 A->apply(*p, *Ap);
128 matvectime += timer.seconds(); timer.reset();
129
130 r->update(1.0,*b,-1.0,*Ap,0.0);
131 addtime += timer.seconds(); timer.reset();
132
133 rtrans = r->dot(*r);
134 dottime += timer.seconds(); timer.reset();
135
136 normr = std::sqrt(rtrans);
137
138 MagnitudeType normr_orig = normr;
139
140 if (print) {
141 std::cout << "Initial Residual = "<< normr << std::endl;
142 }
143
144 MagnitudeType brkdown_tol = KAT::epsilon();
145
146 // Count external so that we keep iteration count in the end
147 LocalOrdinalType k;
148 for(k=1; k <= max_iter && normr / normr_orig > tolerance; ++k) {
149 if (k == 1) {
150 p->update(1.0,*r,0.0,*r,0.0);
151 addtime += timer.seconds(); timer.reset();
152 }
153 else {
154 oldrtrans = rtrans;
155 rtrans = r->dot(*r);
156 dottime += timer.seconds(); timer.reset();
157 MagnitudeType beta = rtrans/oldrtrans;
158 p->update(beta,*p,1.0,*r,0.0);
159 addtime += timer.seconds(); timer.reset();
160 }
161 normr = std::sqrt(rtrans);
162 if (print && (k%print_freq==0 || k==max_iter)) {
163 std::cout << "Iteration = "<<k<<" Residual = "<<normr<<std::endl;
164 }
165
166 MagnitudeType alpha = 0;
167 MagnitudeType p_ap_dot = 0;
168 A->apply(*p, *Ap);
169 matvectime += timer.seconds(); timer.reset();
170
171 p_ap_dot = Ap->dot(*p);
172 dottime += timer.seconds(); timer.reset();
173
174 if (p_ap_dot < brkdown_tol) {
175 if (p_ap_dot < 0 ) {
176 std::cerr << "cg_solve ERROR, numerical breakdown!"<<std::endl;
177 return result_struct();
178 }
179 else brkdown_tol = 0.1 * p_ap_dot;
180 }
181 alpha = rtrans/p_ap_dot;
182
183 x->update(1.0,*x,alpha,*p,0.0);
184 r->update(1.0,*r,-alpha,*Ap,0.0);
185 addtime += timer.seconds(); timer.reset();
186
187 }
188 rtrans = r->dot(*r);
189
190 normr = std::sqrt(rtrans);
191
192 double totaltime = total_timer.seconds();
193 double prec_setup = 0;
194 double prec_apply = 0;
195 return result_struct(addtime,dottime,matvectime,prec_setup,prec_apply,
196 totaltime,k-1,FENL::scalar_norm(normr));
197 }
198
199
200
201
202 } // namespace Example
203 } // namespace Kokkos
204
205 //----------------------------------------------------------------------------
206 //----------------------------------------------------------------------------
207
208 #endif /* #ifndef KOKKOS_EXAMPLE_CG_SOLVE */