Anim: run bezier handle calculation in parallel #119388

Open
Christoph Lendenfeld wants to merge 14 commits from ChrisLend/blender:thread_recalc_handles into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

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


Measuring BKE_fcurve_handles_recalc_ex
with 10.000 keys (the numbers represent a run through a single FCurve)

Action Before After
moving a single key ~0.45 ms ~0.33 ms
moving a key of every FCurve ~0.58ms ~0.60ms
Graph Editor operator Ease ~2.2ms ~3.5ms

When only measuring the threaded loop. So while the specific loop only takes a third of the time, the total function time is only reduced by ~30%

Before After
~0.15 ms ~0.04 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. ------ Measuring `BKE_fcurve_handles_recalc_ex` with 10.000 keys (the numbers represent a run through a single FCurve) | Action | Before | After | | - | - | - | | moving a single key | ~0.45 ms | ~0.33 ms | | moving a key of every FCurve | ~0.58ms | ~0.60ms | | Graph Editor operator Ease | ~2.2ms | ~3.5ms | When only measuring the threaded loop. So while the specific loop only takes a third of the time, the total function time is only reduced by ~30% | Before | After | | - | - | | ~0.15 ms | ~0.04 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
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u thread_recalc_handles:ChrisLend-thread_recalc_handles
git checkout ChrisLend-thread_recalc_handles
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
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
4 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.