Skip to content

Commit

Permalink
further adjustments for FMISensitiviity.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
ThummeTo committed Oct 9, 2023
1 parent 4d65a22 commit ec59113
Show file tree
Hide file tree
Showing 12 changed files with 2,030 additions and 329 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Requires = "ae029012-a4dd-5104-9daa-d747884805df"
ThreadPools = "b189fb0b-2eb5-4ed4-bc0c-d34c51242431"

[compat]
DiffEqCallbacks = "2.26.0"
DifferentialEquations = "7.7.0"
DiffEqCallbacks = "2.33.0"
DifferentialEquations = "7.10.0"
FMIExport = "0.3.0"
FMIImport = "0.16.0"
ProgressMeter = "1.7.0"
Expand Down
1 change: 1 addition & 0 deletions examples/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FMI_*_Benchmark/
1,765 changes: 1,750 additions & 15 deletions examples/src/benchmark.ipynb

Large diffs are not rendered by default.

81 changes: 51 additions & 30 deletions examples/src/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,42 @@
@everywhere workers using BenchmarkTools
@everywhere workers using Sundials

@everywhere function evalBenchmark(b; kwargs...)
res = run(b; kwargs...)
using Suppressor

@everywhere function evalBenchmark(b; samples=2, seconds=600, kwargs...)
res = nothing
try
res = run(b; samples=samples, seconds=seconds, kwargs...)
catch e
@error "Benchmark failed:\n$(e)"
return 0.0, 0, 0
end

if length(res.times) <= 1
@error "Only ran $(length(res.times)) benchmarks, this is not enough because of the first compilation run!"
end

min_time = min(res.times...)
memory = res.memory
allocs = res.allocs
return min_time, memory, allocs
end

@everywhere function benchmark1(fmu, data, cvode)
return @benchmarkable fmiSimulate(fmu, (data.consumption_t[1], data.consumption_t[end]); parameters=data.params, saveat=data.consumption_t, recordValues=realVRs, showProgress=false, solver=cvode, reltol=1e-4);
@everywhere workers function benchmarkSimulation(fmu, data, solver)
return @benchmarkable fmiSimulate(fmu, (data.consumption_t[1], data.consumption_t[end]); parameters=data.params, saveat=data.consumption_t, recordValues=realVRs, showProgress=false, solver=solver, reltol=1e-4);
end

@everywhere workers using ReverseDiff

@everywhere workers function loss(p, x0, fmu, data, solver)
fmu.optim_p = p
fmu.optim_p_refs = collect(fmi2StringToValueReference(fmu, vr) for vr in keys(data.params))
sol = fmiSimulate(fmu, (data.consumption_t[1], 1.0); x0=x0, parameters=data.params, saveat=data.consumption_t, showProgress=false, solver=solver, reltol=1e-4);
return sum(collect(u[1] for u in sol.states))
end

@everywhere workers function benchmarkGradient(p, x0, fmu, data, solver)
return @benchmarkable grad(p, x0, fmu, data, solver)
end

@everywhere function versionName(version)
Expand All @@ -37,51 +63,46 @@ end
Pkg.activate("FMI_" * name * "_Benchmark")

if ispath
Pkg.add(url=version[5:end])
Pkg.develop(url=version[5:end])
else
Pkg.add(name="FMI", version=version)
end

Pkg.add("FMIZoo")
end

futures = Vector{Any}(undef, numVersions)
for i in 1:numVersions
futures[i] = @spawnat workers[i] setupVersion(versions[i]);
end

fetch.(futures)
# install FMISensitivity for FMI.jl >= 0.13.0
if !ispath
f, m, b = split(version, ".")
f = parse(Float64, f)
m = parse(Float64, m)
b = parse(Float64, b)

@everywhere workers using FMI, FMIZoo
@everywhere workers data = FMIZoo.VLDM(:train)
@everywhere workers fmu = fmiLoad("VLDM", "Dymola", "2020x"; type=:ME)
@everywhere workers using FMI.DifferentialEquations: Tsit5
@everywhere workers cvode = CVODE_BDF()

@everywhere workers realBuffer = zeros(fmi2Real, 2)
@everywhere workers realVRs = vcat(fmu.modelDescription.stateValueReferences, fmu.modelDescription.derivativeValueReferences)

@everywhere workers c=FMI.fmi2Instantiate!(fmu)
@everywhere workers FMI.fmi2EnterInitializationMode(fmu)
if f >= 1 || m >= 13
Pkg.add(name="FMISensitivity")
end
end
end

function runBenchmark(b)
function runBenchmark()

min_times = zeros(numVersions)
memories = zeros(numVersions)
allocs = zeros(numVersions)

# simulate one after another, so we don't have negative influences between processes
for i in 1:numVersions
future = @spawnat workers[i] evalBenchmark(b; samples=3, seconds=60)
min_times[i], memories[i], allocs[i] = fetch(future)
@suppress begin
# simulate one after another, so we don't have negative influences between processes
for i in 1:numVersions
future = @spawnat workers[i] evalBenchmark(b; samples=3)
min_times[i], memories[i], allocs[i] = fetch(future)
end
end

return min_times, memories, allocs
end

function round_or_empty(val; suffix="", digits=0)
if val == 0.0
return ""
return "[n.a.]"
elseif digits > 0
return "" * "$(round(val; digits=digits))" * suffix
else
Expand Down Expand Up @@ -117,7 +138,7 @@ function resultPlot(versions, min_times, memories)
for i in 1:length(versions)
annotate!(fig, 2+(i-1)*3, min_times[i]/2.0, text(round_or_empty(min_times[i]; digits=1, suffix=" s"); halign=:center, valign=:center, rotation=90, color=:white))
end
bar!(twinx(), y2s; ylabel="Memory Allocations [MB]", color=:red, xticks=:none, legend=:none)
bar!(twinx(), y2s; ylabel="Allocated Memory [MB]", color=:red, xticks=:none, legend=:none)
for i in 1:length(versions)
annotate!(fig, 3+(i-1)*3, memories[i]/2.0, text(round_or_empty(memories[i];digits=0, suffix=" MB"); halign=:center, valign=:center, rotation=90, color=:white))
end
Expand Down
Loading

0 comments on commit ec59113

Please sign in to comment.