Loading...
Searching...
No Matches
TeamFactor.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#include "TeamFactor.h"
4
5#include <atomic>
6#include <chrono>
7
9#include "LinalgThreads.h"
10#include "TileParallel.h"
11
12namespace gpr::linalg {
13namespace {
14
15void runOnTilePool(void* context, std::ptrdiff_t count,
16 void (*item)(void*, std::ptrdiff_t), void* item_context)
17{
18 const int threads = *static_cast<const int*>(context);
19 TilePool::instance().runWith(threads, count, [&](long i) {
20 item(item_context, static_cast<std::ptrdiff_t>(i));
21 });
22}
23
24struct TeamRunner {
25 int threads = configuredLinalgThreads();
26 [[nodiscard]] portable_tiles::ParallelItems run() const noexcept
27 {
28 return threads > 1 ? runOnTilePool : nullptr;
29 }
30 [[nodiscard]] void* context() noexcept
31 {
32 return &threads;
33 }
34};
35
36// The diagonal block is 32 columns. Thirty-two is the smallest block the
37// reduction treats as a matrix update rather than a vector update. It is
38// not a cache size, a core count, or a training-grid order.
39constexpr int kAlgoBlock = 32;
40
41// Vendor BLAS already holds the requested count. A second OpenMP fork
42// around that work is not started. There is no flop cutoff and no saved
43// timing. The argument stays so callers can pass a count without a
44// second policy living in the caller.
45bool parallelPays(double flops)
46{
47 (void)flops;
48 return false;
49}
50
51double& solveWallSeconds()
52{
53 static double seconds = 0.0;
54 return seconds;
55}
56double& factorWallSeconds()
57{
58 static double seconds = 0.0;
59 return seconds;
60}
61// Serial dense solves can run inside a kernel team, so two outermost
62// scopes may add at once.
63void addWall(double& total, double seconds)
64{
65 std::atomic_ref<double>(total).fetch_add(seconds,
66 std::memory_order_relaxed);
67}
68int& wallDepth()
69{
70 thread_local int depth = 0;
71 return depth;
72}
73std::int64_t nowNanoseconds()
74{
75 return std::chrono::duration_cast<std::chrono::nanoseconds>(
76 std::chrono::steady_clock::now().time_since_epoch())
77 .count();
78}
79} // namespace
80
81std::int64_t teamPanelWidth()
82{
83 return kAlgoBlock;
84}
85
87{
88 // One Eigen product writes the solve. There is no panel width to vary.
89 return true;
90}
91
93{
94 return solveWallSeconds();
95}
96
98{
99 solveWallSeconds() = 0.0;
100}
101
102void teamAddSolveWall(double seconds)
103{
104 if (seconds > 0.0) addWall(solveWallSeconds(), seconds);
105}
106
108{
109 return factorWallSeconds();
110}
111
113{
114 factorWallSeconds() = 0.0;
115}
116
117bool teamParallelPays(double flops)
118{
119 return parallelPays(flops);
120}
121
123 : kind_(kind), outermost_(wallDepth()++ == 0), start_ns_(nowNanoseconds())
124{
125}
126
128{
129 --wallDepth();
130 if (!outermost_) return;
131 const double seconds =
132 1e-9 * static_cast<double>(nowNanoseconds() - start_ns_);
133 addWall(kind_ == Kind::Factor ? factorWallSeconds() : solveWallSeconds(),
134 seconds);
135}
136
137bool teamCholeskyLower(double* lower, std::int64_t lda, std::int64_t n)
138{
139 if (n < 0 || (n > 0 && (lower == nullptr || lda < n))) return false;
141 TeamRunner team;
142 return portable_tiles::factorLowerGeneral(lower, lda, n, team.run(),
143 team.context());
144}
145
146void teamSolveLower(const double* lower, std::int64_t lda, bool lower_factor,
147 double* rhs, std::int64_t ldb, std::int64_t n,
148 std::int64_t cols)
149{
150 if (n <= 0 || cols <= 0 || lower == nullptr || rhs == nullptr) return;
152 TeamRunner team;
153 portable_tiles::solveLeftGeneral(lower, lda, lower_factor, rhs, ldb, n,
154 cols, team.run(), team.context());
155}
156
157void teamSolveLowerVector(const double* lower, std::int64_t lda,
158 bool lower_factor, double* rhs, std::int64_t n)
159{
160 if (n <= 0 || lower == nullptr || rhs == nullptr) return;
162 portable_tiles::solveVectorGeneral(lower, lda, lower_factor, rhs, n);
163}
164
165void teamSymmetricGram(const double* b, std::int64_t ldb, double* c,
166 std::int64_t ldc, std::int64_t n)
167{
168 if (n <= 0 || b == nullptr || c == nullptr) return;
170 TeamRunner team;
171 portable_tiles::assignTransposeGram(b, ldb, c, ldc, n, team.run(),
172 team.context());
173}
174
175} // namespace gpr::linalg
KernelWall(Kind kind) noexcept
void runWith(int threads, long count, F &&fn)
run() with an explicit thread count in place of the configured one.
static TilePool & instance()
void(*)(void *context, std::ptrdiff_t count, void(*item)(void *, std::ptrdiff_t), void *item_context) ParallelItems
void solveLeftGeneral(const double *factor, std::ptrdiff_t factor_stride, bool lower_factor, double *rhs, std::ptrdiff_t rhs_stride, std::ptrdiff_t order, std::ptrdiff_t columns, ParallelItems run, void *run_context)
void solveVectorGeneral(const double *factor, std::ptrdiff_t factor_stride, bool lower_factor, double *rhs, std::ptrdiff_t order)
void assignTransposeGram(const double *b, std::ptrdiff_t b_stride, double *c, std::ptrdiff_t c_stride, std::ptrdiff_t order, ParallelItems run, void *run_context)
bool factorLowerGeneral(double *matrix, std::ptrdiff_t stride, std::ptrdiff_t order, ParallelItems run, void *run_context)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void teamFactorWallReset()
void teamSolveLowerVector(const double *lower, std::int64_t lda, bool lower_factor, double *rhs, std::int64_t n)
void teamSolveLower(const double *lower, std::int64_t lda, bool lower_factor, double *rhs, std::int64_t ldb, std::int64_t n, std::int64_t cols)
double teamFactorWallSeconds()
void teamSymmetricGram(const double *b, std::int64_t ldb, double *c, std::int64_t ldc, std::int64_t n)
double teamSolveWallSeconds()
bool teamParallelPays(double flops)
bool teamPanelBitsIndependent()
void teamSolveWallReset()
bool teamCholeskyLower(double *lower, std::int64_t lda, std::int64_t n)
int configuredLinalgThreads() noexcept
Requested dense numerical budget, independent of nested BLAS controls.
void teamAddSolveWall(double seconds)
std::int64_t teamPanelWidth()