|
1 | 1 | // based on:
|
2 | 2 | // http://shootout.alioth.debian.org/u32/benchmark.php?test=nbody&lang=java
|
3 | 3 |
|
4 |
| -#[abi = "cdecl"] |
| 4 | +use std; |
| 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. |
5 | 11 | #[nolink]
|
6 |
| -native mod llvm { |
| 12 | +native mod libc { |
7 | 13 | fn sqrt(n: float) -> float;
|
8 | 14 | }
|
9 | 15 |
|
10 |
| -fn main() { |
11 |
| - // |
12 |
| - // Leave these commented out to |
13 |
| - // finish in a reasonable time |
14 |
| - // during 'make check' under valgrind |
15 |
| - // 5000000 |
16 |
| - // 50000000 |
17 |
| - let inputs: [int] = [50000, 500000]; |
18 |
| - |
| 16 | +fn main(args: [str]) { |
| 17 | + let n = if vec::len(args) == 2u { |
| 18 | + int::from_str(args[1]) |
| 19 | + } else { |
| 20 | + 1000000 |
| 21 | + }; |
19 | 22 | let bodies: [Body::props] = NBodySystem::MakeNBodySystem();
|
20 |
| - |
21 |
| - |
22 |
| - for n: int in inputs { |
23 |
| - log(debug, NBodySystem::energy(bodies)); |
24 |
| - |
25 |
| - let i: int = 0; |
26 |
| - while i < n { NBodySystem::advance(bodies, 0.01); i += 1; } |
27 |
| - log(debug, NBodySystem::energy(bodies)); |
28 |
| - } |
| 23 | + std::io::println(#fmt("%f", NBodySystem::energy(bodies))); |
| 24 | + let i: int = 0; |
| 25 | + while i < n { NBodySystem::advance(bodies, 0.01); i += 1; } |
| 26 | + std::io::println(#fmt("%f", NBodySystem::energy(bodies))); |
29 | 27 | }
|
30 | 28 |
|
31 | 29 | // Body::props is a record of floats, so
|
@@ -79,7 +77,7 @@ mod NBodySystem {
|
79 | 77 |
|
80 | 78 | let dSquared: float = dx * dx + dy * dy + dz * dz;
|
81 | 79 |
|
82 |
| - let distance: float = llvm::sqrt(dSquared); |
| 80 | + let distance: float = libc::sqrt(dSquared); |
83 | 81 | let mag: float = dt / (dSquared * distance);
|
84 | 82 |
|
85 | 83 | bi.vx -= dx * bj.mass * mag;
|
@@ -117,7 +115,7 @@ mod NBodySystem {
|
117 | 115 | dy = bodies[i].y - bodies[j].y;
|
118 | 116 | dz = bodies[i].z - bodies[j].z;
|
119 | 117 |
|
120 |
| - distance = llvm::sqrt(dx * dx + dy * dy + dz * dz); |
| 118 | + distance = libc::sqrt(dx * dx + dy * dy + dz * dz); |
121 | 119 | e -= bodies[i].mass * bodies[j].mass / distance;
|
122 | 120 |
|
123 | 121 | j += 1;
|
|
0 commit comments