Skip to content

Commit 8849af7

Browse files
authored
Merge pull request #113 from ModiaSim/mo_improved_startup
Improved generation of sysimage
2 parents 6408163 + aebfaf4 commit 8849af7

10 files changed

+126
-50
lines changed

Manifest.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ version = "0.8.6"
253253

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

260260
[[deps.Downloads]]
261261
deps = ["ArgTools", "LibCURL", "NetworkOptions"]

Project.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
authors = ["Andrea Neumayr <[email protected]>", "Martin Otter <[email protected]>", "Gerhard Hippmann <[email protected]>"]
22
name = "Modia3D"
33
uuid = "07f2c1e0-90b0-56cf-bda7-b44b56e34eed"
4-
version = "0.10.3"
4+
version = "0.10.4"
55

66
[compat]
77
Colors = "0.12, 0.11, 0.10"
@@ -23,6 +23,7 @@ URIParser = "0.4"
2323
julia = "1.7"
2424

2525
[deps]
26+
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
2627
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
2728
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
2829
DoubleFloats = "497a8b3b-efae-58df-a0af-a86822472b78"

create_Modia3D_sysimage.jl

+14-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
#
1414
module Create_Modia3D_sysimage
1515

16-
path = dirname(@__FILE__)
17-
file = joinpath(path, "create_Modia3D_sysimage.jl")
16+
path = dirname(@__FILE__)
17+
file = joinpath(path, "create_Modia3D_sysimage.jl")
18+
precompile_file = joinpath(path, "create_Modia3D_sysimage_precompile_statements_file.jl")
1819
Modia3D_sysimage_path = joinpath(pwd(), Sys.iswindows() ? "Modia3D_sysimage.dll" : "Modia3D_sysimage.so")
1920

2021
import Pkg
@@ -31,8 +32,18 @@ if length(addPackages) > 0
3132
Pkg.add(addPackages)
3233
end
3334

35+
#= Seem to make startup slower
36+
println("!!! Define additional artefacts for precompilation ...")
37+
using Modia3D
38+
usePlotPackage("PyPlot")
39+
@usingModiaPlot
40+
include("$path/test/Basic/ModelsForPrecompilation.jl")
41+
=#
42+
43+
# Create sysimage
3444
using PackageCompiler
35-
create_sysimage(sysimage_path = Modia3D_sysimage_path)
45+
create_sysimage(sysimage_path = Modia3D_sysimage_path,
46+
precompile_statements_file = precompile_file)
3647

3748
println("!!! Modia3D sysimage created. Use sysimage by starting julia with:")
3849
println(" julia -J$Modia3D_sysimage_path")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
precompile(Tuple{typeof(Modia3D.ModiatorServer)})
2+
precompile(Tuple{typeof(Base.setindex!), Base.Dict{HTTP.Handlers.Route, Any}, Function, HTTP.Handlers.Route})
3+
precompile(Tuple{typeof(HTTP.Handlers.gh), String})
4+
precompile(Tuple{typeof(Base.println), Base.TTY})
5+
precompile(Tuple{typeof(HTTP.Servers.listen), Function, String, Int64})
6+
precompile(Tuple{typeof(HTTP.Servers.handle_connection), Function, HTTP.ConnectionPool.Connection{Sockets.TCPSocket}, HTTP.Servers.Server{Nothing, Sockets.TCPServer}, Int64, Int64})
7+
precompile(Tuple{typeof(Base.readuntil), HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, Function, Int64})
8+
precompile(Tuple{typeof(Base.alloc_buf_hook), Sockets.TCPSocket, UInt64})
9+
precompile(Tuple{typeof(Base.readuntil), Base.GenericIOBuffer{Array{UInt8, 1}}, typeof(HTTP.Parsers.find_end_of_header)})
10+
precompile(Tuple{typeof(Core.Compiler.convert), Type{DataType}, Type{Tuple{Union{Base.SubString{String}, String}, Int64}}})
11+
precompile(Tuple{Base.Cartesian.var"#@ncall", LineNumberNode, Module, Int64, Any, Vararg{Any}})
12+
precompile(Tuple{Type{Sundials.CVODE_BDF{Method, LinearSolver, P, PS} where PS where P where LinearSolver where Method}})
13+
precompile(Tuple{Type{Sundials.CVODE_BDF{:Newton, :Dense, Nothing, Nothing}}, Int64, Int64, Int64, Bool, Int64, Int64, Int64, Int64, Int64, Nothing, Nothing, Int64})
14+
precompile(Tuple{typeof(Base.hasfield), Type, Symbol})
15+
precompile(Tuple{typeof(Base.fieldindex), DataType, Symbol, Bool})
16+
precompile(Tuple{Type{Sundials.CVODE_BDF{:Newton, :GMRES, Nothing, Nothing}}, Int64, Int64, Int64, Bool, Int64, Int64, Int64, Int64, Int64, Nothing, Nothing, Int64})
17+
precompile(Tuple{typeof(HTTP.Handlers.handle), HTTP.Handlers.RequestHandlerFunction{typeof(Modia3D.simulateModel)}, HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}})
18+
precompile(Tuple{typeof(HTTP.ConnectionPool.hashconn), Type, String, String, Int64, Bool, Bool})
19+
precompile(Tuple{typeof(JSON.Parser.parse_object), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState})
20+
precompile(Tuple{typeof(JSON.Parser.parse_string), JSON.Parser.MemoryParserState})
21+
precompile(Tuple{typeof(JSON.Parser.predict_string), JSON.Parser.MemoryParserState})
22+
precompile(Tuple{typeof(JSON.Parser.parse_string), JSON.Parser.MemoryParserState, Base.GenericIOBuffer{Array{UInt8, 1}}})
23+
precompile(Tuple{typeof(JSON.Parser.parse_number), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState})
24+
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})
25+
precompile(Tuple{typeof(JSON.Parser.hasleadingzero), JSON.Parser.MemoryParserState, Int64, Int64})
26+
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})
27+
precompile(Tuple{typeof(Parsers.tryparse), Type{Float64}, Base.SubString{String}, Parsers.Options, Int64, Int64})
28+
precompile(Tuple{typeof(JSON.Parser.parse_jsconstant), JSON.Parser.ParserContext{Base.Dict{String, Any}, Int64, true, nothing}, JSON.Parser.MemoryParserState})
29+
precompile(Tuple{typeof(Modia3D.generateSimulationResult), Base.Dict{String, Any}})

docs/src/index.md

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ julia -JModia3D_sysimage.so (otherwise)
9393

9494
## Release Notes
9595

96+
### Version 0.10.4
97+
98+
- Script `Modia3D/create_Modia3D_sysimage.jl` improved.
99+
100+
96101
### Version 0.10.3
97102

98103
- New script `Modia3D/create_Modia3D_sysimage.jl` to create a sysimage for a much faster startup

src/Modia3D.jl

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
module Modia3D
55

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

1010
println("\nImporting Modia3D Version $Version ($Date)")
@@ -156,7 +156,6 @@ function reenableRenderer()
156156
return nothing
157157
end
158158

159-
160159
export PointGravityField, NoGravityField
161160

162161
export print_ModelVariables
@@ -167,8 +166,7 @@ export rereadContactPairMaterialFromJSON
167166
import StaticArrays
168167
import LinearAlgebra
169168
import Test
170-
import JSON
171-
import HTTP
172-
import URIParser
169+
170+
include("ModiatorServer.jl")
173171

174172
end # module

src/ModiatorServer.jl

+39-36
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
module ModiatorServer
1+
export ModiatorServer
22

3-
using Modia3D
4-
using Modia3D.StaticArrays
5-
using Modia3D.JSON
6-
using Modia3D.HTTP
7-
using Modia3D.URIParser
8-
using Base64 # from Base
3+
import JSON
4+
import HTTP
5+
import URIParser
6+
using Base64
97

108
# ----------------------------------------------------------------------------------------
11-
129
function saveJSON(req)
1310
jsonString = String(req.body);
1411
index = findfirst("%25NAMEEND%25", jsonString);
1512
name = jsonString[1:index[1]-1]
1613
println("Saving ", name)
1714
jsonString = jsonString[index[end] + 1:end]
18-
jsonString = unescape(jsonString)
15+
jsonString = URIParser.unescape(jsonString)
1916
println(jsonString)
2017
io = open(string(name, ".json"), "w");
2118
write(io, jsonString);
@@ -48,7 +45,7 @@ function saveOBJ(req)
4845
name = objString[1:index[1]-1]
4946
println("Saving ", name)
5047
objString = objString[index[end] + 1:end]
51-
objString = unescape(objString)
48+
objString = URIParser.unescape(objString)
5249
io = open(string(name, ".obj"), "w");
5350
write(io, objString);
5451
close(io)
@@ -105,7 +102,7 @@ function simulateModel(req)
105102
return HTTP.Response(200, headers)
106103
end
107104
model = String(req.body);
108-
model = unescape(model);
105+
model = URIParser.unescape(model);
109106
experiment = JSON.parse(model);
110107
@time json = generateSimulationResult(experiment);
111108
# open("SimRes.txt", "w") do f
@@ -116,7 +113,6 @@ function simulateModel(req)
116113
return response;
117114
end
118115

119-
@usingModiaPlot
120116

121117
function generateSimulationResult(experiment)
122118
println()
@@ -169,29 +165,36 @@ function unknownRequest(req)
169165
return HTTP.Response(200, "OK")
170166
end
171167

172-
const ROUTER = HTTP.Router()
173-
174-
HTTP.@register(ROUTER, "POST", "/Modiator/simulateModel/", simulateModel)
175-
HTTP.@register(ROUTER, "OPTIONS", "/Modiator/simulateModel/", simulateModel)
176-
177-
HTTP.@register(ROUTER, "POST", "/Modiator/saveOBJ/", saveOBJ)
178-
HTTP.@register(ROUTER, "POST", "/Modiator/loadOBJ/", loadOBJ)
179-
HTTP.@register(ROUTER, "POST", "/Modiator/loadImageBase64/", loadImageBase64)
180-
HTTP.@register(ROUTER, "POST", "/Modiator/saveJSON/", saveJSON)
181-
HTTP.@register(ROUTER, "POST", "/Modiator/loadJSON/", loadJSON)
182-
#HTTP.@register(ROUTER, "GET", "/Modiator/public/*", getPublic)
183-
#HTTP.@register(ROUTER, "GET", "/Modiator/icons/*", getIcon)
184-
HTTP.@register(ROUTER, "GET", "/*", unknownRequest)
185-
HTTP.@register(ROUTER, "POST", "/*", unknownRequest)
186-
187-
# println("Precompiling")
188-
# precompile(generateSimulationResult, (Dict,))
189-
# precompile(HTTP.Router)
190-
println("Server ready")
191-
println("Listening to: 127.0.0.1:8000")
192-
println()
193-
println("Note that it might take more than 20 seconds for the first simulation to start.")
168+
function ModiatorServer()::Nothing
169+
ROUTER = HTTP.Router()
170+
171+
HTTP.@register(ROUTER, "POST", "/Modiator/simulateModel/", simulateModel)
172+
HTTP.@register(ROUTER, "OPTIONS", "/Modiator/simulateModel/", simulateModel)
173+
174+
HTTP.@register(ROUTER, "POST", "/Modiator/saveOBJ/", saveOBJ)
175+
HTTP.@register(ROUTER, "POST", "/Modiator/loadOBJ/", loadOBJ)
176+
HTTP.@register(ROUTER, "POST", "/Modiator/loadImageBase64/", loadImageBase64)
177+
HTTP.@register(ROUTER, "POST", "/Modiator/saveJSON/", saveJSON)
178+
HTTP.@register(ROUTER, "POST", "/Modiator/loadJSON/", loadJSON)
179+
#HTTP.@register(ROUTER, "GET", "/Modiator/public/*", getPublic)
180+
#HTTP.@register(ROUTER, "GET", "/Modiator/icons/*", getIcon)
181+
HTTP.@register(ROUTER, "GET", "/*", unknownRequest)
182+
HTTP.@register(ROUTER, "POST", "/*", unknownRequest)
183+
184+
# println("Precompiling")
185+
# precompile(generateSimulationResult, (Dict,))
186+
# precompile(HTTP.Router)
187+
println("Server ready")
188+
println("Listening to: 127.0.0.1:8000")
189+
println()
190+
println("Note that it might take more than 20 seconds for the first simulation to start.")
191+
192+
HTTP.serve(ROUTER, "127.0.0.1", 8000)
193+
return nothing
194+
end
194195

195-
HTTP.serve(ROUTER, "127.0.0.1", 8000)
196+
function julia_main()::Cint
197+
ModiatorServer()
198+
return 0
199+
end
196200

197-
end

test/Basic/ModelsForPrecompilation.jl

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module ModelsForPrecompilation
2+
3+
using Modia3D
4+
5+
Dummy = Model3D(
6+
world = Object3D(feature=Scene(provideAnimationHistory=true, enableVisualization=false,
7+
enableContactDetection=true,
8+
gravityField=UniformGravityField(g=9.81, n=[0, -1, 0]))),
9+
10+
body1 = Object3D(feature=Solid(shape=Beam(axis=1, length=1.0, width=0.1, thickness=0.1),
11+
solidMaterial="Aluminium")),
12+
frame1 = Object3D(parent=:body1, translation=[-0.5, 0.0, 0.0]),
13+
rev = Revolute(obj1=:world, obj2=:frame1),
14+
15+
body2 = Object3D(feature=Solid(shape=Box(lengthX=1.0, lengthY=0.1, lengthZ=0.1), massProperties=MassProperties(mass=1.0))),
16+
prism = Prismatic(obj1=:world , obj2=:body2, axis=2),
17+
18+
body3 = Object3D(feature=Solid(shape=Sphere(diameter=0.2),
19+
visualMaterial=VisualMaterial(color="Blue"),
20+
solidMaterial="Steel",
21+
massProperties=MassPropertiesFromShapeAndMass(mass=0.001),
22+
collision=true)),
23+
free = FreeMotion(obj1=:world, obj2=:body3, r=Var(init=Modia.SVector{3,Float64}(0.0, 1.0, 0.0)),
24+
w=Var(init=Modia.SVector{3,Float64}(10.0, 0.0, -5.0)))
25+
)
26+
27+
dummy = @instantiateModel(Dummy, unitless=true)
28+
simulate!(dummy, stopTime=0.001, interval=0.001)
29+
30+
end

test/Basic/Pendulum.jl

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module PendulumModule
22

3-
using Modia
4-
import Modia3D
5-
using Modia3D.ModiaInterface
3+
using Modia3D
64

75
Pendulum = Model3D(
86
world = Object3D(feature=Scene()),

test/includeTests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Modia3D.loadPalettes!(solidMaterialPalette = "$(Modia3D.path)/palettes/sol
55
visualMaterialPalette = "$(Modia3D.path)/palettes/visualMaterials.json")
66

77
Test.@testset "Basic" begin
8+
include(joinpath("Basic", "ModelsForPrecompilation.jl"))
89
include(joinpath("Basic", "AllShapes.jl"))
910
include(joinpath("Basic", "PendulumWithBar1.jl"))
1011
include(joinpath("Basic", "PendulumWithBar2.jl"))

0 commit comments

Comments
 (0)