From f2f8fbc447b9e3a12c3c0f341fb4946422f64b79 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Thu, 20 Jun 2024 15:22:31 +0200 Subject: [PATCH 1/3] add change of default AD backend --- src/CTBase.jl | 3 ++- src/default.jl | 10 +++++++++- test/test_description.jl | 3 --- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/CTBase.jl b/src/CTBase.jl index 5e03391d..a7205edd 100644 --- a/src/CTBase.jl +++ b/src/CTBase.jl @@ -236,7 +236,8 @@ export Description, add, getFullDescription export CTException, ParsingError, AmbiguousDescription, IncorrectMethod export IncorrectArgument, IncorrectOutput, NotImplemented, UnauthorizedCall -# checking +# AD +export set_AD_backend # functions export Hamiltonian, HamiltonianVectorField, VectorField diff --git a/src/default.jl b/src/default.jl index 3d2f9acb..d9cd0a5e 100644 --- a/src/default.jl +++ b/src/default.jl @@ -1,3 +1,11 @@ +# +global __default_AD_backend = AutoForwardDiff() + +function set_AD_backend(AD) + global __default_AD_backend = AD + nothing +end + """ $(TYPEDSIGNATURES) @@ -5,7 +13,7 @@ Used to set the default value of Automatic Differentiation backend. The default value is `AutoForwardDiff()`, that is the `ForwardDiff` package is used by default. """ -__auto() = AutoForwardDiff() # default AD backend +__auto() = __default_AD_backend # default AD backend """ $(TYPEDSIGNATURES) diff --git a/test/test_description.jl b/test/test_description.jl index 5dd1bf23..87b2931c 100644 --- a/test/test_description.jl +++ b/test/test_description.jl @@ -7,9 +7,6 @@ descriptions = add(descriptions, (:b,)) @test descriptions[1] == (:a,) @test descriptions[2] == (:b,) -# print a tuple of descriptions -@test display(descriptions) isa Nothing - # get the complete description of the chosen method algorithmes = () algorithmes = add(algorithmes, (:descent, :bfgs, :bissection)) From db2c56215347b60215624c87a790834498d69421 Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Thu, 20 Jun 2024 15:37:19 +0200 Subject: [PATCH 2/3] remove global decla --- src/default.jl | 2 +- src/repl.jl | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/default.jl b/src/default.jl index d9cd0a5e..ea784699 100644 --- a/src/default.jl +++ b/src/default.jl @@ -1,5 +1,5 @@ # -global __default_AD_backend = AutoForwardDiff() +__default_AD_backend = AutoForwardDiff() function set_AD_backend(AD) global __default_AD_backend = AD diff --git a/src/repl.jl b/src/repl.jl index b9f31933..a17a5892 100644 --- a/src/repl.jl +++ b/src/repl.jl @@ -14,9 +14,9 @@ end end # -global ct_repl_is_set::Bool = false -global ct_repl_data::CTRepl -global ct_repl_ct_repl_history::HistoryRepl +ct_repl_is_set::Bool = false +ct_repl_data::CTRepl = CTRepl() +ct_repl_history::HistoryRepl = HistoryRepl(0, Vector{ModelRepl}()) """ $(TYPEDSIGNATURES) @@ -47,13 +47,14 @@ Create a ct REPL. """ function ct_repl(; debug=false, demo=false, verbose=false) + global ct_repl_is_set + global ct_repl_data + global ct_repl_history + if !ct_repl_is_set -# - global ct_repl_is_set = true - - # init: ct_repl_data, ct_repl_history - global ct_repl_data = CTRepl() - global ct_repl_history = HistoryRepl(0, Vector{ModelRepl}()) + + # + ct_repl_is_set = true # ct_repl_data.debug = debug From 351de7505682ec5ff7e391cd84ad08749afc79cc Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Thu, 20 Jun 2024 23:21:40 +0200 Subject: [PATCH 3/3] foo --- src/default.jl | 2 +- src/utils.jl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/default.jl b/src/default.jl index ea784699..c72b8f06 100644 --- a/src/default.jl +++ b/src/default.jl @@ -13,7 +13,7 @@ Used to set the default value of Automatic Differentiation backend. The default value is `AutoForwardDiff()`, that is the `ForwardDiff` package is used by default. """ -__auto() = __default_AD_backend # default AD backend +__get_AD_backend() = __default_AD_backend # default AD backend """ $(TYPEDSIGNATURES) diff --git a/src/utils.jl b/src/utils.jl index c608a0eb..f7df643c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -73,7 +73,7 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ -function ctgradient(f::Function, x::ctNumber; backend=__auto()) +function ctgradient(f::Function, x::ctNumber; backend=__get_AD_backend()) extras = prepare_derivative(f, backend, x) return derivative(f, backend, x, extras) end @@ -83,7 +83,7 @@ $(TYPEDSIGNATURES) Return the gradient of `f` at `x`. """ -function ctgradient(f::Function, x; backend=__auto()) +function ctgradient(f::Function, x; backend=__get_AD_backend()) extras = prepare_gradient(f, backend, x) return gradient(f, backend, x, extras) end @@ -100,7 +100,7 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ -function ctjacobian(f::Function, x::ctNumber; backend=__auto()) +function ctjacobian(f::Function, x::ctNumber; backend=__get_AD_backend()) f_number_to_number = only ∘ f ∘ only extras = prepare_derivative(f_number_to_number, backend, x) der = derivative(f_number_to_number, backend, x, extras) @@ -112,7 +112,7 @@ $(TYPEDSIGNATURES) Return the Jacobian of `f` at `x`. """ -function ctjacobian(f::Function, x; backend=__auto()) +function ctjacobian(f::Function, x; backend=__get_AD_backend()) extras = prepare_jacobian(f, backend, x) return jacobian(f, backend, x, extras) end