Anim: run bezier handle calculation in parallel #119388

Merged
Christoph Lendenfeld merged 21 commits from ChrisLend/blender:thread_recalc_handles into main 2024-05-07 10:43:03 +02:00

Bezier handles are recalculated in many places in the
animation code. Threading that code can give a performance
boost all over Blender.


Performance delta

Action Before After
recalcData_graphedit moving a single key 1.06 ms 1.0 ms
recalcData_graphedit moving 300 keys of a single FCurve 1.6 ms 1.4 ms
recalcData_graphedit moving 300 keys of multiple FCurves 60 ms 55 ms
ANIM_animdata_update when using the Breakdown operator in the GE 90 ms 73 ms

Test file used
https://download.blender.org/ftp/sybren/animation-rigging/heavy_mocap_test.blend

Grain Size Tests

Grain Size Time
16 ~0.06ms
64 ~0.04ms
256 ~0.04ms
1024 ~0.07ms
4096 ~0.1ms

BKE_nurb_handle_smooth_fcurve can still be run in parallel,
that's more complicated though, so for this patch I've kept the scope small.

Bezier handles are recalculated in many places in the animation code. Threading that code can give a performance boost all over Blender. ------ Performance delta | Action | Before | After | | - | - | - | | `recalcData_graphedit` moving a single key | 1.06 ms | 1.0 ms | | `recalcData_graphedit` moving 300 keys of a single FCurve | 1.6 ms | 1.4 ms | | `recalcData_graphedit` moving 300 keys of multiple FCurves | 60 ms | 55 ms | | `ANIM_animdata_update` when using the Breakdown operator in the GE | 90 ms | 73 ms | Test file used https://download.blender.org/ftp/sybren/animation-rigging/heavy_mocap_test.blend **Grain Size Tests** | Grain Size | Time | | - | - | | 16 | ~0.06ms | | 64 | ~0.04ms | | 256 | ~0.04ms | | 1024 | ~0.07ms | | 4096 | ~0.1ms | ------ `BKE_nurb_handle_smooth_fcurve` can still be run in parallel, that's more complicated though, so for this patch I've kept the scope small.
Christoph Lendenfeld added the
Module
Animation & Rigging
label 2024-03-12 17:29:17 +01:00
Christoph Lendenfeld added 2 commits 2024-03-12 17:29:27 +01:00
Christoph Lendenfeld added 2 commits 2024-03-14 13:39:03 +01:00
Christoph Lendenfeld added 3 commits 2024-03-15 11:22:41 +01:00
Christoph Lendenfeld changed title from WIP: Anim: run bezier handle calculation in parallel to Anim: run bezier handle calculation in parallel 2024-03-15 11:30:28 +01:00
Christoph Lendenfeld added 1 commit 2024-03-21 11:53:04 +01:00
Christoph Lendenfeld added 2 commits 2024-03-26 10:37:53 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
4447f208eb
grain size to 128
Author
Member

@blender-bot build

@blender-bot build
Christoph Lendenfeld added 1 commit 2024-03-28 14:53:03 +01:00
Christoph Lendenfeld requested review from Hans Goudey 2024-03-28 14:54:12 +01:00
Christoph Lendenfeld requested review from Sybren A. Stüvel 2024-03-28 14:54:17 +01:00
Falk David reviewed 2024-03-28 14:57:51 +01:00
@ -1238,2 +1237,2 @@
BezTriple *prev = cycle_offset_triple(cycle, &tmp, &fcu->bezt[fcu->totvert - 2], last, first);
BezTriple *next = (bezt + 1);
IndexRange bezt_range(0, fcu->totvert);
threading::parallel_for(bezt_range, 128, [&](const IndexRange range) {
Member

Have you tried different grain sizes in your measurements? I feel like this should be much greater like 4096, but of course, feelings are meaningless when it comes to performance :D

Have you tried different grain sizes in your measurements? I feel like this should be much greater like `4096`, but of course, feelings are meaningless when it comes to performance :D
Author
Member

I did and surprisingly it got slower with 256 and 512.
But then again that's on a sample size of 1. Not sure if there are machine differences. And the difference wasn't huge, ~10% with quite a bit of noise

I did and surprisingly it got slower with 256 and 512. But then again that's on a sample size of 1. Not sure if there are machine differences. And the difference wasn't huge, ~10% with quite a bit of noise
Member

Right, I guess there is more going on in this loop than it seems.

Right, I guess there is more going on in this loop than it seems.
Member

I think 4096 is way too big for this use case. There typically aren't tens of thousands of keyframes, and the grain size needs to be low enough to give meaningful usage of multiple CPU cores.

I think 4096 is way too big for this use case. There typically aren't tens of thousands of keyframes, and the grain size needs to be low enough to give meaningful usage of multiple CPU cores.
Author
Member

where is the number 4096 coming from? hardware limits?

where is the number 4096 coming from? hardware limits?
Member

@HooglyBoogly Well only if the loop is actually doing enough work to justify the parallel loop in the first place. But yes I agree.

@HooglyBoogly Well only if the loop is actually doing enough work to justify the parallel loop in the first place. But yes I agree.
Member

Right, true

Right, true
Member

@ChrisLend No just a higher power of two. Remember that the grain size is essentially "how many iterations are assigned to one thread". So in the case of 4096 it would (essentially) mean one thread is running 4096 iterations.

@ChrisLend No just a higher power of two. Remember that the grain size is essentially "how many iterations are assigned to one thread". So in the case of 4096 it would (essentially) mean one thread is running 4096 iterations.
Member

where is the number 4096 coming from? hardware limits?

It's arbitrary, just a compromise between reducing overhead and more threading. I think I remember finding a while ago that using powers of two can actually give noticeable improvements. Also they just seem nice.

> where is the number 4096 coming from? hardware limits? It's arbitrary, just a compromise between reducing overhead and more threading. I think I remember finding a while ago that using powers of two can actually give noticeable improvements. Also they just seem nice.
Hans Goudey reviewed 2024-03-28 17:01:43 +01:00
@ -1240,0 +1241,4 @@
BezTriple *bezt = &fcu->bezt[i];
BezTriple *prev = nullptr;
BezTriple *next = nullptr;
if (i > 0) {
Member

How about pulling the special handling for the first and last keyframes out of the parallel loop, and skipping those two indices? bezt_range.drop_front(1).drop_back(1)

How about pulling the special handling for the first and last keyframes out of the parallel loop, and skipping those two indices? `bezt_range.drop_front(1).drop_back(1)`
Author
Member

To do that without duplicating the code within the loop I'd need to extract the logic into a function. Should I do this within this PR?

To do that without duplicating the code within the loop I'd need to extract the logic into a function. Should I do this within this PR?
Member

Definitely not a big deal either way, it could wait if you preferred!

Definitely not a big deal either way, it could wait if you preferred!
Sybren A. Stüvel reviewed 2024-04-09 11:44:11 +02:00
@ -1240,0 +1245,4 @@
prev = (bezt - 1);
}
else {
prev = cycle_offset_triple(cycle, &tmp, &fcu->bezt[fcu->totvert - 2], last, first);

If fcu->totvert == 2 then prev could be the same as bezt. The old code didn't have that, as it used prev = bezt at the end of the loop (unless I'm missing something, which is entirely possible).

The same goes for next below, that'll also be the same as bezt when bezt is the last key and totvert == 2. That was already the case in the old code, though.

Also (in the old code) the negative counting in a but the positive order of visiting the keys makes my head hurt. I'm glad you're resolving this.

If `fcu->totvert == 2` then `prev` could be the same as `bezt`. The old code didn't have that, as it used `prev = bezt` at the end of the loop (unless I'm missing something, which is entirely possible). The same goes for `next` below, that'll also be the same as `bezt` when `bezt` is the last key and `totvert == 2`. That was already the case in the old code, though. Also (in the old code) the negative counting in `a` but the positive order of visiting the keys makes my head hurt. I'm glad you're resolving this.
Christoph Lendenfeld added 3 commits 2024-04-23 15:53:18 +02:00
Sybren A. Stüvel approved these changes 2024-04-29 11:42:45 +02:00
Sybren A. Stüvel left a comment
Member

One last little thing that can be handled when landing.

One last little thing that can be handled when landing.
@ -1271,2 +1279,2 @@
bezt->vec[2][0] = bezt->vec[1][0];
}
/* Clamp timing of handles to be on either side of beztriple. */
if (bezt->vec[0][0] > bezt->vec[1][0]) {

This can be replaced with CLAMP_MAX(bezt->vec[0][0], bezt->vec[1][0]);, and the below case with CLAMP_MIN().

This can be replaced with `CLAMP_MAX(bezt->vec[0][0], bezt->vec[1][0]);`, and the below case with `CLAMP_MIN()`.
Christoph Lendenfeld added 3 commits 2024-04-30 11:46:18 +02:00
Christoph Lendenfeld added 3 commits 2024-05-03 13:05:16 +02:00
Author
Member

@HooglyBoogly @dr.sybren I updated the performance numbers since #119497: Anim: thread remake_graph_transdata has landed.

As you can see the performance gain very much depends on if something higher up in the call stack is already threaded. If that is the case the gain is very small.
Do let me know if it is still justified to do that change

@HooglyBoogly @dr.sybren I updated the performance numbers since [#119497: Anim: thread remake_graph_transdata](https://projects.blender.org/blender/blender/pulls/119497) has landed. As you can see the performance gain very much depends on if something higher up in the call stack is already threaded. If that is the case the gain is very small. Do let me know if it is still justified to do that change
Iliya Katushenock reviewed 2024-05-03 13:08:02 +02:00
@ -1311,3 +1311,1 @@
bezt++;
}
});

If this is relatively cheap, you can take look at TaskSizeHints.

If this is relatively cheap, you can take look at `TaskSizeHints`.
Hans Goudey approved these changes 2024-05-03 14:27:46 +02:00
Hans Goudey left a comment
Member

Looks good to me!

Looks good to me!
@ -1267,2 +1266,2 @@
BezTriple *prev = cycle_offset_triple(cycle, &tmp, &fcu->bezt[fcu->totvert - 2], last, first);
BezTriple *next = (bezt + 1);
IndexRange bezt_range(0, fcu->totvert);
threading::parallel_for(bezt_range, 256, [&](const IndexRange range) {
Member

I'd just write threading::parallel_for(IndexRange(fcu->totvert),

I'd just write `threading::parallel_for(IndexRange(fcu->totvert), `
Christoph Lendenfeld added 1 commit 2024-05-03 16:31:17 +02:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
0d0085c764
simplify IndexRange
Author
Member

@blender-bot build

@blender-bot build
Christoph Lendenfeld merged commit afe13175da into main 2024-05-07 10:43:03 +02:00
Christoph Lendenfeld deleted branch thread_recalc_handles 2024-05-07 10:43:06 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser Project (Legacy)
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#119388
No description provided.