Commit f3fe5ac3 by FritzFlorian

Fix: use new id type in all tasks.

parent 2eb9674a
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <atomic> #include <atomic>
#include <pls/pls.h> #include <pls/pls.h>
#include <pls/internal/base/prohibit_new.h>
using namespace pls; using namespace pls;
...@@ -42,10 +43,8 @@ protected: ...@@ -42,10 +43,8 @@ protected:
int left_result; int left_result;
int right_result; int right_result;
fib left_child{num_ - 1, &left_result}; spawn_child(fib{num_ - 1, &left_result});
spawn_child(left_child); spawn_child(fib{num_ - 2, &right_result});
fib right_child{num_ - 2, &right_result};
spawn_child(right_child);
wait_for_all(); wait_for_all();
*result_ = left_result + right_result; *result_ = left_result + right_result;
...@@ -58,14 +57,14 @@ public: ...@@ -58,14 +57,14 @@ public:
int main() { int main() {
scheduler my_scheduler{&my_scheduler_memory, 1}; scheduler my_scheduler{&my_scheduler_memory, 4};
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
my_scheduler.perform_work([] (){ my_scheduler.perform_work([] (){
int result; int result;
fib fib_sub_task{45, &result}; fib fib_sub_task{45, &result};
tbb_task tbb_task{&fib_sub_task}; tbb_task tbb_task{&fib_sub_task, task_id{1}};
scheduler::execute_task(tbb_task); scheduler::execute_task(tbb_task);
std::cout << "Result: " << result << std::endl; std::cout << "Result: " << result << std::endl;
......
...@@ -10,23 +10,25 @@ namespace pls { ...@@ -10,23 +10,25 @@ namespace pls {
namespace internal { namespace internal {
namespace scheduling { namespace scheduling {
class abstract_task { class abstract_task {
public:
struct id { struct id {
uint32_t id_; uint32_t id_;
bool auto_generated_; bool auto_generated_;
explicit id(uint32_t id, bool auto_generated=true): id_{id}, auto_generated_{auto_generated} {}; explicit id(uint32_t id, bool auto_generated=false): id_{id}, auto_generated_{auto_generated} {};
bool operator==(const abstract_task::id& other) const { bool operator==(const abstract_task::id& other) const {
return id_ == other.id_ && auto_generated_ == other.auto_generated_; return id_ == other.id_ && auto_generated_ == other.auto_generated_;
} }
}; };
private:
int depth_; int depth_;
abstract_task::id unique_id_; abstract_task::id unique_id_;
abstract_task* child_task_; abstract_task* child_task_;
public: public:
abstract_task(int depth, abstract_task::id unique_id): abstract_task(const int depth, const abstract_task::id& unique_id):
depth_{depth}, depth_{depth},
unique_id_{unique_id}, unique_id_{unique_id},
child_task_{nullptr} {} child_task_{nullptr} {}
...@@ -37,6 +39,7 @@ namespace pls { ...@@ -37,6 +39,7 @@ namespace pls {
void set_depth(int depth) { depth_ = depth; } void set_depth(int depth) { depth_ = depth; }
int depth() const { return depth_; } int depth() const { return depth_; }
id unique_id() const { return unique_id_; }
protected: protected:
virtual bool internal_stealing(abstract_task* other_task) = 0; virtual bool internal_stealing(abstract_task* other_task) = 0;
virtual bool split_task(base::spin_lock* lock) = 0; virtual bool split_task(base::spin_lock* lock) = 0;
......
...@@ -19,7 +19,7 @@ namespace pls { ...@@ -19,7 +19,7 @@ namespace pls {
base::spin_lock finished_lock_; base::spin_lock finished_lock_;
public: public:
explicit root_task(Function function): explicit root_task(Function function):
abstract_task{0, 0}, abstract_task{0, id{0, true}},
function_{function}, function_{function},
finished_{false} {} finished_{false} {}
...@@ -51,7 +51,7 @@ namespace pls { ...@@ -51,7 +51,7 @@ namespace pls {
public: public:
explicit root_worker_task(root_task<Function>* master_task): explicit root_worker_task(root_task<Function>* master_task):
abstract_task{0, 0}, abstract_task{0, id{0, true}},
master_task_{master_task} {} master_task_{master_task} {}
void execute() override { void execute() override {
......
...@@ -37,7 +37,7 @@ namespace pls { ...@@ -37,7 +37,7 @@ namespace pls {
} }
public: public:
run_on_n_threads_task(Function function, int num_threads): run_on_n_threads_task(Function function, int num_threads):
abstract_task{PLS_UNIQUE_ID, 0}, abstract_task{0, id{PLS_UNIQUE_ID, true}},
function_{function}, function_{function},
counter{num_threads - 1} {} counter{num_threads - 1} {}
...@@ -66,7 +66,7 @@ namespace pls { ...@@ -66,7 +66,7 @@ namespace pls {
run_on_n_threads_task<Function>* root_; run_on_n_threads_task<Function>* root_;
public: public:
run_on_n_threads_task_worker(Function function, run_on_n_threads_task<Function>* root): run_on_n_threads_task_worker(Function function, run_on_n_threads_task<Function>* root):
abstract_task{PLS_UNIQUE_ID, 0}, abstract_task{0, id{PLS_UNIQUE_ID, true}},
function_{function}, function_{function},
root_{root} {} root_{root} {}
......
...@@ -70,9 +70,10 @@ namespace pls { ...@@ -70,9 +70,10 @@ namespace pls {
bool split_task(base::spin_lock* /*lock*/) override; bool split_task(base::spin_lock* /*lock*/) override;
public: public:
explicit tbb_task(tbb_sub_task* root_task): explicit tbb_task(tbb_sub_task* root_task, const abstract_task::id& id):
abstract_task{0, 0}, abstract_task{0, id},
root_task_{root_task}, root_task_{root_task},
my_stack_{nullptr},
deque_{}, deque_{},
last_stolen_{nullptr} {}; last_stolen_{nullptr} {};
......
#ifndef PLS_LIBRARY_H #ifndef PLS_LIBRARY_H
#define PLS_LIBRARY_H #define PLS_LIBRARY_H
#include <pls/internal/scheduling/scheduler.h> #include "pls/internal/scheduling/scheduler.h"
#include <pls/internal/scheduling/tbb_task.h> #include "pls/internal/scheduling/tbb_task.h"
#include "pls/internal/scheduling/abstract_task.h"
namespace pls { namespace pls {
using internal::scheduling::scheduler; using internal::scheduling::scheduler;
using internal::scheduling::static_scheduler_memory; using internal::scheduling::static_scheduler_memory;
using task_id = internal::scheduling::abstract_task::id;
using internal::scheduling::tbb_sub_task; using internal::scheduling::tbb_sub_task;
using internal::scheduling::tbb_task; using internal::scheduling::tbb_task;
......
...@@ -82,7 +82,7 @@ namespace pls { ...@@ -82,7 +82,7 @@ namespace pls {
if (stolen_sub_task == nullptr) { if (stolen_sub_task == nullptr) {
return false; return false;
} }
tbb_task task{stolen_sub_task}; tbb_task task{stolen_sub_task, this->unique_id()};
// In success case, unlock. // In success case, unlock.
// TODO: this locking is complicated and error prone. // TODO: this locking is complicated and error prone.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment