\chapter{The three-\/pt-\/stencil-\/solver program }
\hypertarget{three_pt_stencil_solver}{}\label{three_pt_stencil_solver}\index{The three-\/pt-\/stencil-\/solver program@{The three-\/pt-\/stencil-\/solver program}}
The 3-\/point stencil example.

This example depends on simple-\/solver, poisson-\/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}

This example solves a 1D Poisson equation:\+

$    u : [0, 1] \rightarrow R\\
    u\textnormal{\textquotesingle}\textnormal{\textquotesingle} = f\\
    u(0) = u0\\
    u(1) = u1
$

using a finite difference method on an equidistant grid with {\ttfamily K} discretization points ({\ttfamily K} can be controlled with a command line parameter). The discretization is done via the second order Taylor polynomial:\+

$u(x + h) = u(x) - u\textnormal{\textquotesingle}(x)h + 1/2 u\textnormal{\textquotesingle}\textnormal{\textquotesingle}(x)h^2 + O(h^3)\\
u(x - h) = u(x) + u\textnormal{\textquotesingle}(x)h + 1/2 u\textnormal{\textquotesingle}\textnormal{\textquotesingle}(x)h^2 + O(h^3)  / +\\
---------------------- \\
-u(x - h) + 2u(x) + -u(x + h) = -f(x)h^2 + O(h^3)
$

For an equidistant grid with K "{}inner"{} discretization points $x1, ..., xk, $and step size $ h = 1 / (K + 1)$, the formula produces a system of linear equations

$           2u_1 - u_2     = -f_1 h^2 + u0\\
-u_(k-1) + 2u_k - u_(k+1) = -f_k h^2,       k = 2, ..., K - 1\\
-u_(K-1) + 2u_K           = -f_K h^2 + u1\\
$

which is then solved using Ginkgo\textquotesingle{}s implementation of the CG method preconditioned with block-\/\+Jacobi. It is also possible to specify on which executor Ginkgo will solve the system via the command line. The function $`f` $is set to $`f(x) = 6x`$ (making the solution $`u(x) = x^3`$), but that can be changed in the {\ttfamily main} function.

The intention of the example is to show how Ginkgo can be integrated into existing software -\/ the {\ttfamily generate\+\_\+stencil\+\_\+matrix}, {\ttfamily generate\+\_\+rhs}, {\ttfamily print\+\_\+solution}, {\ttfamily compute\+\_\+error} and {\ttfamily main} function do not reference Ginkgo at all (i.\+e. they could have been there before the application developer decided to use Ginkgo, and the only part where Ginkgo is introduced is inside the {\ttfamily solve\+\_\+system} function.

\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{The\ function\ \`{}f\`{}\ is\ set\ to\ \`{}f(x)\ =\ 6x\`{}\ (making\ the\ solution\ \`{}u(x)\ =\ x\string^3\`{}),\ but}
\DoxyCodeLine{that\ can\ be\ changed\ in\ the\ \`{}main\`{}\ function.}
\DoxyCodeLine{}
\DoxyCodeLine{The\ intention\ of\ the\ example\ is\ to\ show\ how\ Ginkgo\ can\ be\ integrated\ into}
\DoxyCodeLine{existing\ software\ -\/\ the\ \`{}generate\_stencil\_matrix\`{},\ \`{}generate\_rhs\`{},}
\DoxyCodeLine{\`{}print\_solution\`{},\ \`{}compute\_error\`{}\ and\ \`{}main\`{}\ function\ \textcolor{keywordflow}{do}\ not\ reference\ Ginkgo\ at}
\DoxyCodeLine{all\ (i.e.\ they\ could\ have\ been\ there\ before\ the\ application\ developer\ decided\ to}
\DoxyCodeLine{use\ Ginkgo,\ and\ the\ only\ part\ where\ Ginkgo\ is\ introduced\ is\ inside\ the}
\DoxyCodeLine{\`{}solve\_system\`{}\ function.}
\DoxyCodeLine{*****************************<DESCRIPTION>**********************************\ /}
\DoxyCodeLine{}
\DoxyCodeLine{\#include\ <iostream>}
\DoxyCodeLine{\#include\ <map>}
\DoxyCodeLine{\#include\ <string>}
\DoxyCodeLine{\#include\ <vector>}
\DoxyCodeLine{}
\DoxyCodeLine{\#include\ <ginkgo/ginkgo.hpp>}

\end{DoxyCode}


Creates a stencil matrix in CSR format for the given number of discretization points.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_stencil\_matrix(IndexType\ discretization\_points,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType*\ row\_ptrs,\ IndexType*\ col\_idxs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ values)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ IndexType\ pos\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ coefs[]\ =\ \{-\/1,\ 2,\ -\/1\};}
\DoxyCodeLine{\ \ \ \ row\_ptrs[0]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\ ofs\ :\ \{-\/1,\ 0,\ 1\})\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (0\ <=\ i\ +\ ofs\ \&\&\ i\ +\ ofs\ <\ discretization\_points)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values[pos]\ =\ coefs[ofs\ +\ 1];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs[pos]\ =\ i\ +\ ofs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++pos;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ row\_ptrs[i\ +\ 1]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\}}

\end{DoxyCode}


Generates the RHS vector given {\ttfamily f} and the boundary conditions.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_rhs(IndexType\ discretization\_points,\ Closure\ f,\ ValueType\ u0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType\ u1,\ ValueType*\ rhs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType\ xi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[i]\ =\ -\/f(xi)\ *\ h\ *\ h;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[0]\ +=\ u0;}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[discretization\_points\ -\/\ 1]\ +=\ u1;}
\DoxyCodeLine{\}}

\end{DoxyCode}


Prints the solution {\ttfamily u}.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_solution(IndexType\ discretization\_points,\ ValueType\ u0,\ ValueType\ u1,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType*\ u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ u0\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ u[i]\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ u1\ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCode}


Computes the 1-\/norm of the error given the computed {\ttfamily u} and the correct solution function {\ttfamily correct\+\_\+u}.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ calculate\_error(IndexType\ discretization\_points,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType*\ u,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Closure\ correct\_u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ error\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }std::abs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType\ xi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ error\ +=\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(u[i]\ -\/\ correct\_u(xi))\ /\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(correct\_u(xi));}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ error;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ solve\_system(\textcolor{keyword}{const}\ std::string\&\ executor\_string,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType\ discretization\_points,\ IndexType*\ row\_ptrs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType*\ col\_idxs,\ ValueType*\ values,\ ValueType*\ rhs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ u,\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor)}
\DoxyCodeLine{\{}

\end{DoxyCode}


Some shortcuts


\begin{DoxyCode}{0}
\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\ }cg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Cg}{gko::solver::Cg<ValueType>}};}
\DoxyCodeLine{\textcolor{keyword}{using\ }bj\ =\ \doxymbox{\hyperlink{classgko_1_1preconditioner_1_1Jacobi}{gko::preconditioner::Jacobi<ValueType,\ IndexType>}};}
\DoxyCodeLine{\textcolor{keyword}{using\ }val\_array\ =\ \doxymbox{\hyperlink{classgko_1_1array}{gko::array<ValueType>}};}
\DoxyCodeLine{\textcolor{keyword}{using\ }idx\_array\ =\ \doxymbox{\hyperlink{classgko_1_1array}{gko::array<IndexType>}};}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ dp\ =\ discretization\_points;}

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


executor where the application initialized the data


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ app\_exec\ =\ exec-\/>get\_master();}

\end{DoxyCode}


Tell Ginkgo to use the data in our application

Matrix:\+ we have to set the executor of the matrix to the one where we want Sp\+MVs to run (in this case {\ttfamily exec}). When creating array views, we have to specify the executor where the data is (in this case {\ttfamily app\+\_\+exec}).

If the two do not match, Ginkgo will automatically create a copy of the data on {\ttfamily exec} (however, it will not copy the data back once it is done
\begin{DoxyItemize}
\item here this is not important since we are not modifying the matrix).
\end{DoxyItemize}


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ matrix\ =\ mtx::create(exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ 3\ *\ dp\ -\/\ 2,\ values),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ idx\_array::view(app\_exec,\ 3\ *\ dp\ -\/\ 2,\ col\_idxs),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ idx\_array::view(app\_exec,\ dp\ +\ 1,\ row\_ptrs));}

\end{DoxyCode}


RHS:\+ similar to matrix


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ b\ =\ vec::create(exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp,\ rhs),\ 1);}

\end{DoxyCode}


Solution:\+ we have to be careful here -\/ if the executors are different, once we compute the solution the array will not be automatically copied back to the original memory locations. Fortunately, whenever {\ttfamily apply} is called on a linear operator (e.\+g. matrix, solver) the arguments automatically get copied to the executor where the operator is, and copied back once the operation is completed. Thus, in this case, we can just define the solution on {\ttfamily app\+\_\+exec}, and it will be automatically transferred to/\+from {\ttfamily exec} if needed.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ x\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp,\ u),\ 1);}

\end{DoxyCode}


Generate solver


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}(dp)),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(reduction\_factor))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_preconditioner(bj::build())}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =\ solver\_gen-\/>generate(\doxymbox{\hyperlink{namespacegko_abc3b2c363d6eb3d9837a6d9efa589844}{gko::give}}(matrix));}

\end{DoxyCode}


Solve system


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>apply(b,\ x);}
\DoxyCodeLine{\}}
\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\ }IndexType\ =\ int;}

\end{DoxyCode}


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{keywordflow}{if}\ (argc\ ==\ 2\ \&\&\ std::string(argv[1])\ ==\ \textcolor{stringliteral}{"{}-\/-\/help"{}})\ \{}
\DoxyCodeLine{\ \ \ \ std::cerr\ <<\ \textcolor{stringliteral}{"{}Usage:\ "{}}\ <<\ argv[0]}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{stringliteral}{"{}\ [executor]\ [DISCRETIZATION\_POINTS]"{}}\ <<\ 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{\textcolor{keyword}{const}\ IndexType\ discretization\_points\ =}
\DoxyCodeLine{\ \ \ \ argc\ >=\ 3\ ?\ std::atoi(argv[2])\ :\ 100;}

\end{DoxyCode}


problem:\+


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ correct\_u\ =\ [](ValueType\ x)\ \{\ \textcolor{keywordflow}{return}\ x\ *\ x\ *\ x;\ \};}
\DoxyCodeLine{\textcolor{keyword}{auto}\ f\ =\ [](ValueType\ x)\ \{\ \textcolor{keywordflow}{return}\ ValueType(6)\ *\ x;\ \};}
\DoxyCodeLine{\textcolor{keyword}{auto}\ u0\ =\ correct\_u(0);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ u1\ =\ correct\_u(1);}

\end{DoxyCode}


matrix


\begin{DoxyCode}{0}
\DoxyCodeLine{std::vector<IndexType>\ row\_ptrs(discretization\_points\ +\ 1);}
\DoxyCodeLine{std::vector<IndexType>\ col\_idxs(3\ *\ discretization\_points\ -\/\ 2);}
\DoxyCodeLine{std::vector<ValueType>\ values(3\ *\ discretization\_points\ -\/\ 2);}

\end{DoxyCode}


right hand side


\begin{DoxyCode}{0}
\DoxyCodeLine{std::vector<ValueType>\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}(discretization\_points);}

\end{DoxyCode}


solution


\begin{DoxyCode}{0}
\DoxyCodeLine{std::vector<ValueType>\ u(discretization\_points,\ 0.0);}
\DoxyCodeLine{\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor\ =\ 1e-\/7;}
\DoxyCodeLine{}
\DoxyCodeLine{generate\_stencil\_matrix(discretization\_points,\ row\_ptrs.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs.data(),\ values.data());}

\end{DoxyCode}


looking for solution u = x\texorpdfstring{$^\wedge$}{\string^}3:\+ f = 6x, u(0) = 0, u(1) = 1


\begin{DoxyCode}{0}
\DoxyCodeLine{generate\_rhs(discretization\_points,\ f,\ u0,\ u1,\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data());}
\DoxyCodeLine{}
\DoxyCodeLine{solve\_system(executor\_string,\ discretization\_points,\ row\_ptrs.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs.data(),\ values.data(),\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data(),\ u.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ reduction\_factor);}

\end{DoxyCode}


Uncomment to print the solution print\+\_\+solution$<$\+Value\+Type, Index\+Type$>$(discretization\+\_\+points, 0, 1, u.\+data());


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}The\ average\ relative\ error\ is\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ calculate\_error(discretization\_points,\ u.data(),\ correct\_u)\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ discretization\_points}
\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{The\ average\ relative\ error\ is\ 2.52236e-\/11}

\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{comment}{/*****************************<DESCRIPTION>***********************************}}
\DoxyCodeLine{\textcolor{comment}{This\ example\ solves\ a\ 1D\ Poisson\ equation:}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u\ :\ [0,\ 1]\ -\/>\ R}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u''\ =\ f}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u(0)\ =\ u0}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u(1)\ =\ u1}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{using\ a\ finite\ difference\ method\ on\ an\ equidistant\ grid\ with\ \`{}K`\ discretization}}
\DoxyCodeLine{\textcolor{comment}{points\ (`K`\ can\ be\ controlled\ with\ a\ command\ line\ parameter).\ The\ discretization}}
\DoxyCodeLine{\textcolor{comment}{is\ done\ via\ the\ second\ order\ Taylor\ polynomial:}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{u(x\ +\ h)\ =\ u(x)\ -\/\ u'(x)h\ +\ 1/2\ u''(x)h\string^2\ +\ O(h\string^3)}}
\DoxyCodeLine{\textcolor{comment}{u(x\ -\/\ h)\ =\ u(x)\ +\ u'(x)h\ +\ 1/2\ u''(x)h\string^2\ +\ O(h\string^3)\ \ /\ +}}
\DoxyCodeLine{\textcolor{comment}{-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/}}
\DoxyCodeLine{\textcolor{comment}{-\/u(x\ -\/\ h)\ +\ 2u(x)\ +\ -\/u(x\ +\ h)\ =\ -\/f(x)h\string^2\ +\ O(h\string^3)}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{For\ an\ equidistant\ grid\ with\ K\ "{}inner"{}\ discretization\ points\ x1,\ ...,\ xk,\ and}}
\DoxyCodeLine{\textcolor{comment}{step\ size\ h\ =\ 1\ /\ (K\ +\ 1),\ the\ formula\ produces\ a\ system\ of\ linear\ equations}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ 2u\_1\ -\/\ u\_2\ \ \ \ \ =\ -\/f\_1\ h\string^2\ +\ u0}}
\DoxyCodeLine{\textcolor{comment}{-\/u\_(k-\/1)\ +\ 2u\_k\ -\/\ u\_(k+1)\ =\ -\/f\_k\ h\string^2,\ \ \ \ \ \ \ k\ =\ 2,\ ...,\ K\ -\/\ 1}}
\DoxyCodeLine{\textcolor{comment}{-\/u\_(K-\/1)\ +\ 2u\_K\ \ \ \ \ \ \ \ \ \ \ =\ -\/f\_K\ h\string^2\ +\ u1}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{which\ is\ then\ solved\ using\ Ginkgo's\ implementation\ of\ the\ CG\ method}}
\DoxyCodeLine{\textcolor{comment}{preconditioned\ with\ block-\/Jacobi.\ It\ is\ also\ possible\ to\ specify\ on\ which}}
\DoxyCodeLine{\textcolor{comment}{executor\ Ginkgo\ will\ solve\ the\ system\ via\ the\ command\ line.}}
\DoxyCodeLine{\textcolor{comment}{The\ function\ \`{}f`\ is\ set\ to\ \`{}f(x)\ =\ 6x`\ (making\ the\ solution\ \`{}u(x)\ =\ x\string^3`),\ but}}
\DoxyCodeLine{\textcolor{comment}{that\ can\ be\ changed\ in\ the\ \`{}main`\ function.}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{The\ intention\ of\ the\ example\ is\ to\ show\ how\ Ginkgo\ can\ be\ integrated\ into}}
\DoxyCodeLine{\textcolor{comment}{existing\ software\ -\/\ the\ \`{}generate\_stencil\_matrix`,\ \`{}generate\_rhs`,}}
\DoxyCodeLine{\textcolor{comment}{\`{}print\_solution`,\ \`{}compute\_error`\ and\ \`{}main`\ function\ do\ not\ reference\ Ginkgo\ at}}
\DoxyCodeLine{\textcolor{comment}{all\ (i.e.\ they\ could\ have\ been\ there\ before\ the\ application\ developer\ decided\ to}}
\DoxyCodeLine{\textcolor{comment}{use\ Ginkgo,\ and\ the\ only\ part\ where\ Ginkgo\ is\ introduced\ is\ inside\ the}}
\DoxyCodeLine{\textcolor{comment}{\`{}solve\_system`\ function.}}
\DoxyCodeLine{\textcolor{comment}{*****************************<DESCRIPTION>**********************************/}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <iostream>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <map>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <string>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <vector>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <ginkgo/ginkgo.hpp>}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_stencil\_matrix(IndexType\ discretization\_points,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType*\ row\_ptrs,\ IndexType*\ col\_idxs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ values)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ IndexType\ pos\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ coefs[]\ =\ \{-\/1,\ 2,\ -\/1\};}
\DoxyCodeLine{\ \ \ \ row\_ptrs[0]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\ ofs\ :\ \{-\/1,\ 0,\ 1\})\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (0\ <=\ i\ +\ ofs\ \&\&\ i\ +\ ofs\ <\ discretization\_points)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values[pos]\ =\ coefs[ofs\ +\ 1];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs[pos]\ =\ i\ +\ ofs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++pos;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ row\_ptrs[i\ +\ 1]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_rhs(IndexType\ discretization\_points,\ Closure\ f,\ ValueType\ u0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType\ u1,\ ValueType*\ rhs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType\ xi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[i]\ =\ -\/f(xi)\ *\ h\ *\ h;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[0]\ +=\ u0;}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[discretization\_points\ -\/\ 1]\ +=\ u1;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_solution(IndexType\ discretization\_points,\ ValueType\ u0,\ ValueType\ u1,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType*\ u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ u0\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ u[i]\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ u1\ <<\ std::endl;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ calculate\_error(IndexType\ discretization\_points,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType*\ u,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Closure\ correct\_u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ error\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }std::abs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ ValueType\ xi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ error\ +=\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(u[i]\ -\/\ correct\_u(xi))\ /\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(correct\_u(xi));}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ error;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ solve\_system(\textcolor{keyword}{const}\ std::string\&\ executor\_string,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType\ discretization\_points,\ IndexType*\ row\_ptrs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType*\ col\_idxs,\ ValueType*\ values,\ ValueType*\ rhs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ u,\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor)}
\DoxyCodeLine{\{}
\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\ }cg\ =\ \doxymbox{\hyperlink{classgko_1_1solver_1_1Cg}{gko::solver::Cg<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }bj\ =\ \doxymbox{\hyperlink{classgko_1_1preconditioner_1_1Jacobi}{gko::preconditioner::Jacobi<ValueType,\ IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }val\_array\ =\ \doxymbox{\hyperlink{classgko_1_1array}{gko::array<ValueType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }idx\_array\ =\ \doxymbox{\hyperlink{classgko_1_1array}{gko::array<IndexType>}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\&\ dp\ =\ discretization\_points;}
\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{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ app\_exec\ =\ exec-\/>get\_master();}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ matrix\ =\ mtx::create(exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ 3\ *\ dp\ -\/\ 2,\ values),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ idx\_array::view(app\_exec,\ 3\ *\ dp\ -\/\ 2,\ col\_idxs),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ idx\_array::view(app\_exec,\ dp\ +\ 1,\ row\_ptrs));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ b\ =\ vec::create(exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp,\ rhs),\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ x\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp,\ u),\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}(dp)),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .with\_reduction\_factor(reduction\_factor))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_preconditioner(bj::build())}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .on(exec);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}\ =\ solver\_gen-\/>generate(\doxymbox{\hyperlink{namespacegko_abc3b2c363d6eb3d9837a6d9efa589844}{gko::give}}(matrix));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1log_a15f71654c6c4f4defc95f9359343fa27a7cbf673200498a0d1324f4330825202a}{solver}}-\/>apply(b,\ x);}
\DoxyCodeLine{\}}
\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\ }IndexType\ =\ int;}
\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]}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{stringliteral}{"{}\ [executor]\ [DISCRETIZATION\_POINTS]"{}}\ <<\ 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{\ \ \ \ \textcolor{keyword}{const}\ IndexType\ discretization\_points\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ argc\ >=\ 3\ ?\ std::atoi(argv[2])\ :\ 100;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ correct\_u\ =\ [](ValueType\ x)\ \{\ \textcolor{keywordflow}{return}\ x\ *\ x\ *\ x;\ \};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ f\ =\ [](ValueType\ x)\ \{\ \textcolor{keywordflow}{return}\ ValueType(6)\ *\ x;\ \};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ u0\ =\ correct\_u(0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ u1\ =\ correct\_u(1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::vector<IndexType>\ row\_ptrs(discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ std::vector<IndexType>\ col\_idxs(3\ *\ discretization\_points\ -\/\ 2);}
\DoxyCodeLine{\ \ \ \ std::vector<ValueType>\ values(3\ *\ discretization\_points\ -\/\ 2);}
\DoxyCodeLine{\ \ \ \ std::vector<ValueType>\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}(discretization\_points);}
\DoxyCodeLine{\ \ \ \ std::vector<ValueType>\ u(discretization\_points,\ 0.0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor\ =\ 1e-\/7;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ generate\_stencil\_matrix(discretization\_points,\ row\_ptrs.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs.data(),\ values.data());}
\DoxyCodeLine{\ \ \ \ generate\_rhs(discretization\_points,\ f,\ u0,\ u1,\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data());}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ solve\_system(executor\_string,\ discretization\_points,\ row\_ptrs.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs.data(),\ values.data(),\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data(),\ u.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ reduction\_factor);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}The\ average\ relative\ error\ is\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ calculate\_error(discretization\_points,\ u.data(),\ correct\_u)\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ discretization\_points}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 