BLI: Optimize utility for index counting #109628

Merged
Hans Goudey merged 9 commits from HooglyBoogly/blender:bli-index-count into main 2023-07-05 16:05:22 +02:00
Member

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:
image

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](https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGIAOykrgAyeAyYAHI%2BAEaYxBIAzKQADqgKhE4MHt6%2BASlpGQKh4VEssfFcSXaYDplCBEzEBNk%2BfoHVtQL1jQTFkTFxibYNTS257SO9Yf1lg5UAlLaoXsTI7BzmCWHI3lgA1CYJbgrJhofYJhoAgpvbu5gHR8gKBPiC55c31wD03wdmWyoewgWBo4XQEAA%2BpCAOIRORuaHzf4ANnMKL2YDAoOmEOhO0MwCR80%2BvwOAFYrJTgAwvCAQKhko4WHgAF6YCDmMwAWioXgYxE8tG5tFQjIUXJJ5IAIiYZRSqRYaXSGUy8Cz2ZyzGYAPIJSVy2Uy0l/TauPBUE17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XP/FtQerS1IPtKyyQWZVxrmvXqHAdSHyJnsVAQIb7t0kJ3QkPcUR9wHhADwZ8lqj3HpPEas96pNUXgvKuq8eob1sHoKeVUq5mG6ujGh9CZ6kFtukZwkggA%3D%3D%3D) The test case: ![image](/attachments/30483a16-d8e8-43b7-9fe0-d61340e7367b)
Hans Goudey added 1 commit 2023-07-03 00:43:47 +02:00
e05df28192 BLI: Add utility for index counting
The utility counts the number of occurences of each index in an array.
This happens 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.

Now that the utility is in one place, it's reasonable to optimize it
with compiler flags. On GCC, unrolling the loop gave me a 1.9x
performance improvement, counting the number corners for each
vertex in a 4 million vertex mesh went from 7.4 to 3.9 ms.

In a couple places this improves code reuse, sharing the
implementation of the pattern where it was repeated before.

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):
image

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): ![image](/attachments/05cfb714-f309-4ed3-bb3e-096b9432aba5)
Hans Goudey added 1 commit 2023-07-03 04:18:06 +02:00
Hans Goudey added this to the Core Libraries project 2023-07-03 13:38:04 +02:00
Hans Goudey requested review from Jacques Lucke 2023-07-03 16:00:08 +02:00
Hans Goudey added 1 commit 2023-07-03 16:02:59 +02:00
Jacques Lucke approved these changes 2023-07-03 19:35:56 +02:00
Jacques Lucke left a comment
Member

I can reproduce the speedup. Noted some possible improvements inline.

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__))
Member

At this point it may be nice to have a define for these optimization flags, what do you think?

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]]++;
Member

Would be interesting to check if using parallelism with atomic increments can help improve performance here.

Would be interesting to check if using parallelism with atomic increments can help improve performance here.
Author
Member

I don't remember the numbers, but I remember that being slower. I'll test it again though!

I don't remember the numbers, but I remember that being slower. I'll test it again though!
Author
Member

I'm glad I tested again, this was much faster! Also simpler in that it doesn't require using compiler specific flags.

I'm glad I tested again, this was much faster! Also simpler in that it doesn't require using compiler specific flags.
Hans Goudey added 4 commits 2023-07-04 01:46:54 +02:00
Hans Goudey changed title from BLI: Add and optimize utility for index counting to BLI: Optimize utility for index counting 2023-07-04 01:48:01 +02:00
Hans Goudey requested review from Jacques Lucke 2023-07-04 01:53:27 +02:00
Member

Can you say something about how the algorithm scales with more threads?

Can you say something about how the algorithm scales with more threads?
Jacques Lucke approved these changes 2023-07-04 10:11:17 +02:00
Author
Member

Good idea!
image
"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.

Good idea! ![image](/attachments/df2f2e1f-6283-4068-9e57-4a9734af16e2) "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.
Hans Goudey added 2 commits 2023-07-05 15:47:38 +02:00
Hans Goudey merged commit 53416281bd into main 2023-07-05 16:05:22 +02:00
Hans Goudey deleted branch bli-index-count 2023-07-05 16:05:23 +02:00
Sign in to join this conversation.
No reviewers
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
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#109628
No description provided.