Home › Forum › SOFA › Using SOFA › [SOLVED] Multithreading in Python
Tagged: 64_bits, Linux_ubuntu, Multithreading, Plugin_Multithreading, SOFA_1812
- This topic has 8 replies, 4 voices, and was last updated 4 years, 8 months ago by JieYing.
-
AuthorPosts
-
24 June 2019 at 19:54 #13853JieYingBlocked
Hi,
Does anyone have experience running multithreading in Python. I tried to add the following
rootNode.createObject('AnimationLoopParallelScheduler', threadNumber='0')
and this gives a bunch of errors as
[ERROR] [ExecParams] [THREAD 2]: invalid ExecParams used, belonging to thread 3
and
[ERROR] [MechanicalObject(spring)] Invalid vOp operation 1 (null(V_DERIV),0(0),0(0),1)
From the .scn examples, it didn’t seem like additional setup was necessary to run multithreaded, but is there something different to do in Python?
Thanks,
Jie Ying28 June 2019 at 12:24 #13867HugoKeymasterHi @jieying
I am sorry, I never used MT in Python.
Maybe some others did : @damien-marchaluniv-lille1-fr @jnbrunet @bmarques ?Best,
Hugo
28 June 2019 at 18:39 #13869jnbrunetModeratorAre you sure it is a python-related error? The same scene with a .scn file works?
7 July 2019 at 18:10 #13915JieYingBlockedYes – I simplified my scene so it’s only two objects, and now it shows a different error. Adding the DataExchange line breaks the simulation in Python, with the error that it is invalid syntax, but the same scene does what I’d expect as a .scn file.
rootNode.createObject('DataExchange', name='exchangeData1', template='vector<float>', from='@FloorScene/Floor/mecha.position', to='@CylinderScene/Cylinder/Cyl.position')
9 July 2019 at 16:26 #139219 July 2019 at 16:40 #13923JieYingBlockedHi Hugo,
My messages to post full scenes keeps on being flagged as spam but I’ve uploaded my code to https://github.com/JieYingWu/sofa_dvrk/blob/master/multithread.py and the scene file i s at the corresponding https://github.com/JieYingWu/sofa_dvrk/blob/master/multithread.scn.
Thanks,
Jie Ying16 December 2019 at 07:18 #14696walemarkBlockedThe Python Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter. All the GIL does is make sure only one thread is executing Python code at a time; control still switches between threads. What the GIL prevents then, is making use of more than one CPU core or separate CPUs to run threads in parallel.
Python threading is great for creating a responsive GUI, or for handling multiple short web requests where I/O is the bottleneck more than the Python code. It is not suitable for parallelizing computationally intensive Python code, stick to the multiprocessing module for such tasks or delegate to a dedicated external library. For actual parallelization in Python, you should use the multiprocessing module to fork multiple processes that execute in parallel (due to the global interpreter lock, Python threads provide interleaving, but they are in fact executed serially, not in parallel, and are only useful when interleaving I/O operations). However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.
21 January 2020 at 17:08 #15123HugoKeymasterHi @jieying
Sorry for the late reply!
I tested your scene multithreading.scn it seems to run.
Here is the simulation:
and no error is given.
Do you still have the error?
Could you copy paste the console output?
Could you tell us your SOFA versions?Below the scene slightly modified:
<?xml version="1.0"?> <Node name="root" gravity="0 -50 0" dt="0.001"> <RequiredPlugin pluginName="MultiThreading" /> <AnimationLoopParallelScheduler name="mainLoop" threadNumber="0" /> <VisualStyle displayFlags="hideBehaviorModels hideVisualModels showCollisionModels showInteractionForcefields" /> <FreeMotionAnimationLoop solveVelocityConstraintFirst="0"/> <LCPConstraintSolver maxIt="1000" tolerance="1e-6" mu="0.9"/> <DefaultPipeline depth="5" verbose="0" draw="0" /> <BruteForceDetection name="N2" /> <MinProximityIntersection alarmDistance="0.03" contactDistance="0.02" /> <DiscreteIntersection/> <DefaultContactManager name="Response" response="FrictionContact"/> <Node name="FloorScene"> <!-- <VisualStyle displayFlags="showBehaviorModels" /> <FreeMotionAnimationLoop solveVelocityConstraintFirst="0"/> <LCPConstraintSolver maxIt="1000" tolerance="1e-6" mu="0.9"/> <DefaultPipeline depth="5" verbose="0" draw="0" /> <BruteForceDetection name="N2" /> <MinProximityIntersection alarmDistance="0.03" contactDistance="0.02" /> <DiscreteIntersection/> <DefaultContactManager name="Response" response="FrictionContact"/> --> <Node name="Floor"> <EulerImplicit rayleighStiffness="0.1" printLog="false" rayleighMass="0.1" /> <CGLinearSolver threshold="1e-8" tolerance="1e-5" iterations="25"/> <MeshObjLoader name="loader" filename="mesh/floorFlat.obj"/> <MeshTopology src="@loader"/> <MechanicalObject name="mecha" src="@loader" template="Rigid3d" scale3d="10 1 10" translation="50 0 50" showObject="1"/> <UniformMass totalMass="10000.0"/> <UncoupledConstraintCorrection/> <OglModel name="visual" src="@loader" color="blue" scale3d="10 1 10" translation="50 0 50"/> <FixedConstraint indices='0 1 2 3'/> <Node name="Coll_Rec"> <MeshTopology src="@../loader"/> <MechanicalObject src="@../loader" name="coll" scale3d="10 1 10" translation="50 0 50"/> <TTriangleModel/> <TLineModel/> <TPointModel/> <RigidMapping input="@.." output="@coll"/> </Node> </Node> </Node> <Node name="CylinderScene"> <!-- <VisualStyle displayFlags="showBehaviorModels" /> <FreeMotionAnimationLoop solveVelocityConstraintFirst="0"/> <LCPConstraintSolver maxIt="1000" tolerance="1e-6" mu="0.9"/> <DefaultPipeline depth="5" verbose="0" draw="0" /> <BruteForceDetection name="N2" /> <MinProximityIntersection alarmDistance="0.03" contactDistance="0.02" /> <DiscreteIntersection/> <DefaultContactManager name="Response" response="FrictionContact"/> --> <Node name="Cylinder"> <EulerImplicit rayleighStiffness="0.1" printLog="false" rayleighMass="0.1" /> <CGLinearSolver threshold="1e-8" tolerance="1e-5" iterations="25"/> <MeshObjLoader name="loader_cyl" filename="mesh/cylinder.obj"/> <MechanicalObject name="Cyl" src="@loader_cyl" template="Rigid3d" scale3d="0.8 0.8 0.8" translation="-30 100 -65"/> <UniformMass totalMass="10000.0"/> <UncoupledConstraintCorrection/> <Node name="Visu_Cyl"> <OglModel name="visual_cyl" src="@../loader_cyl" color="green" scale3d="0.8 0.8 0.8"/> <RigidMapping input="@.." output="@visual_cyl"/> </Node> <Node name="Coll_Cyl"> <MeshTopology src="@../loader_cyl"/> <MechanicalObject src="@../loader_cyl" name="coll_cyl" scale3d="0.8 0.8 0.8"/> <TTriangleModel/> <TLineModel/> <TPointModel/> <RigidMapping input="@.." output="@coll_cyl"/> </Node> </Node> </Node> <DataExchange name="exchangeData1" template="vector<float>" from="@FloorScene/Floor/mecha.position" to="@CylinderScene/Cylinder/Cyl.position"/> </Node>
Best
Hugo
27 March 2020 at 20:40 #15573JieYingBlocked -
AuthorPosts
- You must be logged in to reply to this topic.