Loading...
Searching...
No Matches
BackendInterface.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
46
47#ifndef GPR_LINALG_BACKEND_INTERFACE_H_
48#define GPR_LINALG_BACKEND_INTERFACE_H_
49
50#include <cstddef>
51#include <vector>
52
54
55namespace gpr {
56namespace linalg {
57
59
63
66
67[[nodiscard]] constexpr bool hostAccessible(MemoryKind kind) noexcept
68{
69 return kind == MemoryKind::Host;
70}
71
72[[nodiscard]] constexpr const char* memoryKindName(MemoryKind kind) noexcept
73{
74 switch (kind) {
76 return "host";
78 return "cuda";
80 return "hip";
82 return "sycl";
83 }
84 return "unknown";
85}
86
87[[nodiscard]] constexpr const char* executionKindName(
88 ExecutionKind kind) noexcept
89{
90 switch (kind) {
92 return "serial";
94 return "host-threads";
96 return "cuda";
98 return "hip";
100 return "sycl";
101 }
102 return "unknown";
103}
104
113
114[[nodiscard]] constexpr const char* arithmeticClassName(
115 ArithmeticClass cls) noexcept
116{
117 switch (cls) {
119 return "serial-equal";
121 return "grid-invariant";
123 return "unconstrained";
124 }
125 return "unknown";
126}
127
129[[nodiscard]] constexpr bool satisfies(ArithmeticClass provided,
130 ArithmeticClass required) noexcept
131{
132 return static_cast<int>(provided) >= static_cast<int>(required);
133}
134
140 int workers = 1;
141 int nested_blas = 1;
142
143 [[nodiscard]] constexpr int total() const noexcept
144 {
145 return workers * nested_blas;
146 }
147 [[nodiscard]] constexpr bool fits(int cpu_share) const noexcept
148 {
149 return workers >= 1 && nested_blas >= 1 && total() <= cpu_share;
150 }
151};
152
157 int tile = 0;
158 int grid_rows = 1;
159 int grid_cols = 1;
160
161 [[nodiscard]] constexpr int ranks() const noexcept
162 {
163 return grid_rows * grid_cols;
164 }
165};
166
167namespace backend {
168struct EigenTag { };
169struct CholmodTag { };
170struct StdParTag { };
171struct KokkosTag { };
172struct CusolverTag { };
173struct ScalapackTag { };
174struct SlateTag { };
175} // namespace backend
176
193template <class Backend>
195
196template <>
197struct BackendTraits<backend::EigenTag> {
199 using vector_type = Eigen::VectorXd;
200 static constexpr const char* name = "eigen";
204 static constexpr bool resident = true;
205 static constexpr bool retains_factor = false;
206 static constexpr bool distributed = false;
207};
208
209template <>
210struct BackendTraits<backend::CholmodTag> {
212 using vector_type = Eigen::VectorXd;
213 static constexpr const char* name = "cholmod";
216 static constexpr ArithmeticClass arithmetic =
218 static constexpr bool resident = true;
219 static constexpr bool retains_factor = false;
220 static constexpr bool distributed = false;
221};
222
223template <>
224struct BackendTraits<backend::StdParTag> {
226 using vector_type = Eigen::VectorXd;
227 static constexpr const char* name = "stdpar";
230 static constexpr ArithmeticClass arithmetic =
232 static constexpr bool resident = true;
233 static constexpr bool retains_factor = false;
234 static constexpr bool distributed = false;
235};
236
237template <>
238struct BackendTraits<backend::CusolverTag> {
240 using vector_type = Eigen::VectorXd;
241 static constexpr const char* name = "cusolver";
244 static constexpr ArithmeticClass arithmetic =
246 static constexpr bool resident = false;
247 static constexpr bool retains_factor = false;
248 static constexpr bool distributed = false;
249};
250
254template <>
255struct BackendTraits<backend::ScalapackTag> {
257 using vector_type = Eigen::VectorXd;
258 static constexpr const char* name = "scalapack";
261 static constexpr ArithmeticClass arithmetic =
263 static constexpr bool resident = false;
264 static constexpr bool retains_factor = false;
265 static constexpr bool distributed = true;
266};
267
268template <>
269struct BackendTraits<backend::SlateTag> {
271 using vector_type = Eigen::VectorXd;
272 static constexpr const char* name = "slate";
275 static constexpr ArithmeticClass arithmetic =
277 static constexpr bool resident = false;
278 static constexpr bool retains_factor = false;
279 static constexpr bool distributed = true;
280};
281
284struct TileView {
285 double* data = nullptr;
286 const int* desc = nullptr;
288};
289
291 const double* data = nullptr;
292 const int* desc = nullptr;
294};
295
304
317public:
318 virtual ~DistributedKernelProvider() = default;
319
320 [[nodiscard]] virtual ProviderTraits traits() const noexcept = 0;
321
323 [[nodiscard]] virtual ThreadBudget threadBudget(
324 int cpu_share) const noexcept = 0;
325
327 virtual bool potrf_lower(TileView a, int n) = 0;
328
331 virtual bool potri_lower(TileView a, int n) = 0;
332
334 virtual bool trsm_left_lower(ConstTileView l, int n, TileView b, int nrhs,
335 bool transpose) = 0;
336
339 virtual bool heev_lower(TileView a, int n, TileView z,
340 std::vector<double>& eigenvalues) = 0;
341
345 int cached_n) = 0;
346
348 virtual bool gram_lower(TileView b, int rows, int columns,
349 TileView product) = 0;
350};
351
352} // namespace linalg
353} // namespace gpr
354
355#endif // GPR_LINALG_BACKEND_INTERFACE_H_
@ z
Definition Enums.h:28
virtual ~DistributedKernelProvider()=default
virtual bool rank_block_cholesky_append_lower(TileView a, int n, int cached_n)=0
virtual bool trsm_left_lower(ConstTileView l, int n, TileView b, int nrhs, bool transpose)=0
Solve L X = B (or L^T X = B when transpose) in place on b.
virtual bool potri_lower(TileView a, int n)=0
virtual ThreadBudget threadBudget(int cpu_share) const noexcept=0
Threads this provider spends on one rank, within cpu_share.
virtual bool gram_lower(TileView b, int rows, int columns, TileView product)=0
Lower triangle of B^T B for the rows x columns block b.
virtual bool heev_lower(TileView a, int n, TileView z, std::vector< double > &eigenvalues)=0
virtual ProviderTraits traits() const noexcept=0
virtual bool potrf_lower(TileView a, int n)=0
Cholesky factor of the order-n lower triangle, in place.
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
constexpr bool satisfies(ArithmeticClass provided, ArithmeticClass required) noexcept
True when a provider of class provided meets the class required.
constexpr const char * executionKindName(ExecutionKind kind) noexcept
constexpr const char * memoryKindName(MemoryKind kind) noexcept
ExecutionKind
Which units run the operation.
constexpr const char * arithmeticClassName(ArithmeticClass cls) noexcept
constexpr bool hostAccessible(MemoryKind kind) noexcept
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
STL namespace.
static constexpr ArithmeticClass arithmetic
static constexpr ArithmeticClass arithmetic
constexpr int ranks() const noexcept
Description of a distributed kernel provider.
bool resident
Operands in memory are consumed without staging through host.
constexpr int total() const noexcept
constexpr bool fits(int cpu_share) const noexcept