You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We should add a meta collection utility, for improving algorithm transparency, tests and optimization monitoring. Here's a scratch pad of what I had locally:
using Revise;
import ClimaTimeSteppers as CTS
import OrdinaryDiffEq as ODE
import LinearAlgebra as LA
struct SchurComplementW{T}; p::T; end
Base.similar(w::SchurComplementW) = w
# T_imp!(Yₜ, Y, p, t) = collect_meta(p, t, T_imp!)# T_exp!(Yₜ, Y, p, t) = collect_meta(p, t, T_exp!)# Wfact!(Yₜ, Y, p, Δt, t) = collect_meta(p, t, Wfact!)# implicit_tendency!(Yₜ, Y, p, t) = collect_meta(p, t, implicit_tendency!)# explicit_tendency!(Yₜ, Y, p, t) = collect_meta(p, t, explicit_tendency!)# dss!(Y, p, t) = collect_meta(p, t, dss!)# lim!(U, p, t, u) = collect_meta(p, t, lim!)# T_lim!(Yₜ, Y, p, t) = collect_meta(p, t, T_lim!)# stage_callback!(Y, p, t) = collect_meta(p, t, stage_callback!)# tgrad!(Yₜ, Y, p, t) = collect_meta(p, t, tgrad!)# LA.ldiv!(x, W::SchurComplementW, b) = collect_meta(p, nothing, LA.ldiv!)Wfact!(Yₜ, Y, p, Δt, t) =collect_meta(p, t, :Wfact!)
implicit_tendency!(Yₜ, Y, p, t) =collect_meta(p, t, :implicit_tendency!)
explicit_tendency!(Yₜ, Y, p, t) =collect_meta(p, t, :explicit_tendency!)
dss!(Y, p, t) =nothinglim!(U, p, t, u) =nothingT_lim!(Yₜ, Y, p, t) =nothingstage_callback!(Y, p, t) =nothingtgrad!(Yₜ, Y, p, t) =collect_meta(p, t, :tgrad!)
LA.ldiv!(x, W::SchurComplementW, b) =collect_meta(p, nothing, :ldiv!)
struct AlgoMeta{D, T}
call_counter_dict::D
func_call_order::TfunctionAlgoMeta()
func_call_order = []
call_counter_dict =Dict{Symbol,Vector{Int}}()
D =typeof(call_counter_dict)
T =typeof(func_call_order)
returnnew{D, T}(call_counter_dict, func_call_order)
endendfunctioncollect_meta(p, t, f)
(; meta) = p
fun =Symbol(f)
push!(meta.func_call_order, (fun, t))
ifhaskey(meta.call_counter_dict, fun)
meta.call_counter_dict[fun][1] +=1else
meta.call_counter_dict[fun] = Int[0]
endend
FT = Float64
Y₀ =zeros(FT, 1)
p = (; meta =AlgoMeta())
jac_prototype=SchurComplementW(p)
func_args = (; jac_prototype, Wfact = Wfact!, tgrad = tgrad!)
split_tendency_func = CTS.ClimaODEFunction(;
T_exp! = explicit_tendency!,
T_imp! = ODE.ODEFunction(implicit_tendency!; func_args...),
dss! = dss!,
lim!,
T_lim!,
stage_callback!,
)
prob = ODE.ODEProblem(split_tendency_func, Y₀, (FT(0), FT(1)), p)
alg = CTS.ARS343(CTS.NewtonsMethod(; max_iters=3))
integrator = ODE.init(prob, alg; dt=FT(0.1), save_everystep =true)
CTS.step_u!(integrator);
functionsummarize(meta::AlgoMeta)
println("------------- Function call order")
for (func, t) in meta.func_call_order
println("$func called at time $t")
endprintln("------------- N-total calls")
for k inkeys(meta.call_counter_dict)
meta.call_counter_dict[k] == [0] &&continueprintln("$(meta.call_counter_dict[k]): $k")
endprintln("-------------")
returnnothingendsummarize(p.meta)
using Revise;
import ClimaTimeSteppers as CTS
import OrdinaryDiffEq as ODE
import LinearAlgebra as LA
struct SchurComplementW{T}; p::T; end
Base.similar(w::SchurComplementW) = w
Wfact!(Yₜ, Y, p, Δt, t) =collect_meta(p, t, :Wfact!)
implicit_tendency!(Yₜ, Y, p, t) =collect_meta(p, t, :implicit_tendency!)
explicit_tendency!(Yₜ, Y, p, t) =collect_meta(p, t, :explicit_tendency!)
dss!(Y, p, t) =nothinglim!(U, p, t, u) =nothingT_lim!(Yₜ, Y, p, t) =nothingstage_callback!(Y, p, t) =nothingtgrad!(Yₜ, Y, p, t) =collect_meta(p, t, :tgrad!)
LA.ldiv!(x, W::SchurComplementW, b) =collect_meta(p, nothing, :ldiv!)
struct AlgoMeta{D, T}
call_counter_dict::D
func_call_order::TfunctionAlgoMeta()
func_call_order = []
call_counter_dict =Dict{Symbol,Vector{Int}}()
D =typeof(call_counter_dict)
T =typeof(func_call_order)
returnnew{D, T}(call_counter_dict, func_call_order)
endendfunctioncollect_meta(p, t, f)
(; meta) = p
fun =Symbol(f)
push!(meta.func_call_order, (fun, t))
ifhaskey(meta.call_counter_dict, fun)
meta.call_counter_dict[fun][1] +=1else
meta.call_counter_dict[fun] = Int[0]
endend
FT = Float64;
Y₀ =zeros(FT, 1);
p = (; meta =AlgoMeta());
jac_prototype=SchurComplementW(p);
tspan = (FT(0), FT(1));
jac_kwargs = (; jac_prototype, Wfact = Wfact!);
remaining_func = explicit_tendency!;
prob = ODE.SplitODEProblem(
ODE.ODEFunction(
implicit_tendency!;
jac_kwargs...,
tgrad = tgrad!,
),
remaining_func,
Y₀,
tspan,
p,
);
alg = CTS.ARS343(CTS.NewtonsMethod(; max_iters=3));
integrator = ODE.init(prob, alg; dt=FT(0.1), save_everystep =true);
CTS.step_u!(integrator);
functionsummarize(meta::AlgoMeta)
println("------------- Function call order")
for (func, t) in meta.func_call_order
println("$func called at time $t")
endprintln("------------- N-total calls")
for k inkeys(meta.call_counter_dict)
meta.call_counter_dict[k] == [0] &&continueprintln("$(meta.call_counter_dict[k]): $k")
endprintln("-------------")
returnnothingendsummarize(p.meta)
The text was updated successfully, but these errors were encountered:
We should add a meta collection utility, for improving algorithm transparency, tests and optimization monitoring. Here's a scratch pad of what I had locally:
The text was updated successfully, but these errors were encountered: