Skip to content

Commit

Permalink
use memory::Refcounted for TaskSpace pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsippel committed Feb 1, 2024
1 parent a9c13f2 commit 299a6e6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
7 changes: 3 additions & 4 deletions redGrapes/redGrapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ namespace redGrapes
{
}

std::shared_ptr<TaskSpace> Context::current_task_space() const
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard Context::current_task_space() const
{
if(current_task)
{
if(!current_task->children)
{
auto task_space = std::make_shared<TaskSpace>(current_task);
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard task_space(new TaskSpace(current_task));
SPDLOG_TRACE("create child space = {}", (void*) task_space.get());
current_task->children = task_space;

Expand Down Expand Up @@ -120,7 +120,7 @@ namespace redGrapes
worker_pool = std::make_shared<dispatch::thread::WorkerPool>(hwloc_ctx, n_workers);
worker_pool->emplace_workers(n_workers);

root_space = std::make_shared<TaskSpace>();
root_space.acquire(new TaskSpace());
this->scheduler = scheduler;

worker_pool->start();
Expand Down Expand Up @@ -148,7 +148,6 @@ namespace redGrapes
worker_pool->stop();

scheduler.reset();
root_space.reset();

finalize_tracing();
}
Expand Down
7 changes: 4 additions & 3 deletions redGrapes/redGrapes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// #include <redGrapes/task/future.hpp>
#include <redGrapes/dispatch/thread/worker.hpp>
#include <redGrapes/memory/hwloc_alloc.hpp>
#include <redGrapes/memory/refcounted.hpp>
#include <redGrapes/task/task.hpp>
#include <redGrapes/task/task_space.hpp>

Expand Down Expand Up @@ -58,7 +59,7 @@ namespace redGrapes
std::optional<scheduler::EventPtr> create_event();

unsigned scope_depth() const;
std::shared_ptr<TaskSpace> current_task_space() const;
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard current_task_space() const;

void execute_task(Task& task);

Expand Down Expand Up @@ -92,7 +93,7 @@ namespace redGrapes
HwlocContext hwloc_ctx;
std::shared_ptr<dispatch::thread::WorkerPool> worker_pool;

std::shared_ptr<TaskSpace> root_space;
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard root_space;
std::shared_ptr<scheduler::IScheduler> scheduler;

#if REDGRAPES_ENABLE_TRACE
Expand Down Expand Up @@ -159,7 +160,7 @@ namespace redGrapes
return SingletonContext::get().scope_depth();
}

inline std::shared_ptr<TaskSpace> current_task_space()
inline memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard current_task_space()
{
return SingletonContext::get().current_task_space();
}
Expand Down
27 changes: 12 additions & 15 deletions redGrapes/task/property/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#pragma once

#include <redGrapes/scheduler/event.hpp>
#include <redGrapes/task/property/inherit.hpp>
#include <redGrapes/task/task_space.hpp>

#include <spdlog/spdlog.h>

#include <atomic>
Expand All @@ -16,16 +20,8 @@
#include <optional>
#include <shared_mutex>

// #include <redGrapes/task/task.hpp>
#include <redGrapes/scheduler/event.hpp>
#include <redGrapes/task/property/inherit.hpp>

namespace redGrapes
{

struct Task;
struct TaskSpace;

/*!
* Each task associates with two events:
* A Pre-Event and a Post-Event.
Expand Down Expand Up @@ -63,17 +59,18 @@ namespace redGrapes
uint8_t scope_depth;

//! task space that contains this task, must not be null
std::shared_ptr<TaskSpace> space;
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard space;

//! task space for children, may be null
std::shared_ptr<TaskSpace> children;
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard children;


/*
// in edges dont need a mutex because they are initialized
// once by `init_dependencies()` and only read afterwards.
// expired pointers (null) must be ignored
std::vector<Task*> in_edges;
*/
// in edges dont need a mutex because they are initialized
// once by `init_dependencies()` and only read afterwards.
// expired pointers (null) must be ignored
std::vector<Task*> in_edges;
*/

scheduler::Event pre_event;
scheduler::Event post_event;
Expand Down
4 changes: 2 additions & 2 deletions redGrapes/task/task_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ namespace redGrapes
using Impl = typename std::result_of<BindArgs(Callable, Args...)>::type;
using Result = typename std::result_of<Callable(Args...)>::type;

std::shared_ptr<TaskSpace> space;
memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard space;
FunTask<Impl>* task;

TaskBuilder(bool continuable, Callable&& f, Args&&... args)
: TaskProperties::Builder<TaskBuilder>(*this)
, space(current_task_space())
, space(SingletonContext::get().current_task_space())
{
// allocate
redGrapes::memory::Allocator alloc;
Expand Down
2 changes: 1 addition & 1 deletion redGrapes/task/task_space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace redGrapes
void TaskSpace::submit(Task* task)
{
TRACE_EVENT("TaskSpace", "submit()");
task->space = shared_from_this();
task->space.acquire(this);
task->task = task;

++task_count;
Expand Down
27 changes: 19 additions & 8 deletions redGrapes/task/task_space.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,37 @@
#include <redGrapes/dispatch/thread/cpuset.hpp>
#include <redGrapes/memory/allocator.hpp>
#include <redGrapes/task/queue.hpp>
#include <redGrapes/task/task.hpp>
// #include <redGrapes/task/task.hpp>
#include <redGrapes/memory/refcounted.hpp>

#include <atomic>
#include <mutex>
#include <shared_mutex>
#include <vector>

namespace redGrapes
{
struct Task;

struct TaskSpace;

struct TaskSpaceDeleter
{
void operator()(TaskSpace* space)
{
delete space;
}
};

/*! TaskSpace handles sub-taskspaces of child tasks
*/
struct TaskSpace : std::enable_shared_from_this<TaskSpace>
struct TaskSpace : memory::Refcounted<TaskSpace, TaskSpaceDeleter>
{
std::atomic<unsigned long> task_count;
std::vector<memory::Refcounted<TaskSpace, TaskSpaceDeleter>::Guard> active_child_spaces;
std::shared_mutex active_child_spaces_mutex;

unsigned depth;
std::atomic<unsigned long> task_count;
Task* parent;

std::shared_mutex active_child_spaces_mutex;
std::vector<std::shared_ptr<TaskSpace>> active_child_spaces;
unsigned depth;

virtual ~TaskSpace();

Expand Down

0 comments on commit 299a6e6

Please sign in to comment.