scheduler.cpp 4.33 KB
Newer Older
1
#include "pls/internal/scheduling/scheduler.h"
2

3 4
#include "context_switcher/context_switcher.h"

5
#include "pls/internal/base/thread.h"
6
#include "pls/internal/base/error_handling.h"
7

8
namespace pls::internal::scheduling {
9

10 11 12 13 14 15 16 17
scheduler::scheduler(unsigned int num_threads,
                     size_t computation_depth,
                     size_t stack_size,
                     bool reuse_thread) : scheduler(num_threads,
                                                    computation_depth,
                                                    stack_size,
                                                    reuse_thread,
                                                    base::mmap_stack_allocator{}) {}
18 19 20 21 22


scheduler::~scheduler() {
  terminate();
}
23 24
void scheduler::work_thread_main_loop() {
  auto &scheduler = thread_state::get().get_scheduler();
25
  while (true) {
26
    // Wait to be triggered
27
    scheduler.sync_barrier_.wait();
28 29

    // Check for shutdown
30
    if (scheduler.terminated_) {
31 32 33
      return;
    }

34
    scheduler.work_thread_work_section();
35

36
    // Sync back with main thread
37 38 39 40 41 42
    scheduler.sync_barrier_.wait();
  }
}

void scheduler::work_thread_work_section() {
  auto &my_state = thread_state::get();
43
  auto &my_task_manager = my_state.get_task_manager();
44 45

  auto const num_threads = my_state.get_scheduler().num_threads();
46

47
  if (my_state.get_thread_id() == 0) {
48
    // Main Thread, kick off by executing the user's main code block.
49
    main_thread_starter_function_->run();
50
  }
51

52
  unsigned int failed_steals = 0;
53
  while (!work_section_done_) {
54 55
    PLS_ASSERT(my_task_manager.check_task_chain(), "Must start stealing with a clean task chain.");

56 57
    // TODO: move steal routine into separate function
    const size_t target = my_state.get_rand() % num_threads;
58
    if (target == my_state.get_thread_id()) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      continue;
    }

    auto &target_state = my_state.get_scheduler().thread_state_for(target);
    task *traded_task = target_state.get_task_manager().steal_task(my_task_manager);

    if (traded_task != nullptr) {
      // The stealing procedure correctly changed our chain and active task.
      // Now we need to perform the 'post steal' actions (manage resources and execute the stolen task).
      PLS_ASSERT(my_task_manager.check_task_chain_forward(&my_task_manager.get_active_task()),
                 "We are sole owner of this chain, it has to be valid!");

      // Move the traded in resource of this active task over to the stack of resources.
      auto *stolen_task = &my_task_manager.get_active_task();
      // Push the traded in resource on the resource stack to clear the traded_field for later steals/spawns.
      my_task_manager.push_resource_on_task(stolen_task, traded_task);

      auto optional_exchanged_task = external_trading_deque::get_trade_object(stolen_task);
      if (optional_exchanged_task) {
        // All good, we pushed the task over to the stack, nothing more to do
        PLS_ASSERT(*optional_exchanged_task == traded_task,
                   "We are currently executing this, no one else can put another task in this field!");
      } else {
        // The last other active thread took it as its spare resource...
        // ...remove our traded object from the stack again (it must be empty now and no one must access it anymore).
        auto current_root = stolen_task->resource_stack_root_.load();
        current_root.stamp++;
        current_root.value = 0;
        stolen_task->resource_stack_root_.store(current_root);
88
      }
89 90 91 92

      // Execute the stolen task by jumping to it's continuation.
      PLS_ASSERT(stolen_task->continuation_.valid(),
                 "A task that we can steal must have a valid continuation for us to start working.");
93
      stolen_task->is_synchronized_ = false;
94 95
      context_switcher::switch_context(std::move(stolen_task->continuation_));
      // We will continue execution in this line when we finished the stolen work.
96 97 98 99 100 101
      failed_steals = 0;
    } else {
      failed_steals++;
      if (failed_steals >= num_threads) {
        base::this_thread::yield();
      }
102 103
    }
  }
104 105
}

106
void scheduler::terminate() {
107 108 109 110 111 112 113
  if (terminated_) {
    return;
  }

  terminated_ = true;
  sync_barrier_.wait();

114 115 116
  for (unsigned int i = 0; i < num_threads_; i++) {
    if (reuse_thread_ && i == 0) {
      continue;
117
    }
118
    worker_threads_[i].join();
119 120 121
  }
}

122 123 124 125
void scheduler::sync() {
  thread_state::get().get_task_manager().sync();
}

126
}