Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.

Fix memory allocation TODO in KenCarp step 2 and 4 by introducing chi…#691

Closed
Emanuele-Elias wants to merge 3 commits intoSciML:masterfrom
Emanuele-Elias:master
Closed

Fix memory allocation TODO in KenCarp step 2 and 4 by introducing chi…#691
Emanuele-Elias wants to merge 3 commits intoSciML:masterfrom
Emanuele-Elias:master

Conversation

@Emanuele-Elias
Copy link
Copy Markdown

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the contributor guidelines, in particular the SciML Style Guide and COLPRAC.
  • Any new documentation only uses public API

Additional context

Hi everyone! This is my first PR to SciML, aiming to prepare for GSoC.

I addressed the TODO left in src/perform_step/kencarp.jl inside perform_step! for SKenCarpCache.
Previously, z₄ was being used as temporary storage for g1 * chi2 during Step 2 and Step 4, which could lead to it being overwritten and failing to properly repeat on a step fail.

Changes made:

  • Added a dedicated chi2g1::uType cache variable in the SKenCarpCache struct.
  • Pre-allocated chi2g1 = similar(u) inside alg_cache.
  • Replaced the temporary usage of z₄ with chi2g1 in kencarp.jl.
  • Ran Runic to format the code to adhere to the SciML Style Guide.

Note on checklist: No new tests or docs were added because this is an internal memory/cache optimization and the existing test suite already covers this algorithm. Local tests passed (the only failure was EulerHeun sparse alloc, which seems to be a pre-existing issue on master).

@ChrisRackauckas
Copy link
Copy Markdown
Member

@Emanuele-Elias
Copy link
Copy Markdown
Author

Hi @ChrisRackauckas! Maybe I misunderstood the TODO😅

I read it as referring to z₄ being used as temporary storage for chi2 * g1 on the lines right below it, so I added a cache to replace that temporary z₄ usage.

Maybe what you meant was that in the non-diagonal noise case, g1 is mutated via g1 .-= g4, which permanently overwrites it and ruins it for a repeated step?

To fix this properly, should I revert the chi2g1 changes and instead add a gtmp::rateNoiseType to the cache so we can do @.. gtmp = g1 - g4 and preserve g1? Let me know and I'll push the updated commit

@ChrisRackauckas
Copy link
Copy Markdown
Member

It's about fixing the repeated computation.

@Emanuele-Elias
Copy link
Copy Markdown
Author

Emanuele-Elias commented Mar 15, 2026

I've just pushed a new commit that corrects my previous chi2g1 changes. Instead, I wrapped g(g1, ...) in an if !repeat_step && !integrator.last_stepfail block to prevent the repeated computation. To make this work, I also added a gtmp cache to replace g1 .-= g4 with @.. gtmp = g1 - g4 in Step 4 so g1 isn't destroyed with step failure. let me know if now it's ok and thank you @ChrisRackauckas for the guidance!!

aliases = ODEAliasSpecifier()
if haskey(kwargs, :alias_u0)
message = "`alias_u0` keyword argument is deprecated, to set `alias_u0`,
if use_old_kwargs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is this all changed?

@ChrisRackauckas
Copy link
Copy Markdown
Member

To fix the formatting and add an appropriate test, your changes were pulled into #698 and merged. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants