From f55860b83e4503452d1bfa957588e3e9812593e1 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Thu, 7 Dec 2023 03:10:31 +0100 Subject: [PATCH] GraphProperty: replace redundant `task` member with address calculation through offsetof --- redGrapes/task/property/graph.cpp | 19 +++++++++++---- redGrapes/task/property/graph.hpp | 40 +++++++++++++------------------ redGrapes/task/task_space.cpp | 1 - 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/redGrapes/task/property/graph.cpp b/redGrapes/task/property/graph.cpp index e72714f5..e9197c2e 100644 --- a/redGrapes/task/property/graph.cpp +++ b/redGrapes/task/property/graph.cpp @@ -5,6 +5,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include #include #include @@ -19,6 +20,14 @@ namespace redGrapes { +Task * GraphProperty::get_task() +{ + /* NOTE: this offsetof gives works because we know that `space` is + * the first member that is derived from `GraphProperty` + */ + return (Task*) ( (uintptr_t)this - offsetof(Task, space) ); +} + /*! create a new (external) event which precedes the tasks post-event */ scheduler::EventPtr GraphProperty::make_event() @@ -37,7 +46,7 @@ scheduler::EventPtr GraphProperty::make_event() void GraphProperty::init_graph() { TRACE_EVENT("Graph", "init_graph"); - for( auto r = this->task->unique_resources.rbegin(); r != this->task->unique_resources.rend(); ++r ) + for( auto r = get_task()->unique_resources.rbegin(); r != get_task()->unique_resources.rend(); ++r ) { if( r->task_entry != r->resource->users.rend() ) { @@ -65,7 +74,7 @@ void GraphProperty::init_graph() if( preceding_task->space == this->space && - this->space->is_serial( *preceding_task, *this->task ) + this->space->is_serial( *preceding_task, *get_task() ) ) { add_dependency( *preceding_task ); @@ -87,7 +96,7 @@ void GraphProperty::init_graph() void GraphProperty::delete_from_resources() { TRACE_EVENT("Graph", "delete_from_resources"); - for( auto r = this->task->unique_resources.rbegin(); r != this->task->unique_resources.rend(); ++r ) + for( auto r = get_task()->unique_resources.rbegin(); r != get_task()->unique_resources.rend(); ++r ) { // TODO: can this lock be avoided? // corresponding lock to init_graph() @@ -105,7 +114,7 @@ void GraphProperty::add_dependency( Task & preceding_task ) // scheduling graph auto preceding_event = - SingletonContext::get().scheduler->task_dependency_type(preceding_task, *this->task) + SingletonContext::get().scheduler->task_dependency_type(preceding_task, *get_task()) ? preceding_task->get_pre_event() : preceding_task->get_post_event(); if( ! preceding_event->is_reached() ) @@ -122,7 +131,7 @@ void GraphProperty::update_graph( ) scheduler::EventPtr follower = *it; if( follower.task ) { - if( ! space->is_serial(*this->task, *follower.task) ) + if( ! space->is_serial(*get_task(), *follower.task) ) { // remove dependency //follower.task->in_edges.erase(std::find(std::begin(follower.task->in_edges), std::end(follower.task->in_edges), this)); diff --git a/redGrapes/task/property/graph.hpp b/redGrapes/task/property/graph.hpp index c11447ec..135b0884 100644 --- a/redGrapes/task/property/graph.hpp +++ b/redGrapes/task/property/graph.hpp @@ -48,54 +48,48 @@ struct TaskSpace; */ struct GraphProperty { - Task & operator*() - { - return *task; - } - Task * operator->() - { - return task; - } - //! task space that contains this task, must not be null memory::Refcounted< TaskSpace, TaskSpaceDeleter >::Guard space; //! task space for children, may be null 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 in_edges; - */ - + //! events of the scheduling-graph scheduler::Event pre_event; scheduler::Event post_event; scheduler::Event result_set_event; scheduler::Event result_get_event; - Task * task; - //! number of parents uint8_t scope_depth; + Task * get_task(); + + inline Task & operator*() + { + return *get_task(); + } + + inline Task * operator->() + { + return get_task(); + } + inline scheduler::EventPtr get_pre_event() { - return scheduler::EventPtr { nullptr, this->task, scheduler::T_EVT_PRE }; + return scheduler::EventPtr { nullptr, this->get_task(), scheduler::T_EVT_PRE }; } inline scheduler::EventPtr get_post_event() { - return scheduler::EventPtr { nullptr, this->task, scheduler::T_EVT_POST }; + return scheduler::EventPtr { nullptr, this->get_task(), scheduler::T_EVT_POST }; } inline scheduler::EventPtr get_result_set_event() { - return scheduler::EventPtr { nullptr, this->task, scheduler::T_EVT_RES_SET }; + return scheduler::EventPtr { nullptr, this->get_task(), scheduler::T_EVT_RES_SET }; } inline scheduler::EventPtr get_result_get_event() { - return scheduler::EventPtr { nullptr, this->task, scheduler::T_EVT_RES_GET }; + return scheduler::EventPtr { nullptr, this->get_task(), scheduler::T_EVT_RES_GET }; } inline bool is_ready() diff --git a/redGrapes/task/task_space.cpp b/redGrapes/task/task_space.cpp index 83cb4036..98c49a5f 100644 --- a/redGrapes/task/task_space.cpp +++ b/redGrapes/task/task_space.cpp @@ -74,7 +74,6 @@ namespace redGrapes { TRACE_EVENT("TaskSpace", "submit()"); task->space.acquire( this ); - task->task = task; ++ task_count;