Skip to content

Commit ef1323d

Browse files
committed
odin
1 parent 6a8e2e1 commit ef1323d

File tree

12 files changed

+1649
-1705
lines changed

12 files changed

+1649
-1705
lines changed

.github/odin.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/sh
2+
3+
VERSION=dev-2022-04
4+
FILE_NAME=odin-ubuntu_amd64-$VERSION.zip
5+
6+
mkdir /tmp/odin
7+
cd /tmp/odin
8+
wget https://github.com/odin-lang/Odin/releases/download/$VERSION/$FILE_NAME
9+
unzip -o $FILE_NAME
10+
sudo chmod +x odin
11+
sudo ln -sf $PWD/odin /usr/bin/odin
12+
odin version

.github/workflows/bench.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
lua,
3939
nim,
4040
ocaml,
41+
odin,
4142
perl,
4243
php,
4344
pony,
@@ -130,6 +131,9 @@ jobs:
130131
- name: Install hhvm
131132
if: matrix.lang == 'hack'
132133
run: ./.github/hhvm.sh
134+
- name: Install odin
135+
if: matrix.lang == 'odin'
136+
run: ./.github/odin.sh
133137
- uses: dlang-community/setup-dlang@v1
134138
if: matrix.lang == 'd'
135139
with:

bench/algorithm/helloworld/1.odin

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "core:fmt"
4+
import "core:runtime"
5+
6+
main :: proc() {
7+
args := runtime.args__
8+
name := len(args) > 1 ? args[1] : ""
9+
fmt.printf("Hello world %s!\n", name)
10+
}

bench/algorithm/lru/1.odin

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import "core:container/lru"
4+
import "core:fmt"
5+
import "core:runtime"
6+
import "core:strconv"
7+
8+
A : uint : 1103515245
9+
C : uint : 12345
10+
M : uint : 1 << 31
11+
LCG :: struct { seed: uint }
12+
next_lcg :: proc(lcg : ^LCG) -> uint {
13+
seed := (A * lcg.seed + C) % M
14+
lcg.seed = seed
15+
return seed
16+
}
17+
18+
main :: proc() {
19+
args := runtime.args__
20+
size := strconv.parse_uint(auto_cast args[1]) or_else 100
21+
n := strconv.parse_int(auto_cast args[2]) or_else 100
22+
mod := size * 10
23+
rng0 := &LCG { 0 }
24+
rng1 := &LCG { 1 }
25+
cache := new(lru.Cache(uint, uint))
26+
defer free(cache)
27+
lru.init(cache, auto_cast size)
28+
hit := 0
29+
missed := 0
30+
for _ in 0..<n {
31+
n0 := next_lcg(rng0) % mod
32+
lru.set(cache, n0, n0)
33+
n1 := next_lcg(rng1) % mod
34+
if _, ok := lru.get(cache, n1); ok {
35+
hit += 1
36+
// BUG: below lines should not be needed if lru lib works properly
37+
lru.remove(cache, n1)
38+
lru.set(cache, n1, n1)
39+
}
40+
else {
41+
missed += 1
42+
}
43+
// fmt.printf("hit: %d, missed: %d, n0: %d, n1: %d\n", hit, missed, n0, n1)
44+
}
45+
fmt.println(hit)
46+
fmt.println(missed)
47+
}

bench/algorithm/nbody/1.odin

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import "core:fmt"
4+
import "core:math"
5+
import "core:runtime"
6+
import "core:strconv"
7+
8+
PI :: 3.141592653589793
9+
SOLAR_MASS :: 4 * PI * PI
10+
DAYS_PER_YEAR :: 365.24
11+
12+
Vec3 :: [3]f64
13+
Body :: struct { pos: Vec3, velocity: Vec3, mass: f64 }
14+
Sys :: [5]Body
15+
16+
init_body :: proc(x, y, z, vx, vy, vz, mass : f64) -> Body {
17+
return Body { [3]f64 {x, y, z}, [3]f64 {vx, vy, vz}*DAYS_PER_YEAR, mass*SOLAR_MASS }
18+
}
19+
20+
init_sys :: proc() -> Sys {
21+
return Sys {
22+
// Sun
23+
init_body(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0),
24+
// Jupiter
25+
init_body(4.84143144246472090e+00, -1.16032004402742839e+00, -1.03622044471123109e-01,
26+
1.66007664274403694e-03, 7.69901118419740425e-03, -6.90460016972063023e-05,
27+
9.54791938424326609e-04),
28+
// Saturn
29+
init_body(8.34336671824457987e+00, 4.12479856412430479e+00, -4.03523417114321381e-01,
30+
-2.76742510726862411e-03, 4.99852801234917238e-03, 2.30417297573763929e-05,
31+
2.85885980666130812e-04),
32+
// Uranus
33+
init_body(1.28943695621391310e+01, -1.51111514016986312e+01, -2.23307578892655734e-01,
34+
2.96460137564761618e-03, 2.37847173959480950e-03, -2.96589568540237556e-05,
35+
4.36624404335156298e-05),
36+
// Neptune
37+
init_body(1.53796971148509165e+01, -2.59193146099879641e+01, 1.79258772950371181e-01,
38+
2.68067772490389322e-03, 1.62824170038242295e-03, -9.51592254519715870e-05,
39+
5.15138902046611451e-05),
40+
}
41+
}
42+
43+
advance :: proc(sys: ^Sys, dt: f64) {
44+
for b, i in sys {
45+
v := b.velocity
46+
for j in (i+1)..<5 {
47+
b2 := &sys[j]
48+
dpos := b.pos - b2.pos
49+
distance_square := sum(dpos * dpos)
50+
distance := math.sqrt_f64(distance_square)
51+
mag := dt / (distance * distance_square)
52+
v -= dpos * (mag * b2.mass)
53+
b2.velocity += dpos * (mag * b.mass)
54+
}
55+
b.velocity = v
56+
b.pos += v * dt
57+
}
58+
}
59+
60+
offset_momentum :: proc(sys: ^Sys) {
61+
p := Vec3 {}
62+
for b, _ in sys {
63+
p -= b.velocity * b.mass
64+
}
65+
sys[0].velocity = p / SOLAR_MASS
66+
}
67+
68+
sum :: proc (v: Vec3) -> f64 {
69+
return v[0] + v[1] + v[2]
70+
}
71+
72+
energy :: proc(sys: ^Sys) -> f64 {
73+
e := 0.0
74+
for b, i in sys {
75+
e += 0.5 * b.mass * sum(b.velocity * b.velocity)
76+
for j in (i+1)..<5 {
77+
b2 := sys[j]
78+
dpos := b.pos - b2.pos
79+
distance_square := sum(dpos * dpos)
80+
distance := math.sqrt_f64(distance_square)
81+
e -= b.mass * b2.mass / distance
82+
}
83+
}
84+
return e
85+
}
86+
87+
main :: proc() {
88+
args := runtime.args__
89+
n := strconv.parse_int(auto_cast args[1]) or_else 1000
90+
91+
sys := init_sys()
92+
offset_momentum(&sys)
93+
fmt.printf("%.9f\n", energy(&sys))
94+
for _ in 0..<n {
95+
advance(&sys, 0.01)
96+
}
97+
fmt.printf("%.9f\n", energy(&sys))
98+
}

bench/algorithm/nsieve/1.odin

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "core:fmt"
4+
import "core:runtime"
5+
import "core:strconv"
6+
7+
main :: proc() {
8+
args := runtime.args__
9+
n := strconv.parse_int(runtime.cstring_to_string(args[1])) or_else 4
10+
for i in 0..<3 {
11+
shift : uint = auto_cast (n - i)
12+
nsieve(10000 << shift)
13+
}
14+
}
15+
16+
nsieve :: proc(n : int) {
17+
flags := make([]bool, n)
18+
defer delete(flags)
19+
count := 0
20+
for i in 2..<n {
21+
if !flags[i] {
22+
count += 1
23+
for j := i << 1; j < n; j += i {
24+
flags[j] = true
25+
}
26+
}
27+
}
28+
fmt.printf("Primes up to %8s %8s\n", fmt.tprint(n), fmt.tprint(count))
29+
}

bench/algorithm/nsieve/2.odin

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import "core:container/bit_array"
4+
import "core:fmt"
5+
import "core:runtime"
6+
import "core:strconv"
7+
8+
main :: proc() {
9+
args := runtime.args__
10+
n := strconv.parse_int(runtime.cstring_to_string(args[1])) or_else 4
11+
for i in 0..<3 {
12+
shift : uint = auto_cast (n - i)
13+
nsieve(10000 << shift)
14+
}
15+
}
16+
17+
nsieve :: proc(n : int) {
18+
flags, _ := bit_array.create(n)
19+
defer bit_array.destroy(flags)
20+
count := 0
21+
for i in 2..<n {
22+
if res, _ := bit_array.get(flags, i); !res {
23+
count += 1
24+
for j := i << 1; j < n; j += i {
25+
bit_array.set(flags, j)
26+
}
27+
}
28+
}
29+
fmt.printf("Primes up to %8s %8s\n", fmt.tprint(n), fmt.tprint(count))
30+
}

bench/algorithm/nsieve/5_out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Primes up to 320000 27608
2+
Primes up to 160000 14683
3+
Primes up to 80000 7837

bench/bench.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ problems:
1313
unittests:
1414
- input: 4
1515
output: 4_out
16+
- input: 5
17+
output: 5_out
1618
tests:
1719
- input: 10
1820
- input: 12

bench/bench_odin.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
lang: odin
2+
problems:
3+
- name: helloworld
4+
source:
5+
- 1.odin
6+
- name: nsieve
7+
source:
8+
- 1.odin
9+
- 2.odin
10+
- name: lru
11+
source:
12+
# BUG: core:container/lru
13+
# - 1.odin
14+
- name: nbody
15+
source:
16+
- 1.odin
17+
compiler_version_command: odin version
18+
runtime_version_parameter:
19+
source_rename_to: app.odin
20+
environments:
21+
- os: linux
22+
compiler: odin
23+
version: latest
24+
docker:
25+
include:
26+
build: odin build . -out:out/app -o:speed -disable-assert -no-bounds-check -vet -microarch:broadwell -show-timings
27+
out_dir: out
28+
run_cmd: app
29+
allow_failure: true

0 commit comments

Comments
 (0)