Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching behaviors: #1070, #1072, #1074 #1073

Merged
merged 7 commits into from
Mar 5, 2024
Merged

Fix caching behaviors: #1070, #1072, #1074 #1073

merged 7 commits into from
Mar 5, 2024

Conversation

GabrielKS
Copy link
Contributor

This fixes #1070 and #1072.

On #1070:

There is some inherent overhead to creating large arrays that cannot be avoided, but now that we use the cache the time for repeated calls to _read_results(Matrix{Float64}... on my big dataset has decreased 30x from 0.09s to 0.003s:

julia> @time results_mat = PSI._read_results(Matrix{Float64}, res, variables, timestamps, nothing)
  0.003551 seconds (340 allocations: 7.419 MiB)

This is much better and closes the issue but is still unnecessarily heavy-handed for my PowerAnalytics component-wise reads, so later I may seek to add an option to PowerSimulations to perform read_results_with_keys for only one component at a time.

On #1072:

Ended up being more refactoring than initially planned, but the upshot is that for the DecisionModelSimulationResults method of load_results! the documentation now precisely describes what happens, what happens is good, the bug preventing it from loading more timestamps is gone, some code duplication has been removed, and there are lots more tests. I did not significantly edit the EmulationModelSimulationResults method of load_results!; it appears to have a significantly different interface that does not even take in time steps.

# after the setup detailed in bug #1072
julia> PSI._read_results(res, variable_keys, timestamps, nothing) |> values |> first |> x->x.data |> keys |> collect
7-element Vector{DateTime}:
 2035-01-01T00:00:00
 2035-01-02T00:00:00
 2035-01-03T00:00:00
 2035-01-04T00:00:00
 2035-01-05T00:00:00
 2035-01-06T00:00:00
 2035-01-07T00:00:00

@GabrielKS GabrielKS requested review from daniel-thom and jd-lara March 5, 2024 00:34
@GabrielKS GabrielKS self-assigned this Mar 5, 2024
src/simulation/decision_model_simulation_results.jl Outdated Show resolved Hide resolved
src/simulation/decision_model_simulation_results.jl Outdated Show resolved Hide resolved
src/simulation/emulation_model_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
test/test_simulation_results.jl Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Mar 5, 2024

Performance Results

Version Precompile Time
Main 3.182788781
This Branch 3.171812111
Version Build Time
Main-Build Time Precompile 51.92068835
Main-Build Time Postcompile 2.575826849
This Branch-Build Time Precompile 52.384125003
This Branch-Build Time Postcompile 2.893994498

Copy link

codecov bot commented Mar 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.38%. Comparing base (12288cd) to head (fbfb1ca).
Report is 20 commits behind head on psy4.

❗ Current head fbfb1ca differs from pull request most recent head 8a474bb. Consider uploading reports for the commit 8a474bb to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             psy4    #1073      +/-   ##
==========================================
- Coverage   80.48%   80.38%   -0.10%     
==========================================
  Files         115      115              
  Lines       12403    12379      -24     
==========================================
- Hits         9982     9951      -31     
- Misses       2421     2428       +7     
Flag Coverage Δ
unittests 80.38% <100.00%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...rc/simulation/decision_model_simulation_results.jl 86.88% <100.00%> (+0.05%) ⬆️
...c/simulation/emulation_model_simulation_results.jl 98.19% <ø> (ø)
src/simulation/realized_meta.jl 93.75% <100.00%> (+0.13%) ⬆️

... and 8 files with indirect coverage changes

res::SimulationProblemResults{DecisionModelSimulationResults},
result_keys,
timestamps::Vector{Dates.DateTime},
store::Nothing,
store::Union{Nothing, <:SimulationStore},
Copy link
Member

Choose a reason for hiding this comment

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

This weird interface relates to #1018 and could be fixed in this PR. @daniel-thom we discussed not doing this anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure exactly what the proposal is here. For now I'm just mimicking the other _read_results method and passing the store straight through

Copy link
Member

Choose a reason for hiding this comment

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

This is an interface issue @daniel-thom discussed where we pass a store to read from or nothing when nothing signifies that it is an in-memory store which makes the code confusing since it isn't obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So you'd like me to resolve the store to the in-memory store or not somewhere upstream so there's always a valid store getting passed around?

Copy link
Contributor

Choose a reason for hiding this comment

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

I’m not sure that I would change this, at least not here. nothing also means that the user didn’t pass a store.

Copy link
Member

@jd-lara jd-lara Mar 5, 2024

Choose a reason for hiding this comment

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

what does not passing a store to get result mean? I don't think that is right because there is always a store for a simulation

Copy link
Contributor

Choose a reason for hiding this comment

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

We could easily be talking around each other. There are many nuances here. The user can do either of these:

read_variable(results, “ActivePowerVariable__ThermalStandard”)
open_store(HdfSimulationStore, simulation_store_path, "r") do store
    read_variable(results, “ActivePowerVariable__ThermalStandard”; store=store)
end

In the first case we have to get the store, which could in memory or h5. If it’s h5, we have to open it. It’s in-memory, we can just use it. There is one slightly awkward case one line 288.

Copy link
Member

Choose a reason for hiding this comment

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

no one does this

open_store(HdfSimulationStore, simulation_store_path, "r") do store
    read_variable(results, “ActivePowerVariable__ThermalStandard”; store=store)
end

Assuming this patter will happen make the code way less understandable for developers.

The most common way to access results is calling read_variables or read_realized_variables

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the absence of consensus, are we okay keeping store::Union{Nothing, <:SimulationStore} in this non-user-facing method for the moment?

Copy link
Contributor

Choose a reason for hiding this comment

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

I’ll propose a patch in a separate PR.

@jd-lara
Copy link
Member

jd-lara commented Mar 5, 2024

we should put this in the PSY4 branch to avoid massive rebases later. This improvement can wait until the PSY 4 release.

@GabrielKS
Copy link
Contributor Author

Now fixes #1074

@GabrielKS GabrielKS changed the title Fix caching behaviors: #1070 and #1072 Fix caching behaviors: #1070, #1072, #1074 Mar 5, 2024
@GabrielKS GabrielKS requested review from daniel-thom and jd-lara March 5, 2024 23:23
if _are_results_cached(res, result_keys, timestamps, keys(cached_results))
@debug "reading results from SimulationsResults cache"
@debug "reading results from SimulationsResults cache" # NOTE tests match on this
Copy link
Member

Choose a reason for hiding this comment

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

remove # Note

Copy link
Contributor

Choose a reason for hiding this comment

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

As a developer I find this useful. It calls my attention to it.

Copy link
Member

Choose a reason for hiding this comment

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

ok, lets leave it

else
@debug "reading results from data store"
@debug "reading results from data store" # NOTE tests match on this
Copy link
Member

Choose a reason for hiding this comment

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

remove # Note

Copy link
Member

@jd-lara jd-lara left a comment

Choose a reason for hiding this comment

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

LGTM, good effort. Minor comments to remove unnecessary comments in the code base

Copy link
Contributor

@daniel-thom daniel-thom left a comment

Choose a reason for hiding this comment

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

LGTM

@jd-lara jd-lara self-assigned this Mar 5, 2024
@jd-lara jd-lara merged commit e2c9332 into psy4 Mar 5, 2024
1 of 6 checks passed
@jd-lara jd-lara deleted the gks/fix-1070 branch March 12, 2024 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
3 participants