From 4940f11c338df7671f5f6612ba0c9fa1e8e5e669 Mon Sep 17 00:00:00 2001 From: Jan Bruedigam Date: Wed, 20 Mar 2024 15:44:10 +0100 Subject: [PATCH] Add precompilation --- DojoEnvironments/Project.toml | 2 + DojoEnvironments/src/DojoEnvironments.jl | 5 ++ DojoEnvironments/src/precompile.jl | 61 ++++++++++++++++++++++++ Project.toml | 2 + examples/Project.toml | 4 +- src/Dojo.jl | 4 ++ src/precompile.jl | 40 ++++++++++++++++ 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 DojoEnvironments/src/precompile.jl create mode 100644 src/precompile.jl diff --git a/DojoEnvironments/Project.toml b/DojoEnvironments/Project.toml index dd6d4392..8c5b3d09 100644 --- a/DojoEnvironments/Project.toml +++ b/DojoEnvironments/Project.toml @@ -6,8 +6,10 @@ version = "0.6.0" [deps] Dojo = "ac60b53e-8d92-4c83-b960-e78698fa1916" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [compat] Dojo = "0.3.0, 0.4.0, 0.5.0, 0.6.0, 0.7.0" +PrecompileTools = "1.0, 1.2" julia = "1.6, 1.9" diff --git a/DojoEnvironments/src/DojoEnvironments.jl b/DojoEnvironments/src/DojoEnvironments.jl index 24ad0fb5..f824a0c8 100644 --- a/DojoEnvironments/src/DojoEnvironments.jl +++ b/DojoEnvironments/src/DojoEnvironments.jl @@ -6,12 +6,17 @@ using Random using Dojo import Dojo: string_to_symbol, add_limits, RotX, RotY, RotZ, rotation_vector, SVector +using PrecompileTools + include("mechanisms.jl") include("environments.jl") include("utilities.jl") include("mechanisms/include.jl") include("environments/include.jl") +# Precompilation +include("precompile.jl") + # Mechanism export get_mechanism, diff --git a/DojoEnvironments/src/precompile.jl b/DojoEnvironments/src/precompile.jl new file mode 100644 index 00000000..7a6ed71b --- /dev/null +++ b/DojoEnvironments/src/precompile.jl @@ -0,0 +1,61 @@ +@setup_workload begin + mechanisms = [ + :ant, + :atlas, + :block, + :block2d, + :cartpole, + :dzhanibekov, + :exoskeleton, + :fourbar, + :halfcheetah, + :hopper, + :humanoid, + :npendulum, + :nslider, + :panda, + :pendulum, + :quadrotor, + :quadruped, + :raiberthopper, + :slider, + :snake, + :sphere, + :tippetop, + :twister, + :uuv, + :walker, + :youbot, + ] + + environments = [ + :ant_ars, + :cartpole_dqn, + :pendulum, + :quadruped_waypoint, + :quadruped_sampling, + :quadrotor_waypoint, + :uuv_waypoint, + :youbot_waypoint, + ] + + @compile_workload begin + # Simulate all mechanisms + for name in mechanisms + mech = get_mechanism(name) + initialize!(mech, name) + simulate!(mech, mech.timestep * 2) + end + + # Simulate all environments + for name in environments + env = get_environment(name; horizon=2) + x = get_state(env) + u = DojoEnvironments.input_map(env, nothing) + set_input!(env, nothing) + step!(env, x) + step!(env, x, u) + simulate!(env) + end + end +end \ No newline at end of file diff --git a/Project.toml b/Project.toml index 05e96400..af9f1b56 100644 --- a/Project.toml +++ b/Project.toml @@ -17,6 +17,7 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11" Meshing = "e6723b4c-ebff-59f1-b4b7-d97aa5274f73" Polyhedra = "67491407-f73d-577b-9b50-8179a7c68029" +PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" Quaternions = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" @@ -36,6 +37,7 @@ LightXML = "<0.9.0, 0.9.0" MeshCat = "<0.16.0, 0.16.1" Meshing = "<0.6.0, 0.6.0" Polyhedra = "<0.7.6, 0.7.6" +PrecompileTools = "1.0, 1.2" Quaternions = "0.5.2, 0.7.0, 0.7.6" StaticArrays = "1.2, 1.9" julia = "1.6, 1.10" diff --git a/examples/Project.toml b/examples/Project.toml index 183e9deb..0b786e85 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -17,8 +17,8 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" [compat] ClosedIntervals = "<0.3.1, 0.3.1" ControlSystemsBase = "1.0, 1.10" -Dojo = "<0.7.3, 0.7.3" -DojoEnvironments = "<0.5.3, 0.5.3" +Dojo = "<0.7.4, 0.7.4" +DojoEnvironments = "<0.6.0, 0.6.0" Flux = "<0.13.17, 0.13.17" ForwardDiff = "<0.10.36, 0.10.36" JLD2 = "<0.4.46, 0.4.46" diff --git a/src/Dojo.jl b/src/Dojo.jl index cfd7484d..13f16766 100644 --- a/src/Dojo.jl +++ b/src/Dojo.jl @@ -30,6 +30,7 @@ using GraphBasedSystems using CoordinateTransformations using DocStringExtensions +using PrecompileTools # Utilities include(joinpath("utilities", "methods.jl")) @@ -147,6 +148,9 @@ include(joinpath("gradients", "state.jl")) include(joinpath("gradients", "data.jl")) include(joinpath("gradients", "utilities.jl")) +# Precompilation +include("precompile.jl") + # Bodies export diff --git a/src/precompile.jl b/src/precompile.jl new file mode 100644 index 00000000..0ee90806 --- /dev/null +++ b/src/precompile.jl @@ -0,0 +1,40 @@ +@setup_workload begin + @compile_workload begin + # One full simulation + origin = Origin() + body = Cylinder(0.1, 1, 1) + joint = JointConstraint(Revolute(origin, body, [1;0;0]; child_vertex=[0;0;1/2])) + mechanism = Mechanism(origin, [body], [joint]) + controller!(mechanism, k) = set_input!(joint, [0]) + set_minimal_coordinates!(mechanism, joint, [pi/4]) + set_minimal_velocities!(mechanism, joint, [0.2]) + storage = simulate!(mechanism, mechanism.timestep * 2, controller!, record=true) + + # Common shapes + mesh = Mesh("", 1, rand(3,3)) + box = Box(1,1,1,1.0) + capsule = Capsule(1,1,1.0) + cylinder = Cylinder(1, 1, 1.0) + sphere = Sphere(1,1.0) + pyramid = Pyramid(1,1,1.0) + + # Common joints + joint_axis = [1;0;0] + JointConstraint(Floating(origin, mesh)) + JointConstraint(Fixed(origin, box)) + JointConstraint(Prismatic(origin, capsule, joint_axis)) + JointConstraint(Planar(origin, cylinder, joint_axis)) + JointConstraint(FixedOrientation(origin, sphere)) + JointConstraint(Revolute(origin, pyramid, joint_axis)) + JointConstraint(Cylindrical(origin, mesh, joint_axis)) + JointConstraint(PlanarAxis(origin, box, joint_axis)) + JointConstraint(FreeRevolute(origin, capsule, joint_axis)) + JointConstraint(Orbital(origin, cylinder, joint_axis)) + JointConstraint(PrismaticOrbital(origin, sphere, joint_axis)) + JointConstraint(PlanarOrbital(origin, pyramid, joint_axis)) + JointConstraint(FreeOrbital(origin, mesh, joint_axis)) + JointConstraint(Spherical(origin, box)) + JointConstraint(CylindricalFree(origin, capsule, joint_axis)) + JointConstraint(PlanarFree(origin, cylinder, joint_axis)) + end +end \ No newline at end of file