MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
thread_pool.hpp File Reference
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <memory>
#include <mutex>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>
#include <mtl/detail/exceptions.hpp>

Classes

struct  mtl::parallel::pool_local_noexc::detail::pool_config
 What configure() asked for, and whether the pool already exists. More...
struct  mtl::parallel::pool_local_noexc::detail::region_thread
 This thread's region state (#640). More...
class  mtl::parallel::pool_local_noexc::detail::region_scope
 Marks the scope of a region tid on a thread that is not a pool worker, and restores the state after, so a region nested in another leaves the outer one's state set. More...
class  mtl::parallel::pool_local_noexc::thread_pool

Namespaces

namespace  mtl
namespace  mtl::parallel
namespace  mtl::parallel::pool_local_noexc
namespace  mtl::parallel::pool_local_noexc::detail
namespace  mtl::parallel::pool_local_noexc::pool_select_thread

Macros

#define MTL5_THREAD_POOL_SPIN_US   1000
 thread_pool::spin_us, in microseconds (#805; default 1000); 0 sleeps at once, as the pool did before.

Typedefs

using mtl::parallel::pool_local_noexc::pool_select_thread::default_pool = thread_pool
 The pool type MTL5's kernels run on: thread_pool, or spin_pool under MTL5_POOL_SPIN.

Enumerations

enum  : unsigned char { mtl::parallel::pool_local_noexc::detail::outside_region = 0 , mtl::parallel::pool_local_noexc::detail::inside_region = 1 , mtl::parallel::pool_local_noexc::detail::pool_worker = 2 }

Functions

unsigned mtl::parallel::pool_local_noexc::detail::clamp_pool_threads (unsigned long v)
 Clamp a requested thread count to [1, hardware concurrency].
bool mtl::parallel::pool_local_noexc::detail::env_unsigned (const char *name, unsigned &out)
 Read environment variable name as a whole unsigned decimal: digits only, no sign, space or trailing characters, at most 0xFFFFFFFF.
unsigned mtl::parallel::pool_local_noexc::detail::resolve_pool_threads ()
 Resolve the pool size from MTL5_NUM_THREADS (clamped to hardware concurrency); 1 when unset/invalid (see env_unsigned).
pool_config & mtl::parallel::pool_local_noexc::detail::config ()
region_thread & mtl::parallel::pool_local_noexc::detail::this_region_thread () noexcept
bool mtl::parallel::pool_local_noexc::detail::in_region () noexcept
 True while this thread runs a tid of an MTL5 region (see the contract at the top of this header).
bool mtl::parallel::pool_local_noexc::detail::on_pool_worker () noexcept
 True on the pool's worker threads, which are inside a region whenever they run anything at all.
unsigned mtl::parallel::pool_local_noexc::detail::claim_pool_size ()
unsigned mtl::parallel::pool_local_noexc::detail::team_for (std::size_t n, std::size_t grain, unsigned max_threads, unsigned pool_size) noexcept
 Team for a chunked loop over n units on a pool of pool_size: the pool, or max_threads if that is smaller and nonzero, cut so each chunk holds >= grain units; 1 (serial) when the pool is serial or n < 2 * grain.
template<typename Pool, typename Body>
void mtl::parallel::pool_local_noexc::detail::chunked_for (Pool &pool, std::size_t n, std::size_t grain, unsigned max_threads, Body &&body)
 parallel_for on pool; see thread_pool::parallel_for.
template<typename T, typename Pool, typename MapFn>
T mtl::parallel::pool_local_noexc::detail::chunked_reduce (Pool &pool, std::size_t n, std::size_t grain, unsigned max_threads, MapFn &&map)
 parallel_reduce on pool; see thread_pool::parallel_reduce.
template<typename Acc, typename Pool, typename InitFn, typename MapFn, typename CombineFn>
Acc mtl::parallel::pool_local_noexc::detail::chunked_accumulate (Pool &pool, std::size_t n, std::size_t grain, unsigned max_threads, InitFn &&init, MapFn &&map, CombineFn &&combine)
 parallel_accumulate on pool; see thread_pool::parallel_accumulate.
void mtl::parallel::pool_local_noexc::detail::cpu_relax () noexcept
 One spin-wait iteration: a pause hint to the core, nothing else.
void mtl::parallel::pool_local_noexc::detail::spin_step (unsigned i) noexcept
 One iteration of a spin wait: a pause, and every 64th iteration a yield, so a thread the scheduler has queued on this core behind the spinner runs within a few microseconds instead of after the whole spin.
default_pool & mtl::parallel::pool_local_noexc::pool_select_thread::pool ()
 The process-wide pool that MTL5's kernels run on.
bool mtl::parallel::pool_local_noexc::configure (unsigned threads)
 Size the process-wide pool from code, instead of MTL5_NUM_THREADS.

Macro Definition Documentation

◆ MTL5_THREAD_POOL_SPIN_US

#define MTL5_THREAD_POOL_SPIN_US   1000

thread_pool::spin_us, in microseconds (#805; default 1000); 0 sleeps at once, as the pool did before.

A compile-time setting: define it alike in every translation unit, as MTL5_POOL_SPIN.