\doxysection{workspace.\+hpp}
\hypertarget{workspace_8hpp_source}{}\label{workspace_8hpp_source}\index{ginkgo/core/solver/workspace.hpp@{ginkgo/core/solver/workspace.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{//\ SPDX-\/FileCopyrightText:\ 2017\ -\/\ 2024\ The\ Ginkgo\ authors}}
\DoxyCodeLine{00002\ \textcolor{comment}{//}}
\DoxyCodeLine{00003\ \textcolor{comment}{//\ SPDX-\/License-\/Identifier:\ BSD-\/3-\/Clause}}
\DoxyCodeLine{00004\ }
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#ifndef\ GKO\_PUBLIC\_CORE\_SOLVER\_WORKSPACE\_HPP\_}}
\DoxyCodeLine{00006\ \textcolor{preprocessor}{\#define\ GKO\_PUBLIC\_CORE\_SOLVER\_WORKSPACE\_HPP\_}}
\DoxyCodeLine{00007\ }
\DoxyCodeLine{00008\ }
\DoxyCodeLine{00009\ \textcolor{preprocessor}{\#include\ <typeinfo>}}
\DoxyCodeLine{00010\ }
\DoxyCodeLine{00011\ \textcolor{preprocessor}{\#include\ <ginkgo/core/matrix/dense.hpp>}}
\DoxyCodeLine{00012\ }
\DoxyCodeLine{00013\ }
\DoxyCodeLine{00014\ \textcolor{keyword}{namespace\ }\doxymbox{\hyperlink{namespacegko}{gko}}\ \{}
\DoxyCodeLine{00015\ \textcolor{keyword}{namespace\ }\doxymbox{\hyperlink{namespacegko_1_1solver}{solver}}\ \{}
\DoxyCodeLine{00016\ \textcolor{keyword}{namespace\ }detail\ \{}
\DoxyCodeLine{00017\ }
\DoxyCodeLine{00018\ }
\DoxyCodeLine{00022\ \textcolor{keyword}{class\ }any\_array\ \{}
\DoxyCodeLine{00023\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00024\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00025\ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array<ValueType>}}\&\ init(std::shared\_ptr<const\ Executor>\ exec,\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{size\_type}}\ size)}
\DoxyCodeLine{00026\ \ \ \ \ \{}
\DoxyCodeLine{00027\ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ container\ =\ std::make\_unique<concrete\_container<ValueType>>(}
\DoxyCodeLine{00028\ \ \ \ \ \ \ \ \ \ \ \ \ std::move(exec),\ size);}
\DoxyCodeLine{00029\ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\&\ arr\ =\ container-\/>arr;}
\DoxyCodeLine{00030\ \ \ \ \ \ \ \ \ data\_\ =\ std::move(container);}
\DoxyCodeLine{00031\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ arr;}
\DoxyCodeLine{00032\ \ \ \ \ \}}
\DoxyCodeLine{00033\ }
\DoxyCodeLine{00034\ \ \ \ \ \textcolor{keywordtype}{bool}\ empty()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ data\_.get()\ ==\ \textcolor{keyword}{nullptr};\ \}}
\DoxyCodeLine{00035\ }
\DoxyCodeLine{00036\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00037\ \ \ \ \ \textcolor{keywordtype}{bool}\ contains()\textcolor{keyword}{\ const}}
\DoxyCodeLine{00038\ \textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{00039\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{dynamic\_cast<}\textcolor{keyword}{const\ }concrete\_container<ValueType>*\textcolor{keyword}{>}(data\_.get());}
\DoxyCodeLine{00040\ \ \ \ \ \}}
\DoxyCodeLine{00041\ }
\DoxyCodeLine{00042\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00043\ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array<ValueType>}}\&\ get()}
\DoxyCodeLine{00044\ \ \ \ \ \{}
\DoxyCodeLine{00045\ \ \ \ \ \ \ \ \ GKO\_ASSERT(this-\/>\textcolor{keyword}{template}\ contains<ValueType>());}
\DoxyCodeLine{00046\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{dynamic\_cast<}concrete\_container<ValueType>*\textcolor{keyword}{>}(data\_.get())-\/>arr;}
\DoxyCodeLine{00047\ \ \ \ \ \}}
\DoxyCodeLine{00048\ }
\DoxyCodeLine{00049\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00050\ \ \ \ \ \textcolor{keyword}{const}\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array<ValueType>}}\&\ get()\textcolor{keyword}{\ const}}
\DoxyCodeLine{00051\ \textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{00052\ \ \ \ \ \ \ \ \ GKO\_ASSERT(this-\/>\textcolor{keyword}{template}\ contains<ValueType>());}
\DoxyCodeLine{00053\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \textcolor{keyword}{dynamic\_cast<}\textcolor{keyword}{const\ }concrete\_container<ValueType>*\textcolor{keyword}{>}(data\_.get())}
\DoxyCodeLine{00054\ \ \ \ \ \ \ \ \ \ \ \ \ -\/>arr;}
\DoxyCodeLine{00055\ \ \ \ \ \}}
\DoxyCodeLine{00056\ }
\DoxyCodeLine{00057\ \ \ \ \ \textcolor{keywordtype}{void}\ clear()\ \{\ data\_.reset();\ \}}
\DoxyCodeLine{00058\ }
\DoxyCodeLine{00059\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00060\ \ \ \ \ \textcolor{keyword}{struct\ }generic\_container\ \{}
\DoxyCodeLine{00061\ \ \ \ \ \ \ \ \ \textcolor{keyword}{virtual}\ \string~generic\_container()\ =\ \textcolor{keywordflow}{default};}
\DoxyCodeLine{00062\ \ \ \ \ \};}
\DoxyCodeLine{00063\ }
\DoxyCodeLine{00064\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00065\ \ \ \ \ \textcolor{keyword}{struct\ }concrete\_container\ :\ generic\_container\ \{}
\DoxyCodeLine{00066\ \ \ \ \ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}...\ Args>}
\DoxyCodeLine{00067\ \ \ \ \ \ \ \ \ concrete\_container(Args\&\&...\ args)\ :\ arr\{std::forward<Args>(args)...\}}
\DoxyCodeLine{00068\ \ \ \ \ \ \ \ \ \{\}}
\DoxyCodeLine{00069\ }
\DoxyCodeLine{00070\ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array<ValueType>}}\ arr;}
\DoxyCodeLine{00071\ \ \ \ \ \};}
\DoxyCodeLine{00072\ }
\DoxyCodeLine{00073\ \ \ \ \ std::unique\_ptr<generic\_container>\ data\_;}
\DoxyCodeLine{00074\ \};}
\DoxyCodeLine{00075\ }
\DoxyCodeLine{00076\ }
\DoxyCodeLine{00077\ \textcolor{keyword}{class\ }workspace\ \{}
\DoxyCodeLine{00078\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00079\ \ \ \ \ workspace(std::shared\_ptr<const\ Executor>\ exec)\ :\ exec\_\{std::move(exec)\}\ \{\}}
\DoxyCodeLine{00080\ }
\DoxyCodeLine{00081\ \ \ \ \ workspace(\textcolor{keyword}{const}\ workspace\&\ other)\ :\ workspace\{other.get\_executor()\}\ \{\}}
\DoxyCodeLine{00082\ }
\DoxyCodeLine{00083\ \ \ \ \ workspace(workspace\&\&\ other)\ :\ workspace\{other.get\_executor()\}}
\DoxyCodeLine{00084\ \ \ \ \ \{}
\DoxyCodeLine{00085\ \ \ \ \ \ \ \ \ other.clear();}
\DoxyCodeLine{00086\ \ \ \ \ \}}
\DoxyCodeLine{00087\ }
\DoxyCodeLine{00088\ \ \ \ \ workspace\&\ operator=(\textcolor{keyword}{const}\ workspace\&\ other)\ \{\ \textcolor{keywordflow}{return}\ *\textcolor{keyword}{this};\ \}}
\DoxyCodeLine{00089\ }
\DoxyCodeLine{00090\ \ \ \ \ workspace\&\ operator=(workspace\&\&\ other)}
\DoxyCodeLine{00091\ \ \ \ \ \{}
\DoxyCodeLine{00092\ \ \ \ \ \ \ \ \ other.clear();}
\DoxyCodeLine{00093\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ *\textcolor{keyword}{this};}
\DoxyCodeLine{00094\ \ \ \ \ \}}
\DoxyCodeLine{00095\ }
\DoxyCodeLine{00096\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ LinOpType,\ \textcolor{keyword}{typename}\ CreateOperation>}
\DoxyCodeLine{00097\ \ \ \ \ LinOpType*\ create\_or\_get\_op(\textcolor{keywordtype}{int}\ op\_id,\ CreateOperation\ create,}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ std::type\_info\&\ expected\_type,}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ dim<2>\ size,\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{size\_type}}\ stride)}
\DoxyCodeLine{00100\ \ \ \ \ \{}
\DoxyCodeLine{00101\ \ \ \ \ \ \ \ \ GKO\_ASSERT(op\_id\ >=\ 0\ \&\&\ op\_id\ <\ operators\_.size());}
\DoxyCodeLine{00102\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ does\ the\ existing\ object\ have\ the\ wrong\ type?}}
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ vector\ types\ may\ vary\ e.g.\ if\ users\ derive\ from\ Dense}}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ stored\_op\ =\ operators\_[op\_id].get();}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ LinOpType*\ op\{\};}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!stored\_op\ ||\ \textcolor{keyword}{typeid}(*stored\_op)\ !=\ expected\_type)\ \{}
\DoxyCodeLine{00107\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ new\_op\ =\ create();}
\DoxyCodeLine{00108\ \ \ \ \ \ \ \ \ \ \ \ \ op\ =\ new\_op.get();}
\DoxyCodeLine{00109\ \ \ \ \ \ \ \ \ \ \ \ \ operators\_[op\_id]\ =\ std::move(new\_op);}
\DoxyCodeLine{00110\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ op;}
\DoxyCodeLine{00111\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00112\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ does\ the\ existing\ object\ have\ the\ wrong\ dimensions?}}
\DoxyCodeLine{00113\ \ \ \ \ \ \ \ \ op\ =\ \textcolor{keyword}{dynamic\_cast<}LinOpType*\textcolor{keyword}{>}(operators\_[op\_id].get());}
\DoxyCodeLine{00114\ \ \ \ \ \ \ \ \ GKO\_ASSERT(op);}
\DoxyCodeLine{00115\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (op-\/>get\_size()\ !=\ size\ ||\ op-\/>get\_stride()\ !=\ stride)\ \{}
\DoxyCodeLine{00116\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\ new\_op\ =\ create();}
\DoxyCodeLine{00117\ \ \ \ \ \ \ \ \ \ \ \ \ op\ =\ new\_op.get();}
\DoxyCodeLine{00118\ \ \ \ \ \ \ \ \ \ \ \ \ operators\_[op\_id]\ =\ std::move(new\_op);}
\DoxyCodeLine{00119\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00120\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ op;}
\DoxyCodeLine{00121\ \ \ \ \ \}}
\DoxyCodeLine{00122\ }
\DoxyCodeLine{00123\ \ \ \ \ \textcolor{keyword}{const}\ LinOp*\ get\_op(\textcolor{keywordtype}{int}\ op\_id)\textcolor{keyword}{\ const}}
\DoxyCodeLine{00124\ \textcolor{keyword}{\ \ \ \ }\{}
\DoxyCodeLine{00125\ \ \ \ \ \ \ \ \ GKO\_ASSERT(op\_id\ >=\ 0\ \&\&\ op\_id\ <\ operators\_.size());}
\DoxyCodeLine{00126\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ operators\_[op\_id].get();}
\DoxyCodeLine{00127\ \ \ \ \ \}}
\DoxyCodeLine{00128\ }
\DoxyCodeLine{00129\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00130\ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array<ValueType>}}\&\ init\_or\_get\_array(\textcolor{keywordtype}{int}\ array\_id)}
\DoxyCodeLine{00131\ \ \ \ \ \{}
\DoxyCodeLine{00132\ \ \ \ \ \ \ \ \ GKO\_ASSERT(array\_id\ >=\ 0\ \&\&\ array\_id\ <\ arrays\_.size());}
\DoxyCodeLine{00133\ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\&\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}\ =\ arrays\_[array\_id];}
\DoxyCodeLine{00134\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}.empty())\ \{}
\DoxyCodeLine{00135\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\&\ result\ =}
\DoxyCodeLine{00136\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}.template\ init<ValueType>(this-\/>get\_executor(),\ 0);}
\DoxyCodeLine{00137\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ result;}
\DoxyCodeLine{00138\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00139\ \ \ \ \ \ \ \ \ \textcolor{comment}{//\ array\ types\ should\ not\ change!}}
\DoxyCodeLine{00140\ \ \ \ \ \ \ \ \ GKO\_ASSERT(\doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}.template\ contains<ValueType>());}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}.template\ get<ValueType>();}
\DoxyCodeLine{00142\ \ \ \ \ \}}
\DoxyCodeLine{00143\ }
\DoxyCodeLine{00144\ \ \ \ \ \textcolor{keyword}{template}\ <\textcolor{keyword}{typename}\ ValueType>}
\DoxyCodeLine{00145\ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array<ValueType>}}\&\ create\_or\_get\_array(\textcolor{keywordtype}{int}\ array\_id,\ \doxymbox{\hyperlink{namespacegko_a9579c98b1d85bf4ad02d7cb5b96fc771}{size\_type}}\ size)}
\DoxyCodeLine{00146\ \ \ \ \ \{}
\DoxyCodeLine{00147\ \ \ \ \ \ \ \ \ \textcolor{keyword}{auto}\&\ result\ =\ init\_or\_get\_array<ValueType>(array\_id);}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (result.get\_size()\ !=\ size)\ \{}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ \ \ \ \ result.resize\_and\_reset(size);}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00151\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ result;}
\DoxyCodeLine{00152\ \ \ \ \ \}}
\DoxyCodeLine{00153\ }
\DoxyCodeLine{00154\ \ \ \ \ std::shared\_ptr<const\ Executor>\ get\_executor()\textcolor{keyword}{\ const\ }\{\ \textcolor{keywordflow}{return}\ exec\_;\ \}}
\DoxyCodeLine{00155\ }
\DoxyCodeLine{00156\ \ \ \ \ \textcolor{keywordtype}{void}\ set\_size(\textcolor{keywordtype}{int}\ num\_operators,\ \textcolor{keywordtype}{int}\ num\_arrays)}
\DoxyCodeLine{00157\ \ \ \ \ \{}
\DoxyCodeLine{00158\ \ \ \ \ \ \ \ \ operators\_.resize(num\_operators);}
\DoxyCodeLine{00159\ \ \ \ \ \ \ \ \ arrays\_.resize(num\_arrays);}
\DoxyCodeLine{00160\ \ \ \ \ \}}
\DoxyCodeLine{00161\ }
\DoxyCodeLine{00162\ \ \ \ \ \textcolor{keywordtype}{void}\ clear()}
\DoxyCodeLine{00163\ \ \ \ \ \{}
\DoxyCodeLine{00164\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\&\ op\ :\ operators\_)\ \{}
\DoxyCodeLine{00165\ \ \ \ \ \ \ \ \ \ \ \ \ op.reset();}
\DoxyCodeLine{00166\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00167\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{for}\ (\textcolor{keyword}{auto}\&\ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}\ :\ arrays\_)\ \{}
\DoxyCodeLine{00168\ \ \ \ \ \ \ \ \ \ \ \ \ \doxymbox{\hyperlink{namespacegko_ae749a5ea11a93c1bcc9158d9a6e9fb68af1f713c9e000f5d3f280adbd124df4f5}{array}}.clear();}
\DoxyCodeLine{00169\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00170\ \ \ \ \ \}}
\DoxyCodeLine{00171\ }
\DoxyCodeLine{00172\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00173\ \ \ \ \ std::shared\_ptr<const\ Executor>\ exec\_;}
\DoxyCodeLine{00174\ \ \ \ \ std::vector<std::unique\_ptr<LinOp>>\ operators\_;}
\DoxyCodeLine{00175\ \ \ \ \ std::vector<any\_array>\ arrays\_;}
\DoxyCodeLine{00176\ \};}
\DoxyCodeLine{00177\ }
\DoxyCodeLine{00178\ }
\DoxyCodeLine{00179\ \}\ \ \textcolor{comment}{//\ namespace\ detail}}
\DoxyCodeLine{00180\ \}\ \ \textcolor{comment}{//\ namespace\ solver}}
\DoxyCodeLine{00181\ \}\ \ \textcolor{comment}{//\ namespace\ gko}}
\DoxyCodeLine{00182\ }
\DoxyCodeLine{00183\ \textcolor{preprocessor}{\#endif\ \ }\textcolor{comment}{//\ GKO\_PUBLIC\_CORE\_SOLVER\_WORKSPACE\_HPP\_}}

\end{DoxyCode}
