]> Cypherpunks repositories - gostls13.git/commitdiff
nbody benchmark
authorRob Pike <r@golang.org>
Tue, 4 Aug 2009 21:27:46 +0000 (14:27 -0700)
committerRob Pike <r@golang.org>
Tue, 4 Aug 2009 21:27:46 +0000 (14:27 -0700)
timing.sh improvements

R=rsc
DELTA=334  (319 added, 0 deleted, 15 changed)
OCL=32734
CL=32736

test/bench/nbody.c [new file with mode: 0644]
test/bench/nbody.go [new file with mode: 0644]
test/bench/nbody.txt [new file with mode: 0644]
test/bench/timing.log
test/bench/timing.sh

diff --git a/test/bench/nbody.c b/test/bench/nbody.c
new file mode 100644 (file)
index 0000000..3b95b05
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+    * Neither the name of "The Computer Language Benchmarks Game" nor the
+    name of "The Computer Language Shootout Benchmarks" nor the names of
+    its contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/*
+ * The Great Computer Language Shootout
+ * http://shootout.alioth.debian.org/
+ *
+ * contributed by Christoph Bauer
+ *
+ */
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define pi 3.141592653589793
+#define solar_mass (4 * pi * pi)
+#define days_per_year 365.24
+
+struct planet {
+  double x, y, z;
+  double vx, vy, vz;
+  double mass;
+};
+
+void advance(int nbodies, struct planet * bodies, double dt)
+{
+  int i, j;
+
+  for (i = 0; i < nbodies; i++) {
+    struct planet * b = &(bodies[i]);
+    for (j = i + 1; j < nbodies; j++) {
+      struct planet * b2 = &(bodies[j]);
+      double dx = b->x - b2->x;
+      double dy = b->y - b2->y;
+      double dz = b->z - b2->z;
+      double distance = sqrt(dx * dx + dy * dy + dz * dz);
+      double mag = dt / (distance * distance * distance);
+      b->vx -= dx * b2->mass * mag;
+      b->vy -= dy * b2->mass * mag;
+      b->vz -= dz * b2->mass * mag;
+      b2->vx += dx * b->mass * mag;
+      b2->vy += dy * b->mass * mag;
+      b2->vz += dz * b->mass * mag;
+    }
+  }
+  for (i = 0; i < nbodies; i++) {
+    struct planet * b = &(bodies[i]);
+    b->x += dt * b->vx;
+    b->y += dt * b->vy;
+    b->z += dt * b->vz;
+  }
+}
+
+double energy(int nbodies, struct planet * bodies)
+{
+  double e;
+  int i, j;
+
+  e = 0.0;
+  for (i = 0; i < nbodies; i++) {
+    struct planet * b = &(bodies[i]);
+    e += 0.5 * b->mass * (b->vx * b->vx + b->vy * b->vy + b->vz * b->vz);
+    for (j = i + 1; j < nbodies; j++) {
+      struct planet * b2 = &(bodies[j]);
+      double dx = b->x - b2->x;
+      double dy = b->y - b2->y;
+      double dz = b->z - b2->z;
+      double distance = sqrt(dx * dx + dy * dy + dz * dz);
+      e -= (b->mass * b2->mass) / distance;
+    }
+  }
+  return e;
+}
+
+void offset_momentum(int nbodies, struct planet * bodies)
+{
+  double px = 0.0, py = 0.0, pz = 0.0;
+  int i;
+  for (i = 0; i < nbodies; i++) {
+    px += bodies[i].vx * bodies[i].mass;
+    py += bodies[i].vy * bodies[i].mass;
+    pz += bodies[i].vz * bodies[i].mass;
+  }
+  bodies[0].vx = - px / solar_mass;
+  bodies[0].vy = - py / solar_mass;
+  bodies[0].vz = - pz / solar_mass;
+}
+
+#define NBODIES 5
+struct planet bodies[NBODIES] = {
+  {                               /* sun */
+    0, 0, 0, 0, 0, 0, solar_mass
+  },
+  {                               /* jupiter */
+    4.84143144246472090e+00,
+    -1.16032004402742839e+00,
+    -1.03622044471123109e-01,
+    1.66007664274403694e-03 * days_per_year,
+    7.69901118419740425e-03 * days_per_year,
+    -6.90460016972063023e-05 * days_per_year,
+    9.54791938424326609e-04 * solar_mass
+  },
+  {                               /* saturn */
+    8.34336671824457987e+00,
+    4.12479856412430479e+00,
+    -4.03523417114321381e-01,
+    -2.76742510726862411e-03 * days_per_year,
+    4.99852801234917238e-03 * days_per_year,
+    2.30417297573763929e-05 * days_per_year,
+    2.85885980666130812e-04 * solar_mass
+  },
+  {                               /* uranus */
+    1.28943695621391310e+01,
+    -1.51111514016986312e+01,
+    -2.23307578892655734e-01,
+    2.96460137564761618e-03 * days_per_year,
+    2.37847173959480950e-03 * days_per_year,
+    -2.96589568540237556e-05 * days_per_year,
+    4.36624404335156298e-05 * solar_mass
+  },
+  {                               /* neptune */
+    1.53796971148509165e+01,
+    -2.59193146099879641e+01,
+    1.79258772950371181e-01,
+    2.68067772490389322e-03 * days_per_year,
+    1.62824170038242295e-03 * days_per_year,
+    -9.51592254519715870e-05 * days_per_year,
+    5.15138902046611451e-05 * solar_mass
+  }
+};
+
+int main(int argc, char ** argv)
+{
+  int n = atoi(argv[1]);
+  int i;
+
+  offset_momentum(NBODIES, bodies);
+  printf ("%.9f\n", energy(NBODIES, bodies));
+  for (i = 1; i <= n; i++)
+    advance(NBODIES, bodies, 0.01);
+  printf ("%.9f\n", energy(NBODIES, bodies));
+  return 0;
+}
diff --git a/test/bench/nbody.go b/test/bench/nbody.go
new file mode 100644 (file)
index 0000000..1664d29
--- /dev/null
@@ -0,0 +1,177 @@
+/*
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in the
+    documentation and/or other materials provided with the distribution.
+
+    * Neither the name of "The Computer Language Benchmarks Game" nor the
+    name of "The Computer Language Shootout Benchmarks" nor the names of
+    its contributors may be used to endorse or promote products derived
+    from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+*/
+
+/* The Computer Language Benchmarks Game
+ * http://shootout.alioth.debian.org/
+ *
+ * contributed by The Go Authors.
+ * based on C program by Christoph Bauer
+ */
+
+package main
+
+import (
+       "flag";
+       "fmt";
+       "math";
+)
+
+var n = flag.Int("n", 1000, "number of iterations")
+
+type Body struct {
+       x, y, z, vx, vy, vz, mass float64
+}
+
+const (
+       solarMass = 4 * math.Pi * math.Pi;
+       daysPerYear = 365.24;
+)
+
+func (b *Body) offsetMomentum(px, py, pz float64){
+       b.vx = -px / solarMass;
+       b.vy = -py / solarMass;
+       b.vz = -pz / solarMass;
+}
+
+type System []*Body
+
+func NewSystem(body []Body) System {
+       n := make(System, len(body));
+       for i := 0; i < len(body); i++ {
+               n[i] = new(Body);       // copy to avoid overwriting the inputs
+               *n[i] = body[i];
+       }
+       var px, py, pz float64;
+       for _, body := range n {
+               px += body.vx * body.mass;
+               py += body.vy * body.mass;
+               pz += body.vz * body.mass;
+       }
+       n[0].offsetMomentum(px, py, pz);
+       return n;
+}
+
+func (sys System) energy() float64 {
+       var e float64;
+       for i, body := range sys {
+               e += 0.5 * body.mass *
+                       (body.vx*body.vx + body.vy*body.vy + body.vz*body.vz);
+               for j := i+1; j < len(sys); j++ {
+                       body2 := sys[j];
+                       dx := body.x - body2.x;
+                       dy := body.y - body2.y;
+                       dz := body.z - body2.z;
+                       distance := math.Sqrt(dx*dx + dy*dy + dz*dz);
+                       e -= (body.mass * body2.mass) / distance;
+               }
+       }
+       return e;
+}
+
+func (sys System) advance(dt float64) {
+       for i, body := range sys {
+               for j := i+1; j < len(sys); j++ {
+                       body2 := sys[j];
+                       dx := body.x - body2.x;
+                       dy := body.y - body2.y;
+                       dz := body.z - body2.z;
+
+                       dSquared := dx*dx + dy*dy + dz*dz;
+                       distance := math.Sqrt(dSquared);
+                       mag := dt / (dSquared * distance);
+
+                       body.vx -= dx * body2.mass * mag;
+                       body.vy -= dy * body2.mass * mag;
+                       body.vz -= dz * body2.mass * mag;
+
+                       body2.vx += dx * body.mass * mag;
+                       body2.vy += dy * body.mass * mag;
+                       body2.vz += dz * body.mass * mag;
+               }
+       }
+
+       for _, body := range sys {
+               body.x += dt * body.vx;
+               body.y += dt * body.vy;
+               body.z += dt * body.vz;
+       }
+}
+
+var (
+       jupiter = Body {
+               x: 4.84143144246472090e+00,
+               y: -1.16032004402742839e+00,
+               z: -1.03622044471123109e-01,
+               vx: 1.66007664274403694e-03 * daysPerYear,
+               vy: 7.69901118419740425e-03 * daysPerYear,
+               vz: -6.90460016972063023e-05 * daysPerYear,
+               mass: 9.54791938424326609e-04 * solarMass,
+       };
+       saturn = Body {
+               x: 8.34336671824457987e+00,
+               y: 4.12479856412430479e+00,
+               z: -4.03523417114321381e-01,
+               vx: -2.76742510726862411e-03 * daysPerYear,
+               vy: 4.99852801234917238e-03 * daysPerYear,
+               vz: 2.30417297573763929e-05 * daysPerYear,
+               mass: 2.85885980666130812e-04 * solarMass,
+       };
+       uranus = Body {
+               x: 1.28943695621391310e+01,
+               y: -1.51111514016986312e+01,
+               z: -2.23307578892655734e-01,
+               vx: 2.96460137564761618e-03 * daysPerYear,
+               vy: 2.37847173959480950e-03 * daysPerYear,
+               vz: -2.96589568540237556e-05 * daysPerYear,
+               mass: 4.36624404335156298e-05 * solarMass,
+       };
+       neptune = Body {
+               x: 1.53796971148509165e+01,
+               y: -2.59193146099879641e+01,
+               z: 1.79258772950371181e-01,
+               vx: 2.68067772490389322e-03 * daysPerYear,
+               vy: 1.62824170038242295e-03 * daysPerYear,
+               vz: -9.51592254519715870e-05 * daysPerYear,
+               mass: 5.15138902046611451e-05 * solarMass,
+       };
+       sun = Body {
+               mass: solarMass
+       }
+)
+
+func main() {
+       flag.Parse();
+
+       system := NewSystem([]Body{sun, jupiter, saturn, uranus, neptune});
+       fmt.Printf("%.9f\n", system.energy());
+       for i := 0; i < *n; i++ {
+               system.advance(0.01)
+       }
+       fmt.Printf("%.9f\n", system.energy());
+}
diff --git a/test/bench/nbody.txt b/test/bench/nbody.txt
new file mode 100644 (file)
index 0000000..a6a8ff5
--- /dev/null
@@ -0,0 +1,2 @@
+-0.169075164
+-0.169059907
index 9f25b573c9fa31dfc06f319165cca7e17c2d9f58..8060d8e9f8762c3730f07ebeaa6eb1fd5546ad93 100644 (file)
@@ -29,5 +29,8 @@ reverse-complement < output-of-fasta-25000000
        gc reverse-complement   6.55u 0.70s 7.26r
        gc_B reverse-complement 6.32u 0.70s 7.10r
 
-nbody -n 50000000
-       gcc -O2 nbody.c 21.33u 0.00s 21.34r
+nbody 50000000
+       gcc -O2 nbody.c 21.61u 0.01s 24.80r
+       gccgo -O2 nbody.go      118.55u 0.02s 120.32r
+       gc nbody        100.84u 0.00s 100.85r
+       gc_B nbody      103.33u 0.00s 103.39r
index f80f247a4622d6664da1cb97eea310b6dec30b76..2cd5d8d7856291a034f1df8c34d33241f06c7268 100755 (executable)
@@ -21,18 +21,42 @@ run() {
        (/home/r/plan9/bin/time $* 2>&1 >/dev/null) |  sed 's/r.*/r/'
 }
 
-echo 'fasta -n 25000000'
-run 'gcc -O2 fasta.c' a.out 25000000
-#run 'gccgo -O2 fasta.go' a.out -n 25000000    #commented out until WriteString is in bufio
-run 'gc fasta' $O.out -n 25000000
-run 'gc_B fasta' $O.out -n 25000000
-
-echo
-6.out -n 25000000 > x
-echo 'reverse-complement < output-of-fasta-25000000'
-run 'gcc -O2 reverse-complement.c' a.out 25000000 < x
-run 'gccgo -O2 reverse-complement.go' a.out -n 25000000 < x
-run 'gc reverse-complement' $O.out -n 25000000 < x
-run 'gc_B reverse-complement' $O.out -n 25000000 < x
-rm x
+fasta() {
+       echo 'fasta -n 25000000'
+       run 'gcc -O2 fasta.c' a.out 25000000
+       #run 'gccgo -O2 fasta.go' a.out -n 25000000     #commented out until WriteString is in bufio
+       run 'gc fasta' $O.out -n 25000000
+       run 'gc_B fasta' $O.out -n 25000000
+}
+
+revcomp() {
+       6.out -n 25000000 > x
+       echo 'reverse-complement < output-of-fasta-25000000'
+       run 'gcc -O2 reverse-complement.c' a.out 25000000 < x
+       run 'gccgo -O2 reverse-complement.go' a.out -n 25000000 < x
+       run 'gc reverse-complement' $O.out -n 25000000 < x
+       run 'gc_B reverse-complement' $O.out -n 25000000 < x
+       rm x
+}
+
+nbody() {
+       echo 'nbody -n 50000000'
+       run 'gcc -O2 nbody.c' a.out 50000000
+       run 'gccgo -O2 nbody.go' a.out -n 50000000
+       run 'gc nbody' $O.out -n 50000000
+       run 'gc_B nbody' $O.out -n 50000000
+}
+
+case $# in
+0)
+       run="fasta revcom nbody"
+       ;;
+*)
+       run=$*
+esac
 
+for i in $run
+do
+       $i
+       echo
+done