diff --git a/benchmarks/Julia/benchmark.jl b/benchmarks/Julia/benchmark.jl new file mode 100644 index 00000000..d0a5b76b --- /dev/null +++ b/benchmarks/Julia/benchmark.jl @@ -0,0 +1,11 @@ +function benchmark end +function verify_result end + +function inner_benchmark_loop(bench, inner_iterations) + for _ in 1:inner_iterations + if !verify_result(bench, benchmark(bench)) + return false + end + end + return true +end \ No newline at end of file diff --git a/benchmarks/Julia/bounce.jl b/benchmarks/Julia/bounce.jl new file mode 100644 index 00000000..5146ba19 --- /dev/null +++ b/benchmarks/Julia/bounce.jl @@ -0,0 +1,96 @@ +# This code is derived from the SOM benchmarks, see AUTHORS.md file. +# +# Copyright (c) 2023 Valentin Churavy + +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the 'Software'), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +module Bounce + +include(joinpath(@__DIR__, "benchmark.jl")) +include(joinpath(@__DIR__, "som", "random.jl")) + +mutable struct Ball + x::Int32 + y::Int32 + xVel::Int32 + yVel::Int32 +end + +function Ball(rand) + x = next!(rand) % 500 + y = next!(rand) % 500 + xVel = (next!(rand) % 300) - 150 + yVel = (next!(rand) % 300) - 150 + return Ball(x, y, xVel, yVel) +end + +function bounce!(b::Ball) + xLimit = Int32(500) + yLimit = Int32(500) + bounced = false + + b.x += b.xVel + b.y += b.yVel + + if b.x > xLimit + b.x = xLimit + b.xVel = 0 - abs(b.xVel) + bounced = true + end + if b.x < 0 + b.x = 0 + b.xVel = abs(b.xVel) + bounced = true + end + if b.y > yLimit + b.y = yLimit + b.yVel = 0 - abs(b.yVel) + bounced = true + end + if b.y < 0 + b.y = 0 + b.yVel = abs(b.yVel) + bounced = true + end + return bounced +end + +struct Benchmark end + +function benchmark(::Benchmark) + ballCount = 100 + bounces = 0 + rand = Random() + + balls = [Ball(rand) for _ in 1:ballCount] + + for _ in 1:50 + for ball in balls + if bounce!(ball) + bounces += 1 + end + end + end + return bounces +end + +function verify_result(::Benchmark, result) + result == 1331 +end + +end # module diff --git a/benchmarks/Julia/example.jl b/benchmarks/Julia/example.jl deleted file mode 100644 index a2c2b7f1..00000000 --- a/benchmarks/Julia/example.jl +++ /dev/null @@ -1,6 +0,0 @@ -function inner_benchmark_loop(inner_iterations) - for iter in 1:inner_iterations - @info "Here we go again" - end - return true -end \ No newline at end of file diff --git a/benchmarks/Julia/harness.jl b/benchmarks/Julia/harness.jl index bb3d56af..24ca772f 100755 --- a/benchmarks/Julia/harness.jl +++ b/benchmarks/Julia/harness.jl @@ -28,14 +28,17 @@ mutable struct Run end function Run(name, num_iterations, inner_iterations) - benchmark = Module() - Base.include(benchmark, "$name.jl") + anon = Module() + path = joinpath(@__DIR__, "$(lowercase(name)).jl") + Base.include(anon, path) + benchmark = getproperty(anon, Symbol(name)) Run(name, benchmark, 0.0, num_iterations, inner_iterations) end function measure!(run::Run) start_time = time() - @assert run.benchmark.inner_benchmark_loop(run.inner_iterations) "Benchmark failed with incorrect result" + benchmark = run.benchmark + @assert benchmark.inner_benchmark_loop(benchmark.Benchmark(), run.inner_iterations) "Benchmark failed with incorrect result" end_time = time() run_time = end_time - start_time diff --git a/benchmarks/Julia/som/random.jl b/benchmarks/Julia/som/random.jl new file mode 100644 index 00000000..517ae607 --- /dev/null +++ b/benchmarks/Julia/som/random.jl @@ -0,0 +1,10 @@ +mutable struct Random + _seed::Int32 + Random() = new(74755) +end + +function next!(self::Random) + self._seed = ((self._seed * Int32(1309)) + Int32(13849)) & Int32(65535) + + return self._seed +end \ No newline at end of file