ilpincy / argos3

A parallel, multi-engine simulator for heterogeneous swarm robotics
http://www.argos-sim.info/
262 stars 121 forks source link

Access to ARGoS threads in loop functions? #108

Closed jharwell closed 3 years ago

jharwell commented 5 years ago

Currently it does not seem possible to access the ARGoS threads from the loops functions, and if I want to iterate through all robots and collect some metrics from them (what task they are current executing, are they in collision avoidance, etc.) in order to better understand why I'm observing the performance that I am, I have to create my own threads to do this. Right now I'm using OpenMP, and incur an overhead cost of doing this, and will eventually switch to using regular threads, so I'm wondering if there would be any issue with adding a function to the CSpace class and/or its derived classes to allow loop functions to use the existing ARGoS threads? In both the PreStep() and PostStep() functions, the threads aren't used, and so should be available, unless I've missed something.

If this is possible, I'm thinking of modifying ARGoS such that the loop functions can pass a lambda as a std::function callback that each thread will call that gets passed a CControllableEntity pointer.

@ilpincy @allsey87 Thoughts?

jharwell commented 5 years ago

@ilpincy,@allsey87 gentlest of bumps on this. Do you think this idea has merit? Thanks!

allsey87 commented 5 years ago

Just to be clear, you are using OpenMP inside a robot controller and want to collect data on the state of that controller from the loop function?

If so, what is your motivation for using threads? I ask since I have only every used threads in a robot controller on real hardware. Never in the simulator...

jharwell commented 5 years ago

@allsey87 what I'm asking about is thr feasibility of using ARGoS threads in the loop functions to iterate over each robot in the swarm via GetEntitiesByType(). In the controllers I'm using they each implement a number of interfaces which I use to collect information about what the swarm as a whole is doing by collecting said information from each robot and averaging it. Right now I'm using OpenMP in the loop fuGetEntitiesByType()nctions (*not* the controller) to iterate over the map provided byGetEntitiesByType()` (I put the entity pointers into a vector before iterating with OpenMP to reduce overhead). This works reasonably well, BUT there are still additional threads that have to be spun up /down each time step I collect from the swarm, which adds additional overhead.

If instead of this I could use the general purpose ARGoS threads for this I would not have to take the performance hit of the overhead of OpenMP/additional thread maintenence. What I'm not sure on is if it such functionality could be added safely. I think it could, and it would be as simple as adding a target in the multithreaded CSpace classes and then exposing it to the loop functions via a callback. But I don't yet know ARGoS well enough to be sure.

Does that make sense?

allsey87 commented 5 years ago

I am not very familiar with how @ilpincy has implemented the threads in ARGoS and can't really comment further on this matter. However, I would think it would be simpler to use a thread pool to avoid spining up/down the threads, no?

allsey87 commented 5 years ago

But I don't yet know ARGoS well enough to be sure.

I think for these sorts of things you will need to investigate and come to your own conclusions about (and to implement your own solution if needed). @ilpincy and I have almost no time for considering new features at the moment. It is even difficult to find time to track down and resolve non-critical bugs!