Skip to content

Commit cab626e

Browse files
authored
Merge pull request #769 from dpad/s/new-build_function
Work around to specify cache module when building RuntimeGeneratedFunctions
2 parents 41130c8 + 5128b18 commit cab626e

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

src/build_function.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ function _build_and_inject_function(mod::Module, ex)
101101
elseif ex.head == :(->)
102102
return _build_and_inject_function(mod, Expr(:function, ex.args...))
103103
end
104-
@RuntimeGeneratedFunction(mod, ex)
104+
# XXX: Workaround to specify the module as both the cache module AND context module.
105+
# Currently, the @RuntimeGeneratedFunction macro only sets the context module.
106+
module_tag = getproperty(mod, RuntimeGeneratedFunctions._tagname)
107+
RuntimeGeneratedFunctions.RuntimeGeneratedFunction(module_tag, module_tag, ex)
105108
end
106109

107110
# Detect heterogeneous element types of "arrays of matrices/sparce matrices"

test/precompile_test.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,15 @@ using ODEPrecompileTest
88
u = collect(1:3)
99
p = collect(4:6)
1010

11-
# This case does not work, because "f_bad" gets defined in ModelingToolkit
12-
# instead of in the compiled module!
11+
# These cases do not work, because they get defined in the ModelingToolkit's RGF cache.
1312
@test parentmodule(typeof(ODEPrecompileTest.f_bad.f.f_iip).parameters[2]) == ModelingToolkit
1413
@test parentmodule(typeof(ODEPrecompileTest.f_bad.f.f_oop).parameters[2]) == ModelingToolkit
1514
@test parentmodule(typeof(ODEPrecompileTest.f_noeval_bad.f.f_iip).parameters[2]) == ModelingToolkit
1615
@test parentmodule(typeof(ODEPrecompileTest.f_noeval_bad.f.f_oop).parameters[2]) == ModelingToolkit
1716
@test_throws KeyError ODEPrecompileTest.f_bad(u, p, 0.1)
1817
@test_throws KeyError ODEPrecompileTest.f_noeval_bad(u, p, 0.1)
1918

20-
# This case works, because "f_good" gets defined in the precompiled module.
21-
@test parentmodule(typeof(ODEPrecompileTest.f_good.f.f_iip).parameters[2]) == ODEPrecompileTest
22-
@test parentmodule(typeof(ODEPrecompileTest.f_good.f.f_oop).parameters[2]) == ODEPrecompileTest
19+
# This case works, because it gets defined with the appropriate cache and context tags.
2320
@test parentmodule(typeof(ODEPrecompileTest.f_noeval_good.f.f_iip).parameters[2]) == ODEPrecompileTest
2421
@test parentmodule(typeof(ODEPrecompileTest.f_noeval_good.f.f_oop).parameters[2]) == ODEPrecompileTest
25-
@test ODEPrecompileTest.f_good(u, p, 0.1) == [4, 0, -16]
2622
@test ODEPrecompileTest.f_noeval_good(u, p, 0.1) == [4, 0, -16]

test/precompile_test/ODEPrecompileTest.jl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module ODEPrecompileTest
55
# Define some variables
66
@parameters t σ ρ β
77
@variables x(t) y(t) z(t)
8-
@derivatives D'~t
8+
D = Differential(t)
99

1010
# Define a differential equation
1111
eqs = [D(x) ~ σ*(y-x),
@@ -16,17 +16,14 @@ module ODEPrecompileTest
1616
return ODEFunction(de, [x,y,z], [σ,ρ,β]; kwargs...)
1717
end
1818

19-
# Build an ODEFunction as part of the module's precompilation. This case
20-
# will not work, because the generated RGFs will be put into
21-
# ModelingToolkit's RGF cache.
19+
# Build an ODEFunction as part of the module's precompilation. These cases
20+
# will not work, because the generated RGFs are put into the ModelingToolkit cache.
2221
const f_bad = system()
22+
const f_noeval_bad = system(; eval_expression=false)
2323

24-
# This case will work, because it will be put into our own module's cache.
24+
# Setting eval_expression=false and eval_module=[this module] will ensure
25+
# the RGFs are put into our own cache, initialised below.
2526
using RuntimeGeneratedFunctions
2627
RuntimeGeneratedFunctions.init(@__MODULE__)
27-
const f_good = system(; eval_module=@__MODULE__)
28-
29-
# Also test that eval_expression=false works
30-
const f_noeval_bad = system(; eval_expression=false)
3128
const f_noeval_good = system(; eval_expression=false, eval_module=@__MODULE__)
3229
end

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ using SafeTestsets, Test
2929
@safetestset "Depdendency Graph Test" begin include("dep_graphs.jl") end
3030
@safetestset "Function Registration Test" begin include("function_registration.jl") end
3131
@safetestset "Array of Array Test" begin include("build_function_arrayofarray.jl") end
32+
@safetestset "Precompiled Modules Test" begin include("precompile_test.jl") end
3233
@testset "Distributed Test" begin include("distributed.jl") end
3334
@safetestset "Variable Utils Test" begin include("variable_utils.jl") end
3435
println("Last test requires gcc available in the path!")
3536
@safetestset "C Compilation Test" begin include("ccompile.jl") end
3637
@safetestset "Latexify recipes Test" begin include("latexify.jl") end
37-
@safetestset "Precompiled Modules Test" begin include("precompile_test.jl") end

0 commit comments

Comments
 (0)