Skip to content

No recompile ODE setup #208

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

Merged
merged 5 commits into from
May 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 43 additions & 42 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
name = "DiffEqBase"
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
authors = ["Chris Rackauckas <[email protected]>"]
version = "v5.8.1"

[deps]
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
TreeViews = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
RecursiveFactorization = "f2c3362d-daeb-58d1-803e-2bc74f2840b4"
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"

[compat]
julia = "1"
RecipesBase = ">= 0.1.0"
RecursiveArrayTools = ">= 0.12.3"
IteratorInterfaceExtensions = "^0.1, ^1"
StaticArrays = ">= 0.8.3"
TableTraits = "^0.3, ^0.4, ^1"
MuladdMacro = ">= 0.2.1"
Parameters = ">= 0.10.0"

[extras]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Distributions", "SafeTestsets"]
name = "DiffEqBase"
uuid = "2b5f629d-d688-5b77-993f-72d75c75574e"
authors = ["Chris Rackauckas <[email protected]>"]
version = "5.8.1"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd"
RecursiveFactorization = "f2c3362d-daeb-58d1-803e-2bc74f2840b4"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c"
TreeViews = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7"

[compat]
IteratorInterfaceExtensions = "^0.1, ^1"
MuladdMacro = ">= 0.2.1"
Parameters = ">= 0.10.0"
RecipesBase = ">= 0.1.0"
RecursiveArrayTools = ">= 0.12.3"
StaticArrays = ">= 0.8.3"
TableTraits = "^0.3, ^0.4, ^1"
julia = "1"

[extras]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Distributions", "SafeTestsets"]
3 changes: 3 additions & 0 deletions src/DiffEqBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ using LinearAlgebra, Statistics

using DocStringExtensions

using FunctionWrappers: FunctionWrapper

using MuladdMacro, Parameters


# Problems
"""
$(TYPEDEF)
Expand Down
12 changes: 12 additions & 0 deletions src/problems/ode_problems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ struct ODEProblem{uType,tType,isinplace,P,F,C,PT} <:
function ODEProblem{iip}(f,u0,tspan,p=nothing;kwargs...) where {iip}
ODEProblem(convert(ODEFunction{iip},f),u0,tspan,p;kwargs...)
end

@add_kwonly function ODEProblem{iip,recompile}(f,u0,tspan,p=nothing;kwargs...) where {iip,recompile}
if !recompile
if iip
ODEProblem{iip}(wrapfun_iip(f,(u0,u0,p,tspan[1])),u0,tspan,p;kwargs...)
else
ODEProblem{iip}(wrapfun_oop(f,(u0,p,tspan[1])),u0,tspan,p;kwargs...)
end
else
ODEProblem{iip}(f,u0,tspan,p;kwargs...)
end
end
end

"""
Expand Down
17 changes: 17 additions & 0 deletions src/remake.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ function remake(thing; kwargs...)
T(; struct_as_namedtuple(thing)...,kwargs...)
end

isrecompile(prob::ODEProblem{iip}) where {iip} = (typeof(prob.f) isa ODEFunction) ? !(typeof(prob.f.f) <: FunctionWrapper) : true

function remake(thing::ODEProblem; kwargs...)
T = remaker_of(thing)
tup = merge(struct_as_namedtuple(thing),kwargs)
if !isrecompile(thing)
if isinplace(thing)
f = wrapfun_iip(unwrap_fw(tup.f.f),(tup.u0,tup.u0,tup.p,tup.tspan[1]))
else
f = wrapfun_oop(unwrap_fw(tup.f.f),(tup.u0,tup.p,tup.tspan[1]))
end
tup2 = (f = convert(ODEFunction{isinplace(thing)},f),)
tup = merge(tup, tup2)
end
T(; tup...)
end

function remake(thing::AbstractJumpProblem; kwargs...)
parameterless_type(thing)(remake(thing.prob;kwargs...))
end
15 changes: 14 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ function undefined_exports(mod)
return undefined
end

function wrapfun_oop(ff, inputs::Tuple)
IT = map(typeof, inputs)
FunctionWrapper{IT[1], Tuple{IT...}}((args...)->(ff(args...)))
end

function wrapfun_iip(ff, inputs::Tuple)
IT = map(typeof, inputs)
FunctionWrapper{Nothing, Tuple{IT...}}((args...)->(ff(args...); nothing))
end

function unwrap_fw(fw::FunctionWrapper)
fw.obj[]
end
_vec(v) = vec(v)
_vec(v::Number) = v
_vec(v::AbstractVector) = v
Expand All @@ -191,4 +204,4 @@ macro tight_loop_macros(ex)
end

# Overloaded in other repositories
function unwrap_cache end
function unwrap_cache end
9 changes: 9 additions & 0 deletions test/remake_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ f1 = DiffEqBase.TwoPointBVPFunction(() -> 1)
f2 = remake(f1; bc = () -> 2)
@test f1.bc() == 1
@test f2.bc() == 2

# Testing remake for no recompile
u0 = [0;2.]
tspan = (0.0,6.3)
prob = ODEProblem{true,false}((du,u,p,t) -> 2u,u0,tspan)

@test remake(prob) == prob
@test remake(prob; u0 = [1;2]).u0 == [1;2]
@test remake(prob; p = (1,2)).p == (1,2)