Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 207 lines (169 sloc) 5.941 kB
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
1 // based on:
2 // http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=java
3
ea01ee2 @brson Convert 'use' to 'extern mod'. Remove old 'use' syntax
brson authored
4 extern mod std;
1588ae2 @brson bench: Update shootout-nbody for style
brson authored
5
6 // Using sqrt from the standard library is way slower than using libc
7 // directly even though std just calls libc, I guess it must be
8 // because the the indirection through another dynamic linker
9 // stub. Kind of shocking. Might be able to make it faster still with
10 // an llvm intrinsic.
7ddd353 @gmfawcett implement #[nolink]; deprecate #[link_name = ""]; note in stdlib to r…
gmfawcett authored
11 #[nolink]
debb7e4 @graydon Switch 'native' to 'extern' (or 'foreign' in some descriptions)
graydon authored
12 extern mod libc {
dffe188 @graydon Install new pub/priv/export rules as defaults, old rules accessible u…
graydon authored
13 #[legacy_exports];
8f2d75d @nikomatsakis switch over sqrt from llvm to c-stack-cdecl, exposing a bug in
nikomatsakis authored
14 fn sqrt(n: float) -> float;
1cb3ce8 @pcwalton shootout: Hoist out the movement too; switch square root to an LLVM i…
pcwalton authored
15 }
16
edc317b @brson Remove arg vectors from main functions. Stop supporting them.
brson authored
17 fn main() {
18 let args = os::args();
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
19 let args = if os::getenv(~"RUST_BENCH").is_some() {
20 ~[~"", ~"4000000"]
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
21 } else if args.len() <= 1u {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
22 ~[~"", ~"100000"]
1588ae2 @brson bench: Update shootout-nbody for style
brson authored
23 } else {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
24 args
1588ae2 @brson bench: Update shootout-nbody for style
brson authored
25 };
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
26 let n = int::from_str(args[1]).get();
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
27 let bodies: ~[Body::props] = NBodySystem::make();
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
28 io::println(fmt!("%f", NBodySystem::energy(bodies)));
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
29 let mut i = 0;
1588ae2 @brson bench: Update shootout-nbody for style
brson authored
30 while i < n { NBodySystem::advance(bodies, 0.01); i += 1; }
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
31 io::println(fmt!("%f", NBodySystem::energy(bodies)));
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
32 }
33
34 mod NBodySystem {
dffe188 @graydon Install new pub/priv/export rules as defaults, old rules accessible u…
graydon authored
35 #[legacy_exports];
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
36
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
37 fn make() -> ~[Body::props] {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
38 let bodies: ~[Body::props] =
39 ~[Body::sun(), Body::jupiter(), Body::saturn(), Body::uranus(),
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
40 Body::neptune()];
df7f21d @marijnh Reformat for new syntax
marijnh authored
41
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
42 let mut px = 0.0;
43 let mut py = 0.0;
44 let mut pz = 0.0;
df7f21d @marijnh Reformat for new syntax
marijnh authored
45
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
46 let mut i = 0;
df7f21d @marijnh Reformat for new syntax
marijnh authored
47 while i < 5 {
518dc52 @brson Reformat
brson authored
48 px += bodies[i].vx * bodies[i].mass;
49 py += bodies[i].vy * bodies[i].mass;
50 pz += bodies[i].vz * bodies[i].mass;
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
51
52 i += 1;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
53 }
1c1c672 @lkuper Updates to the nbody demo.
lkuper authored
54
55 // side-effecting
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
56 Body::offset_momentum(bodies[0], px, py, pz);
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
57
b355936 @brson Convert ret to return
brson authored
58 return bodies;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
59 }
60
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
61 fn advance(bodies: ~[Body::props], dt: float) {
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
62
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
63 let mut i = 0;
df7f21d @marijnh Reformat for new syntax
marijnh authored
64 while i < 5 {
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
65 let mut j = i + 1;
518dc52 @brson Reformat
brson authored
66 while j < 5 { advance_one(bodies[i], bodies[j], dt); j += 1; }
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
67
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
68 i += 1;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
69 }
70
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
71 i = 0;
c5437c0 @brson Fix failing tests
brson authored
72 while i < 5 { move_(bodies[i], dt); i += 1; }
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
73 }
74
e39db5a @nikomatsakis all tests pass
nikomatsakis authored
75 fn advance_one(bi: Body::props, bj: Body::props, dt: float) unsafe {
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
76 let dx = bi.x - bj.x;
77 let dy = bi.y - bj.y;
78 let dz = bi.z - bj.z;
9c5affd @pcwalton shootout: Hoist out the vector indexing on nbody; don't rely on LICM,…
pcwalton authored
79
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
80 let dSquared = dx * dx + dy * dy + dz * dz;
9c5affd @pcwalton shootout: Hoist out the vector indexing on nbody; don't rely on LICM,…
pcwalton authored
81
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
82 let distance = libc::sqrt(dSquared);
83 let mag = dt / (dSquared * distance);
9c5affd @pcwalton shootout: Hoist out the vector indexing on nbody; don't rely on LICM,…
pcwalton authored
84
85 bi.vx -= dx * bj.mass * mag;
86 bi.vy -= dy * bj.mass * mag;
87 bi.vz -= dz * bj.mass * mag;
88
89 bj.vx += dx * bi.mass * mag;
90 bj.vy += dy * bi.mass * mag;
91 bj.vz += dz * bi.mass * mag;
92 }
93
c5437c0 @brson Fix failing tests
brson authored
94 fn move_(b: Body::props, dt: float) {
1cb3ce8 @pcwalton shootout: Hoist out the movement too; switch square root to an LLVM i…
pcwalton authored
95 b.x += dt * b.vx;
96 b.y += dt * b.vy;
97 b.z += dt * b.vz;
98 }
99
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
100 fn energy(bodies: ~[Body::props]) -> float unsafe {
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
101 let mut dx;
102 let mut dy;
103 let mut dz;
104 let mut distance;
105 let mut e = 0.0;
df7f21d @marijnh Reformat for new syntax
marijnh authored
106
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
107 let mut i = 0;
df7f21d @marijnh Reformat for new syntax
marijnh authored
108 while i < 5 {
109 e +=
518dc52 @brson Reformat
brson authored
110 0.5 * bodies[i].mass *
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
111 (bodies[i].vx * bodies[i].vx + bodies[i].vy * bodies[i].vy
112 + bodies[i].vz * bodies[i].vz);
df7f21d @marijnh Reformat for new syntax
marijnh authored
113
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
114 let mut j = i + 1;
df7f21d @marijnh Reformat for new syntax
marijnh authored
115 while j < 5 {
518dc52 @brson Reformat
brson authored
116 dx = bodies[i].x - bodies[j].x;
117 dy = bodies[i].y - bodies[j].y;
118 dz = bodies[i].z - bodies[j].z;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
119
1588ae2 @brson bench: Update shootout-nbody for style
brson authored
120 distance = libc::sqrt(dx * dx + dy * dy + dz * dz);
518dc52 @brson Reformat
brson authored
121 e -= bodies[i].mass * bodies[j].mass / distance;
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught…
graydon authored
122
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
123 j += 1;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
124 }
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
125
126 i += 1;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
127 }
b355936 @brson Convert ret to return
brson authored
128 return e;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
129
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
130 }
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
131 }
132
133 mod Body {
dffe188 @graydon Install new pub/priv/export rules as defaults, old rules accessible u…
graydon authored
134 #[legacy_exports];
2650487 @lkuper Got the nbody demo working, and hopefully faster, too!
lkuper authored
135
df7f21d @marijnh Reformat for new syntax
marijnh authored
136 const PI: float = 3.141592653589793;
137 const SOLAR_MASS: float = 39.478417604357432;
518dc52 @brson Reformat
brson authored
138 // was 4 * PI * PI originally
df7f21d @marijnh Reformat for new syntax
marijnh authored
139 const DAYS_PER_YEAR: float = 365.24;
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
140
df7f21d @marijnh Reformat for new syntax
marijnh authored
141 type props =
6e6798c @graydon Bulk-edit mutable -> mut.
graydon authored
142 {mut x: float,
143 mut y: float,
144 mut z: float,
145 mut vx: float,
146 mut vy: float,
147 mut vz: float,
df7f21d @marijnh Reformat for new syntax
marijnh authored
148 mass: float};
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
149
3816e57 @marijnh Downcase std modules again, move to :: for module dereferencing
marijnh authored
150 fn jupiter() -> Body::props {
b355936 @brson Convert ret to return
brson authored
151 return {mut x: 4.84143144246472090e+00,
6e6798c @graydon Bulk-edit mutable -> mut.
graydon authored
152 mut y: -1.16032004402742839e+00,
153 mut z: -1.03622044471123109e-01,
154 mut vx: 1.66007664274403694e-03 * DAYS_PER_YEAR,
155 mut vy: 7.69901118419740425e-03 * DAYS_PER_YEAR,
156 mut vz: -6.90460016972063023e-05 * DAYS_PER_YEAR,
df7f21d @marijnh Reformat for new syntax
marijnh authored
157 mass: 9.54791938424326609e-04 * SOLAR_MASS};
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
158 }
159
3816e57 @marijnh Downcase std modules again, move to :: for module dereferencing
marijnh authored
160 fn saturn() -> Body::props {
b355936 @brson Convert ret to return
brson authored
161 return {mut x: 8.34336671824457987e+00,
6e6798c @graydon Bulk-edit mutable -> mut.
graydon authored
162 mut y: 4.12479856412430479e+00,
163 mut z: -4.03523417114321381e-01,
164 mut vx: -2.76742510726862411e-03 * DAYS_PER_YEAR,
165 mut vy: 4.99852801234917238e-03 * DAYS_PER_YEAR,
166 mut vz: 2.30417297573763929e-05 * DAYS_PER_YEAR,
df7f21d @marijnh Reformat for new syntax
marijnh authored
167 mass: 2.85885980666130812e-04 * SOLAR_MASS};
168 }
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
169
3816e57 @marijnh Downcase std modules again, move to :: for module dereferencing
marijnh authored
170 fn uranus() -> Body::props {
b355936 @brson Convert ret to return
brson authored
171 return {mut x: 1.28943695621391310e+01,
6e6798c @graydon Bulk-edit mutable -> mut.
graydon authored
172 mut y: -1.51111514016986312e+01,
173 mut z: -2.23307578892655734e-01,
174 mut vx: 2.96460137564761618e-03 * DAYS_PER_YEAR,
175 mut vy: 2.37847173959480950e-03 * DAYS_PER_YEAR,
176 mut vz: -2.96589568540237556e-05 * DAYS_PER_YEAR,
df7f21d @marijnh Reformat for new syntax
marijnh authored
177 mass: 4.36624404335156298e-05 * SOLAR_MASS};
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
178 }
179
3816e57 @marijnh Downcase std modules again, move to :: for module dereferencing
marijnh authored
180 fn neptune() -> Body::props {
b355936 @brson Convert ret to return
brson authored
181 return {mut x: 1.53796971148509165e+01,
6e6798c @graydon Bulk-edit mutable -> mut.
graydon authored
182 mut y: -2.59193146099879641e+01,
183 mut z: 1.79258772950371181e-01,
184 mut vx: 2.68067772490389322e-03 * DAYS_PER_YEAR,
185 mut vy: 1.62824170038242295e-03 * DAYS_PER_YEAR,
186 mut vz: -9.51592254519715870e-05 * DAYS_PER_YEAR,
df7f21d @marijnh Reformat for new syntax
marijnh authored
187 mass: 5.15138902046611451e-05 * SOLAR_MASS};
188 }
bcc104c @lkuper A first stab at the 'nbody' demo, with support for calling C sqrt().
lkuper authored
189
df7f21d @marijnh Reformat for new syntax
marijnh authored
190 fn sun() -> Body::props {
b355936 @brson Convert ret to return
brson authored
191 return {mut x: 0.0,
6e6798c @graydon Bulk-edit mutable -> mut.
graydon authored
192 mut y: 0.0,
193 mut z: 0.0,
194 mut vx: 0.0,
195 mut vy: 0.0,
196 mut vz: 0.0,
df7f21d @marijnh Reformat for new syntax
marijnh authored
197 mass: SOLAR_MASS};
198 }
199
7a4ebce @lkuper Update nbody benchmark to more idiomatic Rust; nix obsolete comments
lkuper authored
200 fn offset_momentum(props: Body::props, px: float, py: float, pz: float) {
df7f21d @marijnh Reformat for new syntax
marijnh authored
201 props.vx = -px / SOLAR_MASS;
202 props.vy = -py / SOLAR_MASS;
203 props.vz = -pz / SOLAR_MASS;
204 }
205
f91351a @lkuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
206 }
Something went wrong with that request. Please try again.