\chapter{The mixed-\/multigrid-\/preconditioned-\/solver program }
\hypertarget{mixed_multigrid_preconditioned_solver}{}\label{mixed_multigrid_preconditioned_solver}\index{The mixed-\/multigrid-\/preconditioned-\/solver program@{The mixed-\/multigrid-\/preconditioned-\/solver program}}
The preconditioned solver example.

This example depends on multigrid-\/preconditioned-\/solver, mixed-\/multigrid-\/solver.

 \label{_Multigrid Preconditioner}%
 \label{mixed_multigrid_preconditioned_solver_Thisexampleshowshowtousethemixedprecisionmultigridpreconditioner}%
\Hypertarget{mixed_multigrid_preconditioned_solver_Thisexampleshowshowtousethemixedprecisionmultigridpreconditioner}%
\doxysection*{This example shows how to use the mixed-\/precision multigrid preconditioner.}

\doxysubsubsection*{In this example, we first read in a matrix from a file. The preconditioned CG solver is enhanced with a mixed-\/precision multigrid preconditioner. The example features the generating time and runtime of the CG solver.}

\label{three_pt_stencil_solver_CommProg}%
\Hypertarget{three_pt_stencil_solver_CommProg}%
 \doxysection*{The commented program}

Print version information


\begin{DoxyCode}{0}
\DoxyCodeLine{std::cout\ <<\ \doxymbox{\hyperlink{classgko_1_1version__info_ab13efac1405dd90951d2d9485acc4f30}{gko::version\_info::get}}()\ <<\ std::endl;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ executor\_string\ =\ argc\ >=\ 2\ ?\ argv[1]\ :\ \textcolor{stringliteral}{"{}reference"{}};}

\end{DoxyCode}


Figure out where to run the code


\begin{DoxyCode}{0}
\DoxyCodeLine{std::map<std::string,\ std::function<std::shared\_ptr<gko::Executor>()>>}
\DoxyCodeLine{\ \ \ \ exec\_map\{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}omp"{}},\ []\ \{\ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::create}}();\ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}cuda"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ []\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1CudaExecutor_a075a1957241de65c0cad59f9534430a2}{gko::CudaExecutor::create}}(0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::create}}());}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}hip"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ []\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1HipExecutor_a8afc5210bf8dd8aedfceab4b73f0047b}{gko::HipExecutor::create}}(0,\ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::create}}());}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}dpcpp"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ []\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1DpcppExecutor_af69a356ee07f641010df5d2e9e518bb0}{gko::DpcppExecutor::create}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 0,\ gko::ReferenceExecutor::create());}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}reference"{}},\ []\ \{\ \textcolor{keywordflow}{return}\ gko::ReferenceExecutor::create();\ \}\}\};}

\end{DoxyCode}


executor where Ginkgo will perform the computation


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ exec\ =\ exec\_map.at(executor\_string)();\ \ \textcolor{comment}{//\ throws\ if\ not\ valid}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keywordtype}{int}\ mixed\_int\ =\ argc\ >=\ 3\ ?\ std::atoi(argv[2])\ :\ 1;}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keywordtype}{bool}\ use\_mixed\ =\ mixed\_int\ !=\ 0;\ \ \textcolor{comment}{//\ nonzero\ uses\ mixed}}
\DoxyCodeLine{std::cout\ <<\ \textcolor{stringliteral}{"{}Using\ mixed\ precision?\ "{}}\ <<\ use\_mixed\ <<\ std::endl;}

\end{DoxyCode}


Read data


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ A\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{share}}(\doxymbox{\hyperlink{namespacegko_abc58509ecdcb48399e12f9a081ed7545}{gko::read<mtx>}}(std::ifstream(\textcolor{stringliteral}{"{}data/A.mtx"{}}),\ exec));}

\end{DoxyCode}


Create RHS as 1 and initial guess as 0


\begin{DoxyCode}{0}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ size\ =\ A-\/>get\_size()[0];}
\DoxyCodeLine{\textcolor{keyword}{auto}\ host\_x\ =\ vec::create(exec-\/>get\_master(),\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(size,\ 1));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ host\_b\ =\ vec::create(exec-\/>get\_master(),\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(size,\ 1));}
\DoxyCodeLine{\textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\ i\ =\ 0;\ i\ <\ size;\ i++)\ \{}
\DoxyCodeLine{\ \ \ \ host\_x-\/>at(i,\ 0)\ =\ 0.;}
\DoxyCodeLine{\ \ \ \ host\_b-\/>at(i,\ 0)\ =\ 1.;}
\DoxyCodeLine{\}}
\DoxyCodeLine{\textcolor{keyword}{auto}\ x\ =\ vec::create(exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ b\ =\ vec::create(exec);}
\DoxyCodeLine{x-\/>copy\_from(host\_x);}
\DoxyCodeLine{b-\/>copy\_from(host\_b);}

\end{DoxyCode}


Calculate initial residual by overwriting b


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_a0059e27f8f4bc348ff65c1e60caf47c8}{one}}\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{1.0\},\ exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ neg\_one\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{-\/1.0\},\ exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ initres\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{b-\/>compute\_norm2(initres);}

\end{DoxyCode}


copy b again


\begin{DoxyCode}{0}
\DoxyCodeLine{b-\/>copy\_from(host\_b);}

\end{DoxyCode}


Prepare the stopping criteria


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ tolerance\ =\ 1e-\/8;}
\DoxyCodeLine{\textcolor{keyword}{auto}\ iter\_stop\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(gko::stop::Iteration::build().with\_max\_iters(100u).on(exec));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ tol\_stop\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_baseline(gko::stop::mode::absolute)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(tolerance)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{}
\DoxyCodeLine{std::shared\_ptr<const\ gko::log::Convergence<ValueType>>\ logger\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{classgko_1_1log_1_1Convergence_ab0ec7b5f1f08c7237e2181e71cf7d5ed}{gko::log::Convergence<ValueType>::create}}();}
\DoxyCodeLine{iter\_stop-\/>add\_logger(logger);}
\DoxyCodeLine{tol\_stop-\/>add\_logger(logger);}

\end{DoxyCode}


Create smoother factory (ir with bj)


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ inner\_solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(bj::build().with\_max\_block\_size(1u).on(exec));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ inner\_solver\_gen\_f\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(bj\_f::build().with\_max\_block\_size(1u).on(exec));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ smoother\_gen\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ ir::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ smoother\_gen\_f\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ ir\_f::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}MixedType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec));}

\end{DoxyCode}


Create Multigrid\+Level factory


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ mg\_level\_gen\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(pgm::build().with\_deterministic(\textcolor{keyword}{true}).on(exec));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ mg\_level\_gen\_f\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(pgm\_f::build().with\_deterministic(\textcolor{keyword}{true}).on(exec));}

\end{DoxyCode}


Create Coarsest\+Solver factory


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ coarsest\_gen\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ ir::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(4u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ coarsest\_gen\_f\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ ir\_f::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}MixedType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(4u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec));}

\end{DoxyCode}


Create multigrid factory


\begin{DoxyCode}{0}
\DoxyCodeLine{std::shared\_ptr<gko::LinOpFactory>\ multigrid\_gen;}
\DoxyCodeLine{\textcolor{keywordflow}{if}\ (use\_mixed)\ \{}
\DoxyCodeLine{\ \ \ \ multigrid\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ mg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_max\_levels(10u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_min\_coarse\_rows(2u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_pre\_smoother(smoother\_gen,\ smoother\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_post\_uses\_pre(\textcolor{keyword}{true})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_mg\_level(mg\_level\_gen,\ mg\_level\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_level\_selector([](\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ level,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1LinOp}{gko::LinOp}}*)\ -\/>\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ \{}

\end{DoxyCode}


The first (index 0) level will use the first mg\+\_\+level\+\_\+gen, smoother\+\_\+gen which are the factories with Value\+Type. The rest of levels (\texorpdfstring{$>$}{>}= 1) will use the second (index 1) mg\+\_\+level\+\_\+gen2 and smoother\+\_\+gen2 which use the Mixed\+Type. The rest of levels will use different type than the normal multigrid.


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ level\ >=\ 1\ ?\ 1\ :\ 0;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_coarsest\_solver(coarsest\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_default\_initial\_guess(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aad02c4c4cde7ae76252540d116a40f23a}{gko::solver::initial\_guess\_mode::zero}})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{\ \ \ \ multigrid\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ mg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_max\_levels(10u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_min\_coarse\_rows(2u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_pre\_smoother(smoother\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_post\_uses\_pre(\textcolor{keyword}{true})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_mg\_level(mg\_level\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_coarsest\_solver(coarsest\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_default\_initial\_guess(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aad02c4c4cde7ae76252540d116a40f23a}{gko::solver::initial\_guess\_mode::zero}})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\}}

\end{DoxyCode}


Create solver factory


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ solver\_gen\ =\ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(iter\_stop,\ tol\_stop)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_preconditioner(multigrid\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}

\end{DoxyCode}


Create solver


\begin{DoxyCode}{0}
\DoxyCodeLine{std::chrono::nanoseconds\ gen\_time(0);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ gen\_tic\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =\ solver\_gen-\/>generate(A);}
\DoxyCodeLine{exec-\/>synchronize();}
\DoxyCodeLine{\textcolor{keyword}{auto}\ gen\_toc\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{gen\_time\ +=}
\DoxyCodeLine{\ \ \ \ std::chrono::duration\_cast<std::chrono::nanoseconds>(gen\_toc\ -\/\ gen\_tic);}

\end{DoxyCode}


Solve system


\begin{DoxyCode}{0}
\DoxyCodeLine{exec-\/>synchronize();}
\DoxyCodeLine{std::chrono::nanoseconds\ time(0);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ tic\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>apply(b,\ x);}
\DoxyCodeLine{exec-\/>synchronize();}
\DoxyCodeLine{\textcolor{keyword}{auto}\ toc\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{time\ +=\ std::chrono::duration\_cast<std::chrono::nanoseconds>(toc\ -\/\ tic);}

\end{DoxyCode}


Calculate residual


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ res\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{b-\/>compute\_norm2(res);}
\DoxyCodeLine{}
\DoxyCodeLine{std::cout\ <<\ \textcolor{stringliteral}{"{}Initial\ residual\ norm\ sqrt(r\string^T\ r):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ initres);}
\DoxyCodeLine{std::cout\ <<\ \textcolor{stringliteral}{"{}Final\ residual\ norm\ sqrt(r\string^T\ r):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ res);}

\end{DoxyCode}


Print solver statistics


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ iteration\ count:\ \ \ \ \ "{}}\ <<\ logger-\/>get\_num\_iterations()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ generation\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(gen\_time.count())\ /\ 1000000.0\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ execution\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ execution\ time\ per\ iteration[ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logger-\/>get\_num\_iterations()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCode}
 \label{three_pt_stencil_solver_Results}%
\Hypertarget{three_pt_stencil_solver_Results}%
\doxysection*{Results}

This is the expected output:\+


\begin{DoxyCode}{0}
\DoxyCodeLine{Initial\ residual\ norm\ sqrt(r\string^T\ r):}
\DoxyCodeLine{\%\%MatrixMarket\ matrix\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}\ \doxymbox{\hyperlink{namespacegko_af21648097306adbabc6234b4609ce297}{real}}\ general}
\DoxyCodeLine{1\ 1}
\DoxyCodeLine{4.3589}
\DoxyCodeLine{Final\ residual\ norm\ sqrt(r\string^T\ r):}
\DoxyCodeLine{\%\%MatrixMarket\ matrix\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}\ \doxymbox{\hyperlink{namespacegko_af21648097306adbabc6234b4609ce297}{real}}\ general}
\DoxyCodeLine{1\ 1}
\DoxyCodeLine{1.69858e-\/09}
\DoxyCodeLine{CG\ iteration\ count:\ \ \ \ \ 39}
\DoxyCodeLine{CG\ generation\ time\ [ms]:\ 2.04293}
\DoxyCodeLine{CG\ execution\ time\ [ms]:\ 22.3874}
\DoxyCodeLine{CG\ execution\ time\ per\ iteration[ms]:\ 0.574036}

\end{DoxyCode}


\label{three_pt_stencil_solver_Commentsaboutprogramminganddebugging}%
\Hypertarget{three_pt_stencil_solver_Commentsaboutprogramminganddebugging}%
\doxysubsubsection*{Comments about programming and debugging }

\label{three_pt_stencil_solver_PlainProg}%
\Hypertarget{three_pt_stencil_solver_PlainProg}%
 \doxysection*{The plain program}


\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <fstream>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <iomanip>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <iostream>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <map>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <string>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <ginkgo/ginkgo.hpp>}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}*\ argv[])}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }ValueType\ =\ double;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }MixedType\ =\ float;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }IndexType\ =\ int;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }vec\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }mtx\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<ValueType,\ IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }fcg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Fcg}{gko::solver::Fcg<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }cg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Cg}{gko::solver::Cg<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }ir\ =\ \doxymbox{\hyperlink{namespacegko_1_1solver_a1dac6a1b61c52db896845967e899aef0}{gko::solver::Ir<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }mg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Multigrid}{gko::solver::Multigrid}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }bj\ =\ \doxymbox{\hyperlink{classgko_1_1preconditioner_1_1Jacobi}{gko::preconditioner::Jacobi<ValueType,\ IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }pgm\ =\ \doxymbox{\hyperlink{classgko_1_1multigrid_1_1Pgm}{gko::multigrid::Pgm<ValueType,\ IndexType>}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }cg\_f\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Cg}{gko::solver::Cg<MixedType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }ir\_f\ =\ \doxymbox{\hyperlink{namespacegko_1_1solver_a1dac6a1b61c52db896845967e899aef0}{gko::solver::Ir<MixedType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }bj\_f\ =\ \doxymbox{\hyperlink{classgko_1_1preconditioner_1_1Jacobi}{gko::preconditioner::Jacobi<MixedType,\ IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }pgm\_f\ =\ \doxymbox{\hyperlink{classgko_1_1multigrid_1_1Pgm}{gko::multigrid::Pgm<MixedType,\ IndexType>}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \doxymbox{\hyperlink{classgko_1_1version__info_ab13efac1405dd90951d2d9485acc4f30}{gko::version\_info::get}}()\ <<\ std::endl;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ executor\_string\ =\ argc\ >=\ 2\ ?\ argv[1]\ :\ \textcolor{stringliteral}{"{}reference"{}};}
\DoxyCodeLine{\ \ \ \ std::map<std::string,\ std::function<std::shared\_ptr<gko::Executor>()>>}
\DoxyCodeLine{\ \ \ \ \ \ \ \ exec\_map\{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}omp"{}},\ []\ \{\ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::create}}();\ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}cuda"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ []\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1CudaExecutor_a075a1957241de65c0cad59f9534430a2}{gko::CudaExecutor::create}}(0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::create}}());}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}hip"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ []\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1HipExecutor_a8afc5210bf8dd8aedfceab4b73f0047b}{gko::HipExecutor::create}}(0,\ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::create}}());}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}dpcpp"{}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ []\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{classgko_1_1DpcppExecutor_af69a356ee07f641010df5d2e9e518bb0}{gko::DpcppExecutor::create}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 0,\ gko::ReferenceExecutor::create());}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \}\},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \{\textcolor{stringliteral}{"{}reference"{}},\ []\ \{\ \textcolor{keywordflow}{return}\ gko::ReferenceExecutor::create();\ \}\}\};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ exec\ =\ exec\_map.at(executor\_string)();\ \ \textcolor{comment}{//\ throws\ if\ not\ valid}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{int}\ mixed\_int\ =\ argc\ >=\ 3\ ?\ std::atoi(argv[2])\ :\ 1;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{bool}\ use\_mixed\ =\ mixed\_int\ !=\ 0;\ \ \textcolor{comment}{//\ nonzero\ uses\ mixed}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Using\ mixed\ precision?\ "{}}\ <<\ use\_mixed\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ A\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{share}}(\doxymbox{\hyperlink{namespacegko_abc58509ecdcb48399e12f9a081ed7545}{gko::read<mtx>}}(std::ifstream(\textcolor{stringliteral}{"{}data/A.mtx"{}}),\ exec));}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ size\ =\ A-\/>get\_size()[0];}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ host\_x\ =\ vec::create(exec-\/>get\_master(),\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(size,\ 1));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ host\_b\ =\ vec::create(exec-\/>get\_master(),\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(size,\ 1));}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\ i\ =\ 0;\ i\ <\ size;\ i++)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ host\_x-\/>at(i,\ 0)\ =\ 0.;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ host\_b-\/>at(i,\ 0)\ =\ 1.;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ x\ =\ vec::create(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ b\ =\ vec::create(exec);}
\DoxyCodeLine{\ \ \ \ x-\/>copy\_from(host\_x);}
\DoxyCodeLine{\ \ \ \ b-\/>copy\_from(host\_b);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_a0059e27f8f4bc348ff65c1e60caf47c8}{one}}\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{1.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ neg\_one\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{-\/1.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ initres\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{\ \ \ \ b-\/>compute\_norm2(initres);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ b-\/>copy\_from(host\_b);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ tolerance\ =\ 1e-\/8;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ iter\_stop\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(gko::stop::Iteration::build().with\_max\_iters(100u).on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ tol\_stop\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_baseline(gko::stop::mode::absolute)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(tolerance)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::shared\_ptr<const\ gko::log::Convergence<ValueType>>\ logger\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1log_1_1Convergence_ab0ec7b5f1f08c7237e2181e71cf7d5ed}{gko::log::Convergence<ValueType>::create}}();}
\DoxyCodeLine{\ \ \ \ iter\_stop-\/>add\_logger(logger);}
\DoxyCodeLine{\ \ \ \ tol\_stop-\/>add\_logger(logger);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ inner\_solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(bj::build().with\_max\_block\_size(1u).on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ inner\_solver\_gen\_f\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(bj\_f::build().with\_max\_block\_size(1u).on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ smoother\_gen\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ir::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ smoother\_gen\_f\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ir\_f::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}MixedType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ mg\_level\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(pgm::build().with\_deterministic(\textcolor{keyword}{true}).on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ mg\_level\_gen\_f\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(pgm\_f::build().with\_deterministic(\textcolor{keyword}{true}).on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ coarsest\_gen\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ir::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(4u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ coarsest\_gen\_f\ =\ \doxymbox{\hyperlink{namespacegko_ae99fdd8bfa5ac694adcb7297d432967e}{gko::share}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ir\_f::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_solver(inner\_solver\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_relaxation\_factor(\textcolor{keyword}{static\_cast<}MixedType\textcolor{keyword}{>}(0.9))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(4u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec));}
\DoxyCodeLine{\ \ \ \ std::shared\_ptr<gko::LinOpFactory>\ multigrid\_gen;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{if}\ (use\_mixed)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ multigrid\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ mg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_max\_levels(10u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_min\_coarse\_rows(2u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_pre\_smoother(smoother\_gen,\ smoother\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_post\_uses\_pre(\textcolor{keyword}{true})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_mg\_level(mg\_level\_gen,\ mg\_level\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_level\_selector([](\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ level,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1LinOp}{gko::LinOp}}*)\ -\/>\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ level\ >=\ 1\ ?\ 1\ :\ 0;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_coarsest\_solver(coarsest\_gen\_f)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_default\_initial\_guess(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aad02c4c4cde7ae76252540d116a40f23a}{gko::solver::initial\_guess\_mode::zero}})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ multigrid\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ mg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_max\_levels(10u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_min\_coarse\_rows(2u)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_pre\_smoother(smoother\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_post\_uses\_pre(\textcolor{keyword}{true})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_mg\_level(mg\_level\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_coarsest\_solver(coarsest\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_default\_initial\_guess(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aad02c4c4cde7ae76252540d116a40f23a}{gko::solver::initial\_guess\_mode::zero}})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(1u))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ solver\_gen\ =\ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(iter\_stop,\ tol\_stop)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_preconditioner(multigrid\_gen)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\ \ \ \ std::chrono::nanoseconds\ gen\_time(0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ gen\_tic\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =\ solver\_gen-\/>generate(A);}
\DoxyCodeLine{\ \ \ \ exec-\/>synchronize();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ gen\_toc\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ gen\_time\ +=}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::chrono::duration\_cast<std::chrono::nanoseconds>(gen\_toc\ -\/\ gen\_tic);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ exec-\/>synchronize();}
\DoxyCodeLine{\ \ \ \ std::chrono::nanoseconds\ time(0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ tic\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>apply(b,\ x);}
\DoxyCodeLine{\ \ \ \ exec-\/>synchronize();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ toc\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ time\ +=\ std::chrono::duration\_cast<std::chrono::nanoseconds>(toc\ -\/\ tic);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ res\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{\ \ \ \ b-\/>compute\_norm2(res);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Initial\ residual\ norm\ sqrt(r\string^T\ r):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ initres);}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Final\ residual\ norm\ sqrt(r\string^T\ r):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ res);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ iteration\ count:\ \ \ \ \ "{}}\ <<\ logger-\/>get\_num\_iterations()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ generation\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(gen\_time.count())\ /\ 1000000.0\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ execution\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}CG\ execution\ time\ per\ iteration[ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ logger-\/>get\_num\_iterations()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 