\chapter{The iterative-\/refinement program }
\hypertarget{iterative_refinement}{}\label{iterative_refinement}\index{The iterative-\/refinement program@{The iterative-\/refinement program}}
The iterative refinement solver example.

This example depends on simple-\/solver.

 \label{_Iterative Refinement}%
 \label{iterative_refinement_Thisexampleshowshowtousetheiterativerefinementsolver}%
\Hypertarget{iterative_refinement_Thisexampleshowshowtousetheiterativerefinementsolver}%
\doxysection*{This example shows how to use the iterative refinement 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 is used as the inner solver to an iterative refinement (IR) method which solves a linear system. The example features the iteration count and runtime of the IR solver. }

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


\begin{DoxyCode}{0}
\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}}(0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::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\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_ae198b3130d8966418047baa8dabe466c}{gko::matrix::Dense<ValueType>::create}}(exec-\/>get\_master(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \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\ =\ \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);}

\end{DoxyCode}


copy b again


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

\end{DoxyCode}


Create solver factory


\begin{DoxyCode}{0}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ max\_iters\ =\ 10000u;}
\DoxyCodeLine{RealValueType\ outer\_reduction\_factor\{1e-\/12\};}
\DoxyCodeLine{RealValueType\ inner\_reduction\_factor\{1e-\/2\};}
\DoxyCodeLine{\textcolor{keyword}{auto}\ solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ ir::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_solver(cg::build().with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(inner\_reduction\_factor)))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::Iteration::build().with\_max\_iters(max\_iters),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(outer\_reduction\_factor))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec);}

\end{DoxyCode}


Create solver


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =\ solver\_gen-\/>generate(A);}
\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{\doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>add\_logger(logger);}

\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{\textcolor{keyword}{auto}\ toc\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{time\ +=\ std::chrono::duration\_cast<std::chrono::nanoseconds>(toc\ -\/\ tic);}

\end{DoxyCode}


Get residual


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ res\ =\ \doxymbox{\hyperlink{namespacegko_ae3e355a655feea026a7c46814d37a9c9}{gko::as<real\_vec>}}(logger-\/>get\_residual\_norm());}
\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}{"{}IR\ iteration\ count:\ \ \ \ \ "{}}\ <<\ logger-\/>get\_num\_iterations()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}IR\ 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{4.23821e-\/11}
\DoxyCodeLine{IR\ iteration\ count:\ \ \ \ \ 24}
\DoxyCodeLine{IR\ execution\ time\ [ms]:\ 0.794962}

\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\ }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\ }mtx\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<ValueType,\ IndexType>}};}
\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{}
\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}}(0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1OmpExecutor_af977cb680e0427ca4eb71eb7b1266211}{gko::OmpExecutor::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\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_ae198b3130d8966418047baa8dabe466c}{gko::matrix::Dense<ValueType>::create}}(exec-\/>get\_master(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \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\ =\ \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);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ b-\/>copy\_from(host\_x);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ max\_iters\ =\ 10000u;}
\DoxyCodeLine{\ \ \ \ RealValueType\ outer\_reduction\_factor\{1e-\/12\};}
\DoxyCodeLine{\ \ \ \ RealValueType\ inner\_reduction\_factor\{1e-\/2\};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ ir::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_solver(cg::build().with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(inner\_reduction\_factor)))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::Iteration::build().with\_max\_iters(max\_iters),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(outer\_reduction\_factor))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =\ solver\_gen-\/>generate(A);}
\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{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>add\_logger(logger);}
\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{\ \ \ \ \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{namespacegko_ae3e355a655feea026a7c46814d37a9c9}{gko::as<real\_vec>}}(logger-\/>get\_residual\_norm());}
\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}{"{}IR\ iteration\ count:\ \ \ \ \ "{}}\ <<\ logger-\/>get\_num\_iterations()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}IR\ execution\ time\ [ms]:\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(time.count())\ /\ 1000000.0\ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 