Skip to content

Commit

Permalink
Merge pull request #113 from ModiaSim/mo_improved_startup
Browse files Browse the repository at this point in the history
Improved generation of sysimage
  • Loading branch information
MartinOtter authored Apr 18, 2022
2 parents 6408163 + aebfaf4 commit 8849af7
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ version = "0.8.6"

[[deps.DoubleFloats]]
deps = ["GenericLinearAlgebra", "LinearAlgebra", "Polynomials", "Printf", "Quadmath", "Random", "Requires", "SpecialFunctions"]
git-tree-sha1 = "58b203210416b70fc4f41eed1d55bfe876ef90db"
git-tree-sha1 = "da91494c1ebc1c057f21b6bef21e89ad5288c2c1"
uuid = "497a8b3b-efae-58df-a0af-a86822472b78"
version = "1.2.0"
version = "1.2.1"

[[deps.Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
Expand Down
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
authors = ["Andrea Neumayr <andrea.neumayr@dlr.de>", "Martin Otter <Martin.Otter@dlr.de>", "Gerhard Hippmann <gerhard.hippmann@dlr.de>"]
name = "Modia3D"
uuid = "07f2c1e0-90b0-56cf-bda7-b44b56e34eed"
version = "0.10.3"
version = "0.10.4"

[compat]
Colors = "0.12, 0.11, 0.10"
Expand All @@ -23,6 +23,7 @@ URIParser = "0.4"
julia = "1.7"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DoubleFloats = "497a8b3b-efae-58df-a0af-a86822472b78"
Expand Down
17 changes: 14 additions & 3 deletions create_Modia3D_sysimage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#
module Create_Modia3D_sysimage

path = dirname(@__FILE__)
file = joinpath(path, "create_Modia3D_sysimage.jl")
path = dirname(@__FILE__)
file = joinpath(path, "create_Modia3D_sysimage.jl")
precompile_file = joinpath(path, "create_Modia3D_sysimage_precompile_statements_file.jl")
Modia3D_sysimage_path = joinpath(pwd(), Sys.iswindows() ? "Modia3D_sysimage.dll" : "Modia3D_sysimage.so")

import Pkg
Expand All @@ -31,8 +32,18 @@ if length(addPackages) > 0
Pkg.add(addPackages)
end

#= Seem to make startup slower
println("!!! Define additional artefacts for precompilation ...")
using Modia3D
usePlotPackage("PyPlot")
@usingModiaPlot
include("$path/test/Basic/ModelsForPrecompilation.jl")
=#

# Create sysimage
using PackageCompiler
create_sysimage(sysimage_path = Modia3D_sysimage_path)
create_sysimage(sysimage_path = Modia3D_sysimage_path,
precompile_statements_file = precompile_file)

println("!!! Modia3D sysimage created. Use sysimage by starting julia with:")
println(" julia -J$Modia3D_sysimage_path")
Expand Down
29 changes: 29 additions & 0 deletions create_Modia3D_sysimage_precompile_statements_file.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
precompile(Tuple{typeof(Modia3D.ModiatorServer)})
precompile(Tuple{typeof(Base.setindex!), Base.Dict{HTTP.Handlers.Route, Any}, Function, HTTP.Handlers.Route})
precompile(Tuple{typeof(HTTP.Handlers.gh), String})
precompile(Tuple{typeof(Base.println), Base.TTY})
precompile(Tuple{typeof(HTTP.Servers.listen), Function, String, Int64})
precompile(Tuple{typeof(HTTP.Servers.handle_connection), Function, HTTP.ConnectionPool.Connection{Sockets.TCPSocket}, HTTP.Servers.Server{Nothing, Sockets.TCPServer}, Int64, Int64})
precompile(Tuple{typeof(Base.readuntil), HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, Function, Int64})
precompile(Tuple{typeof(Base.alloc_buf_hook), Sockets.TCPSocket, UInt64})
precompile(Tuple{typeof(Base.readuntil), Base.GenericIOBuffer{Array{UInt8, 1}}, typeof(HTTP.Parsers.find_end_of_header)})
precompile(Tuple{typeof(Core.Compiler.convert), Type{DataType}, Type{Tuple{Union{Base.SubString{String}, String}, Int64}}})
precompile(Tuple{Base.Cartesian.var"#@ncall", LineNumberNode, Module, Int64, Any, Vararg{Any}})
precompile(Tuple{Type{Sundials.CVODE_BDF{Method, LinearSolver, P, PS} where PS where P where LinearSolver where Method}})
precompile(Tuple{Type{Sundials.CVODE_BDF{:Newton, :Dense, Nothing, Nothing}}, Int64, Int64, Int64, Bool, Int64, Int64, Int64, Int64, Int64, Nothing, Nothing, Int64})
precompile(Tuple{typeof(Base.hasfield), Type, Symbol})
precompile(Tuple{typeof(Base.fieldindex), DataType, Symbol, Bool})
precompile(Tuple{Type{Sundials.CVODE_BDF{:Newton, :GMRES, Nothing, Nothing}}, Int64, Int64, Int64, Bool, Int64, Int64, Int64, Int64, Int64, Nothing, Nothing, Int64})
precompile(Tuple{typeof(HTTP.Handlers.handle), HTTP.Handlers.RequestHandlerFunction{typeof(Modia3D.simulateModel)}, HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}})
precompile(Tuple{typeof(HTTP.ConnectionPool.hashconn), Type, String, String, Int64, Bool, Bool})
precompile(Tuple{typeof(JSON.Parser.parse_object), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState})
precompile(Tuple{typeof(JSON.Parser.parse_string), JSON.Parser.MemoryParserState})
precompile(Tuple{typeof(JSON.Parser.predict_string), JSON.Parser.MemoryParserState})
precompile(Tuple{typeof(JSON.Parser.parse_string), JSON.Parser.MemoryParserState, Base.GenericIOBuffer{Array{UInt8, 1}}})
precompile(Tuple{typeof(JSON.Parser.parse_number), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState})
precompile(Tuple{typeof(JSON.Parser.number_from_bytes), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState, Bool, JSON.Parser.MemoryParserState, Int64, Int64})
precompile(Tuple{typeof(JSON.Parser.hasleadingzero), JSON.Parser.MemoryParserState, Int64, Int64})
precompile(Tuple{typeof(JSON.Parser.int_from_bytes), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState, JSON.Parser.MemoryParserState, Int64, Int64})
precompile(Tuple{typeof(Parsers.tryparse), Type{Float64}, Base.SubString{String}, Parsers.Options, Int64, Int64})
precompile(Tuple{typeof(JSON.Parser.parse_jsconstant), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState})
precompile(Tuple{typeof(Modia3D.generateSimulationResult), Base.Dict{String, Any}})
5 changes: 5 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ julia -JModia3D_sysimage.so (otherwise)

## Release Notes

### Version 0.10.4

- Script `Modia3D/create_Modia3D_sysimage.jl` improved.


### Version 0.10.3

- New script `Modia3D/create_Modia3D_sysimage.jl` to create a sysimage for a much faster startup
Expand Down
8 changes: 3 additions & 5 deletions src/Modia3D.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Modia3D

const path = dirname(dirname(@__FILE__)) # Absolute path of package directory
const Version = "0.10.3"
const Version = "0.10.4"
const Date = "2022-04-18"

println("\nImporting Modia3D Version $Version ($Date)")
Expand Down Expand Up @@ -156,7 +156,6 @@ function reenableRenderer()
return nothing
end


export PointGravityField, NoGravityField

export print_ModelVariables
Expand All @@ -167,8 +166,7 @@ export rereadContactPairMaterialFromJSON
import StaticArrays
import LinearAlgebra
import Test
import JSON
import HTTP
import URIParser

include("ModiatorServer.jl")

end # module
75 changes: 39 additions & 36 deletions src/ModiatorServer.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
module ModiatorServer
export ModiatorServer

using Modia3D
using Modia3D.StaticArrays
using Modia3D.JSON
using Modia3D.HTTP
using Modia3D.URIParser
using Base64 # from Base
import JSON
import HTTP
import URIParser
using Base64

# ----------------------------------------------------------------------------------------

function saveJSON(req)
jsonString = String(req.body);
index = findfirst("%25NAMEEND%25", jsonString);
name = jsonString[1:index[1]-1]
println("Saving ", name)
jsonString = jsonString[index[end] + 1:end]
jsonString = unescape(jsonString)
jsonString = URIParser.unescape(jsonString)
println(jsonString)
io = open(string(name, ".json"), "w");
write(io, jsonString);
Expand Down Expand Up @@ -48,7 +45,7 @@ function saveOBJ(req)
name = objString[1:index[1]-1]
println("Saving ", name)
objString = objString[index[end] + 1:end]
objString = unescape(objString)
objString = URIParser.unescape(objString)
io = open(string(name, ".obj"), "w");
write(io, objString);
close(io)
Expand Down Expand Up @@ -105,7 +102,7 @@ function simulateModel(req)
return HTTP.Response(200, headers)
end
model = String(req.body);
model = unescape(model);
model = URIParser.unescape(model);
experiment = JSON.parse(model);
@time json = generateSimulationResult(experiment);
# open("SimRes.txt", "w") do f
Expand All @@ -116,7 +113,6 @@ function simulateModel(req)
return response;
end

@usingModiaPlot

function generateSimulationResult(experiment)
println()
Expand Down Expand Up @@ -169,29 +165,36 @@ function unknownRequest(req)
return HTTP.Response(200, "OK")
end

const ROUTER = HTTP.Router()

HTTP.@register(ROUTER, "POST", "/Modiator/simulateModel/", simulateModel)
HTTP.@register(ROUTER, "OPTIONS", "/Modiator/simulateModel/", simulateModel)

HTTP.@register(ROUTER, "POST", "/Modiator/saveOBJ/", saveOBJ)
HTTP.@register(ROUTER, "POST", "/Modiator/loadOBJ/", loadOBJ)
HTTP.@register(ROUTER, "POST", "/Modiator/loadImageBase64/", loadImageBase64)
HTTP.@register(ROUTER, "POST", "/Modiator/saveJSON/", saveJSON)
HTTP.@register(ROUTER, "POST", "/Modiator/loadJSON/", loadJSON)
#HTTP.@register(ROUTER, "GET", "/Modiator/public/*", getPublic)
#HTTP.@register(ROUTER, "GET", "/Modiator/icons/*", getIcon)
HTTP.@register(ROUTER, "GET", "/*", unknownRequest)
HTTP.@register(ROUTER, "POST", "/*", unknownRequest)

# println("Precompiling")
# precompile(generateSimulationResult, (Dict,))
# precompile(HTTP.Router)
println("Server ready")
println("Listening to: 127.0.0.1:8000")
println()
println("Note that it might take more than 20 seconds for the first simulation to start.")
function ModiatorServer()::Nothing
ROUTER = HTTP.Router()

HTTP.@register(ROUTER, "POST", "/Modiator/simulateModel/", simulateModel)
HTTP.@register(ROUTER, "OPTIONS", "/Modiator/simulateModel/", simulateModel)

HTTP.@register(ROUTER, "POST", "/Modiator/saveOBJ/", saveOBJ)
HTTP.@register(ROUTER, "POST", "/Modiator/loadOBJ/", loadOBJ)
HTTP.@register(ROUTER, "POST", "/Modiator/loadImageBase64/", loadImageBase64)
HTTP.@register(ROUTER, "POST", "/Modiator/saveJSON/", saveJSON)
HTTP.@register(ROUTER, "POST", "/Modiator/loadJSON/", loadJSON)
#HTTP.@register(ROUTER, "GET", "/Modiator/public/*", getPublic)
#HTTP.@register(ROUTER, "GET", "/Modiator/icons/*", getIcon)
HTTP.@register(ROUTER, "GET", "/*", unknownRequest)
HTTP.@register(ROUTER, "POST", "/*", unknownRequest)

# println("Precompiling")
# precompile(generateSimulationResult, (Dict,))
# precompile(HTTP.Router)
println("Server ready")
println("Listening to: 127.0.0.1:8000")
println()
println("Note that it might take more than 20 seconds for the first simulation to start.")

HTTP.serve(ROUTER, "127.0.0.1", 8000)
return nothing
end

HTTP.serve(ROUTER, "127.0.0.1", 8000)
function julia_main()::Cint
ModiatorServer()
return 0
end

end
30 changes: 30 additions & 0 deletions test/Basic/ModelsForPrecompilation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module ModelsForPrecompilation

using Modia3D

Dummy = Model3D(
world = Object3D(feature=Scene(provideAnimationHistory=true, enableVisualization=false,
enableContactDetection=true,
gravityField=UniformGravityField(g=9.81, n=[0, -1, 0]))),

body1 = Object3D(feature=Solid(shape=Beam(axis=1, length=1.0, width=0.1, thickness=0.1),
solidMaterial="Aluminium")),
frame1 = Object3D(parent=:body1, translation=[-0.5, 0.0, 0.0]),
rev = Revolute(obj1=:world, obj2=:frame1),

body2 = Object3D(feature=Solid(shape=Box(lengthX=1.0, lengthY=0.1, lengthZ=0.1), massProperties=MassProperties(mass=1.0))),
prism = Prismatic(obj1=:world , obj2=:body2, axis=2),

body3 = Object3D(feature=Solid(shape=Sphere(diameter=0.2),
visualMaterial=VisualMaterial(color="Blue"),
solidMaterial="Steel",
massProperties=MassPropertiesFromShapeAndMass(mass=0.001),
collision=true)),
free = FreeMotion(obj1=:world, obj2=:body3, r=Var(init=Modia.SVector{3,Float64}(0.0, 1.0, 0.0)),
w=Var(init=Modia.SVector{3,Float64}(10.0, 0.0, -5.0)))
)

dummy = @instantiateModel(Dummy, unitless=true)
simulate!(dummy, stopTime=0.001, interval=0.001)

end
4 changes: 1 addition & 3 deletions test/Basic/Pendulum.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module PendulumModule

using Modia
import Modia3D
using Modia3D.ModiaInterface
using Modia3D

Pendulum = Model3D(
world = Object3D(feature=Scene()),
Expand Down
1 change: 1 addition & 0 deletions test/includeTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Modia3D.loadPalettes!(solidMaterialPalette = "$(Modia3D.path)/palettes/sol
visualMaterialPalette = "$(Modia3D.path)/palettes/visualMaterials.json")

Test.@testset "Basic" begin
include(joinpath("Basic", "ModelsForPrecompilation.jl"))
include(joinpath("Basic", "AllShapes.jl"))
include(joinpath("Basic", "PendulumWithBar1.jl"))
include(joinpath("Basic", "PendulumWithBar2.jl"))
Expand Down

0 comments on commit 8849af7

Please sign in to comment.