Geometry Nodes: Edge Paths to Curves node speedup #115115

Open
Iliya Katushenock wants to merge 29 commits from mod_moder/blender:tmp_speedup_edge_paths_to_curves into main

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

Shortest Path is the set of vertices where each point know index of next one.
For some subset of such points (selection input), is built curve from each point
to next one. In a lot of cases, at some point, multiple curve is merged in some
vertex and all next vertices is shared.

To know size of each curve, and allocate result array, each curve need to be traversed.
While traversing of some curves many vertices can be already visited by previous curves.
Idea is to cache count of all next points for each one. If rephrase this, this is traversal
for directed graph in depth. And by caching depth of visited point, this algorithm is
come to be linear for number of points.

Improvement of curves sizes exploring and point indices gathering (ms):

Number of points: 5'577'362 4'040'246 2'752'442 1'905'715 1'367'849 1'020'922
Old: indices 2'303.94 1'910.15 1'161.3 771.58 489.07 350.28
New: indices 427.94 285.64 169.2 104.76 70.27 49.9
New/Old Diff 5.38x 6.68x 6.86x 7.36x 6.99x 7.09x
curves from indices 358.92 286.53 181.02 109.04 73.54 44.34

Total time of node evaluation (ms):

Number of points: 5'577'362 4'040'246 2'752'442 1'905'715 1'367'849 1'020'922
Old 2'730.42 2026 1222.84 854.09 561.47 403.97
New 959.93 638.06 706.43 349.53 238.29 149.29

Measurement are made in the attached file.

Possible future improvements:

  1. This will not make this 10x faster, but this can be multithreaded graph traversal algorithm.
  2. Gathering of indices of curve points can be less random-memory-read. Each point of graph also can know begin of already written curve and its list of indices.
  3. Hot loop of branch size computation can be less random-memory-writes if use std::set (with sorting by index) or blender::Set stack to hold visited vertices, instead of write -i in each of them.
  4. Statically check if input field is Shortest Edge Path and use more fast algorithm without handling of cycle case.
Shortest Path is the set of vertices where each point know index of next one. For some subset of such points (selection input), is built curve from each point to next one. In a lot of cases, at some point, multiple curve is merged in some vertex and all next vertices is shared. To know size of each curve, and allocate result array, each curve need to be traversed. While traversing of some curves many vertices can be already visited by previous curves. Idea is to cache count of all next points for each one. If rephrase this, this is traversal for directed graph in depth. And by caching depth of visited point, this algorithm is come to be linear for number of points. Improvement of curves sizes exploring and point indices gathering (ms): | Number of points: | 5'577'362 | 4'040'246 | 2'752'442 | 1'905'715 | 1'367'849 | 1'020'922 | | -- | -- | -- | -- | -- | -- | -- | | Old: indices | 2'303.94 | 1'910.15 | 1'161.3 | 771.58 | 489.07 | 350.28 | | New: indices | 427.94 | 285.64 | 169.2 | 104.76 | 70.27 | 49.9 | | New/Old Diff | 5.38x | 6.68x | 6.86x | 7.36x | 6.99x | 7.09x | | curves from indices | 358.92 | 286.53 | 181.02 | 109.04 | 73.54 | 44.34 | Total time of node evaluation (ms): | Number of points: | 5'577'362 | 4'040'246 | 2'752'442 | 1'905'715 | 1'367'849 | 1'020'922 | | -- | -- | -- | -- | -- | -- | -- | | Old | 2'730.42 | 2026 | 1222.84 | 854.09 | 561.47 | 403.97 | | New | 959.93 | 638.06 | 706.43 | 349.53 | 238.29 | 149.29 | Measurement are made in the attached file. Possible future improvements: 1. This will not make this 10x faster, but this can be multithreaded graph traversal algorithm. 2. Gathering of indices of curve points can be less random-memory-read. Each point of graph also can know begin of already written curve and its list of indices. 3. Hot loop of branch size computation can be less random-memory-writes if use `std::set` (with sorting by index) or `blender::Set` stack to hold visited vertices, instead of write -i in each of them. 4. Statically check if input field is `Shortest Edge Path` and use more fast algorithm without handling of cycle case.
Iliya Katushenock added the
Interest
Geometry Nodes
label 2023-11-19 00:22:42 +01:00
Iliya Katushenock added 2 commits 2023-11-19 00:22:53 +01:00
Iliya Katushenock added this to the Nodes & Physics project 2023-11-19 00:22:57 +01:00
Iliya Katushenock requested review from Hans Goudey 2023-11-19 00:23:01 +01:00
Iliya Katushenock added 2 commits 2023-11-19 11:52:33 +01:00
Iliya Katushenock changed title from Geometry Nodes: Edge Paths to Curves speedup to Geometry Nodes: Edge Paths to Curves node speedup 2023-11-19 12:48:10 +01:00
Hans Goudey reviewed 2023-11-19 17:00:08 +01:00
Hans Goudey left a comment
Member

Big picture, the goals seem nice. I do wonder how much improvement just reserving the vectors with a guess of the final size would give though.

It's not totally clear what's meant by rang. Maybe the translation is off. Is it possibly "range"?

It seems the need to modify the input next_indices array might also cause unnecessary copying in the future (if the values are already stored in a contiguous attribute, for example). Maybe if we traded more strict behavior with invalid indices we could avoid the need to modify that array? (i.e. skipping the entire next vert path if it contains an invalid index).

Big picture, the goals seem nice. I do wonder how much improvement just reserving the vectors with a guess of the final size would give though. It's not totally clear what's meant by `rang`. Maybe the translation is off. Is it possibly "range"? It seems the need to modify the input `next_indices` array might also cause unnecessary copying in the future (if the values are already stored in a contiguous attribute, for example). Maybe if we traded more strict behavior with invalid indices we could avoid the need to modify that array? (i.e. skipping the entire next vert path if it contains an invalid index).
@ -49,1 +51,3 @@
if (next_vert < 0 || next_vert >= mesh.totvert) {
Array<int> rang(mesh.totvert, non_checked);
Array<bool> visited(mesh.totvert, false);
const auto rang_for_vertex = [&](const int vertex) -> int {
Member

Seems there's no need for this to be a lambda

Seems there's no need for this to be a lambda
Author
Member

I changed that and it seems that come to be looks more complicated.

I changed that and it seems that come to be looks more complicated.
mod_moder marked this conversation as resolved
Author
Member

I do wonder how much improvement just reserving the vectors with a guess of the final size would give though.

Just not sure if that makes sense to use vector is size is known at the begin.

It's not totally clear what's meant by rang. Maybe the translation is off. Is it possibly "range"?

Ah, that is my mistake, that is rank!.

It seems the need to modify the input next_indices array might also cause unnecessary copying in the future (if the values are already stored in a contiguous attribute, for example).

To me, problem is in another side. destination span should be used in evaluation process instead of simply replace evaluator.get(0).materialize(in);. Not for now, but hope in future, with new evaluation system...

Maybe if we traded more strict behavior with invalid indices we could avoid the need to modify that array? (i.e. skipping the entire next vert path if it contains an invalid index).

I just avoiding extra branch by representing invalid indices as loop (a->b->c->i->-5) -> (a->b->c->i->i).

> I do wonder how much improvement just reserving the vectors with a guess of the final size would give though. Just not sure if that makes sense to use vector is size is known at the begin. > It's not totally clear what's meant by `rang`. Maybe the translation is off. Is it possibly "range"? Ah, that is my mistake, that is `rank`!. > It seems the need to modify the input `next_indices` array might also cause unnecessary copying in the future (if the values are already stored in a contiguous attribute, for example). To me, problem is in another side. `destination` span should be used in evaluation process instead of simply replace `evaluator.get(0).materialize(in);`. Not for now, but hope in future, with new evaluation system... > Maybe if we traded more strict behavior with invalid indices we could avoid the need to modify that array? (i.e. skipping the entire next vert path if it contains an invalid index). I just avoiding extra branch by representing invalid indices as loop (`a`->`b`->`c`->`i`->`-5`) -> (`a`->`b`->`c`->`i`->`i`).
Iliya Katushenock added 1 commit 2023-11-19 17:44:50 +01:00
Author
Member

More details about current PR state (file from pr description, but with x10 mesh copies):

Timer 'Roots mask': (Average: 0.64 ms, Min: 0.57 ms, Last: 0.60 ms)
Timer 'Loops incorrect indices': (Average: 0.44 ms, Min: 0.38 ms, Last: 0.47 ms)
Timer 'Count rank': (Average: 61.38 ms, Min: 57.80 ms, Last: 60.72 ms)
Timer 'Accumulate': (Average: 1.55 ms, Min: 1.43 ms, Last: 1.48 ms)
Timer 'Gather indices': (Average: 18.80 ms, Min: 17.02 ms, Last: 17.63 ms)
Timer 'New curves': (Average: 48.38 ms, Min: 44.02 ms, Last: 48.94 ms)

I think i still able to improve Count rank part by add lock-free parallelism and maybe by lock-free pool of branches. New curves part will be fixed in #115693.

More details about current PR state (file from pr description, but with x10 mesh copies): Timer 'Roots mask': (Average: 0.64 ms, Min: 0.57 ms, Last: 0.60 ms) Timer 'Loops incorrect indices': (Average: 0.44 ms, Min: 0.38 ms, Last: 0.47 ms) Timer 'Count rank': (Average: 61.38 ms, Min: 57.80 ms, Last: 60.72 ms) Timer 'Accumulate': (Average: 1.55 ms, Min: 1.43 ms, Last: 1.48 ms) Timer 'Gather indices': (Average: 18.80 ms, Min: 17.02 ms, Last: 17.63 ms) Timer 'New curves': (Average: 48.38 ms, Min: 44.02 ms, Last: 48.94 ms) I think i still able to improve `Count rank` part by add lock-free parallelism and maybe by lock-free pool of branches. `New curves` part will be fixed in #115693.
Author
Member

I just found the fact that my current approach is works differently as current one in main with cycle case (currently loops is always checked until first collision, but in PR, we splits all loops by some certain point and do not check all unreached points...).

I just found the fact that my current approach is works differently as current one in main with cycle case (currently loops is always checked until first collision, but in PR, we splits all loops by some certain point and do not check all unreached points...).
Author
Member

Here is a test case for cyclic attractor for Edge Path to Curves. Currently, in main, is expected to go in to the loop body and gather all points of loop single time. In another hand, i did the mistake in this PR, and stop gathering of the loop points at some certain one.

Expected PR
image image

I did the test file to cover this case.

Here is a test case for cyclic attractor for Edge Path to Curves. Currently, in main, is expected to go in to the loop body and gather all points of loop single time. In another hand, i did the mistake in this PR, and stop gathering of the loop points at some certain one. | Expected | PR | | -- | -- | | ![image](/attachments/22ed6245-c3ca-4147-bcb0-80f4f4c37e20) | ![image](/attachments/8d91b15d-e72b-4d1c-a4ad-0b7c60dc0b31) | I did the test file to cover this case.
Iliya Katushenock added 4 commits 2023-12-19 20:32:42 +01:00
Iliya Katushenock added 1 commit 2023-12-20 16:15:55 +01:00
Iliya Katushenock added 2 commits 2023-12-22 20:39:27 +01:00
Iliya Katushenock added 2 commits 2023-12-23 21:12:57 +01:00
Author
Member

Main:
Timer 'Main loop': (Average: 382.35 ms, Min: 365.30 ms, Last: 378.61 ms)
Timer 'New curves': (Average: 51.83 ms, Min: 48.92 ms, Last: 51.25 ms)
Total: 430 ms

Optimized:
Timer 'Roots mask': (Average: 0.66 ms, Min: 0.57 ms, Last: 0.68 ms)
Timer 'Loops incorrect indices': (Average: 0.45 ms, Min: 0.37 ms, Last: 0.40 ms)
Timer 'Count rank': (Average: 49.03 ms, Min: 45.66 ms, Last: 47.92 ms)
Timer 'Accumulate': (Average: 1.57 ms, Min: 1.43 ms, Last: 1.46 ms)
Timer 'Gather indices': (Average: 19.62 ms, Min: 17.79 ms, Last: 18.88 ms)
Timer 'New curves': (Average: 50.64 ms, Min: 47.03 ms, Last: 51.42 ms)
Total: 120 ms

Parallel rank:
Timer 'Roots mask': (Average: 0.79 ms, Min: 0.57 ms, Last: 0.68 ms)
Timer 'Loops incorrect indices': (Average: 0.54 ms, Min: 0.39 ms, Last: 0.49 ms)
Timer 'Init rank': (Average: 0.37 ms, Min: 0.29 ms, Last: 0.37 ms)
Timer 'Count rank': (Average: 14.73 ms, Min: 11.54 ms, Last: 14.37 ms)
Timer 'Accumulate': (Average: 1.69 ms, Min: 1.44 ms, Last: 1.53 ms)
Timer 'Gather indices': (Average: 20.51 ms, Min: 18.47 ms, Last: 20.47 ms)
Timer 'New curves': (Average: 53.69 ms, Min: 47.52 ms, Last: 53.69 ms)
Total: 90 ms

And i found that in my test file result in different in the main (currently my pr (both simple, and parallel)) creates extra points and the roots of all branches.

Main: Timer 'Main loop': (Average: 382.35 ms, Min: 365.30 ms, Last: 378.61 ms) Timer 'New curves': (Average: 51.83 ms, Min: 48.92 ms, Last: 51.25 ms) Total: 430 ms Optimized: Timer 'Roots mask': (Average: 0.66 ms, Min: 0.57 ms, Last: 0.68 ms) Timer 'Loops incorrect indices': (Average: 0.45 ms, Min: 0.37 ms, Last: 0.40 ms) Timer 'Count rank': (Average: 49.03 ms, Min: 45.66 ms, Last: 47.92 ms) Timer 'Accumulate': (Average: 1.57 ms, Min: 1.43 ms, Last: 1.46 ms) Timer 'Gather indices': (Average: 19.62 ms, Min: 17.79 ms, Last: 18.88 ms) Timer 'New curves': (Average: 50.64 ms, Min: 47.03 ms, Last: 51.42 ms) Total: 120 ms Parallel rank: Timer 'Roots mask': (Average: 0.79 ms, Min: 0.57 ms, Last: 0.68 ms) Timer 'Loops incorrect indices': (Average: 0.54 ms, Min: 0.39 ms, Last: 0.49 ms) Timer 'Init rank': (Average: 0.37 ms, Min: 0.29 ms, Last: 0.37 ms) Timer 'Count rank': (Average: 14.73 ms, Min: 11.54 ms, Last: 14.37 ms) Timer 'Accumulate': (Average: 1.69 ms, Min: 1.44 ms, Last: 1.53 ms) Timer 'Gather indices': (Average: 20.51 ms, Min: 18.47 ms, Last: 20.47 ms) Timer 'New curves': (Average: 53.69 ms, Min: 47.52 ms, Last: 53.69 ms) Total: 90 ms And i found that in my test file result in different in the main (currently my pr (both simple, and parallel)) creates extra points and the roots of all branches.
Iliya Katushenock added 5 commits 2023-12-26 16:13:39 +01:00
Iliya Katushenock added 1 commit 2023-12-26 20:41:12 +01:00
Iliya Katushenock changed title from Geometry Nodes: Edge Paths to Curves node speedup to WIP: Geometry Nodes: Edge Paths to Curves node speedup 2024-01-07 00:28:46 +01:00
Author
Member

Multithreading might introduce a lot of complexity and not so many benefits, so i'll take out that for now.

Multithreading might introduce a lot of complexity and not so many benefits, so i'll take out that for now.
Iliya Katushenock added 5 commits 2024-02-13 11:33:52 +01:00
Iliya Katushenock added 1 commit 2024-02-14 12:32:06 +01:00
Iliya Katushenock added 1 commit 2024-02-14 14:13:04 +01:00
Iliya Katushenock added 1 commit 2024-02-14 19:33:22 +01:00
Iliya Katushenock changed title from WIP: Geometry Nodes: Edge Paths to Curves node speedup to Geometry Nodes: Edge Paths to Curves node speedup 2024-02-14 20:21:16 +01:00
Iliya Katushenock added 1 commit 2024-02-14 20:26:35 +01:00
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u tmp_speedup_edge_paths_to_curves:mod_moder-tmp_speedup_edge_paths_to_curves
git checkout mod_moder-tmp_speedup_edge_paths_to_curves
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 Assignees
2 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#115115
No description provided.