Page MenuHome

Geometry Nodes: Lazy threading.
ClosedPublic

Authored by Jacques Lucke (JacquesLucke) on Sep 15 2022, 11:01 AM.

Details

Summary

The goal is to reduce threading overhead when evaluating geometry nodes.

Most existing .blend files don't benefit from this because their execution time is bottlenecked by individual nodes. However, in some cases the number of nodes can result in a bottleneck itself. That's mainly the case when there are lots of "small" nodes (which are fast to execute). In this case, using lots of multi-threading is counter productive. The goal of "lazy threading" is to only use multi-threading when it is likely to be useful. For more details see the description in BLI_lazy_threading.hh.

I mainly tested the performance improvements in three files (one was actually used in practice, and two I made myself which are not too relevant but test the bottlenecks of the system). Performance in files with less extreme node counts, performance is expected to be about the same. If a file does become slower, it can usually be fixed by adding a blender::lazy_threading::send_hint() call in some place.

While the modifier execution time reduced in all three cases, it's actually more interesting to look at the result of the linux time command. That's because it not only shows wall clock time, but also the cpu time spent (in user and kernel mode).

time ./blender -b "/run/media/jacques/Jacques Data/Projekte/geometry_nodes_demo_files/many_math_nodes.blend"
  before:
    real    0m1.120s
    user    0m7.948s
    sys     0m1.765s
  after:
    real    0m0.726s
    user    0m2.775s
    sys     0m0.345s

time ./blender -b "/home/jacques/Downloads/Geo node tree test 1-1-1 (no viewport).blend"
  before:
    real    0m0.744s
    user    0m2.173s
    sys     0m1.153s
  after:
    real    0m0.669s
    user    0m1.466s
    sys     0m0.541s

time ./blender -b "/run/media/jacques/Jacques Data/Projekte/geometry_nodes_demo_files/large_nested_field_test.blend"
  before:
    real    0m0.517s
    user    0m1.869s
    sys     0m0.586s
  after:
    real    0m0.449s
    user    0m0.411s
    sys     0m0.120s

As can be seen, the real time is often only reduced by a bit, but the user and sys time is reduced significantly. This shows that threads are used better.



Diff Detail

Repository
rB Blender

Event Timeline

Jacques Lucke (JacquesLucke) requested review of this revision.Sep 15 2022, 11:01 AM
Jacques Lucke (JacquesLucke) created this revision.

I can't help feeling a bit skeptical, but I guess that might happen no matter the heuristic.
I guess with performance numbers this might be more convincing.
Another thing that would help is some discussion of the cost when this code isn't run in a context that uses the lazy threading.
One issue I see with this is that it can't detect large single-threaded work, which is fairly common when iterating over a mesh.

The comments make sense though, they do describe the problem well.

source/blender/blenlib/BLI_lazy_threading.hh
15
54–55

Example of making things a bit more concise
The assumption here is that when the task thinks the current work load is big enough to justify using threads, it's also..
When the task thinks its work load is large enough to use threads, it should be...

64
70
source/blender/blenlib/intern/lazy_threading.cc
10
source/blender/functions/intern/lazy_function_graph_executor.cc
1144–1153

There's a couple non-obvious things here that could be commented like the reversing. Might be nice for a comment briefly mentioning the purpose of the whole lambda for people without context

  • Merge branch 'master' into temp-lazy-threading
  • reduce overhead
  • disable showing thread ids
  • cleanup
  • use approximate node group size as a heuristic
  • typos
  • send hint in BLI_task_parallel_range
  • cleanup
  • update description
  • Merge branch 'master' into temp-lazy-threading
  • cleanup

https://builder.blender.org/admin/#/builders/136/builds/51

  • Merge branch 'master' into temp-lazy-threading
  • fix compile issue
  • cleanup

https://builder.blender.org/admin/#/builders/136/builds/52

Jacques Lucke (JacquesLucke) retitled this revision from Geometry Nodes: Lazy threading (WIP). to Geometry Nodes: Lazy threading..Sep 17 2022, 12:42 PM
Jacques Lucke (JacquesLucke) edited the summary of this revision. (Show Details)
Hans Goudey (HooglyBoogly) added inline comments.
source/blender/blenlib/BLI_lazy_threading.hh
33
source/blender/functions/intern/lazy_function_graph_executor.cc
1139

This comment implies there's some sort of timer. Maybe it should refer to the "hint" system.

source/blender/nodes/intern/geometry_nodes_lazy_function.cc
616

Do you mean "small" here? I'm confused otherwise.

1377–1379

I haven't seen this before, but what about using static constexpr bool to avoid code rot here?

This revision is now accepted and ready to land.Sep 19 2022, 6:50 PM