Loading...
Searching...
No Matches
LocalExecutor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#pragma once
4
5#include <algorithm>
6#include <concepts>
7#include <cstddef>
8#include <exception>
9#include <numeric>
10#include <vector>
11
12#if defined(USE_OPENMP)
13#include <omp.h>
14#endif
15
16#ifdef GPR_HAVE_OPENBLAS
17extern "C" int openblas_get_num_threads();
18extern "C" void openblas_set_num_threads(int);
19#endif
20
21#ifdef GPR_USE_STDPAR
22#include <tbb/task_arena.h>
23
24#include <execution>
25#endif
26
27namespace gpr::linalg {
28
32void configureKernelThreads(int threads);
33
38
43public:
45 explicit LocalExecutor(int concurrency);
46 [[nodiscard]] int concurrency() const noexcept
47 {
48 return concurrency_;
49 }
50 [[nodiscard]] static bool parallelAvailable() noexcept;
53 enum class Grain { Fine, Coarse };
54 template <class Operation>
55 requires std::invocable<Operation&, std::size_t>
56 void forEach(std::size_t count, Operation&& operation,
57 Grain grain = Grain::Fine) const
58 {
59 // Eight tasks per thread. A shorter count stays on the calling
60 // thread: a team of stripes lost to one thread on this host.
61 // A team that does run holds the vendor count at one so the
62 // stripes do not multiply it, then restores that count.
63 const std::size_t tasks_per_thread = grain == Grain::Coarse ? 1 : 8;
64 if (concurrency_ <= 1 ||
65 count < static_cast<std::size_t>(concurrency_) * tasks_per_thread) {
66 for (std::size_t index = 0; index < count; ++index)
67 operation(index);
68 return;
69 }
70#ifdef GPR_HAVE_OPENBLAS
71 const int saved_blas = openblas_get_num_threads();
72 if (saved_blas > 1) openblas_set_num_threads(1);
73#endif
74#ifdef GPR_USE_STDPAR
75 std::vector<std::size_t> indices(count);
76 std::iota(indices.begin(), indices.end(), std::size_t{0});
77 std::vector<std::exception_ptr> failures(count);
78 tbb::task_arena arena(concurrency_);
79 arena.execute([&] {
80 std::for_each(std::execution::par, indices.begin(), indices.end(),
81 [&](std::size_t index) noexcept {
82 try {
83 operation(index);
84 } catch (...) {
85 failures[index] = std::current_exception();
86 }
87 });
88 });
89 for (const auto& failure: failures)
90 if (failure) std::rethrow_exception(failure);
91#elif defined(USE_OPENMP)
92 std::vector<std::exception_ptr> failures(count);
93#pragma omp parallel for schedule(static) num_threads(concurrency_)
94 for (long index = 0; index < static_cast<long>(count); ++index) {
95 try {
96 operation(static_cast<std::size_t>(index));
97 } catch (...) {
98 failures[static_cast<std::size_t>(index)] =
99 std::current_exception();
100 }
101 }
102 for (const auto& failure: failures)
103 if (failure) std::rethrow_exception(failure);
104#else
105 for (std::size_t index = 0; index < count; ++index)
106 operation(index);
107#endif
108#ifdef GPR_HAVE_OPENBLAS
109 if (saved_blas > 1) openblas_set_num_threads(saved_blas);
110#endif
111 }
112
113private:
114 int concurrency_;
115};
116
117} // namespace gpr::linalg
int concurrency() const noexcept
static bool parallelAvailable() noexcept
void forEach(std::size_t count, Operation &&operation, Grain grain=Grain::Fine) const
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
void refreshKernelTeam()
void configureKernelThreads(int threads)