Skip to content

Performance: only check independence for filled component scenarios#1368

Merged
TonyXiang8787 merged 1 commit intomainfrom
pgm/feature/performance-prepare-scenario
Apr 15, 2026
Merged

Performance: only check independence for filled component scenarios#1368
TonyXiang8787 merged 1 commit intomainfrom
pgm/feature/performance-prepare-scenario

Conversation

@mgovers
Copy link
Copy Markdown
Member

@mgovers mgovers commented Apr 14, 2026

In the current main, for every component, every scenario in a batch was checked to determine the update independence, even if there are no components of that type in the entire batch. That is, of course, not needed.

This PR changes the iteration over all scenarios to an iteration over all filled scenarios. This is much more efficient. In particular, there is an early-out if there are no components of a type in the update dataset, and another one if the batch update is not uniform for that component.

Test

Script

import numpy as np

from power_grid_model import (
    AttributeType as AT,
    ComponentType as CT,
    DatasetType as DT,
    PowerGridModel,
    initialize_array,
)

node = initialize_array(DT.input, CT.node, 1)
node[AT.id] = 0
node[AT.u_rated] = 100.0

source = initialize_array(DT.input, CT.source, 1)
source[AT.id] = 1
source[AT.node] = 0
source[AT.status] = 1
source[AT.u_ref] = 1.0
source[AT.sk] = 1e20
source[AT.rx_ratio] = 0.0

sym_load = initialize_array(DT.input, CT.sym_load, 1)
sym_load[AT.id] = 2
sym_load[AT.node] = 0
sym_load[AT.status] = 1
sym_load[AT.type] = 2
sym_load[AT.p_specified] = 0.0
sym_load[AT.q_specified] = 500.0

input_data = {
    CT.node: node,
    CT.source: source,
    CT.sym_load: sym_load,
}

source_update = initialize_array(DT.update, CT.source, (100_000, 1))
source_update[AT.id] = 1
source_update[AT.u_ref] = 1
source_update[AT.u_ref][0] = 1.1

gigantic_update_data = {CT.source: source_update}

model = PowerGridModel(input_data)
print("Starting calculation...")
result = model.calculate_power_flow(update_data=gigantic_update_data)
print("Done.")

np.testing.assert_allclose(result[CT.node][AT.u][1:], 100.0, rtol=0.0, atol=1e-8)
np.testing.assert_allclose(result[CT.node][AT.u][0], 110.0, rtol=0.0, atol=1e-8)

Current main

All the different components all take up some amount of time in the calculations. While small for one component, the sheer amount of component types adds up. This is even more so for large batch calculations
image

This branch

Only the source component (the only component that is updated in the update data) has any significant time being spent on
image

@mgovers mgovers self-assigned this Apr 14, 2026
@mgovers mgovers added the improvement Improvement on internal implementation label Apr 14, 2026
@mgovers mgovers requested review from Copilot, figueroa1395 and nitbharambe and removed request for figueroa1395 and nitbharambe April 14, 2026 10:58
@mgovers mgovers added improvement Improvement on internal implementation and removed improvement Improvement on internal implementation labels Apr 14, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes update-independence checking by iterating only over scenarios that actually contain component data (“filled scenarios”), avoiding unnecessary per-scenario work when a component is absent/empty in the batch.

Changes:

  • Replaced “all scenarios” buffer-span APIs with “filled scenarios” variants and updated call sites accordingly.
  • Updated independence checking to use filled-scenario spans (with early-outs) instead of scanning every scenario.
  • Adjusted unit tests to validate the new filled-scenario span behavior for row-based and columnar datasets.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
tests/cpp_unit_tests/test_dataset.cpp Updates tests to validate filled-scenario span enumeration instead of all-scenario enumeration.
power_grid_model_c/power_grid_model/include/power_grid_model/main_core/update.hpp Uses filled-scenario spans to reduce work during independence checks.
power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/dataset.hpp Introduces filled-scenario span views and refactors span retrieval helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/cpp_unit_tests/test_dataset.cpp Outdated
@TonyXiang8787
Copy link
Copy Markdown
Member

@mgovers can you put some demostrative example in your PR description to show what exactly is improved? By looking at code change it is not that clear.

… data

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>

fix compilation

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>

resolve copilot comments

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>

some cleanup regarding filtering distance overhead

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>

partial revert and go down a different route

Signed-off-by: Martijn Govers <Martijn.Govers@Alliander.com>
@mgovers mgovers force-pushed the pgm/feature/performance-prepare-scenario branch from 8da13e4 to 2107db7 Compare April 15, 2026 10:07
@mgovers
Copy link
Copy Markdown
Member Author

mgovers commented Apr 15, 2026

@mgovers can you put some demostrative example in your PR description to show what exactly is improved? By looking at code change it is not that clear.

done

@sonarqubecloud
Copy link
Copy Markdown

@TonyXiang8787 TonyXiang8787 enabled auto-merge April 15, 2026 10:54
@TonyXiang8787 TonyXiang8787 added this pull request to the merge queue Apr 15, 2026
Merged via the queue into main with commit 2d3b2c4 Apr 15, 2026
31 checks passed
@TonyXiang8787 TonyXiang8787 deleted the pgm/feature/performance-prepare-scenario branch April 15, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improvement on internal implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants