Skip to content

HTTPS clone URL

Subversion checkout URL

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