From f902618f14a452f0cbae66f95c468b86ae8ea2cf Mon Sep 17 00:00:00 2001 From: alxbilger Date: Thu, 8 Apr 2021 16:45:30 +0200 Subject: [PATCH 1/4] [SofaSimulationCore] Move CpuTask class into its own file Also remove ThreadSpecificTask class, as it was not used --- .../modules/SofaSimulationCore/CMakeLists.txt | 2 + .../TaskSchedulerTestTasks.h | 2 +- .../src/sofa/simulation/InitTasks.cpp | 3 + .../src/sofa/simulation/InitTasks.h | 2 +- .../src/sofa/simulation/Task.cpp | 127 +++++++++--------- .../src/sofa/simulation/Task.h | 123 ++--------------- .../src/MultiThreading/AnimationLoopTasks.h | 2 +- .../src/MultiThreading/BeamLinearMapping_mt.h | 2 +- 8 files changed, 80 insertions(+), 183 deletions(-) diff --git a/SofaKernel/modules/SofaSimulationCore/CMakeLists.txt b/SofaKernel/modules/SofaSimulationCore/CMakeLists.txt index c99022c827b..e981a6ea7b6 100644 --- a/SofaKernel/modules/SofaSimulationCore/CMakeLists.txt +++ b/SofaKernel/modules/SofaSimulationCore/CMakeLists.txt @@ -18,6 +18,7 @@ set(HEADER_FILES ${SRC_ROOT}/CollisionEndEvent.h ${SRC_ROOT}/CollisionVisitor.h ${SRC_ROOT}/Colors.h + ${SRC_ROOT}/CpuTask.h ${SRC_ROOT}/DeactivatedNodeVisitor.h ${SRC_ROOT}/DefaultAnimationLoop.h ${SRC_ROOT}/DefaultVisualManagerLoop.h @@ -90,6 +91,7 @@ set(SOURCE_FILES ${SRC_ROOT}/CollisionBeginEvent.cpp ${SRC_ROOT}/CollisionEndEvent.cpp ${SRC_ROOT}/CollisionVisitor.cpp + ${SRC_ROOT}/CpuTask.cpp ${SRC_ROOT}/DeactivatedNodeVisitor.cpp ${SRC_ROOT}/DefaultAnimationLoop.cpp ${SRC_ROOT}/DefaultVisualManagerLoop.cpp diff --git a/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTestTasks.h b/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTestTasks.h index 03d1dfe81e3..a7b3901746c 100644 --- a/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTestTasks.h +++ b/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTestTasks.h @@ -1,4 +1,4 @@ -#include +#include namespace sofa diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.cpp b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.cpp index cb49bea722b..faed18fa471 100644 --- a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.cpp +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.cpp @@ -7,6 +7,9 @@ #include #include #include +#include + +#include namespace sofa { diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.h b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.h index 0335c3c7390..01eb371bb2d 100644 --- a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.h +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/InitTasks.h @@ -22,7 +22,7 @@ #ifndef InitTasks_h__ #define InitTasks_h__ -#include +#include namespace sofa { diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.cpp b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.cpp index 6a431947be4..0445dfa84f8 100644 --- a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.cpp +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.cpp @@ -1,73 +1,68 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ #include -#include -#include +namespace sofa::simulation +{ + Task::Allocator* Task::_allocator = nullptr; + Task::Task(int scheduledThread) + : m_scheduledThread(scheduledThread) + , m_id(0) + { + } -namespace sofa -{ - namespace simulation - { - - - Task::Allocator* Task::_allocator = nullptr; - - - Task::Task(int scheduledThread) - : m_scheduledThread(scheduledThread) - , m_id(0) - { - } - - Task::~Task() - { - } - - - CpuTask::CpuTask(CpuTask::Status* status, int scheduledThread) - : Task(scheduledThread) - , m_status(status) - { - } - - CpuTask::~CpuTask() - { - } - - - - - ThreadSpecificTask::ThreadSpecificTask(std::atomic* atomicCounter, std::mutex* mutex, CpuTask::Status* status ) - : CpuTask(status) - , m_atomicCounter(atomicCounter) - , m_threadSpecificMutex(mutex) - {} - - ThreadSpecificTask::~ThreadSpecificTask() - { - } - - Task::MemoryAlloc ThreadSpecificTask::run() - { - - runThreadSpecific(); - - { - std::lock_guard lock(*m_threadSpecificMutex); - runCriticalThreadSpecific(); - } - - m_atomicCounter->fetch_sub(1, std::memory_order_acq_rel); - - while(m_atomicCounter->load(std::memory_order_relaxed) > 0) - { - // yield while waiting - std::this_thread::yield(); - } - return Task::MemoryAlloc::Stack; - } + Task::~Task() + { + } + + void *Task::operator new(std::size_t sz) + { + return _allocator->allocate(sz); + } + + void Task::operator delete(void *ptr) + { + _allocator->free(ptr, 0); + } + + void Task::operator delete(void *ptr, std::size_t sz) + { + _allocator->free(ptr, sz); + } + + int Task::getScheduledThread() const + { + return m_scheduledThread; + } + + Task::Allocator *Task::getAllocator() + { + return _allocator; + } - - } // namespace simulation + void Task::setAllocator(Task::Allocator *allocator) + { + _allocator = allocator; + } } // namespace sofa diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.h b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.h index 08a9fba1a1b..1b81b606a7e 100644 --- a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.h +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.h @@ -19,8 +19,7 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ -#ifndef MultiThreadingTask_h__ -#define MultiThreadingTask_h__ +#pragma once #include @@ -28,12 +27,8 @@ #include -namespace sofa +namespace sofa::simulation { - namespace simulation - { - - /** Task class interface */ class SOFA_SIMULATION_CORE_API Task { @@ -76,23 +71,14 @@ namespace sofa virtual MemoryAlloc run() = 0; - static void* operator new (std::size_t sz) - { - return _allocator->allocate(sz); - } + static void* operator new (std::size_t sz); // when c++14 is available delete the void operator delete (void* ptr) // and define the void operator delete (void* ptr, std::size_t sz) - static void operator delete (void* ptr) - { - _allocator->free(ptr, 0); - } + static void operator delete (void* ptr); // only available in c++14. - static void operator delete (void* ptr, std::size_t sz) - { - _allocator->free(ptr, sz); - } + static void operator delete (void* ptr, std::size_t sz); // no array new and delete operators static void* operator new[](std::size_t sz) = delete; @@ -103,16 +89,14 @@ namespace sofa virtual Task::Status* getStatus(void) const = 0; - int getScheduledThread() const { return m_scheduledThread; } + int getScheduledThread() const; - static Task::Allocator* getAllocator() { return _allocator; } + static Task::Allocator* getAllocator(); - static void setAllocator(Task::Allocator* allocator) { _allocator = allocator; } + static void setAllocator(Task::Allocator* allocator); protected: - - // Task::Status* m_status; - + int m_scheduledThread; public: @@ -122,91 +106,4 @@ namespace sofa static Task::Allocator * _allocator; }; - - - - /** Base class to implement a CPU task - * all the tasks running on the CPU should inherits from this class - */ - class SOFA_SIMULATION_CORE_API CpuTask : public Task - { - public: - - /** CPU Task Status class definition: - * used to synchronize CPU tasks */ - class Status : public Task::Status - { - public: - Status() : m_busy(0) {} - - virtual bool isBusy() const override final - { - return (m_busy.load(std::memory_order_relaxed) > 0); - } - - virtual int setBusy(bool busy) override final - { - if (busy) - { - return m_busy.fetch_add(1, std::memory_order_relaxed); - } - else - { - return m_busy.fetch_sub(1, std::memory_order_relaxed); - } - } - - private: - std::atomic m_busy; - }; - - - virtual CpuTask::Status* getStatus(void) const override final { return m_status; } - - - public: - - CpuTask(CpuTask::Status* status, int scheduledThread = -1); - - virtual ~CpuTask(); - - private: - CpuTask::Status* m_status; - }; - - - - - // This task is called once by each thread used by the TasScheduler - // this is useful to initialize the thread specific variables - class SOFA_SIMULATION_CORE_API ThreadSpecificTask : public CpuTask - { - - public: - - ThreadSpecificTask(std::atomic* atomicCounter, std::mutex* mutex, CpuTask::Status* status); - - ~ThreadSpecificTask() override; - - MemoryAlloc run() final; - - - private: - - virtual bool runThreadSpecific() { return true; } - - virtual bool runCriticalThreadSpecific() { return true; } - - - std::atomic* m_atomicCounter; - std::mutex* m_threadSpecificMutex; - }; - - - } // namespace simulation - -} // namespace sofa - - - -#endif // MultiThreadingTask_h__ +} \ No newline at end of file diff --git a/applications/plugins/MultiThreading/src/MultiThreading/AnimationLoopTasks.h b/applications/plugins/MultiThreading/src/MultiThreading/AnimationLoopTasks.h index b46f0d17c44..8f9f56c8095 100644 --- a/applications/plugins/MultiThreading/src/MultiThreading/AnimationLoopTasks.h +++ b/applications/plugins/MultiThreading/src/MultiThreading/AnimationLoopTasks.h @@ -22,7 +22,7 @@ #ifndef AnimationLoopTasks_h__ #define AnimationLoopTasks_h__ -#include +#include namespace sofa diff --git a/applications/plugins/MultiThreading/src/MultiThreading/BeamLinearMapping_mt.h b/applications/plugins/MultiThreading/src/MultiThreading/BeamLinearMapping_mt.h index 0939257b6ca..5cf1d056fa4 100644 --- a/applications/plugins/MultiThreading/src/MultiThreading/BeamLinearMapping_mt.h +++ b/applications/plugins/MultiThreading/src/MultiThreading/BeamLinearMapping_mt.h @@ -26,7 +26,7 @@ #include -#include +#include From c1c9490185c393b004aad2c59eb3698e5c179e3b Mon Sep 17 00:00:00 2001 From: alxbilger Date: Thu, 8 Apr 2021 16:48:03 +0200 Subject: [PATCH 2/4] [SofaSimulationCore] forgot added files --- .../src/sofa/simulation/CpuTask.cpp | 60 +++++++++++++++++ .../src/sofa/simulation/CpuTask.h | 66 +++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp create mode 100644 SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp new file mode 100644 index 00000000000..7552cf098e8 --- /dev/null +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include + +#include + + +namespace sofa::simulation +{ + CpuTask::CpuTask(CpuTask::Status* status, int scheduledThread) + : Task(scheduledThread) + , m_status(status) + { + } + + CpuTask::~CpuTask() + { + } + + CpuTask::Status *CpuTask::getStatus(void) const + { + return m_status; + } + +bool CpuTask::Status::isBusy() const + { + return (m_busy.load(std::memory_order_relaxed) > 0); + } + + int CpuTask::Status::setBusy(bool busy) + { + if (busy) + { + return m_busy.fetch_add(1, std::memory_order_relaxed); + } + else + { + return m_busy.fetch_sub(1, std::memory_order_relaxed); + } + } +} diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h new file mode 100644 index 00000000000..f7abd39fa55 --- /dev/null +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h @@ -0,0 +1,66 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include + +#include + +namespace sofa::simulation +{ + /** Base class to implement a CPU task + * all the tasks running on the CPU should inherits from this class + */ + class SOFA_SIMULATION_CORE_API CpuTask : public Task + { + public: + + /** CPU Task Status class definition: + * used to synchronize CPU tasks */ + class Status : public Task::Status + { + public: + Status() : m_busy(0) {} + + bool isBusy() const override final; + + int setBusy(bool busy) override final; + + private: + std::atomic m_busy; + }; + + + CpuTask::Status* getStatus(void) const override final; + + + public: + + CpuTask(CpuTask::Status* status, int scheduledThread = -1); + + virtual ~CpuTask(); + + private: + CpuTask::Status* m_status; + }; + +} From 5be7e736cd2111c1248549688e9a8ac1be92ed40 Mon Sep 17 00:00:00 2001 From: alxbilger Date: Thu, 8 Apr 2021 17:06:39 +0200 Subject: [PATCH 3/4] Require extra include --- .../SofaSimulationCore_test/TaskSchedulerTests.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTests.cpp b/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTests.cpp index bc0b0f6b827..21e9bd598d0 100644 --- a/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTests.cpp +++ b/SofaKernel/modules/SofaSimulationCore/SofaSimulationCore_test/TaskSchedulerTests.cpp @@ -1,6 +1,7 @@ #include "TaskSchedulerTestTasks.h" #include +#include #include #include From 9f578b54099862aba533ca69d98b3d10d608f55d Mon Sep 17 00:00:00 2001 From: alxbilger Date: Fri, 9 Apr 2021 09:20:52 +0200 Subject: [PATCH 4/4] Missing export for Windows --- .../modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp | 2 +- .../modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp index 7552cf098e8..423e67fb1a6 100644 --- a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.cpp @@ -41,7 +41,7 @@ namespace sofa::simulation return m_status; } -bool CpuTask::Status::isBusy() const + bool CpuTask::Status::isBusy() const { return (m_busy.load(std::memory_order_relaxed) > 0); } diff --git a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h index f7abd39fa55..d3a123d6035 100644 --- a/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h +++ b/SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/CpuTask.h @@ -36,7 +36,7 @@ namespace sofa::simulation /** CPU Task Status class definition: * used to synchronize CPU tasks */ - class Status : public Task::Status + class SOFA_SIMULATION_CORE_API Status : public Task::Status { public: Status() : m_busy(0) {}