\chapter{The nine-\/pt-\/stencil-\/solver program }
\hypertarget{nine_pt_stencil_solver}{}\label{nine_pt_stencil_solver}\index{The nine-\/pt-\/stencil-\/solver program@{The nine-\/pt-\/stencil-\/solver program}}
The 9-\/point stencil example.

This example depends on simple-\/solver, three-\/pt-\/stencil-\/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 2D Poisson equation:\+

\+[ \textbackslash{}\+Omega = (0,1)\texorpdfstring{$^\wedge$}{\string^}2 \textbackslash{} \textbackslash{}\+Omega\+\_\+b = \+[0,1]\+\texorpdfstring{$^\wedge$}{\string^}2 \textbackslash{}text\{ (with boundary)\} \textbackslash{} \textbackslash{}partial\textbackslash{}\+Omega = \textbackslash{}\+Omega\+\_\+b \textbackslash{}backslash \textbackslash{}\+Omega \textbackslash{} u :\+ \textbackslash{}\+Omega\+\_\+b -\/\texorpdfstring{$>$}{>} R \textbackslash{} u\textquotesingle{}\textquotesingle{} = f \textbackslash{}in \textbackslash{}\+Omega \textbackslash{} u = u\+\_\+D \textbackslash{}in \textbackslash{}partial\textbackslash{}\+Omega \textbackslash{} ]\+

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 may be done by any order Taylor polynomial. For an equidistant grid with K "{}inner"{} discretization points ((x1,y1), \textbackslash{}ldots, (xk,y1),(x1,y2), \textbackslash{}ldots, (xk,yk,z1)) step size (h = 1 /\+ (K + 1)) and a stencil (\textbackslash{}in \textbackslash{}mathbb\{R\}\texorpdfstring{$^\wedge$}{\string^}\{3 \textbackslash{}times 3\}), the formula produces a system of linear equations

(\textbackslash{}sum\+\_\+\{a,b=-\/1\}\texorpdfstring{$^\wedge$}{\string^}1 stencil(a,b) \texorpdfstring{$\ast$}{*} u\+\_\+\{(i+a,j+b\} = -\/f\+\_\+k h\texorpdfstring{$^\wedge$}{\string^}2), on any inner node with a neighborhood of inner nodes

On any node, where neighbor is on the border, the neighbor is replaced with a (-\/stencil(a,b) \texorpdfstring{$\ast$}{*} u\+\_\+\{i+a,j+b\}) and added to the right hand side vector. For example a node with a neighborhood of only edge nodes may look like this

\+[ \textbackslash{}sum\+\_\+\{a,b=-\/1\}\texorpdfstring{$^\wedge$}{\string^}(1,0) stencil(a,b) \texorpdfstring{$\ast$}{*} u\+\_\+\{(i+a,j+b\} = -\/f\+\_\+k h\texorpdfstring{$^\wedge$}{\string^}2 -\/ \textbackslash{}sum\+\_\+\{a=-\/1\}\texorpdfstring{$^\wedge$}{\string^}1 stencil(a,1) \texorpdfstring{$\ast$}{*} u\+\_\+\{(i+a,j+1\} ]\+

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 {\ttfamily f} is set to (f(x,y) = 6x + 6y) (making the solution (u(x,y) = x\texorpdfstring{$^\wedge$}{\string^}3
\begin{DoxyItemize}
\item y\texorpdfstring{$^\wedge$}{\string^}3)), but that can be changed in the {\ttfamily main} function. Also the stencil values for the core, the faces, the edge and the corners can be changed when passing additional parameters.
\end{DoxyItemize}

The intention of this is to show how generation of stencil values and the right hand side vector changes when increasing the dimension.

\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{preconditioned\ with\ block-\/Jacobi.\ It\ is\ also\ possible\ to\ specify\ on\ which}
\DoxyCodeLine{executor\ Ginkgo\ will\ solve\ the\ system\ via\ the\ command\ line.}
\DoxyCodeLine{The\ function\ \`{}f\`{}\ is\ set\ to\ \`{}f(x,y)\ =\ 6x\ +\ 6y\`{}\ (making\ the\ solution\ \`{}u(x,y)\ =\ x\string^3}
\DoxyCodeLine{+\ y\string^3\`{}),\ but\ that\ can\ be\ changed\ in\ the\ \`{}main\`{}\ function.\ Also\ the\ stencil\ values}
\DoxyCodeLine{\textcolor{keywordflow}{for}\ the\ core,\ the\ faces,\ the\ edge\ and\ the\ corners\ can\ be\ changed\ when\ passing}
\DoxyCodeLine{additional\ parameters.}
\DoxyCodeLine{}
\DoxyCodeLine{The\ intention\ of\ \textcolor{keyword}{this}\ is\ to\ show\ how\ generation\ of\ stencil\ values\ and\ the\ right}
\DoxyCodeLine{hand\ side\ vector\ changes\ when\ increasing\ the\ dimension.}
\DoxyCodeLine{*****************************<DESCRIPTION>**********************************\ /}
\DoxyCodeLine{}
\DoxyCodeLine{\#include\ <array>}
\DoxyCodeLine{\#include\ <chrono>}
\DoxyCodeLine{\#include\ <iostream>}
\DoxyCodeLine{\#include\ <map>}
\DoxyCodeLine{\#include\ <string>}
\DoxyCodeLine{\#include\ <vector>}
\DoxyCodeLine{}
\DoxyCodeLine{\#include\ <ginkgo/ginkgo.hpp>}

\end{DoxyCode}


Stencil values. Ordering can be seen in the main function Can also be changed by passing additional parameter when executing


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{constexpr}\ \textcolor{keywordtype}{double}\ default\_alpha\ =\ 10.0\ /\ 3.0;}
\DoxyCodeLine{\textcolor{keyword}{constexpr}\ \textcolor{keywordtype}{double}\ default\_beta\ =\ -\/2.0\ /\ 3.0;}
\DoxyCodeLine{\textcolor{keyword}{constexpr}\ \textcolor{keywordtype}{double}\ default\_gamma\ =\ -\/1.0\ /\ 6.0;}
\DoxyCodeLine{}
\DoxyCodeLine{/\ *\ Possible\ alternative\ \textcolor{keywordflow}{default}\ values\ are}
\DoxyCodeLine{\ *\ default\_alpha\ =\ 8.0;}
\DoxyCodeLine{\ *\ default\_beta\ =\ -\/1.0;}
\DoxyCodeLine{\ *\ default\_gamma\ =\ -\/1.0;}
\DoxyCodeLine{\ *\ /}

\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\ dp,\ IndexType*\ row\_ptrs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType*\ col\_idxs,\ ValueType*\ values,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ coefs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ IndexType\ pos\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ dp\_2\ =\ dp\ *\ dp;}
\DoxyCodeLine{\ \ \ \ row\_ptrs[0]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ k\ =\ 0;\ k\ <\ dp;\ ++k)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ index\ =\ i\ +\ k\ *\ dp;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ -\/1;\ j\ <=\ 1;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ l\ =\ -\/1;\ l\ <=\ 1;\ ++l)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ IndexType\ offset\ =\ l\ +\ 1\ +\ 3\ *\ (j\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((k\ +\ j)\ >=\ 0\ \&\&\ (k\ +\ j)\ <\ dp\ \&\&\ (i\ +\ l)\ >=\ 0\ \&\&}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (i\ +\ l)\ <\ dp)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values[pos]\ =\ coefs[offset];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs[pos]\ =\ index\ +\ l\ +\ dp\ *\ j;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++pos;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ row\_ptrs[index\ +\ 1]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\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}\ ClosureT,\ \textcolor{keyword}{typename}\ ValueType,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_rhs(IndexType\ dp,\ Closure\ f,\ ClosureT\ u,\ ValueType*\ rhs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ coefs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ dp\_2\ =\ dp\ *\ dp;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (dp\ +\ 1.0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ yi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ 0;\ j\ <\ dp;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ ValueType(j\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\ =\ i\ *\ dp\ +\ j;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index]\ =\ -\/f(xi,\ yi)\ *\ h\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}

\end{DoxyCode}


Iterating over the edges to add boundary values and adding the overlapping 3x1 to the rhs


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{size\_t}\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_top\ =\ i;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_bot\ =\ i\ +\ dp\ *\ (dp\ -\/\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_top]\ -\/=\ u(xi\ -\/\ h,\ 0.0)\ *\ coefs[0];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_top]\ -\/=\ u(xi,\ 0.0)\ *\ coefs[1];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_top]\ -\/=\ u(xi\ +\ h,\ 0.0)\ *\ coefs[2];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_bot]\ -\/=\ u(xi\ -\/\ h,\ 1.0)\ *\ coefs[6];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_bot]\ -\/=\ u(xi,\ 1.0)\ *\ coefs[7];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_bot]\ -\/=\ u(xi\ +\ h,\ 1.0)\ *\ coefs[8];}
\DoxyCodeLine{\}}
\DoxyCodeLine{\textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{size\_t}\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ yi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_left\ =\ i\ *\ dp;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_right\ =\ i\ *\ dp\ +\ (dp\ -\/\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_left]\ -\/=\ u(0.0,\ yi\ -\/\ h)\ *\ coefs[0];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_left]\ -\/=\ u(0.0,\ yi)\ *\ coefs[3];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_left]\ -\/=\ u(0.0,\ yi\ +\ h)\ *\ coefs[6];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_right]\ -\/=\ u(1.0,\ yi\ -\/\ h)\ *\ coefs[2];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_right]\ -\/=\ u(1.0,\ yi)\ *\ coefs[5];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_right]\ -\/=\ u(1.0,\ yi\ +\ h)\ *\ coefs[8];}
\DoxyCodeLine{\}}

\end{DoxyCode}


remove the double corner values


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[0]\ +=\ u(0.0,\ 0.0)\ *\ coefs[0];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[(dp\ -\/\ 1)]\ +=\ u(1.0,\ 0.0)\ *\ coefs[2];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[(dp\ -\/\ 1)\ *\ dp]\ +=\ u(0.0,\ 1.0)\ *\ coefs[6];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[dp\ *\ dp\ -\/\ 1]\ +=\ u(1.0,\ 1.0)\ *\ coefs[8];}
\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\ dp,\ \textcolor{keyword}{const}\ ValueType*\ u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ 0;\ j\ <\ dp;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ u[i\ *\ dp\ +\ j]\ <<\ \textcolor{charliteral}{'\ '};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ 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\ dp,\ \textcolor{keyword}{const}\ ValueType*\ u,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Closure\ correct\_u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (dp\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ error\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ 0;\ j\ <\ dp;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ ValueType(j\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }std::abs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ yi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ error\ +=}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(u[i\ *\ dp\ +\ j]\ -\/\ correct\_u(xi,\ yi))\ /\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(correct\_u(xi,\ yi));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ error;}
\DoxyCodeLine{\}}
\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{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{int}\ 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;}
\DoxyCodeLine{\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ dp\_2\ =\ dp\ *\ dp;}

\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(}
\DoxyCodeLine{\ \ \ \ exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp\_2),}
\DoxyCodeLine{\ \ \ \ val\_array::view(app\_exec,\ (3\ *\ dp\ -\/\ 2)\ *\ (3\ *\ dp\ -\/\ 2),\ values),}
\DoxyCodeLine{\ \ \ \ idx\_array::view(app\_exec,\ (3\ *\ dp\ -\/\ 2)\ *\ (3\ *\ dp\ -\/\ 2),\ col\_idxs),}
\DoxyCodeLine{\ \ \ \ idx\_array::view(app\_exec,\ dp\_2\ +\ 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\_2,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp\_2,\ 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\_2,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp\_2,\ 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(dp\_2),}
\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}
\DoxyCodeLine{\ \ \ \ \ \ \ \ <<\ \textcolor{stringliteral}{"{}Usage:\ "{}}\ <<\ argv[0]}
\DoxyCodeLine{\ \ \ \ \ \ \ \ <<\ \textcolor{stringliteral}{"{}\ [executor]\ [DISCRETIZATION\_POINTS]\ [alpha]\ [beta]\ [gamma]"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ <<\ 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{\textcolor{keyword}{const}\ ValueType\ alpha\_c\ =\ argc\ >=\ 4\ ?\ std::atof(argv[3])\ :\ default\_alpha;}
\DoxyCodeLine{\textcolor{keyword}{const}\ ValueType\ beta\_c\ =\ argc\ >=\ 5\ ?\ std::atof(argv[4])\ :\ default\_beta;}
\DoxyCodeLine{\textcolor{keyword}{const}\ ValueType\ gamma\_c\ =\ argc\ >=\ 6\ ?\ std::atof(argv[5])\ :\ default\_gamma;}

\end{DoxyCode}


clang-\/format off


\begin{DoxyCode}{0}
\DoxyCodeLine{std::array<ValueType,\ 9>\ coefs\{}
\DoxyCodeLine{\ \ \ \ gamma\_c,\ beta\_c,\ gamma\_c,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ beta\_c,\ alpha\_c,\ beta\_c,}
\DoxyCodeLine{\ \ \ \ gamma\_c,\ beta\_c,\ gamma\_c\};}

\end{DoxyCode}


clang-\/format on


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ dp\ =\ discretization\_points;}
\DoxyCodeLine{\textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ dp\_2\ =\ dp\ *\ dp;}

\end{DoxyCode}


problem:\+


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

\end{DoxyCode}


matrix


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

\end{DoxyCode}


right hand side


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

\end{DoxyCode}


solution


\begin{DoxyCode}{0}
\DoxyCodeLine{std::vector<ValueType>\ u(dp\_2,\ 0.0);}
\DoxyCodeLine{}
\DoxyCodeLine{generate\_stencil\_matrix(dp,\ row\_ptrs.data(),\ col\_idxs.data(),\ values.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ coefs.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(dp,\ f,\ correct\_u,\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data(),\ coefs.data());}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor\ =\ 1e-\/7;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{auto}\ start\_time\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{solve\_system(executor\_string,\ dp,\ row\_ptrs.data(),\ col\_idxs.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ values.data(),\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data(),\ u.data(),\ reduction\_factor);}
\DoxyCodeLine{\textcolor{keyword}{auto}\ stop\_time\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\textcolor{keyword}{auto}\ runtime\_duration\ =}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::chrono::duration\_cast<std::chrono::nanoseconds>(stop\_time\ -\/}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ start\_time)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .count())\ *}
\DoxyCodeLine{\ \ \ \ 1e-\/6;}

\end{DoxyCode}


Uncomment to print the solution print\+\_\+solution(dp, u.\+data());


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}The\ average\ relative\ error\ is\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ calculate\_error(dp,\ u.data(),\ correct\_u)\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{static\_cast<}\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\textcolor{keyword}{>}(dp\_2)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}The\ runtime\ is\ "{}}\ <<\ std::to\_string(runtime\_duration)\ <<\ \textcolor{stringliteral}{"{}\ ms"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCode}
 \label{three_pt_stencil_solver_Results}%
\Hypertarget{three_pt_stencil_solver_Results}%
\doxysection*{Results}

The expected output should be


\begin{DoxyCode}{0}
\DoxyCodeLine{The\ average\ relative\ error\ is\ 6.35715e-\/06}
\DoxyCodeLine{The\ runtime\ is\ 167.320520\ ms}

\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\ 2D\ Poisson\ equation:}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ \(\backslash\)Omega\ =\ (0,1)\string^2}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ \(\backslash\)Omega\_b\ =\ [0,1]\string^2\ \ \ (with\ boundary)}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ \(\backslash\)partial\(\backslash\)Omega\ =\ \(\backslash\)Omega\_b\ \(\backslash\)backslash\ \(\backslash\)Omega}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u\ :\ \(\backslash\)Omega\_b\ -\/>\ R}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u''\ =\ f\ in\ \(\backslash\)Omega}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ u\ =\ u\_D\ on\ \(\backslash\)partial\(\backslash\)Omega}}
\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}{may\ be\ done\ by\ any\ order\ Taylor\ polynomial.}}
\DoxyCodeLine{\textcolor{comment}{For\ an\ equidistant\ grid\ with\ K\ "{}inner"{}\ discretization\ points\ (x1,y1),\ ...,}}
\DoxyCodeLine{\textcolor{comment}{(xk,y1),(x1,y2),\ ...,\ (xk,yk)\ step\ size\ h\ =\ 1\ /\ (K\ +\ 1)\ and\ a\ stencil\ \(\backslash\)in}}
\DoxyCodeLine{\textcolor{comment}{\(\backslash\)R\string^\{3\ x\ 3\},\ the\ formula\ produces\ a\ system\ of\ linear\ equations}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\(\backslash\)sum\_\{a,b=-\/1\}\string^1\ stencil(a,b)\ *\ u\_\{(i+a,j+b\}\ =\ -\/f\_k\ h\string^2,\ \ on\ any\ inner\ node\ with}}
\DoxyCodeLine{\textcolor{comment}{a\ neighborhood\ of\ inner\ nodes}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{On\ any\ node,\ where\ neighbor\ is\ on\ the\ border,\ the\ neighbor\ is\ replaced\ with\ a}}
\DoxyCodeLine{\textcolor{comment}{'-\/stencil(a,b)\ *\ u\_\{i+a,j+b\}'\ and\ added\ to\ the\ right\ hand\ side\ vector.\ For}}
\DoxyCodeLine{\textcolor{comment}{example\ a\ node\ with\ a\ neighborhood\ of\ only\ edge\ nodes\ may\ look\ like\ this}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\(\backslash\)sum\_\{a,b=-\/1\}\string^(1,0)\ stencil(a,b)\ *\ u\_\{(i+a,j+b\}\ =\ -\/f\_k\ h\string^2\ -\/\ \(\backslash\)sum\_\{a=-\/1\}\string^1}}
\DoxyCodeLine{\textcolor{comment}{stencil(a,1)\ *\ u\_\{(i+a,j+1\}}}
\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,y)\ =\ 6x\ +\ 6y`\ (making\ the\ solution\ \`{}u(x,y)\ =\ x\string^3}}
\DoxyCodeLine{\textcolor{comment}{+\ y\string^3`),\ but\ that\ can\ be\ changed\ in\ the\ \`{}main`\ function.\ Also\ the\ stencil\ values}}
\DoxyCodeLine{\textcolor{comment}{for\ the\ core,\ the\ faces,\ the\ edge\ and\ the\ corners\ can\ be\ changed\ when\ passing}}
\DoxyCodeLine{\textcolor{comment}{additional\ parameters.}}
\DoxyCodeLine{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{The\ intention\ of\ this\ is\ to\ show\ how\ generation\ of\ stencil\ values\ and\ the\ right}}
\DoxyCodeLine{\textcolor{comment}{hand\ side\ vector\ changes\ when\ increasing\ the\ dimension.}}
\DoxyCodeLine{\textcolor{comment}{*****************************<DESCRIPTION>**********************************/}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <array>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <chrono>}}
\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{\textcolor{keyword}{constexpr}\ \textcolor{keywordtype}{double}\ default\_alpha\ =\ 10.0\ /\ 3.0;}
\DoxyCodeLine{\textcolor{keyword}{constexpr}\ \textcolor{keywordtype}{double}\ default\_beta\ =\ -\/2.0\ /\ 3.0;}
\DoxyCodeLine{\textcolor{keyword}{constexpr}\ \textcolor{keywordtype}{double}\ default\_gamma\ =\ -\/1.0\ /\ 6.0;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{/*\ Possible\ alternative\ default\ values\ are}}
\DoxyCodeLine{\textcolor{comment}{\ *\ default\_alpha\ =\ 8.0;}}
\DoxyCodeLine{\textcolor{comment}{\ *\ default\_beta\ =\ -\/1.0;}}
\DoxyCodeLine{\textcolor{comment}{\ *\ default\_gamma\ =\ -\/1.0;}}
\DoxyCodeLine{\textcolor{comment}{\ */}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_stencil\_matrix(IndexType\ dp,\ IndexType*\ row\_ptrs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ IndexType*\ col\_idxs,\ ValueType*\ values,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ coefs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ IndexType\ pos\ =\ 0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ dp\_2\ =\ dp\ *\ dp;}
\DoxyCodeLine{\ \ \ \ row\_ptrs[0]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ k\ =\ 0;\ k\ <\ dp;\ ++k)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ index\ =\ i\ +\ k\ *\ dp;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ -\/1;\ j\ <=\ 1;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ l\ =\ -\/1;\ l\ <=\ 1;\ ++l)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ IndexType\ offset\ =\ l\ +\ 1\ +\ 3\ *\ (j\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((k\ +\ j)\ >=\ 0\ \&\&\ (k\ +\ j)\ <\ dp\ \&\&\ (i\ +\ l)\ >=\ 0\ \&\&}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (i\ +\ l)\ <\ dp)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values[pos]\ =\ coefs[offset];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\_idxs[pos]\ =\ index\ +\ l\ +\ dp\ *\ j;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ++pos;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ row\_ptrs[index\ +\ 1]\ =\ pos;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Closure,\ \textcolor{keyword}{typename}\ ClosureT,\ \textcolor{keyword}{typename}\ ValueType,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ generate\_rhs(IndexType\ dp,\ Closure\ f,\ ClosureT\ u,\ ValueType*\ rhs,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ValueType*\ coefs)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ dp\_2\ =\ dp\ *\ dp;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (dp\ +\ 1.0);}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ yi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ 0;\ j\ <\ dp;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ ValueType(j\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\ =\ i\ *\ dp\ +\ j;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index]\ =\ -\/f(xi,\ yi)\ *\ h\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{size\_t}\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_top\ =\ i;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_bot\ =\ i\ +\ dp\ *\ (dp\ -\/\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_top]\ -\/=\ u(xi\ -\/\ h,\ 0.0)\ *\ coefs[0];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_top]\ -\/=\ u(xi,\ 0.0)\ *\ coefs[1];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_top]\ -\/=\ u(xi\ +\ h,\ 0.0)\ *\ coefs[2];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_bot]\ -\/=\ u(xi\ -\/\ h,\ 1.0)\ *\ coefs[6];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_bot]\ -\/=\ u(xi,\ 1.0)\ *\ coefs[7];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_bot]\ -\/=\ u(xi\ +\ h,\ 1.0)\ *\ coefs[8];}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{size\_t}\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ yi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_left\ =\ i\ *\ dp;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ index\_right\ =\ i\ *\ dp\ +\ (dp\ -\/\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_left]\ -\/=\ u(0.0,\ yi\ -\/\ h)\ *\ coefs[0];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_left]\ -\/=\ u(0.0,\ yi)\ *\ coefs[3];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_left]\ -\/=\ u(0.0,\ yi\ +\ h)\ *\ coefs[6];}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_right]\ -\/=\ u(1.0,\ yi\ -\/\ h)\ *\ coefs[2];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_right]\ -\/=\ u(1.0,\ yi)\ *\ coefs[5];}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[index\_right]\ -\/=\ u(1.0,\ yi\ +\ h)\ *\ coefs[8];}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[0]\ +=\ u(0.0,\ 0.0)\ *\ coefs[0];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[(dp\ -\/\ 1)]\ +=\ u(1.0,\ 0.0)\ *\ coefs[2];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[(dp\ -\/\ 1)\ *\ dp]\ +=\ u(0.0,\ 1.0)\ *\ coefs[6];}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}[dp\ *\ dp\ -\/\ 1]\ +=\ u(1.0,\ 1.0)\ *\ coefs[8];}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType,\ \textcolor{keyword}{typename}\ IndexType>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_solution(IndexType\ dp,\ \textcolor{keyword}{const}\ ValueType*\ u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ 0;\ j\ <\ dp;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ u[i\ *\ dp\ +\ j]\ <<\ \textcolor{charliteral}{'\ '};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{charliteral}{'\(\backslash\)n'};}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ 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\ dp,\ \textcolor{keyword}{const}\ ValueType*\ u,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Closure\ correct\_u)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ h\ =\ 1.0\ /\ (dp\ +\ 1);}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ error\ =\ 0.0;}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ j\ =\ 0;\ j\ <\ dp;\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ xi\ =\ ValueType(j\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (IndexType\ i\ =\ 0;\ i\ <\ dp;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{using\ }std::abs;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ yi\ =\ ValueType(i\ +\ 1)\ *\ h;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ error\ +=}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(u[i\ *\ dp\ +\ j]\ -\/\ correct\_u(xi,\ yi))\ /\ \doxymbox{\hyperlink{namespacegko_a0cb7d0ece3444fcf3a7ebd82e1cb0a69}{abs}}(correct\_u(xi,\ yi));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ error;}
\DoxyCodeLine{\}}
\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{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{int}\ 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{\ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ dp\_2\ =\ dp\ *\ dp;}
\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(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp\_2),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ (3\ *\ dp\ -\/\ 2)\ *\ (3\ *\ dp\ -\/\ 2),\ values),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ idx\_array::view(app\_exec,\ (3\ *\ dp\ -\/\ 2)\ *\ (3\ *\ dp\ -\/\ 2),\ col\_idxs),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ idx\_array::view(app\_exec,\ dp\_2\ +\ 1,\ row\_ptrs));}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ b\ =\ vec::create(exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp\_2,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp\_2,\ rhs),\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ x\ =\ vec::create(app\_exec,\ \doxymbox{\hyperlink{structgko_1_1dim}{gko::dim<2>}}(dp\_2,\ 1),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ val\_array::view(app\_exec,\ dp\_2,\ u),\ 1);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ solver\_gen\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ cg::build()}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ .with\_criteria(gko::stop::Iteration::build().with\_max\_iters(dp\_2),}
\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}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{stringliteral}{"{}Usage:\ "{}}\ <<\ argv[0]}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ <<\ \textcolor{stringliteral}{"{}\ [executor]\ [DISCRETIZATION\_POINTS]\ [alpha]\ [beta]\ [gamma]"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ <<\ 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{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ alpha\_c\ =\ argc\ >=\ 4\ ?\ std::atof(argv[3])\ :\ default\_alpha;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ beta\_c\ =\ argc\ >=\ 5\ ?\ std::atof(argv[4])\ :\ default\_beta;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ ValueType\ gamma\_c\ =\ argc\ >=\ 6\ ?\ std::atof(argv[5])\ :\ default\_gamma;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::array<ValueType,\ 9>\ coefs\{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ gamma\_c,\ beta\_c,\ gamma\_c,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ beta\_c,\ alpha\_c,\ beta\_c,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ gamma\_c,\ beta\_c,\ gamma\_c\};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ dp\ =\ discretization\_points;}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{size\_t}\ dp\_2\ =\ dp\ *\ dp;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ correct\_u\ =\ [](ValueType\ x,\ ValueType\ y)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ x\ *\ x\ *\ x\ +\ y\ *\ y\ *\ y;}
\DoxyCodeLine{\ \ \ \ \};}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ f\ =\ [](ValueType\ x,\ ValueType\ y)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ ValueType(6)\ *\ x\ +\ ValueType(6)\ *\ y;}
\DoxyCodeLine{\ \ \ \ \};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::vector<IndexType>\ row\_ptrs(dp\_2\ +\ 1);}
\DoxyCodeLine{\ \ \ \ std::vector<IndexType>\ col\_idxs((3\ *\ dp\ -\/\ 2)\ *\ (3\ *\ dp\ -\/\ 2));}
\DoxyCodeLine{\ \ \ \ std::vector<ValueType>\ values((3\ *\ dp\ -\/\ 2)\ *\ (3\ *\ dp\ -\/\ 2));}
\DoxyCodeLine{\ \ \ \ std::vector<ValueType>\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}(dp\_2);}
\DoxyCodeLine{\ \ \ \ std::vector<ValueType>\ u(dp\_2,\ 0.0);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ generate\_stencil\_matrix(dp,\ row\_ptrs.data(),\ col\_idxs.data(),\ values.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ coefs.data());}
\DoxyCodeLine{\ \ \ \ generate\_rhs(dp,\ f,\ correct\_u,\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data(),\ coefs.data());}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\ reduction\_factor\ =\ 1e-\/7;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ start\_time\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ solve\_system(executor\_string,\ dp,\ row\_ptrs.data(),\ col\_idxs.data(),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ values.data(),\ \doxymbox{\hyperlink{namespacegko_1_1solver_a82d21321745fbd3dc65c276d5838bb8aa83ff9f9e3dd7561d3dd91204cf546b7e}{rhs}}.data(),\ u.data(),\ reduction\_factor);}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ stop\_time\ =\ std::chrono::steady\_clock::now();}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ runtime\_duration\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{static\_cast<}\textcolor{keywordtype}{double}\textcolor{keyword}{>}(}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::chrono::duration\_cast<std::chrono::nanoseconds>(stop\_time\ -\/}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ start\_time)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ .count())\ *}
\DoxyCodeLine{\ \ \ \ \ \ \ \ 1e-\/6;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}The\ average\ relative\ error\ is\ "{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ calculate\_error(dp,\ u.data(),\ correct\_u)\ /}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{static\_cast<}\doxymbox{\hyperlink{namespacegko_a107302a750bfa82a2107c77293f791c9}{gko::remove\_complex<ValueType>}}\textcolor{keyword}{>}(dp\_2)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}The\ runtime\ is\ "{}}\ <<\ std::to\_string(runtime\_duration)\ <<\ \textcolor{stringliteral}{"{}\ ms"{}}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ <<\ std::endl;}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 