scheduler.h 4.54 KB
Newer Older
1 2 3 4

#ifndef PLS_SCHEDULER_H
#define PLS_SCHEDULER_H

5
#include <atomic>
6 7
#include <thread>
#include <vector>
8
#include <memory>
9 10

#include "pls/internal/base/barrier.h"
11 12
#include "pls/internal/base/stack_allocator.h"

13
#include "pls/internal/scheduling/thread_state.h"
14
#include "pls/internal/scheduling/task_manager.h"
15

16 17
#include "pls/internal/profiling/profiler.h"

18
namespace pls::internal::scheduling {
19 20 21 22 23
/**
 * The scheduler is the central part of the dispatching-framework.
 * It manages a pool of worker threads (creates, sleeps/wakes up, destroys)
 * and allows to execute parallel sections.
 *
24 25 26
 * It works in close relation with the 'task' and 'task_manager' class for scheduling.
 * The task_manager handles the data structure for stealing/resource trading,
 * the scheduler handles the high level execution flow (allowing the stealing implementation to be exchanged).
27
 */
28 29
class scheduler {
 public:
30 31
  /**
   * Initializes a scheduler instance with the given number of threads.
32 33 34 35 36 37
   * This will allocate ALL runtime resources, spawn the  worker threads
   * and put them to sleep, ready to process an upcoming parallel section.
   *
   * The initialization should be seen as a heavy and not very predictable operation.
   * After it is done the scheduler must (if configured correctly) never run out of resources
   * and deliver tight time bounds of randomized work-stealing.
38 39 40
   *
   * @param num_threads The number of worker threads to be created.
   */
41 42 43 44 45 46 47 48 49 50 51
  explicit scheduler(unsigned int num_threads,
                     size_t computation_depth,
                     size_t stack_size,
                     bool reuse_thread = true);

  template<typename ALLOC>
  explicit scheduler(unsigned int num_threads,
                     size_t computation_depth,
                     size_t stack_size,
                     bool reuse_thread,
                     ALLOC &&stack_allocator);
52 53 54

  /**
   * The scheduler is implicitly terminated as soon as it leaves the scope.
55
   * Resources follow a clean RAII style.
56
   */
57 58 59 60 61
  ~scheduler();

  /**
   * Wakes up the thread pool.
   * Code inside the Function lambda can invoke all parallel APIs.
62 63
   * This is meant to cleanly sleep and wake up the scheduler during an application run,
   * e.g. to run parallel code on a timer loop/after interrupts.
64 65 66 67 68 69
   *
   * @param work_section generic function or lambda to be executed in the scheduler's context.
   */
  template<typename Function>
  void perform_work(Function work_section);

70 71 72 73 74 75 76 77 78
  /**
   * Main parallelism construct, spawns a function for potential parallel execution.
   *
   * The result of the spawned function must not be relied on until sync() is called.
   * Best see the lambda as if executed on a thread, e.g. it can cause race conditions
   * and it is only finished after you join it back into the parent thread using sync().
   *
   * @param lambda the lambda to be executed in parallel.
   */
79
  template<typename Function>
80
  static void spawn(Function &&lambda);
81 82 83 84

  /**
   * Waits for all potentially parallel child tasks created with spawn(...).
   */
85
  static void sync();
86

87
  /**
88
   * Explicitly terminate the worker threads. Scheduler must not be used after this.
89
   */
90
  void terminate();
91

92
  [[nodiscard]] unsigned int num_threads() const { return num_threads_; }
93 94 95 96 97 98 99 100
  [[nodiscard]] static base_task &task_chain_at(unsigned int depth, thread_state &calling_state);

  static bool check_task_chain_forward(base_task &start_task);
  static bool check_task_chain_backward(base_task &start_task);
  static bool check_task_chain(base_task &start_task);

  thread_state &thread_state_for(unsigned int thread_id) { return *thread_states_[thread_id]; }
  task_manager &task_manager_for(unsigned int thread_id) { return *task_managers_[thread_id]; }
101

FritzFlorian committed
102 103 104 105 106 107
#if PLS_PROFILING_ENABLED
  profiling::profiler &get_profiler() {
    return profiler_;
  }
#endif

108
 private:
109 110
  static context_switcher::continuation slow_return(thread_state &calling_state);

111 112
  static void work_thread_main_loop();
  void work_thread_work_section();
113

114 115 116 117
  const unsigned int num_threads_;
  const bool reuse_thread_;
  base::barrier sync_barrier_;

118 119 120 121
  std::vector<std::thread> worker_threads_;
  std::vector<std::unique_ptr<task_manager>> task_managers_;
  std::vector<std::unique_ptr<thread_state>> thread_states_;

122 123 124 125 126 127 128 129
  class init_function;
  template<typename F>
  class init_function_impl;

  init_function *main_thread_starter_function_;
  std::atomic<bool> work_section_done_;

  bool terminated_;
130

131
  std::shared_ptr<base::stack_allocator> stack_allocator_;
132 133 134 135

#if PLS_PROFILING_ENABLED
  profiling::profiler profiler_;
#endif
136 137 138
};

}
139
#include "scheduler_impl.h"
140 141

#endif //PLS_SCHEDULER_H