perf: Vectorize KDTree queries and edge insertion in connect_nodes_across_graphs#142
Open
Raj-Taware wants to merge 5 commits into
Open
perf: Vectorize KDTree queries and edge insertion in connect_nodes_across_graphs#142Raj-Taware wants to merge 5 commits into
Raj-Taware wants to merge 5 commits into
Conversation
added 5 commits
April 23, 2026 02:30
- Fix 1: Add xfail marker for k=1 in nearest_neighbours parametrize (line 53-58) k=1 triggers scalar-vs-array bug in serial loop; fixed by vectorization - Fix 2: Add non-empty guard in within_radius test (line 83) Assert that edges exist before testing their properties - Fix 3: Change '<= k' to '== k' in nearest_neighbours test (line 68) Source has 25 nodes >= max k=8, so every target always gets exactly k neighbours - Fix 4: Remove duplicate node-set check in containing_rectangle test (line 101-104) _assert_edge_attrs_valid already performs this check - Fix 5: Fix import ordering (line 1-7) Add blank line between third-party and first-party imports Sort imports correctly per isort convention
…nodes_across_graphs
…ross_graphs - Batch all target positions and run one KDTree query per method - Flatten nearest_neighbours result with ravel+repeat - Flatten within_radius ragged output with np.repeat/np.concatenate - Compute vdiff and len in one numpy pass over all edges - Replace per-edge add_edge loop with add_edges_from - Remove now-resolved xfail on nearest_neighbours k=1 (scalar-vs-array bug no longer exists in the vectorized path)
20 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes
Replaces per-target Python loop with batch NumPy/SciPy operations in
connect_nodes_across_graphs. Also fixes a latent bug: KDTree was built fromlist(G_source.nodes)but indices resolved againstsorted(G_source.nodes), connecting the wrong source node when dict iteration order != sort order.Motivation: Called for every edge type (g2m, m2g, m2m). Old code issued one KDTree query per target node and computed edge attributes scalar-by-scalar. Batch queries + vectorized NumPy ops eliminate the Python loop entirely.
Changes:
nearest_neighbours:.ravel()+np.repeatfor flat index arrayswithin_radius: concatenate raggedquery_ball_pointresultsvdiff/lenvianp.linalg.normacross all edgessource_nodes_listsorted before KDTree build, fixing index mismatch_find_neighbour_node_idxs_in_source_meshclosurePerformance Benchmarks
Benchmark script: wmg#117.
graphcastkeisleroskarsson_hierarchicalScaling plots
graphcast
keisler
oskarsson_hierarchical
Issue Link
Closes #138
Type of change
Checklist before requesting a review
Checklist for reviewers
Author checklist after completed review
Checklist for assignee