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

Set a default Clock depending on the grid #4096

Merged
merged 29 commits into from
Mar 9, 2025
Merged

Set a default Clock depending on the grid #4096

merged 29 commits into from
Mar 9, 2025

Conversation

glwagner
Copy link
Member

@glwagner glwagner commented Feb 12, 2025

This PR adds a constructor for Clock which depends on grid, and therefore architecture. This allows the clock to be traced when using arch = ReactantState().

In addition to that, this PR discontinues support for run! with arch = ReactantState(). Our rationale is that we cannot yet support the Simulation paradigm, which executes time-steps in a while loop until !(simulation.running), where simulation.running is typically controlled by if statements that depend, for example, on clock.iteration. Basically we're deciding not to try to use @trace if, yet.

As a result, time step loops with Reactant should be managed manually. To facilitate this, this PR also adds a function first_time_step! which first initialize!(model) and update_state!(model) before taking a time-step (which is limited to Euler step for AB2).

cc @giordano

@glwagner
Copy link
Member Author

I believe this is our error now:

Reactant Super Simple Simulation Tests: Error During Test at /Users/gregorywagner/Projects/dev/OC1.jl/test/test_reactant.jl:154
  Got exception outside of a @test
  MethodError: no method matching rem(::Reactant.TracedRNumber{Int64}, ::Int64)

  Closest candidates are:
    rem(::Any, ::Any, ::RoundingMode{:FromZero})
     @ Base div.jl:103
    rem(::Any, ::Any, ::RoundingMode{:Nearest})
     @ Base div.jl:100
    rem(::Any, ::Any, ::RoundingMode{:Up})
     @ Base div.jl:99
    ...

  Stacktrace:
    [1] macro expansion
      @ ~/.julia/packages/Reactant/qEA3Y/src/utils.jl:0 [inlined]
    [2] call_with_reactant(::typeof(rem), ::Reactant.TracedRNumber{Int64}, ::Int64)
      @ Reactant ~/.julia/packages/Reactant/qEA3Y/src/utils.jl:767
    [3] IterationInterval
      @ ~/Projects/dev/OC1.jl/src/Utils/schedules.jl:109 [inlined]

@wsmoses can we add support for rem? If not I think there may be other workarounds.

@glwagner
Copy link
Member Author

@giordano @wsmoses I'm hitting a road block and trying to figure out what is going wrong. I put together a very simple prototype of a function that iterates until a stopping criteria:

using Reactant

mutable struct TestClock{I}
    iteration :: I
end

mutable struct Stopper{I}
    iteration :: I
end

function iterate_until!(clock, stopper)
    iterate = true
    while iterate
        @trace if clock.iteration >= stopper.iteration
            iterate = false
        else
            clock.iteration += 1
        end
    end
    return nothing
end

clock = TestClock(ConcreteRNumber(0))
#stopper = Stopper(ConcreteRNumber(2))
stopper = Stopper(2)

r_iterate! = @compile sync=true iterate_until!(clock, stopper)
r_iterate!(clock, stopper)
@show clock.iteration

This fails with

ERROR: LoadError: TypeError: non-boolean (Reactant.TracedRNumber{Bool}) used in boolean context
Stacktrace:
  [1] macro expansion
    @ ~/.julia/packages/ReactantCore/t40zc/src/ReactantCore.jl:378 [inlined]
  [2] iterate_until!
    @ ~/Projects/Oceananigans.jl/test/test_reactant.jl:31 [inlined]
  [3] iterate_until!(none::TestClock{Reactant.TracedRNumber{Int64}}, none::Stopper{Int64})
    @ Reactant ./<missing>:0
  [4] iterate_until!
    @ ~/Projects/Oceananigans.jl/test/test_reactant.jl:30 [inlined]

where line 31 is @trace if clock.iteration >= stopper.iteration.

I'm also wondering whether the stopper.iteration needs to be traced, or not?

I think getting this prototype to work will show me how to get this PR merged

@glwagner
Copy link
Member Author

@glwagner
Copy link
Member Author

@giordano I think we've made progress on this and now run! actually "starts to" compile. However, it takes a long time. In fact it is still going

image

@glwagner
Copy link
Member Author

Note running the tests requires adding TestEnv to your global environment and then

using TestEnv; TestEnv.activate()
using Pkg; Pkg.add("Reactant", rev="glw-wm/traced-set-path")
include("test/test_reactant.jl")

@glwagner
Copy link
Member Author

glwagner commented Mar 9, 2025

This is ready and tests will hopefully pass soon @wsmoses @simone-silvestri @giordano . I've updated the top post to describe what we've done.

@glwagner glwagner merged commit 5b0cec5 into main Mar 9, 2025
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants