\chapter{The mixed-\/precision-\/ir program }
\hypertarget{mixed_precision_ir}{}\label{mixed_precision_ir}\index{The mixed-\/precision-\/ir program@{The mixed-\/precision-\/ir program}}
The Mixed Precision Iterative Refinement (MPIR) solver example.

This example depends on iterative-\/refinement.

 \label{_Mixed Precision Iterative Refinement (MPIR)}%
 \label{mixed_precision_ir_ThisexamplemanuallyimplementsaMixedPrecisionIterativeRefinementMPIRsolver}%
\Hypertarget{mixed_precision_ir_ThisexamplemanuallyimplementsaMixedPrecisionIterativeRefinementMPIRsolver}%
\doxysection*{This example manually implements a Mixed Precision Iterative Refinement (MPIR) solver.}

\doxysubsubsection*{In this example, we first read in a matrix from file, then generate a right-\/hand side and an initial guess. An inaccurate CG solver in single precision is used as the inner solver to an iterative refinement (IR) in double precision method which solves a linear system. }

\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;}

\end{DoxyCode}


Figure out where to run the code


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keywordflow}{if}\ (argc\ ==\ 2\ \&\&\ (std::string(argv[1])\ ==\ \textcolor{stringliteral}{"{}-\/-\/help"{}}))\ \{}
\DoxyCodeLine{\ \ \ \ std::cerr\ <<\ \textcolor{stringliteral}{"{}Usage:\ "{}}\ <<\ argv[0]\ <<\ \textcolor{stringliteral}{"{}\ [executor]"{}}\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::exit(-\/1);}
\DoxyCodeLine{\}}
\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();\ \}\}\};}

\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}}

\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 and initial guess as 1


\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{keywordflow}{for}\ (\textcolor{keyword}{auto}\ i\ =\ 0;\ i\ <\ size;\ i++)\ \{}
\DoxyCodeLine{\ \ \ \ host\_x-\/>at(i,\ 0)\ =\ 1.;}
\DoxyCodeLine{\}}
\DoxyCodeLine{\textcolor{keyword}{auto}\ x\ =\ \doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{gko::clone}}(exec,\ host\_x);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ b\ =\ \doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{gko::clone}}(exec,\ host\_x);}

\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\_vec\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<real\_vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{b-\/>compute\_norm2(initres\_vec);}

\end{DoxyCode}


Build lower-\/precision system matrix and residual


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ solver\_A\ =\ solver\_mtx::create(exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ inner\_residual\ =\ solver\_vec::create(exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ outer\_residual\ =\ vec::create(exec);}
\DoxyCodeLine{A-\/>convert\_to(solver\_A);}
\DoxyCodeLine{b-\/>convert\_to(outer\_residual);}

\end{DoxyCode}


restore b


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

\end{DoxyCode}


Create inner solver


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ inner\_solver\ =}
\DoxyCodeLine{\ \ \ \ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<SolverType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(inner\_reduction\_factor),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::Iteration::build().with\_max\_iters(max\_inner\_iters))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ -\/>generate(\doxymbox{\hyperlink{namespacegko_abc3b2c363d6eb3d9837a6d9efa589844}{give}}(solver\_A));}

\end{DoxyCode}


Solve system


\begin{DoxyCode}{0}
\DoxyCodeLine{exec-\/>synchronize();}
\DoxyCodeLine{std::chrono::nanoseconds\ time(0);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ res\_vec\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<real\_vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ initres\ =\ exec-\/>copy\_val\_to\_host(initres\_vec-\/>get\_const\_values());}
\DoxyCodeLine{\textcolor{keyword}{auto}\ inner\_solution\ =\ solver\_vec::create(exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ outer\_delta\ =\ vec::create(exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ tic\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ iter\ =\ -\/1;}
\DoxyCodeLine{\textcolor{keywordflow}{while}\ (\textcolor{keyword}{true})\ \{}
\DoxyCodeLine{\ \ \ \ ++iter;}

\end{DoxyCode}


convert residual to inner precision


\begin{DoxyCode}{0}
\DoxyCodeLine{outer\_residual-\/>convert\_to(inner\_residual);}
\DoxyCodeLine{outer\_residual-\/>compute\_norm2(res\_vec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ res\ =\ exec-\/>copy\_val\_to\_host(res\_vec-\/>get\_const\_values());}

\end{DoxyCode}


break if we exceed the number of iterations or have converged


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keywordflow}{if}\ (iter\ >\ max\_outer\_iters\ ||\ res\ /\ initres\ <\ outer\_reduction\_factor)\ \{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\}}

\end{DoxyCode}


Use the inner solver to solve A \texorpdfstring{$\ast$}{*} inner\+\_\+solution = inner\+\_\+residual with residual as initial guess.


\begin{DoxyCode}{0}
\DoxyCodeLine{inner\_solution-\/>copy\_from(inner\_residual);}
\DoxyCodeLine{inner\_solver-\/>apply(inner\_residual,\ inner\_solution);}

\end{DoxyCode}


convert inner solution to outer precision


\begin{DoxyCode}{0}
\DoxyCodeLine{inner\_solution-\/>convert\_to(outer\_delta);}

\end{DoxyCode}


x = x + inner\+\_\+solution


\begin{DoxyCode}{0}
\DoxyCodeLine{x-\/>add\_scaled(one,\ outer\_delta);}

\end{DoxyCode}


residual = b -\/ A \texorpdfstring{$\ast$}{*} x


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ outer\_residual-\/>copy\_from(b);}
\DoxyCodeLine{\ \ \ \ A-\/>apply(neg\_one,\ x,\ one,\ outer\_residual);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\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{A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{b-\/>compute\_norm2(res\_vec);}
\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\_vec);}
\DoxyCodeLine{std::cout\ <<\ \textcolor{stringliteral}{"{}Final\ residual\ norm\ sqrt(r\string^T\ r):\(\backslash\)n"{}};}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ res\_vec);}

\end{DoxyCode}


Print solver statistics


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}MPIR\ iteration\ count:\ \ \ \ \ "{}}\ <<\ iter\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}MPIR\ execution\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ <<\ 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{194.679}
\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.22728e-\/10}
\DoxyCodeLine{MPIR\ iteration\ count:\ \ \ \ \ 25}
\DoxyCodeLine{MPIR\ execution\ time\ [ms]:\ 0.846559}

\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\ }RealValueType\ =\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }SolverType\ =\ float;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }RealSolverType\ =\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<SolverType>}};}
\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\ }real\_vec\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<RealValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }solver\_vec\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<SolverType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }mtx\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<ValueType,\ IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }solver\_mtx\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<SolverType,\ IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }cg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Cg}{gko::solver::Cg<SolverType>}};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ max\_outer\_iters\ =\ 100u;}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ max\_inner\_iters\ =\ 100u;}
\DoxyCodeLine{\ \ \ \ RealValueType\ outer\_reduction\_factor\{1e-\/12\};}
\DoxyCodeLine{\ \ \ \ RealSolverType\ inner\_reduction\_factor\{1e-\/2\};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \doxymbox{\hyperlink{classgko_1_1version__info_ab13efac1405dd90951d2d9485acc4f30}{gko::version\_info::get}}()\ <<\ std::endl;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{if}\ (argc\ ==\ 2\ \&\&\ (std::string(argv[1])\ ==\ \textcolor{stringliteral}{"{}-\/-\/help"{}}))\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cerr\ <<\ \textcolor{stringliteral}{"{}Usage:\ "{}}\ <<\ argv[0]\ <<\ \textcolor{stringliteral}{"{}\ [executor]"{}}\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::exit(-\/1);}
\DoxyCodeLine{\ \ \ \ \}}
\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}{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{keywordflow}{for}\ (\textcolor{keyword}{auto}\ i\ =\ 0;\ i\ <\ size;\ i++)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ host\_x-\/>at(i,\ 0)\ =\ 1.;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ x\ =\ \doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{gko::clone}}(exec,\ host\_x);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ b\ =\ \doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{gko::clone}}(exec,\ host\_x);}
\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\_vec\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<real\_vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{\ \ \ \ b-\/>compute\_norm2(initres\_vec);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ solver\_A\ =\ solver\_mtx::create(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ inner\_residual\ =\ solver\_vec::create(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ outer\_residual\ =\ vec::create(exec);}
\DoxyCodeLine{\ \ \ \ A-\/>convert\_to(solver\_A);}
\DoxyCodeLine{\ \ \ \ b-\/>convert\_to(outer\_residual);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ b-\/>copy\_from(host\_x);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ inner\_solver\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<SolverType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(inner\_reduction\_factor),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::Iteration::build().with\_max\_iters(max\_inner\_iters))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ -\/>generate(\doxymbox{\hyperlink{namespacegko_abc3b2c363d6eb3d9837a6d9efa589844}{give}}(solver\_A));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ exec-\/>synchronize();}
\DoxyCodeLine{\ \ \ \ std::chrono::nanoseconds\ time(0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ res\_vec\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<real\_vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ initres\ =\ exec-\/>copy\_val\_to\_host(initres\_vec-\/>get\_const\_values());}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ inner\_solution\ =\ solver\_vec::create(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ outer\_delta\ =\ vec::create(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ tic\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordtype}{int}\ iter\ =\ -\/1;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{while}\ (\textcolor{keyword}{true})\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ++iter;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ outer\_residual-\/>convert\_to(inner\_residual);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ outer\_residual-\/>compute\_norm2(res\_vec);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ res\ =\ exec-\/>copy\_val\_to\_host(res\_vec-\/>get\_const\_values());}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (iter\ >\ max\_outer\_iters\ ||\ res\ /\ initres\ <\ outer\_reduction\_factor)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ inner\_solution-\/>copy\_from(inner\_residual);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ inner\_solver-\/>apply(inner\_residual,\ inner\_solution);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ inner\_solution-\/>convert\_to(outer\_delta);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ x-\/>add\_scaled(one,\ outer\_delta);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ outer\_residual-\/>copy\_from(b);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ A-\/>apply(neg\_one,\ x,\ one,\ outer\_residual);}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ toc\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ time\ +=\ std::chrono::duration\_cast<std::chrono::nanoseconds>(toc\ -\/\ tic);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{\ \ \ \ b-\/>compute\_norm2(res\_vec);}
\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\_vec);}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Final\ residual\ norm\ sqrt(r\string^T\ r):\(\backslash\)n"{}};}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ res\_vec);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}MPIR\ iteration\ count:\ \ \ \ \ "{}}\ <<\ iter\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}MPIR\ execution\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 