Loading...
Searching...
No Matches
gpr::linalg::LocalExecutor Class Reference

#include <gpr/linalg/LocalExecutor.h>

Public Types

enum class  Grain { Fine , Coarse }

Public Member Functions

 LocalExecutor ()
 LocalExecutor (int concurrency)
int concurrency () const noexcept
template<class Operation>
requires std::invocable<Operation&, std::size_t>
void forEach (std::size_t count, Operation &&operation, Grain grain=Grain::Fine) const

Static Public Member Functions

static bool parallelAvailable () noexcept

Detailed Description

Bounded host execution of independent outputs, without MPI calls or numerical reductions. Each callback owns its output storage. Exceptions reach the calling thread after workers join, in increasing work-index order.

Definition at line 42 of file LocalExecutor.h.

Member Enumeration Documentation

◆ Grain

Fine work stays serial below eight tasks per thread. Coarse work is one directional tile, so a team runs from one task per thread.

Enumerator
Fine 
Coarse 

Definition at line 53 of file LocalExecutor.h.

53{ Fine, Coarse };

Constructor & Destructor Documentation

◆ LocalExecutor() [1/2]

gpr::linalg::LocalExecutor::LocalExecutor ( )

Definition at line 36 of file LocalExecutor.cpp.

37 : LocalExecutor(default_concurrency.load(std::memory_order_relaxed))
38{
39}

◆ LocalExecutor() [2/2]

gpr::linalg::LocalExecutor::LocalExecutor ( int concurrency)
explicit

Definition at line 41 of file LocalExecutor.cpp.

41 : concurrency_(concurrency)
42{
43 if (concurrency < 1 || (concurrency > 1 && !parallelAvailable()))
44 throw std::invalid_argument(
45 "kernel threads require a positive count and parallel STL support");
46}
int concurrency() const noexcept
static bool parallelAvailable() noexcept

Member Function Documentation

◆ concurrency()

int gpr::linalg::LocalExecutor::concurrency ( ) const
inlinenodiscardnoexcept

Definition at line 46 of file LocalExecutor.h.

47 {
48 return concurrency_;
49 }

◆ forEach()

template<class Operation>
requires std::invocable<Operation&, std::size_t>
void gpr::linalg::LocalExecutor::forEach ( std::size_t count,
Operation && operation,
Grain grain = Grain::Fine ) const
inline

Definition at line 56 of file LocalExecutor.h.

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 }

◆ parallelAvailable()

bool gpr::linalg::LocalExecutor::parallelAvailable ( )
staticnodiscardnoexcept

Definition at line 48 of file LocalExecutor.cpp.

49{
50#if defined(GPR_USE_STDPAR) || defined(USE_OPENMP)
51 return true;
52#else
53 return false;
54#endif
55}

The documentation for this class was generated from the following files: