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

Apply callbacks with a type-stable generated function. #2021

Merged
merged 3 commits into from
Aug 24, 2023

Commits on Aug 23, 2023

  1. Apply callbacks with a type-stable generated function.

    Currently, as referenced in SciML/DifferentialEquations.jl#971, the old
    implementation of `handle_callbacks!` directly calls. `apply_callback!`
    on `continuous_callbacks[idx]`, which is inherently type-unstable
    because `apply_callback!` is specialized on the callback type.
    
    This commit adds a generated function `apply_ith_callback!` which
    generates type-stable code to do the same thing, where for each callback
    tuple type, the generated function unrolls the tuple by checking the
    callback index against static indicies. As a nice bonus, this generated
    function seems to often be converted into a switch statement at the LLVM
    level:
    ```
       switch i64 %4, label %L46 [
        i64 9, label %L3
        i64 8, label %L8
        i64 7, label %L13
        i64 6, label %L18
        i64 5, label %L23
        i64 4, label %L28
        i64 3, label %L33
        i64 2, label %L38
        i64 1, label %L43
      ]
    ```
    
    For testing, I added an allocation test which sets up a simple ODE
    problem, steps the integrator manually to before the first callback,
    then manipulates integrator state past the first callback point. This
    way, we can directly call `handle_callbacks!` and write a test on the
    allocation count. I confirm that (at least testing against commit
    SciML/DiffEqBase.jl@1799fc3, the current master branch tip in
    DiffEqBase.jl), the new method does not allocate, whereas the old one
    allocates. This may not be the case until a new release is cut of
    DiffEqBase.jl, because the old version of
    `find_first_continuous_callback` might allocate.
    meson800 committed Aug 23, 2023
    Configuration menu
    Copy the full SHA
    9c350f5 View commit details
    Browse the repository at this point in the history

Commits on Aug 24, 2023

  1. Configuration menu
    Copy the full SHA
    918c8d9 View commit details
    Browse the repository at this point in the history
  2. Update Project.toml

    ChrisRackauckas authored Aug 24, 2023
    Configuration menu
    Copy the full SHA
    264edd9 View commit details
    Browse the repository at this point in the history