\chapter{The poisson-\/solver program }
\hypertarget{poisson_solver}{}\label{poisson_solver}\index{The poisson-\/solver program@{The poisson-\/solver program}}
The poisson solver example.

This example depends on simple-\/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 used to build an application solving a real-\/world problem, which includes a solution of a large, sparse linear system as a component.

\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{\ \ \ \ \ \ \ \ 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>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_rhs(Closure\ f,\ ValueType\ u0,\ ValueType\ u1,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}}*\ rhs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ discretization\_points\ =\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}-\/>get\_size()[0];}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ values\ =\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}-\/>get\_values();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ values[i]\ =\ -\/f(xi)\ *\ h\ *\ h;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ values[0]\ +=\ u0;}
\DoxyCodeLine{\ \ \ \ values[discretization\_points\ -\/\ 1]\ +=\ u1;}
\DoxyCodeLine{\}}

\end{DoxyCode}


Prints the solution {\ttfamily u}.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_solution(ValueType\ u0,\ ValueType\ u1,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}}*\ u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ u0\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ u-\/>get\_size()[0];\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ u-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_a78f311b47437fb90e470b7506234292a}{get\_const\_values}}()[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>}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ calculate\_error(}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordtype}{int}\ discretization\_points,\ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}}*\ u,}
\DoxyCodeLine{\ \ \ \ Closure\ correct\_u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ error\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }std::abs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ error\ +=}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(u-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_a78f311b47437fb90e470b7506234292a}{get\_const\_values}}()[i]\ -\/\ correct\_u(xi))\ /\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(correct\_u(xi));}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ error;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ main(\textcolor{keywordtype}{int}\ argc,\ \textcolor{keywordtype}{char}*\ argv[])}
\DoxyCodeLine{\{}

\end{DoxyCode}


Some shortcuts


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{using\ }ValueType\ =\ double;}
\DoxyCodeLine{\textcolor{keyword}{using\ }IndexType\ =\ int;}
\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>}};}

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

\end{DoxyCode}


Get number of discretization points


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{int}\ discretization\_points\ =}
\DoxyCodeLine{\ \ \ \ argc\ >=\ 3\ ?\ std::atoi(argv[2])\ :\ 100;}

\end{DoxyCode}


Get the executor string


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


executor used by the application


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

\end{DoxyCode}


Set up the problem:\+ define the exact solution, the right hand side and the Dirichlet boundary condition.


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


initialize matrix and vectors


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ matrix\ =\ mtx::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(discretization\_points),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 3\ *\ discretization\_points\ -\/\ 2);}
\DoxyCodeLine{generate\_stencil\_matrix(matrix.get());}
\DoxyCodeLine{\textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(discretization\_points,\ 1));}
\DoxyCodeLine{generate\_rhs(f,\ u0,\ u1,\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.get());}
\DoxyCodeLine{\textcolor{keyword}{auto}\ u\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(discretization\_points,\ 1));}
\DoxyCodeLine{\textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ u-\/>get\_size()[0];\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ u-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_aea71dbb699d506b1f10dd67404b6bc01}{get\_values}}()[i]\ =\ 0.0;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor\ =\ 1e-\/7;}

\end{DoxyCode}


Generate solver and solve the system


\begin{DoxyCode}{0}
\DoxyCodeLine{cg::build()}
\DoxyCodeLine{\ \ \ \ .with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ gko::stop::Iteration::build().with\_max\_iters(discretization\_points),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build().with\_reduction\_factor(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ reduction\_factor))}
\DoxyCodeLine{\ \ \ \ .with\_preconditioner(bj::build())}
\DoxyCodeLine{\ \ \ \ .on(exec)}
\DoxyCodeLine{\ \ \ \ -\/>generate(\doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{clone}}(exec,\ matrix))\ \ \textcolor{comment}{//\ copy\ the\ matrix\ to\ the\ executor}}
\DoxyCodeLine{\ \ \ \ -\/>apply(rhs,\ u);}

\end{DoxyCode}


Uncomment to print the solution print\+\_\+solution$<$\+Value\+Type$>$(u0, u1, u.\+get());


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Solve\ complete.\(\backslash\)nThe\ average\ relative\ error\ is\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ calculate\_error(discretization\_points,\ u.get(),\ correct\_u)\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{static\_cast<}\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\textcolor{keyword}{>}(}
\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{Solve\ complete.}
\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{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(\doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr}{gko::matrix::Csr<ValueType,\ IndexType>}}*\ matrix)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ discretization\_points\ =\ matrix-\/>get\_size()[0];}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ row\_ptrs\ =\ matrix-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr_a83f2a63175bbffa39808543a3d4b3f3b}{get\_row\_ptrs}}();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ col\_idxs\ =\ matrix-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr_ae701fb37da0c745c314fb3472cef5df4}{get\_col\_idxs}}();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ values\ =\ matrix-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Csr_ade22bdc09b0ef6d9740feeaa702fdd51}{get\_values}}();}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordtype}{int}\ pos\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ coefs[]\ =\ \{-\/1,\ 2,\ -\/1\};}
\DoxyCodeLine{\ \ \ \ row\_ptrs[0]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ 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>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_rhs(Closure\ f,\ ValueType\ u0,\ ValueType\ u1,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}}*\ rhs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ discretization\_points\ =\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}-\/>get\_size()[0];}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ values\ =\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}-\/>get\_values();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ values[i]\ =\ -\/f(xi)\ *\ h\ *\ h;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ values[0]\ +=\ u0;}
\DoxyCodeLine{\ \ \ \ values[discretization\_points\ -\/\ 1]\ +=\ u1;}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_solution(ValueType\ u0,\ ValueType\ u1,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}}*\ u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ u0\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ u-\/>get\_size()[0];\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ u-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_a78f311b47437fb90e470b7506234292a}{get\_const\_values}}()[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>}
\DoxyCodeLine{\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ calculate\_error(}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordtype}{int}\ discretization\_points,\ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense}{gko::matrix::Dense<ValueType>}}*\ u,}
\DoxyCodeLine{\ \ \ \ Closure\ correct\_u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(discretization\_points\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ error\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ discretization\_points;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }std::abs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ \textcolor{keyword}{static\_cast<}ValueType\textcolor{keyword}{>}(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ error\ +=}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(u-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_a78f311b47437fb90e470b7506234292a}{get\_const\_values}}()[i]\ -\/\ correct\_u(xi))\ /\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(correct\_u(xi));}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ error;}
\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{\ \ \ \ \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{}
\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{keywordtype}{unsigned}\ \textcolor{keywordtype}{int}\ discretization\_points\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ argc\ >=\ 3\ ?\ std::atoi(argv[2])\ :\ 100;}
\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{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ app\_exec\ =\ exec-\/>get\_master();}
\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{\ \ \ \ \textcolor{keyword}{auto}\ matrix\ =\ mtx::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(discretization\_points),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 3\ *\ discretization\_points\ -\/\ 2);}
\DoxyCodeLine{\ \ \ \ generate\_stencil\_matrix(matrix.get());}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(discretization\_points,\ 1));}
\DoxyCodeLine{\ \ \ \ generate\_rhs(f,\ u0,\ u1,\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.get());}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ u\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(discretization\_points,\ 1));}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ u-\/>get\_size()[0];\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ u-\/>\doxymbox{\hyperlink{classgko_1_1matrix_1_1Dense_aea71dbb699d506b1f10dd67404b6bc01}{get\_values}}()[i]\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor\ =\ 1e-\/7;}
\DoxyCodeLine{\ \ \ \ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_criteria(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::Iteration::build().with\_max\_iters(discretization\_points),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ gko::stop::ResidualNorm<ValueType>::build().with\_reduction\_factor(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ reduction\_factor))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .with\_preconditioner(bj::build())}
\DoxyCodeLine{\ \ \ \ \ \ \ \ .on(exec)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ -\/>generate(\doxymbox{\hyperlink{namespacegko_aa194e34282452493231ca7b01ffb5e64}{clone}}(exec,\ matrix))\ \ \textcolor{comment}{//\ copy\ the\ matrix\ to\ the\ executor}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ -\/>apply(rhs,\ u);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}Solve\ complete.\(\backslash\)nThe\ average\ relative\ error\ is\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ calculate\_error(discretization\_points,\ u.get(),\ correct\_u)\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{static\_cast<}\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\textcolor{keyword}{>}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ discretization\_points)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 