Skip to content

Commit

Permalink
implement bounce
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Aug 10, 2023
1 parent d463833 commit f32ccdb
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 9 deletions.
11 changes: 11 additions & 0 deletions benchmarks/Julia/benchmark.jl
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions benchmarks/Julia/bounce.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This code is derived from the SOM benchmarks, see AUTHORS.md file.
#
# Copyright (c) 2023 Valentin Churavy <[email protected]>

# 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
6 changes: 0 additions & 6 deletions benchmarks/Julia/example.jl

This file was deleted.

9 changes: 6 additions & 3 deletions benchmarks/Julia/harness.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions benchmarks/Julia/som/random.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f32ccdb

Please sign in to comment.