BLI: Optimize utility for index counting #109628
No reviewers
Labels
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
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#109628
Loading…
Reference in New Issue
No description provided.
Delete Branch "HooglyBoogly/blender:bli-index-count"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The utility counts the number of occurrences of each index in an array.
It's used to build offsets for mesh topology maps, or to count the
number of connected elements. Some users are geometry nodes,
the subdivision draw cache, and mesh to curve conversion.
This PR parallelizes the counting to take advantage of multiple
threads. On a Ryzen 7950x, when counting connected edges to
vertices, I observed an improvement from 10.2 to 3.0 ms.
This most likely makes the counting less efficient, but it is
quite a nice performance improvement.
The new code was much slower for me at less than four threads,
so I added a check so that counting remains single threaded in
that case.
Here you can see the change in assembly in godbolt
The test case:
MSVC 23
Timer 'new': (Average: 13.7 ms, Min: 8.8 ms, Last: 12.8 ms)
Timer 'old': (Average: 13.8 ms, Min: 11.1 ms, Last: 14.7 ms)
Test (cube with subdiv):
I can reproduce the speedup. Noted some possible improvements inline.
@ -45,6 +45,27 @@ void gather(const GSpan src, const IndexMask &indices, GMutableSpan dst, const i
gather(GVArray::ForSpan(src), indices, dst, grain_size);
}
#if (defined(__GNUC__) && !defined(__clang__))
At this point it may be nice to have a define for these optimization flags, what do you think?
@ -48,0 +57,4 @@
# pragma unroll
#endif
for (int64_t i = 0; i < indices_num; i++) {
counts[indices[i]]++;
Would be interesting to check if using parallelism with atomic increments can help improve performance here.
I don't remember the numbers, but I remember that being slower. I'll test it again though!
I'm glad I tested again, this was much faster! Also simpler in that it doesn't require using compiler specific flags.
BLI: Add and optimize utility for index countingto BLI: Optimize utility for index countingCan you say something about how the algorithm scales with more threads?
Good idea!
"Before" was at 10.2 ms BTW. Raw data: https://docs.google.com/spreadsheets/d/1a4IVNPM2Gud7yYLuaRJmz3viCb8PCAsnVyeZXY8pCqk/edit?usp=sharing
I was a little surprised by that, since I expected atomics to be cheap when there isn't much contention, but I think the higher thread counts just make up for the new overhead quite effectively.
I added a check so the code is still single threaded if there are less than 4 threads. Seems good to make sure to avoid the worst of the performance degradation, though I'm sure that heuristic will be less effective with different source data or hardware.