From 096e762455f36892d25780d998bae167f4e3ab43 Mon Sep 17 00:00:00 2001 From: Dan Padilha Date: Fri, 5 Feb 2021 13:27:49 +0900 Subject: [PATCH 1/2] Add work-around for RGF to set the cache module tag appropriately. --- src/build_function.jl | 5 ++++- test/precompile_test.jl | 8 ++------ test/precompile_test/ODEPrecompileTest.jl | 15 ++++++--------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/build_function.jl b/src/build_function.jl index 8d157f200e..1d6035755c 100644 --- a/src/build_function.jl +++ b/src/build_function.jl @@ -101,7 +101,10 @@ function _build_and_inject_function(mod::Module, ex) elseif ex.head == :(->) return _build_and_inject_function(mod, Expr(:function, ex.args...)) end - @RuntimeGeneratedFunction(mod, ex) + # XXX: Workaround to specify the module as both the cache module AND context module. + # Currently, the @RuntimeGeneratedFunction macro only sets the context module. + module_tag = getproperty(mod, RuntimeGeneratedFunctions._tagname) + RuntimeGeneratedFunctions.RuntimeGeneratedFunction(module_tag, module_tag, ex) end # Detect heterogeneous element types of "arrays of matrices/sparce matrices" diff --git a/test/precompile_test.jl b/test/precompile_test.jl index 61abebe55e..40a2c2452c 100644 --- a/test/precompile_test.jl +++ b/test/precompile_test.jl @@ -8,8 +8,7 @@ using ODEPrecompileTest u = collect(1:3) p = collect(4:6) -# This case does not work, because "f_bad" gets defined in ModelingToolkit -# instead of in the compiled module! +# These cases do not work, because they get defined in the ModelingToolkit's RGF cache. @test parentmodule(typeof(ODEPrecompileTest.f_bad.f.f_iip).parameters[2]) == ModelingToolkit @test parentmodule(typeof(ODEPrecompileTest.f_bad.f.f_oop).parameters[2]) == ModelingToolkit @test parentmodule(typeof(ODEPrecompileTest.f_noeval_bad.f.f_iip).parameters[2]) == ModelingToolkit @@ -17,10 +16,7 @@ p = collect(4:6) @test_throws KeyError ODEPrecompileTest.f_bad(u, p, 0.1) @test_throws KeyError ODEPrecompileTest.f_noeval_bad(u, p, 0.1) -# This case works, because "f_good" gets defined in the precompiled module. -@test parentmodule(typeof(ODEPrecompileTest.f_good.f.f_iip).parameters[2]) == ODEPrecompileTest -@test parentmodule(typeof(ODEPrecompileTest.f_good.f.f_oop).parameters[2]) == ODEPrecompileTest +# This case works, because it gets defined with the appropriate cache and context tags. @test parentmodule(typeof(ODEPrecompileTest.f_noeval_good.f.f_iip).parameters[2]) == ODEPrecompileTest @test parentmodule(typeof(ODEPrecompileTest.f_noeval_good.f.f_oop).parameters[2]) == ODEPrecompileTest -@test ODEPrecompileTest.f_good(u, p, 0.1) == [4, 0, -16] @test ODEPrecompileTest.f_noeval_good(u, p, 0.1) == [4, 0, -16] \ No newline at end of file diff --git a/test/precompile_test/ODEPrecompileTest.jl b/test/precompile_test/ODEPrecompileTest.jl index 453cb0d774..fc8d784d91 100644 --- a/test/precompile_test/ODEPrecompileTest.jl +++ b/test/precompile_test/ODEPrecompileTest.jl @@ -5,7 +5,7 @@ module ODEPrecompileTest # Define some variables @parameters t σ ρ β @variables x(t) y(t) z(t) - @derivatives D'~t + D = Differential(t) # Define a differential equation eqs = [D(x) ~ σ*(y-x), @@ -16,17 +16,14 @@ module ODEPrecompileTest return ODEFunction(de, [x,y,z], [σ,ρ,β]; kwargs...) end - # Build an ODEFunction as part of the module's precompilation. This case - # will not work, because the generated RGFs will be put into - # ModelingToolkit's RGF cache. + # Build an ODEFunction as part of the module's precompilation. These cases + # will not work, because the generated RGFs are put into the ModelingToolkit cache. const f_bad = system() + const f_noeval_bad = system(; eval_expression=false) - # This case will work, because it will be put into our own module's cache. + # Setting eval_expression=false and eval_module=[this module] will ensure + # the RGFs are put into our own cache, initialised below. using RuntimeGeneratedFunctions RuntimeGeneratedFunctions.init(@__MODULE__) - const f_good = system(; eval_module=@__MODULE__) - - # Also test that eval_expression=false works - const f_noeval_bad = system(; eval_expression=false) const f_noeval_good = system(; eval_expression=false, eval_module=@__MODULE__) end \ No newline at end of file From 5128b187c390d9acf8d3c00cfcbe284ba90705c9 Mon Sep 17 00:00:00 2001 From: Dan Padilha Date: Fri, 5 Feb 2021 13:55:19 +0900 Subject: [PATCH 2/2] Move precompiled modules test above the un-safe distributed test. --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 8be142e333..8d5bb1f18b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -29,9 +29,9 @@ using SafeTestsets, Test @safetestset "Depdendency Graph Test" begin include("dep_graphs.jl") end @safetestset "Function Registration Test" begin include("function_registration.jl") end @safetestset "Array of Array Test" begin include("build_function_arrayofarray.jl") end +@safetestset "Precompiled Modules Test" begin include("precompile_test.jl") end @testset "Distributed Test" begin include("distributed.jl") end @safetestset "Variable Utils Test" begin include("variable_utils.jl") end println("Last test requires gcc available in the path!") @safetestset "C Compilation Test" begin include("ccompile.jl") end @safetestset "Latexify recipes Test" begin include("latexify.jl") end -@safetestset "Precompiled Modules Test" begin include("precompile_test.jl") end