\chapter{The custom-\/stopping-\/criterion program }
\hypertarget{custom_stopping_criterion}{}\label{custom_stopping_criterion}\index{The custom-\/stopping-\/criterion program@{The custom-\/stopping-\/criterion program}}
The custom stopping criterion creation example.

This example depends on simple-\/solver, minimal-\/cuda-\/solver.

 \label{three_pt_stencil_solver_Intro}%
\Hypertarget{three_pt_stencil_solver_Intro}%
 \label{three_pt_stencil_solver_Introduction}%
\Hypertarget{three_pt_stencil_solver_Introduction}%
\doxysection*{Introduction}

\label{three_pt_stencil_solver_Abouttheexample}%
\Hypertarget{three_pt_stencil_solver_Abouttheexample}%
\doxysubsubsection*{About the example }

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


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ \};}
\DoxyCodeLine{\ \ \ \ GKO\_ENABLE\_CRITERION\_FACTORY(ByInteraction,\ parameters,\ Factory);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{group__LinOp_ga521f65604cc4cf427dcb2ecfa49b757c}{GKO\_ENABLE\_BUILD\_METHOD}}(Factory);}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{protected}:}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordtype}{bool}\ check\_impl(\doxymbox{\hyperlink{namespacegko_a06d96f1ecdca6b86399173969771c3fb}{gko::uint8}}\ stoppingId,\ \textcolor{keywordtype}{bool}\ setFinalized,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1array}{gko::array<gko::stopping\_status>}}*\ stop\_status,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}*\ one\_changed,\ \textcolor{keyword}{const}\ Criterion::Updater\&)\textcolor{keyword}{\ override}}
\DoxyCodeLine{\textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ result\ =\ *(parameters\_.stop\_iteration\_process);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (result)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ this-\/>set\_all\_statuses(stoppingId,\ setFinalized,\ stop\_status);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ *one\_changed\ =\ \textcolor{keyword}{true};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ result;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{explicit}\ ByInteraction(std::shared\_ptr<const\ gko::Executor>\ exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ :\ EnablePolymorphicObject<ByInteraction,\ Criterion>(\doxymbox{\hyperlink{namespacestd}{std}}::move(exec))}
\DoxyCodeLine{\ \ \ \ \{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{explicit}\ ByInteraction(\textcolor{keyword}{const}\ Factory*\ factory,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{structgko_1_1stop_1_1CriterionArgs}{gko::stop::CriterionArgs}}\&\ args)}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ :\ EnablePolymorphicObject<ByInteraction,\ Criterion>(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a9549dd6065d019211460c59a86dd6536}{factory}}-\/>get\_executor()),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ parameters\_\{\doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a9549dd6065d019211460c59a86dd6536}{factory}}-\/>get\_parameters()\}}
\DoxyCodeLine{\ \ \ \ \{\}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ run\_solver(\textcolor{keyword}{volatile}\ \textcolor{keywordtype}{bool}*\ stop\_iteration\_process,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ std::shared\_ptr<gko::Executor>\ exec)}
\DoxyCodeLine{\{}

\end{DoxyCode}


Some shortcuts


\begin{DoxyCode}{0}
\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{}
\DoxyCodeLine{\textcolor{keyword}{using\ }mtx\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<ValueType,\ IndexType>}};}
\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\ }bicg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Bicgstab}{gko::solver::Bicgstab<ValueType>}};}

\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));}
\DoxyCodeLine{\textcolor{keyword}{auto}\ b\ =\ \doxymbox{\hyperlink{namespacegko_abc58509ecdcb48399e12f9a081ed7545}{gko::read<vec>}}(std::ifstream(\textcolor{stringliteral}{"{}data/b.mtx"{}}),\ exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ x\ =\ \doxymbox{\hyperlink{namespacegko_abc58509ecdcb48399e12f9a081ed7545}{gko::read<vec>}}(std::ifstream(\textcolor{stringliteral}{"{}data/x0.mtx"{}}),\ exec);}

\end{DoxyCode}


Create solver factory and solve system


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =}
\DoxyCodeLine{\ \ \ \ bicg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(ByInteraction::build().with\_stop\_iteration\_process(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ stop\_iteration\_process))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ -\/>generate(A);}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>add\_logger(\doxymbox{\hyperlink{classgko_1_1log_1_1Stream_a70d1f2a01372287ae1467fbed6965ea3}{gko::log::Stream<ValueType>::create}}(}
\DoxyCodeLine{\ \ \ \ gko::log::Logger::iteration\_complete\_mask,\ std::cout,\ \textcolor{keyword}{true}));}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>apply(b,\ x);}
\DoxyCodeLine{}
\DoxyCodeLine{std::cout\ <<\ \textcolor{stringliteral}{"{}Solver\ stopped"{}}\ <<\ std::endl;}

\end{DoxyCode}


Print solution


\begin{DoxyCode}{0}
\DoxyCodeLine{std::cout\ <<\ \textcolor{stringliteral}{"{}Solution\ (x):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ x);}

\end{DoxyCode}


Calculate residual


\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}\ res\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<real\_vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{\ \ \ \ b-\/>compute\_norm2(res);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Residual\ norm\ sqrt(r\string^T\ r):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ res);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}*\ argv[])}
\DoxyCodeLine{\{}

\end{DoxyCode}


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

\end{DoxyCode}


Figure out where to run the code


\begin{DoxyCode}{0}
\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}}(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}


Declare a user controlled boolean for the iteration process


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{volatile}\ \textcolor{keywordtype}{bool}\ stop\_iteration\_process\{\};}

\end{DoxyCode}


Create a new a thread to launch the solver


\begin{DoxyCode}{0}
\DoxyCodeLine{std::thread\ t(run\_solver,\ \&stop\_iteration\_process,\ exec);}

\end{DoxyCode}


Look for an input command "{}stop"{} in the console, which sets the boolean to true


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Type\ 'stop'\ to\ stop\ the\ iteration\ process"{}}\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::string\ command;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{while}\ (std::cin\ >>\ command)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (command\ ==\ \textcolor{stringliteral}{"{}stop"{}})\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Unknown\ command"{}}\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}User\ input\ command\ 'stop'\ -\/\ The\ solver\ will\ stop!"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ stop\_iteration\_process\ =\ \textcolor{keyword}{true};}
\DoxyCodeLine{\ \ \ \ t.join();}
\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{.}
\DoxyCodeLine{.}
\DoxyCodeLine{.}
\DoxyCodeLine{.}
\DoxyCodeLine{.}
\DoxyCodeLine{.}
\DoxyCodeLine{[LOG]\ >>>\ iteration\ 22516\ completed\ with\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ LinOp[\doxymbox{\hyperlink{classgko_1_1solver_1_1Bicgstab}{gko::solver::Bicgstab<double>}},0x7fe6a4003710]\ with\ residual\ LinOp[\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<double>}},0x7fe6a40050b0],\ solution\ LinOp[\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<double>}},0x7fe6a40048e0]\ and\ residual\_norm\ LinOp[\doxymbox{\hyperlink{classgko_1_1LinOp}{gko::LinOp}}\ \textcolor{keyword}{const}*,0]}
\DoxyCodeLine{LinOp[\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<double>}},0x7fe6a40050b0][}
\DoxyCodeLine{\ \ \ \ 5.17803e-\/164}
\DoxyCodeLine{\ \ \ \ -\/7.6865e-\/165}
\DoxyCodeLine{\ \ \ \ -\/2.06149e-\/164}
\DoxyCodeLine{\ \ \ \ -\/4.84737e-\/165}
\DoxyCodeLine{\ \ \ \ -\/3.36597e-\/164}
\DoxyCodeLine{\ \ \ \ 2.22353e-\/164}
\DoxyCodeLine{\ \ \ \ 1.47594e-\/165}
\DoxyCodeLine{\ \ \ \ -\/1.78592e-\/165}
\DoxyCodeLine{\ \ \ \ -\/6.17274e-\/166}
\DoxyCodeLine{\ \ \ \ -\/3.02681e-\/166}
\DoxyCodeLine{\ \ \ \ 7.82009e-\/166}
\DoxyCodeLine{\ \ \ \ 8.57102e-\/165}
\DoxyCodeLine{\ \ \ \ -\/1.28879e-\/164}
\DoxyCodeLine{\ \ \ \ -\/2.62076e-\/165}
\DoxyCodeLine{\ \ \ \ 2.55329e-\/165}
\DoxyCodeLine{\ \ \ \ -\/5.95988e-\/166}
\DoxyCodeLine{\ \ \ \ -\/5.79273e-\/166}
\DoxyCodeLine{\ \ \ \ -\/5.20172e-\/166}
\DoxyCodeLine{\ \ \ \ -\/6.79458e-\/166}
\DoxyCodeLine{]}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{//\ Typing\ 'stop'\ stops\ the\ solver.}}
\DoxyCodeLine{User\ input\ command\ \textcolor{stringliteral}{'stop'}\ -\/\ The\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ will\ stop}
\DoxyCodeLine{}
\DoxyCodeLine{LinOp[\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<double>}},0x7fe6a40048e0][}
\DoxyCodeLine{\ \ \ \ 0.252218}
\DoxyCodeLine{\ \ \ \ 0.108645}
\DoxyCodeLine{\ \ \ \ 0.0662811}
\DoxyCodeLine{\ \ \ \ 0.0630433}
\DoxyCodeLine{\ \ \ \ 0.0384088}
\DoxyCodeLine{\ \ \ \ 0.0396536}
\DoxyCodeLine{\ \ \ \ 0.0402648}
\DoxyCodeLine{\ \ \ \ 0.0338935}
\DoxyCodeLine{\ \ \ \ 0.0193098}
\DoxyCodeLine{\ \ \ \ 0.0234653}
\DoxyCodeLine{\ \ \ \ 0.0211499}
\DoxyCodeLine{\ \ \ \ 0.0196413}
\DoxyCodeLine{\ \ \ \ 0.0199151}
\DoxyCodeLine{\ \ \ \ 0.0181674}
\DoxyCodeLine{\ \ \ \ 0.0162722}
\DoxyCodeLine{\ \ \ \ 0.0150714}
\DoxyCodeLine{\ \ \ \ 0.0107016}
\DoxyCodeLine{\ \ \ \ 0.0121141}
\DoxyCodeLine{\ \ \ \ 0.0123025}
\DoxyCodeLine{]}
\DoxyCodeLine{}
\DoxyCodeLine{Solver\ stopped}
\DoxyCodeLine{Solution\ (x):}
\DoxyCodeLine{\%\%MatrixMarket\ matrix\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}\ \doxymbox{\hyperlink{namespacegko_af21648097306adbabc6234b4609ce297}{real}}\ general}
\DoxyCodeLine{19\ 1}
\DoxyCodeLine{0.252218}
\DoxyCodeLine{0.108645}
\DoxyCodeLine{0.0662811}
\DoxyCodeLine{0.0630433}
\DoxyCodeLine{0.0384088}
\DoxyCodeLine{0.0396536}
\DoxyCodeLine{0.0402648}
\DoxyCodeLine{0.0338935}
\DoxyCodeLine{0.0193098}
\DoxyCodeLine{0.0234653}
\DoxyCodeLine{0.0211499}
\DoxyCodeLine{0.0196413}
\DoxyCodeLine{0.0199151}
\DoxyCodeLine{0.0181674}
\DoxyCodeLine{0.0162722}
\DoxyCodeLine{0.0150714}
\DoxyCodeLine{0.0107016}
\DoxyCodeLine{0.0121141}
\DoxyCodeLine{0.0123025}
\DoxyCodeLine{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{6.50306e-\/16}

\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\ <iostream>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <map>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <string>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <thread>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <ginkgo/ginkgo.hpp>}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{class\ }ByInteraction}
\DoxyCodeLine{\ \ \ \ :\ \textcolor{keyword}{public}\ \doxymbox{\hyperlink{classgko_1_1EnablePolymorphicObject}{gko::EnablePolymorphicObject}}<ByInteraction,\ gko::stop::Criterion>\ \{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{friend}\ \textcolor{keyword}{class\ }gko::EnablePolymorphicObject<ByInteraction,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::Criterion>;}
\DoxyCodeLine{\ \ \ \ using\ Criterion\ =\ gko::stop::Criterion;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{public}:}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{group__LinOp_ga1fc8e9d8be0c9ad2d72bc1ddfc6d8358}{GKO\_CREATE\_FACTORY\_PARAMETERS}}(parameters,\ Factory)}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::add\_pointer<volatile\ bool>::type\ \doxymbox{\hyperlink{group__LinOp_ga49d4c09c39f06b22bebbbfd1d8bfe450}{GKO\_FACTORY\_PARAMETER\_SCALAR}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ stop\_iteration\_process,\ \textcolor{keyword}{nullptr});}
\DoxyCodeLine{\ \ \ \ \};}
\DoxyCodeLine{\ \ \ \ GKO\_ENABLE\_CRITERION\_FACTORY(ByInteraction,\ parameters,\ Factory);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{group__LinOp_ga521f65604cc4cf427dcb2ecfa49b757c}{GKO\_ENABLE\_BUILD\_METHOD}}(Factory);}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{protected}:}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordtype}{bool}\ check\_impl(\doxymbox{\hyperlink{namespacegko_a06d96f1ecdca6b86399173969771c3fb}{gko::uint8}}\ stoppingId,\ \textcolor{keywordtype}{bool}\ setFinalized,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::array<gko::stopping\_status>*\ stop\_status,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}*\ one\_changed,\ \textcolor{keyword}{const}\ Criterion::Updater\&)\textcolor{keyword}{\ override}}
\DoxyCodeLine{\textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordtype}{bool}\ result\ =\ *(parameters\_.stop\_iteration\_process);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (result)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ this-\/>set\_all\_statuses(stoppingId,\ setFinalized,\ stop\_status);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ *one\_changed\ =\ \textcolor{keyword}{true};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ result;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{explicit}\ ByInteraction(std::shared\_ptr<const\ gko::Executor>\ exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ :\ EnablePolymorphicObject<ByInteraction,\ Criterion>(std::move(exec))}
\DoxyCodeLine{\ \ \ \ \{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{explicit}\ ByInteraction(\textcolor{keyword}{const}\ Factory*\ factory,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ gko::stop::CriterionArgs\&\ args)}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ :\ EnablePolymorphicObject<ByInteraction,\ Criterion>(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a9549dd6065d019211460c59a86dd6536}{factory}}-\/>get\_executor()),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ parameters\_\{\doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a9549dd6065d019211460c59a86dd6536}{factory}}-\/>get\_parameters()\}}
\DoxyCodeLine{\ \ \ \ \{\}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ run\_solver(\textcolor{keyword}{volatile}\ \textcolor{keywordtype}{bool}*\ stop\_iteration\_process,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ std::shared\_ptr<gko::Executor>\ exec)}
\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{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }mtx\ =\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<ValueType,\ IndexType>}};}
\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\ }bicg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Bicgstab}{gko::solver::Bicgstab<ValueType>}};}
\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{\ \ \ \ \textcolor{keyword}{auto}\ b\ =\ \doxymbox{\hyperlink{namespacegko_abc58509ecdcb48399e12f9a081ed7545}{gko::read<vec>}}(std::ifstream(\textcolor{stringliteral}{"{}data/b.mtx"{}}),\ exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ x\ =\ \doxymbox{\hyperlink{namespacegko_abc58509ecdcb48399e12f9a081ed7545}{gko::read<vec>}}(std::ifstream(\textcolor{stringliteral}{"{}data/x0.mtx"{}}),\ exec);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ bicg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(ByInteraction::build().with\_stop\_iteration\_process(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ stop\_iteration\_process))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ -\/>generate(A);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>add\_logger(\doxymbox{\hyperlink{classgko_1_1log_1_1Stream_a70d1f2a01372287ae1467fbed6965ea3}{gko::log::Stream<ValueType>::create}}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ gko::log::Logger::iteration\_complete\_mask,\ std::cout,\ \textcolor{keyword}{true}));}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>apply(b,\ x);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Solver\ stopped"{}}\ <<\ std::endl;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Solution\ (x):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ 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}\ res\ =\ \doxymbox{\hyperlink{group__LinOp_ga70851b4c3c9b01bb667b3a8e438fb6d2}{gko::initialize<real\_vec>}}(\{0.0\},\ exec);}
\DoxyCodeLine{\ \ \ \ A-\/>apply(one,\ x,\ neg\_one,\ b);}
\DoxyCodeLine{\ \ \ \ b-\/>compute\_norm2(res);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Residual\ norm\ sqrt(r\string^T\ r):\ \(\backslash\)n"{}};}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_af82d8f7795fefcf5f38a38895e316c02}{write}}(std::cout,\ res);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}*\ argv[])}
\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{}
\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}{volatile}\ \textcolor{keywordtype}{bool}\ stop\_iteration\_process\{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::thread\ t(run\_solver,\ \&stop\_iteration\_process,\ exec);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Type\ 'stop'\ to\ stop\ the\ iteration\ process"{}}\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::string\ command;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{while}\ (std::cin\ >>\ command)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (command\ ==\ \textcolor{stringliteral}{"{}stop"{}})\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{break};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}\ \textcolor{keywordflow}{else}\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Unknown\ command"{}}\ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}User\ input\ command\ 'stop'\ -\/\ The\ solver\ will\ stop!"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ stop\_iteration\_process\ =\ \textcolor{keyword}{true};}
\DoxyCodeLine{\ \ \ \ t.join();}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 