Skip to content
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

[Draft] Support setting parallel form in Symbolics dispatch #17

Merged
merged 3 commits into from
Feb 15, 2024
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
14 changes: 11 additions & 3 deletions src/parametric_problem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function ParametricMCP(
lower_bounds,
upper_bounds,
parameter_dimension;
compute_sensitivities = true,
kwargs...,
)
# TODO
length(lower_bounds) == length(upper_bounds) ||
Expand All @@ -67,7 +67,7 @@ function ParametricMCP(
θ_symbolic,
lower_bounds,
upper_bounds;
compute_sensitivities,
kwargs...,
)
end

Expand All @@ -81,6 +81,7 @@ function ParametricMCP(
lower_bounds,
upper_bounds;
compute_sensitivities = true,
parallel = nothing,
)
length(lower_bounds) == length(upper_bounds) ||
throw(ArgumentError("lower_bounds and upper_bounds have inconsistent lenghts."))
Expand All @@ -95,7 +96,12 @@ function ParametricMCP(

# compile all the symbolic expressions into callable julia code
f! = let
_f! = Symbolics.build_function(f_symbolic, [z_symbolic; θ_symbolic]; expression = Val{false})[2]
_f! = Symbolics.build_function(
f_symbolic,
[z_symbolic; θ_symbolic];
expression = Val{false},
parallel,
)[2]
(result, z, θ) -> _f!(result, [z; θ])
end

Expand All @@ -104,6 +110,7 @@ function ParametricMCP(
jacobian_z_symbolic,
[z_symbolic; θ_symbolic];
expression = Val{false},
parallel,
)[2]
rows, cols, _ = SparseArrays.findnz(jacobian_z_symbolic)

Expand All @@ -119,6 +126,7 @@ function ParametricMCP(
jacobian_θ_symbolic,
[z_symbolic; θ_symbolic];
expression = Val{false},
parallel,
)[2]
rows, cols, _ = SparseArrays.findnz(jacobian_θ_symbolic)
SparseFunction(rows, cols, size(jacobian_θ_symbolic)) do result, z, θ
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Random: Random
using LinearAlgebra: norm
using Zygote: Zygote
using FiniteDiff: FiniteDiff
using Symbolics: Symbolics

@testset "ParametricMCPs.jl" begin
rng = Random.MersenneTwister(1)
Expand All @@ -15,7 +16,7 @@ using FiniteDiff: FiniteDiff
lower_bounds = [-Inf, -Inf, 0, 0]
upper_bounds = [Inf, Inf, Inf, Inf]
problem = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension)
problem_no_jacobian = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension; compute_sensitivities=false)
problem_no_jacobian = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension; compute_sensitivities=false, parallel=Symbolics.ShardedForm())

feasible_parameters = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [rand(rng, 2) for _ in 1:10]...]
infeasible_parameters = -feasible_parameters
Expand Down
Loading