-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Binding to setup task scheduler (nb threads) (#366)
* Binding to setup task scheduler (nb threads) * Update examples/taskScheduler.py Co-authored-by: Hugo <hugo.talbot@sofa-framework.org> --------- Co-authored-by: Hugo <hugo.talbot@sofa-framework.org>
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_TaskScheduler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/****************************************************************************** | ||
* SofaPython3 plugin * | ||
* (c) 2021 CNRS, University of Lille, INRIA * | ||
* * | ||
* 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 <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Contact information: contact@sofa-framework.org * | ||
******************************************************************************/ | ||
|
||
#include <SofaPython3/Sofa/Core/Binding_TaskScheduler.h> | ||
#include <sofa/simulation/MainTaskSchedulerFactory.h> | ||
#include <sofa/simulation/TaskScheduler.h> | ||
#include <SofaPython3/Sofa/Core/Binding_TaskScheduler_doc.h> | ||
|
||
|
||
/// Makes an alias for the pybind11 namespace to increase readability. | ||
namespace py { using namespace pybind11; } | ||
|
||
namespace sofapython3 | ||
{ | ||
py::module moduleAddTaskScheduler(py::module& m) | ||
{ | ||
py::module taskSchedulerModule = m.def_submodule("TaskScheduler"); | ||
|
||
taskSchedulerModule.doc() = R"doc( | ||
TaskScheduler | ||
----------------------- | ||
Configuration of the main task scheduler. | ||
)doc"; | ||
|
||
|
||
taskSchedulerModule.def("init", [](const unsigned int nbThreads) | ||
{ | ||
auto* const taskScheduler = sofa::simulation::MainTaskSchedulerFactory::createInRegistry(); | ||
assert(taskScheduler); | ||
taskScheduler->init(nbThreads); | ||
}, sofapython3::doc::taskscheduler::init); | ||
|
||
taskSchedulerModule.def("getThreadCount", []() | ||
{ | ||
const auto* const taskScheduler = sofa::simulation::MainTaskSchedulerFactory::createInRegistry(); | ||
assert(taskScheduler); | ||
return taskScheduler->getThreadCount(); | ||
}, sofapython3::doc::taskscheduler::getThreadCount); | ||
|
||
taskSchedulerModule.def("stop", []() | ||
{ | ||
auto* const taskScheduler = sofa::simulation::MainTaskSchedulerFactory::createInRegistry(); | ||
assert(taskScheduler); | ||
return taskScheduler->stop(); | ||
}); | ||
|
||
taskSchedulerModule.def("GetHardwareThreadsCount", []() | ||
{ | ||
return sofa::simulation::TaskScheduler::GetHardwareThreadsCount(); | ||
}, sofapython3::doc::taskscheduler::GetHardwareThreadsCount); | ||
|
||
return taskSchedulerModule; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_TaskScheduler.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/****************************************************************************** | ||
* SofaPython3 plugin * | ||
* (c) 2021 CNRS, University of Lille, INRIA * | ||
* * | ||
* 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 <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Contact information: contact@sofa-framework.org * | ||
******************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace sofapython3 { | ||
|
||
pybind11::module moduleAddTaskScheduler(pybind11::module& m); | ||
|
||
} /// namespace sofapython3 |
40 changes: 40 additions & 0 deletions
40
bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_TaskScheduler_doc.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/****************************************************************************** | ||
* SofaPython3 plugin * | ||
* (c) 2021 CNRS, University of Lille, INRIA * | ||
* * | ||
* 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 <http://www.gnu.org/licenses/>. * | ||
******************************************************************************* | ||
* Contact information: contact@sofa-framework.org * | ||
******************************************************************************/ | ||
|
||
#pragma once | ||
|
||
namespace sofapython3::doc::taskscheduler | ||
{ | ||
|
||
static auto init = | ||
R"( | ||
Initialize the main task scheduler with a number of threads. | ||
)"; | ||
|
||
static auto getThreadCount = | ||
R"( | ||
Returns the number of threads used by the main task scheduler. | ||
)"; | ||
|
||
static auto GetHardwareThreadsCount = | ||
R"( | ||
Assuming 2 concurrent threads by CPU core, return the number of CPU cores on the system | ||
)"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Sofa | ||
from Sofa.Core import TaskScheduler | ||
import time | ||
|
||
def createScene(root): | ||
root.dt = 0.01 | ||
|
||
root.addObject("RequiredPlugin", pluginName=[ | ||
"MultiThreading", | ||
"Sofa.Component.Constraint.Projective", | ||
"Sofa.Component.Engine.Select", | ||
"Sofa.Component.LinearSolver.Iterative", | ||
"Sofa.Component.Mass", | ||
"Sofa.Component.ODESolver.Backward", | ||
"Sofa.Component.StateContainer", | ||
"Sofa.Component.Topology.Container.Grid", | ||
"Sofa.Component.Visual", | ||
"Sofa.Component.Topology.Container.Dynamic", | ||
]) | ||
root.addObject('VisualStyle', displayFlags="showForceFields") | ||
root.addObject('DefaultAnimationLoop') | ||
root.addObject('EulerImplicitSolver', rayleighStiffness=0.1, rayleighMass=0.1) | ||
root.addObject('CGLinearSolver', iterations=25, tolerance=1.0e-9, threshold=1.0e-9) | ||
root.addObject('MechanicalObject', template="Vec3") | ||
root.addObject('UniformMass', vertexMass=1) | ||
root.addObject('RegularGridTopology', nx=8, ny=8, nz=40, xmin=-1.5, xmax=1.5, ymin=-1.5, ymax=1.5, zmin=0, zmax=19) | ||
root.addObject('BoxROI', box=[-1.5, -1.5, 0, 1.5, 1.5, 0.0001], name="box") | ||
root.addObject('FixedConstraint', indices="@box.indices") | ||
root.addObject('ParallelHexahedronFEMForceField', youngModulus=400000, poissonRatio=0.4, method="large", updateStiffnessMatrix=False) | ||
|
||
|
||
# When not using runSofa, this main function will be called python | ||
def main(): | ||
root = Sofa.Core.Node("root") | ||
createScene(root) | ||
Sofa.Simulation.init(root) | ||
|
||
for nb_threads in range(1, TaskScheduler.GetHardwareThreadsCount() + 1): | ||
print(f'Number of threads #{nb_threads}') | ||
Sofa.Simulation.reset(root) | ||
TaskScheduler.init(nb_threads) | ||
|
||
start = time.time() | ||
for iteration in range(500): | ||
Sofa.Simulation.animate(root, root.dt.value) | ||
end = time.time() | ||
print("500 iterations computed in " + str(end - start) + " seconds") | ||
|
||
print("Stopping...") | ||
TaskScheduler.stop() | ||
print("Stopped") | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
main() |