\chapter{The ginkgo-\/ranges program }
\hypertarget{ginkgo_ranges}{}\label{ginkgo_ranges}\index{The ginkgo-\/ranges program@{The ginkgo-\/ranges program}}
The ranges and accessor example.

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

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

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

a utility function for printing the factorization on screen


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Accessor>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_lu(\textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1range}{gko::range<Accessor>}}\&\ A)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ std::setprecision(2)\ <<\ std::fixed;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}L\ =\ ["{}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0);\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n\ \ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ j\ =\ 0;\ j\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(1);\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ (i\ >\ j\ ?\ A(i,\ j)\ :\ (i\ ==\ j)\ *\ 1.)\ <<\ \textcolor{stringliteral}{"{}\ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n]\(\backslash\)n\(\backslash\)nU\ =\ ["{}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0);\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n\ \ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ j\ =\ 0;\ j\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(1);\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ (i\ <=\ j\ ?\ A(i,\ j)\ :\ 0.)\ <<\ \textcolor{stringliteral}{"{}\ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n]"{}}\ <<\ std::endl;}
\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;}

\end{DoxyCode}


Create some test data, add some padding just to demonstrate how to use it with ranges. clang-\/format off


\begin{DoxyCode}{0}
\DoxyCodeLine{ValueType\ data[]\ =\ \{}
\DoxyCodeLine{\ \ \ \ 2.,\ \ 4.,\ \ 5.,\ -\/1.0,}
\DoxyCodeLine{\ \ \ \ 4.,\ 11.,\ 12.,\ -\/1.0,}
\DoxyCodeLine{\ \ \ \ 6.,\ 24.,\ 24.,\ -\/1.0}
\DoxyCodeLine{\};}

\end{DoxyCode}


clang-\/format on

Create a 3-\/by-\/3 range, with a 2D row-\/major accessor using data as the underlying storage. Set the stride (a.\+k.\+a. "{}\+LDA"{}) to 4.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{keyword}{auto}\ A\ =}
\DoxyCodeLine{\ \ \ \ \doxymbox{\hyperlink{classgko_1_1range}{gko::range<gko::accessor::row\_major<ValueType,\ 2>}}>(data,\ 3u,\ 3u,\ 4u);}

\end{DoxyCode}


use the LU factorization routine defined above to factorize the matrix


\begin{DoxyCode}{0}
\DoxyCodeLine{factorize(A);}

\end{DoxyCode}


print the factorization on screen


\begin{DoxyCode}{0}
\DoxyCodeLine{\ \ \ \ print\_lu(A);}
\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{L\ =\ [}
\DoxyCodeLine{\ \ 1.00\ 0.00\ 0.00}
\DoxyCodeLine{\ \ 2.00\ 1.00\ 0.00}
\DoxyCodeLine{\ \ 3.00\ 4.00\ 1.00}
\DoxyCodeLine{]}
\DoxyCodeLine{}
\DoxyCodeLine{U\ =\ [}
\DoxyCodeLine{\ \ 2.00\ 4.00\ 5.00}
\DoxyCodeLine{\ \ 0.00\ 3.00\ 2.00}
\DoxyCodeLine{\ \ 0.00\ 0.00\ 1.00}
\DoxyCodeLine{]}

\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\ <iomanip>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <iostream>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <ginkgo/ginkgo.hpp>}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Accessor>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ factorize(\textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1range}{gko::range<Accessor>}}\&\ A)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{using\ }\doxymbox{\hyperlink{structgko_1_1span}{gko::span}};}
\DoxyCodeLine{\ \ \ \ assert(A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0)\ ==\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(1));}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{gko::size\_type}}\ i\ =\ 0;\ i\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0)\ -\/\ 1;\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keyword}{auto}\ trail\ =\ span\{i\ +\ 1,\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0)\};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ A(trail,\ i)\ =\ A(trail,\ i)\ /\ A(i,\ i);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ A(trail,\ trail)\ =\ A(trail,\ trail)\ -\/\ mmul(A(trail,\ i),\ A(i,\ trail));}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ Accessor>}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ print\_lu(\textcolor{keyword}{const}\ \doxymbox{\hyperlink{classgko_1_1range}{gko::range<Accessor>}}\&\ A)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ std::setprecision(2)\ <<\ std::fixed;}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}L\ =\ ["{}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0);\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n\ \ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ j\ =\ 0;\ j\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(1);\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ (i\ >\ j\ ?\ A(i,\ j)\ :\ (i\ ==\ j)\ *\ 1.)\ <<\ \textcolor{stringliteral}{"{}\ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n]\(\backslash\)n\(\backslash\)nU\ =\ ["{}};}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ i\ =\ 0;\ i\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(0);\ ++i)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n\ \ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keywordtype}{int}\ j\ =\ 0;\ j\ <\ A.\doxymbox{\hyperlink{classgko_1_1range_a3ac8b238c377da9cc05d7c728efabfc8}{length}}(1);\ ++j)\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ std::cout\ <<\ (i\ <=\ j\ ?\ A(i,\ j)\ :\ 0.)\ <<\ \textcolor{stringliteral}{"{}\ "{}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ std::cout\ <<\ \textcolor{stringliteral}{"{}\(\backslash\)n]"{}}\ <<\ std::endl;}
\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{\ \ \ \ ValueType\ data[]\ =\ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ 2.,\ \ 4.,\ \ 5.,\ -\/1.0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ 4.,\ 11.,\ 12.,\ -\/1.0,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ 6.,\ 24.,\ 24.,\ -\/1.0}
\DoxyCodeLine{\ \ \ \ \};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{auto}\ A\ =}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \doxymbox{\hyperlink{classgko_1_1range}{gko::range<gko::accessor::row\_major<ValueType,\ 2>}}>(data,\ 3u,\ 3u,\ 4u);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ factorize(A);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ print\_lu(A);}
\DoxyCodeLine{\}}

\end{DoxyCodeInclude}
 