Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map canonical links to their models #736

Merged
merged 1 commit into from
May 11, 2021

Conversation

adlarkin
Copy link
Contributor

@adlarkin adlarkin commented Apr 1, 2021

Signed-off-by: Ashton Larkin ashton@openrobotics.org

🎉 New feature

Related to #706

Follow-up to #685 and #678

Summary

When calling UpdateSim in the physics system, we have to update a model's pose if the model's canonical link changed. We are currently handling this by iterating through all models in the ECM because:

  1. The ECM implicitly gives us a topological ordering of models (Support individual canonical links for nested models #685 (comment)), which allows us to handle nested model updates correctly since nested model pose calculations depend on the parent model pose.
  2. For each changed canonical link, we have no way of knowing which models view this link as its canonical link, unless we recurse upward via parent models, checking model canonical links along the way. This is inefficient to do for every canonical link in each simulation update, and is also difficult code to maintain/understand. So, we instead get each model and check if its canonical link has changed.

This PR should resolve both of these issues, and also removes the need for looping through the ECM to check for model updates, which should result in a performance improvement for worlds with a lot of non-static models that do not change often (see #711 to learn more about why this change results in a performance improvement).

Test it

The physics system integration test has been modified in this PR (click on the files changed tab). However, to get a better grasp of the performance improvement that this PR creates, a testing methodology similar to #678 was used:

Test results are shown below. "Before this PR" uses commit 4e40b36 of ign-gazebo. Notice how the yellow Models block is now essentially gone for most of the tests cases now, except for the non-static case with DART (see the Takeaways section below for more information about this).

It's important to note that running the profiler decreases performance, so the RTF numbers reported here are with the profiler turned off.

TPE

Static

Before this PR:

  • RTF: 50%
  • UpdateSim time: 0.67ms

static_tpe_pre_PR

After this PR:

  • RTF: 85% 😀
  • UpdateSim time: 0.004ms

static_tpe_post_pr

Non-Static

Before this PR:

  • RTF: 14.75%
  • UpdateSim time: 0.8ms

non_static_tpe_pre_PR

After this PR:

  • RTF: 17%
  • UpdateSim time: 0.003ms

non_static_tpe_post_pr

DART

Static

Before this PR:

  • RTF: 12.5%
  • UpdateSim time: 1.1ms

static_dart_pre_PR

After this PR:

  • RTF: 15%
  • UpdateSim time: .011ms

dart_static_post_pr

Non-Static

Before this PR:

  • RTF: 1.1%
  • UpdateSim time: 2.8ms

non_static_dart_pre_PR

After this PR:

  • RTF: 1.1%
  • UpdateSim time: 1.7ms

dart_non_static_post_pr

Takeaways

  • Static model performance for TPE is near optimal now - RTF is close to 100% when running headless, as expected for static models with TPE.
  • DART does not benefit from these changes as much as TPE because the Step in DART still accounts for most of the work being done in a physics cycle.
  • Why is there still a yellow Models profiler block for non-static models with DART? -> For non-static models with DART, some models are still processed as changed due to contact forces (this isn't an issue for TPE). If gravity is set to 0 for a world that uses DART, then no models are perceived as changed. I have found that around 25-50% of models that are affected by contact forces in DART are registered as "changed" by ign-physics, but we are still better off only updating a subset of the models than all of them in UpdateSim.
  • There is a chance that the performance gains won't be as noticeable for a world with a lot of non-static objects that change often. The reason for this is because we are now returning a std::map instead of a std::unordered_map in the ChangedLinks method in order to guarantee topological ordering of links (consider a nested model where each model has its own canonical link - we still want to make sure the parent model is updated first, so we must process the parent's canonical link first in order to preserve topological ordering/update behavior). Because of this change, inserting changed links into this map is now of time complexity O(log(n)) instead of O(1). However, we still have the advantage of only iterating through the changed links in UpdateSim to update models instead of updating through every model, so I don't believe that the changes in this PR would cause any noticeable performance decreases (especially since the use case of a lot of non-static models that move often is probably rare anyways). In fact, if we look at the non-static test case for DART above (where 25-50% of the 3000 models are still being processed as "changed" due to contact forces, which means we are inserting into this map often), UpdateSim time still improved by about 1ms.

Future work

Next, I'd like to work on #711. After this, it would be worth investigating performance when using a GUI (so far, all testing has been done headless). I spoke with @iche033 offline, and we found that the RTF between headless and GUI simulation results in significantly different RTF numbers when using the changes in this PR (on my machine, I saw that RTF dropped from 80%-30% once the GUI was populated for the 3000 static shapes test with TPE). We also found that RTF drops significantly (from 90% to 10% for me running headless, 3000 static shapes with TPE) when echoing the /world/shapes/dynamic_pose/info topic). Here are a few more issues that I created so that I can address them as I continue to work on performance improvements:

Checklist

  • Signed all commits for DCO
  • Added tests
  • Added example and/or tutorial
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge

@adlarkin adlarkin added performance Runtime performance 🏢 edifice Ignition Edifice labels Apr 1, 2021
@adlarkin adlarkin force-pushed the adlarkin/canonical_link_to_models branch from de6da01 to fb4a177 Compare April 2, 2021 20:28
@codecov
Copy link

codecov bot commented Apr 2, 2021

Codecov Report

Merging #736 (fb4a177) into ign-gazebo5 (d3ee64a) will increase coverage by 0.08%.
The diff coverage is 88.88%.

❗ Current head fb4a177 differs from pull request most recent head 48acd0e. Consider uploading reports for the commit 48acd0e to get more accurate results
Impacted file tree graph

@@               Coverage Diff               @@
##           ign-gazebo5     #736      +/-   ##
===============================================
+ Coverage        65.32%   65.41%   +0.08%     
===============================================
  Files              240      241       +1     
  Lines            17624    17650      +26     
===============================================
+ Hits             11513    11545      +32     
+ Misses            6111     6105       -6     
Impacted Files Coverage Δ
src/systems/physics/Physics.cc 69.44% <83.01%> (ø)
src/systems/physics/EntityFeatureMap.hh 93.65% <90.90%> (ø)
...lude/ignition/gazebo/components/ReferenceModels.hh 100.00% <100.00%> (ø)
src/SdfEntityCreator.cc 87.43% <100.00%> (+0.20%) ⬆️
src/SimulationRunner.cc 93.77% <0.00%> (+1.03%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dca877a...48acd0e. Read the comment docs.

@adlarkin adlarkin force-pushed the adlarkin/canonical_link_to_models branch 3 times, most recently from d209de7 to b2b0ee8 Compare April 6, 2021 17:30
@adlarkin adlarkin requested review from azeey, chapulina and iche033 April 6, 2021 17:38
@adlarkin adlarkin marked this pull request as ready for review April 6, 2021 17:38
@adlarkin adlarkin mentioned this pull request Apr 6, 2021
Copy link
Contributor

@iche033 iche033 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice I also see a good increase in RTF. For static models in TPE, I get 30% on my laptop and 55% on desktop.

Just some minor comments but changes look good to me

src/systems/physics/Physics.cc Show resolved Hide resolved
include/ignition/gazebo/components/ReferenceModels.hh Outdated Show resolved Hide resolved
include/ignition/gazebo/components/ReferenceModels.hh Outdated Show resolved Hide resolved
include/ignition/gazebo/components/ReferenceModels.hh Outdated Show resolved Hide resolved
src/SdfEntityCreator.cc Outdated Show resolved Hide resolved
src/systems/physics/Physics.cc Show resolved Hide resolved
test/integration/components.cc Outdated Show resolved Hide resolved
test/integration/components.cc Outdated Show resolved Hide resolved
@adlarkin
Copy link
Contributor Author

After some discussion that was had offline, I have moved this new canonical link mapping functionality into a class internal to the physics system in 24ebe9e. This approach has the following advantages:

  • We do not need to introduce a new component to help achieve this functionality (originally, I created a ReferenceModels component which had the potential for introducing complications later on: Map canonical links to their models #736 (comment))
  • We can leave the SdfEntityCreator untouched, and also no longer need to deprecate the CanonicalLink or ModelCanonicalLink components since we are still using them (Map canonical links to their models #736 (comment))
  • If we ever need to change how this mapping is implemented, we can do so without breaking API or ABI since this is now internal to the physics system plugin

The topological ordering works here based on the assumption that entities are created from parent to child, with entity IDs created in ascending order (notice how links are stored in a std::map and how models are stored in a std::set since each of these data structures preserve ascending order). This should mean that behavior isn't broken for users who create entities with or without the SdfEntityCreator (i.e., there's no implicit dependency on the SdfEntityCreator). I believe that the only edge case here would be if a user created a child link/model entity before creating the parent, because then, the child entity ID would be less than the parent entity ID, but is this even possible with our current API? cc @azeey


All tests still pass for me, and I also re-ran performance tests to verify that this still helps TPE performance. This should now be ready for final review!

@adlarkin adlarkin requested review from iche033 and azeey April 30, 2021 15:49
Copy link
Contributor

@iche033 iche033 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latest changes look good to me.

Copy link
Contributor

@chapulina chapulina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I just have a couple of comments.

src/systems/physics/CanonicalLinkModelTracker.hh Outdated Show resolved Hide resolved
test/integration/physics_system.cc Show resolved Hide resolved
Copy link
Contributor

@chapulina chapulina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Feel free to merge with happy CI ✔️

Copy link
Contributor

@azeey azeey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that the only edge case here would be if a user created a child link/model entity before creating the parent, because then, the child entity ID would be less than the parent entity ID, but is this even possible with our current API?

This is certainly possible if users create link/model entities without going through the SdfEntityCreator. They can first create a link, then the model and set the model as the parent of the link. Of course, this is not a common use case, so I'm good with merging this PR. But perhaps we can leave #706 open until we implement the topological ordering by using parent-child relationships encoded by the ParentEntity component or the relationships maintained by the graph in the ECM.

test/integration/physics_system.cc Outdated Show resolved Hide resolved
test/integration/physics_system.cc Show resolved Hide resolved
test/integration/physics_system.cc Outdated Show resolved Hide resolved
adlarkin added a commit that referenced this pull request May 11, 2021
Signed-off-by: Ashton Larkin <ashton@openrobotics.org>
@adlarkin adlarkin force-pushed the adlarkin/canonical_link_to_models branch from 3b80549 to 570cb2b Compare May 11, 2021 21:51
@adlarkin
Copy link
Contributor Author

perhaps we can leave #706 open until we implement the topological ordering by using parent-child relationships encoded by the ParentEntity component or the relationships maintained by the graph in the ECM

Sounds good to me 👍 I updated the PR description so that this doesn't close #706. I also went ahead and left a comment in #706 that links to the discussion we had here to give others an update: #706 (comment)

I'm just waiting for all checks to run one last time, but if they come back green, I'll merge this.

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>
@adlarkin adlarkin force-pushed the adlarkin/canonical_link_to_models branch from 570cb2b to 48acd0e Compare May 11, 2021 22:53
@adlarkin adlarkin merged commit 48acd0e into ign-gazebo5 May 11, 2021
@adlarkin adlarkin deleted the adlarkin/canonical_link_to_models branch May 11, 2021 23:40
scpeters added a commit that referenced this pull request May 19, 2021
* 🎈 3.8.0 (#688)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Make it so joint state publisher is quieter (#696)

Signed-off-by: Michael Carroll <michael@openrobotics.org>

* [BULLET] Making GetContactsFromLastStepFeature optional in Collision Features (#690)

* GetContactsFromLastStepFeature made optional

Signed-off-by: Tomas Lorente <jtlorente@ekumenlabs.com>

Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>

* Add test for thermal object temperatures below 0 kelvin (#621)

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>

* Scenebroadcaster sensors (#698)

* Add sensors to scene broadcaster

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Update src/systems/scene_broadcaster/SceneBroadcaster.cc

Co-authored-by: Michael Carroll <michael@openrobotics.org>

* Fix codecheck

Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>
Co-authored-by: Michael Carroll <michael@openrobotics.org>

* Fix diffuse and ambient values for ackermann example (#707)

Signed-off-by: Ammaar Solkar <asketch8@gmail.com>

* 🎈 5.0.0 (#731)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Support configuring particle scatter ratio in particle emitter system (#674)

* set particle scatter ratio through sdf

Signed-off-by: Ian Chen <ichen@osrfoundation.org>

* address feedback

Signed-off-by: Ian Chen <ichen@osrfoundation.org>

* add todo note about merging forward

Signed-off-by: Ian Chen <ichen@osrfoundation.org>

Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>

* Update PlaybackScrubber description (#733)

Signed-off-by: Ammaar Solkar <asketch8@gmail.com>

* Iterate through changed links only in UpdateSim (#678)

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>

* Do not pass -Wno-unused-parameter to MSVC compiler (#716)

Signed-off-by: Silvio Traversaro <silvio.traversaro@iit.it>

* Use Protobuf_IMPORT_DIRS instead of PROTOBUF_IMPORT_DIRS for compatibility with Protobuf CMake config (#715)

Signed-off-by: Silvio Traversaro <silvio.traversaro@iit.it>

* Fix component inspector shutdown crash (#724)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com>

* Validate step size and RTF parameters (#740)

Only set them if they are strictly positive.

Signed-off-by: Luca Della Vedova <luca@openrobotics.org>

* Fix compute_rtfs arguments (#737)

Signed-off-by: Caio Amaral <caioaamaral@gmail.com>

* Fixed collision visual bounding boxes (#746)

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* Fix CMakelists.txt merge

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* ECM's ChangedState gets message with modified components (#742)

* ecm's ChangedState to contain modified components

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* updated log_system test

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* removed unnecessary calls

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

Co-authored-by: Ian Chen <ichen@osrfoundation.org>

* fixed particle emitter forward playback (#745)

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* Merge pull request #730 from ignitionrobotics/particle_emitter

Particle emitter based on SDF

* 4 7 0 prep (#755)

* Prepare for 4.7.0

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Added placeholder

Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>

* Fix 'invalid animation update data' msg for actors (#754)

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>

* Update benchmark comparison instructions (#766) (#766)

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>

* [DiffDrive] add enable/disable (#772)

* add enable/disable diffdrive

Signed-off-by: Guillaume Doisy <guillaume.doisy@wyca.fr>

* remove debug

Signed-off-by: Guillaume Doisy <guillaume.doisy@wyca.fr>

* do not subscribe to enable if topic is empty

Signed-off-by: Guillaume Doisy <guillaume.doisy@wyca.fr>

* add test

Signed-off-by: Guillaume Doisy <guillaume.doisy@wyca.fr>

* lint and style

Signed-off-by: Guillaume Doisy <guillaume.doisy@wyca.fr>

* change enable type to bool and renamed to enabled

Signed-off-by: Guillaume Doisy <guillaume.doisy@wyca.fr>

* Add odometry publisher system (#547)

* Create Initial Odometry Publisher system plugin

Add code for initial plugin that gets position from Pose component and
calculates velocities based on rolling mean from displacement data.

Signed-off-by: Maganty Rushyendra <mrushyendra@yahoo.com.sg>

* Remove Linear and Angular Velocity components

Also renames frames in Odometry msg to include model name, and makes
various style changes.

Signed-off-by: Maganty Rushyendra <mrushyendra@yahoo.com.sg>

* Get World pose instead of pose of robot base frame

Signed-off-by: Maganty Rushyendra <mrushyendra@yahoo.com.sg>

* Add documentation for variables and functions

Includes minor stylistic changes.

Signed-off-by: Maganty Rushyendra <mrushyendra@yahoo.com.sg>

* Check for valid odomTopic and update copyright year

Signed-off-by: Maganty Rushyendra <mrushyendra@yahoo.com.sg>

* Add tests for OdometryPublisherSystem and fix velocity calculation bug

Swap X and Y linear velocities when calculating odometry velocities
relative to robotBaseFrame.

Signed-off-by: Maganty Rushyendra <mrushyendra@yahoo.com.sg>

Co-authored-by: ahcorde <ahcorde@gmail.com>

* Patch particle emitter2 service (#777)

* Patch particle emitter2 service

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Remove condition variable

Signed-off-by: Nate Koenig <nate@openrobotics.org>

* Set emitter frame and relative pose

Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>

* Preparing for 4.8.0 release (#780)

Signed-off-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Nate Koenig <nate@openrobotics.org>

* 👩‍🌾 Enable Focal CI (#646)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

Co-authored-by: Michael Carroll <michael@openrobotics.org>

* [TPE] Support setting individual link velocity  (#427)

Signed-off-by: claireyywang <22240514+claireyywang@users.noreply.github.com>
Signed-off-by: Ian Chen <ichen@osrfoundation.org>

Co-authored-by: Ian Chen <ichen@osrfoundation.org>
Co-authored-by: Louise Poubel <louise@openrobotics.org>

* Don't store duplicate ComponentTypeId in ECM (#751)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Feature/hydrodynamics (#749)

Implement hydrodynamics and thruster plugin.

Signed-off-by: Arjo Chakravarty <arjo@openrobotics.org>
Co-authored-by: Mabel Zhang <mabel@openrobotics.org>
Co-authored-by: Carlos Agüero <caguero@openrobotics.org>

* Fix macOS build: components::Name in benchmark (#784)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

Co-authored-by: Steve Peters <scpeters@openrobotics.org>

* Fix ColladaExporter submesh index bug (#763)

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>

* 👩‍🌾 Fix Windows build and some warnings (#782)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com>

* Prevent crash on Plotting plugin with mutex (#747)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Bump ign-physics version to 3.2 (#792)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Bump to ign-msgs 7.1 / sdformat 11.1, Windows fixes (#758)

Signed-off-by: Louise Poubel <louise@openrobotics.org>

* Util: Use public API from libsdformat for detecting non-file source (#794)

Signed-off-by: Eric Cousineau <eric.cousineau@tri.global>

* Fix included nested model expansion in SDF generation (#768)

* fixed included nested model expansion

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* added resource path to test

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* use orig URIs & support multi level nesting

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* save fuel version when enabled

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* retrieve uri from map

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* copy included element

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* clear attributes before copying include element

Signed-off-by: Jenn Nguyen <jenn@openrobotics.org>

* Map canonical links to their models (#736)

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>

* ColladaExporter, export submesh selected (#802)

* Export only submesh if selected
* Add test case for the PR
* Attempting a unified solution

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>
Co-authored-by: Nate Koenig <nate@openrobotics.org>

Co-authored-by: Michael Carroll <michael@openrobotics.org>
Co-authored-by: Jose Tomas Lorente <jtlorente@ekumenlabs.com>
Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>
Co-authored-by: Ashton Larkin <ashton@openrobotics.org>
Co-authored-by: Nate Koenig <nkoenig@users.noreply.github.com>
Co-authored-by: Nate Koenig <nate@openrobotics.org>
Co-authored-by: Ammaar Solkar <asketch8@gmail.com>
Co-authored-by: Ian Chen <ichen@osrfoundation.org>
Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
Co-authored-by: Silvio Traversaro <pegua1@gmail.com>
Co-authored-by: Alejandro Hernández Cordero <ahcorde@gmail.com>
Co-authored-by: Luca Della Vedova <luca@openrobotics.org>
Co-authored-by: Caio Amaral <caioaamaral@gmail.com>
Co-authored-by: Jenn Nguyen <jenn@openrobotics.org>
Co-authored-by: G.Doisy <doisyg@users.noreply.github.com>
Co-authored-by: Rushyendra Maganty <mrushyendra@yahoo.com.sg>
Co-authored-by: Claire Wang <22240514+claireyywang@users.noreply.github.com>
Co-authored-by: Arjo Chakravarty <arjo129@gmail.com>
Co-authored-by: Mabel Zhang <mabel@openrobotics.org>
Co-authored-by: Carlos Agüero <caguero@openrobotics.org>
Co-authored-by: Steve Peters <scpeters@openrobotics.org>
Co-authored-by: Jorge Perez <j.j.perez13@hotmail.com>
Co-authored-by: Eric Cousineau <eric.cousineau@tri.global>
Co-authored-by: Jorge Perez <jjperez@ekumenlabs.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏢 edifice Ignition Edifice performance Runtime performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants