The Gaudi Framework  master (bdbe7744)
Loading...
Searching...
No Matches
FiberManager Class Reference

The FiberManager manages a pool of threads used to run boost::fiber fibers. More...

#include </builds/gaudi/Gaudi/GaudiHive/src/FiberManager.h>

Collaboration diagram for FiberManager:

Public Member Functions

 FiberManager (int n_threads)
 FiberManager constructor.
 ~FiberManager ()
template<typename F>
void schedule (F &&func)
 Schedule work to run on the asynchronous pool.

Private Types

using SchedAlgo = boost::fibers::algo::shared_work

Private Attributes

boost::fibers::condition_variable m_shuttingDown_cv {}
boost::fibers::mutex m_shuttingDown_mtx {}
std::vector< std::jthread > m_threads {}
std::vector< SchedAlgo * > m_schedAlgoList {}

Detailed Description

The FiberManager manages a pool of threads used to run boost::fiber fibers.

These fibers can be suspended while waiting for GPU operations (with CUDA and HIP support built-in), remote I/O operations, and the like, providing a base for asynchronous algorithm support.

Author
Beojan Stanislaus
Version
1.0
Date
November 2023

Definition at line 32 of file FiberManager.h.

Member Typedef Documentation

◆ SchedAlgo

using FiberManager::SchedAlgo = boost::fibers::algo::shared_work
private

Definition at line 70 of file FiberManager.h.

Constructor & Destructor Documentation

◆ FiberManager()

FiberManager::FiberManager ( int n_threads)

FiberManager constructor.

Parameters
n_threadsNumber of threads for CPU portion of asynchronous algorithms. These are in addition to the TBB worker threads used for CPU algorithms.
Note
The calling thread is initialized with fiber scheduler and starts participating in sharing the global work queue of fibers, the current context becomes the main fiber of the calling thread. The main fiber should not be suspended, preemptive blocking is allowed. After construction, the calling thread must not be assigned different fiber scheduling algorithm.

Definition at line 18 of file FiberManager.cpp.

18 {
19 // Start a fiber scheduler with a shared work queue on the current thread.
20 // The fiber scheduler is configured not to sleep in case there are no ready fibers, but this situation should never
21 // happen as the main fiber of this thread should never be suspended so it's always active and scheduler will never
22 // try to pick-up another fiber.
23 boost::fibers::use_scheduling_algorithm<SchedAlgo>();
24
25 // Create a pool of threads running fiber schedulers with shared work queue.
26 // The scheduler in each thread is configured to sleep while no fibers are ready and needs to be manual
27 // notified if it's asleep when a new work item is added.
28 const auto sleep_while_idle = true;
29 m_schedAlgoList.resize( n_threads );
30 m_threads.reserve( n_threads );
31 for ( int i = 0; i < n_threads; ++i ) {
32 m_threads.emplace_back( [this, i]() {
33 // Save handle to the scheduler of this thread so it can be manually notified.
34 auto* algo_ptr = new SchedAlgo( sleep_while_idle );
35 m_schedAlgoList.at( i ) = algo_ptr;
36#if ( BOOST_VERSION >= 108400 )
37 boost::fibers::initialize_thread( algo_ptr,
38 boost::fibers::make_stack_allocator_wrapper<boost::fibers::default_stack>() );
39#else
40 boost::fibers::context::active()->get_scheduler()->set_algo( algo_ptr );
41#endif
42 // Suspend the main fiber of this worker so it doesn't exit prematurely.
43 std::unique_lock lck{ m_shuttingDown_mtx };
44 m_shuttingDown_cv.wait( lck );
45 } );
46 }
47}
boost::fibers::condition_variable m_shuttingDown_cv
boost::fibers::algo::shared_work SchedAlgo
std::vector< SchedAlgo * > m_schedAlgoList
boost::fibers::mutex m_shuttingDown_mtx
std::vector< std::jthread > m_threads

◆ ~FiberManager()

FiberManager::~FiberManager ( )

Definition at line 49 of file FiberManager.cpp.

49 {
50 // Notify the main fibers of the worker threads to exit.
51 m_shuttingDown_cv.notify_all();
52}

Member Function Documentation

◆ schedule()

template<typename F>
void FiberManager::schedule ( F && func)
inline

Schedule work to run on the asynchronous pool.

AsynchronousAlgorithms should use a relatively small amount of CPU time, and should use boost::fiber functionality to yield while waiting for offloaded work to complete.

Parameters
funcThe AlgTask, when used in AvalancheSchedulerSvc
Note
This function should only be called from a thread participating in the global work sharing of fibers, e.g. the thread that constructed the FiberManager.

Definition at line 62 of file FiberManager.h.

62 {
63 // Add a new fiber invoking the function to the shared work queue, then notify the workers in case they where
64 // sleeping.
65 boost::fibers::fiber( boost::fibers::launch::post, std::forward<F>( func ) ).detach();
66 for ( auto* p : m_schedAlgoList ) { p->notify(); }
67 }

Member Data Documentation

◆ m_schedAlgoList

std::vector<SchedAlgo*> FiberManager::m_schedAlgoList {}
private

Definition at line 74 of file FiberManager.h.

74{};

◆ m_shuttingDown_cv

boost::fibers::condition_variable FiberManager::m_shuttingDown_cv {}
private

Definition at line 71 of file FiberManager.h.

71{};

◆ m_shuttingDown_mtx

boost::fibers::mutex FiberManager::m_shuttingDown_mtx {}
private

Definition at line 72 of file FiberManager.h.

72{};

◆ m_threads

std::vector<std::jthread> FiberManager::m_threads {}
private

Definition at line 73 of file FiberManager.h.

73{};

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