From 4053437e97c6fa213c0a76a61b438f7dcc742759 Mon Sep 17 00:00:00 2001 From: Hongyang Zhou Date: Sun, 9 Jun 2024 15:30:37 -0400 Subject: [PATCH] Add precompilation (#48) * Add precompilation; drop Requires * Update benchmarks --- Project.toml | 6 +++--- benchmark/benchmarks.jl | 15 ++++++++------- src/FieldTracer.jl | 5 ++++- src/precompile.jl | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 src/precompile.jl diff --git a/Project.toml b/Project.toml index 276a683..c3424da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,18 +1,18 @@ name = "FieldTracer" uuid = "05065131-34d1-456a-ba22-faf972bb2934" authors = ["Hongyang Zhou "] -version = "0.1.14" +version = "0.1.15" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa" MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221" -Requires = "ae029012-a4dd-5104-9daa-d747884805df" +PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" [compat] Meshes = "0.44" MuladdMacro = "0.2" -Requires = "1.1" +PrecompileTools = "1" julia = "1.6" [extras] diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index 8951687..30a8c7d 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -10,7 +10,7 @@ SUITE["trace"]["2D structured"] = BenchmarkGroup() SUITE["trace"]["3D structured"] = BenchmarkGroup() # Euler2 and RK4 functions -ds, maxstep = 0.1, 150 +ds, maxstep = 0.1, 100 x = range(0, 1, step=0.1) y = range(0, 1, step=0.1) # ndgrid @@ -22,10 +22,12 @@ xstart = 0.1 ystart = 0.9 SUITE["trace"]["2D structured"]["euler"] = - @benchmarkable FieldTracer.euler($maxstep, $ds, $xstart, $ystart, $x, $y, $u, $v) + @benchmarkable trace($u, $v, $xstart, $ystart, $x, $y; + alg=Euler(), ds=$ds, maxstep=$maxstep) SUITE["trace"]["2D structured"]["rk4"] = - @benchmarkable FieldTracer.rk4($maxstep, $ds, $xstart, $ystart, $x, $y, $u, $v) + @benchmarkable trace($u, $v, $xstart, $ystart, $x, $y; + alg=RK4(), ds=$ds, maxstep=$maxstep) x = range(0, 10, length=15) y = range(0, 10, length=20) @@ -36,8 +38,7 @@ bz = fill(1.0, length(x), length(y), length(z)) xs, ys, zs = 1.0, 1.0, 1.0 SUITE["trace"]["3D structured"]["euler"] = - @benchmarkable trace($bx, $bz, $bz, $xs, $ys, $zs, $x, $y, $z; - alg=Euler(), ds=0.2, maxstep=200) + @benchmarkable trace($bx, $by, $bz, $xs, $ys, $zs, $x, $y, $z; + alg=Euler(), ds=$ds, maxstep=$maxstep) SUITE["trace"]["3D structured"]["rk4"] = - @benchmarkable trace($bx, $by, $bz, $xs, $ys, $zs, $x, $y, $z; - ds=0.2, maxstep=200, direction="forward") + @benchmarkable trace($bx, $by, $bz, $xs, $ys, $zs, $x, $y, $z; ds=$ds, maxstep=$maxstep) diff --git a/src/FieldTracer.jl b/src/FieldTracer.jl index 16aace5..d25ef3a 100644 --- a/src/FieldTracer.jl +++ b/src/FieldTracer.jl @@ -1,6 +1,7 @@ module FieldTracer -using Meshes, Requires, MuladdMacro +using Meshes, MuladdMacro +using PrecompileTools: @setup_workload, @compile_workload export trace export Euler, RK4 @@ -48,4 +49,6 @@ function trace(args...; alg::Algorithm=RK4(), kwargs...) end +include("precompile.jl") + end diff --git a/src/precompile.jl b/src/precompile.jl new file mode 100644 index 0000000..842bab4 --- /dev/null +++ b/src/precompile.jl @@ -0,0 +1,37 @@ +# Precompiling workloads + +@setup_workload begin + @compile_workload begin + x = range(0, 1, step=0.1) + y = range(0, 1, step=0.1) + # ndgrid + u, v = let + xgrid = [i for i in x, _ in y] + ygrid = [j for _ in x, j in y] + copy(xgrid), -copy(ygrid) + end + xstart = 0.1 + ystart = 0.9 + + #xt, yt = FieldTracer.euler(150, 0.1, 0.1, 0.9, x, y, u, v) + xt, yt = trace(u, v, xstart, ystart, x, y; + maxstep=100, ds=0.1, direction="both", alg=Euler()) + xt, yt = trace(u, v, xstart, ystart, x, y; + maxstep=100, ds=0.1, direction="both", alg=RK4()) + + x = range(0, 10, length=10) + y = range(0, 10, length=10) + z = range(0, 10, length=10) + bx = fill(1.0, length(x), length(y), length(z)) + by = fill(1.0, length(x), length(y), length(z)) + bz = fill(1.0, length(x), length(y), length(z)) + xs, ys, zs = 1.0, 1.0, 1.0 + + # Euler 2nd order + x1, y1, z1 = trace(bx, by, bz, xs, ys, zs, x, y, z; + maxstep=10, ds=0.2, direction="both", alg=Euler()) + # RK4 + x1, y1, z1 = trace(bx, by, bz, xs, ys, zs, x, y, z; + maxstep=10, ds=0.2, direction="both", alg=RK4()) + end +end \ No newline at end of file