Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

nbody.perl-2.perl: 2x faster by loop unrolling to AELEMFAST

  • Loading branch information...
commit 62b216756320e8c224eef2c933326924ab73c18a 1 parent f2a0dc0
Reini Urban authored October 06, 2012

Showing 1 changed file with 141 additions and 0 deletions. Show diff stats Hide diff stats

  1. 141  bench/nbody/nbody.perl-2.perl
141  bench/nbody/nbody.perl-2.perl
... ...
@@ -0,0 +1,141 @@
  1
+# The Computer Language Shootout
  2
+# http://shootout.alioth.debian.org/
  3
+#
  4
+# contributed by Christoph Bauer
  5
+# converted into Perl by Márton Papp
  6
+# fixed and cleaned up by Danny Sauer
  7
+# optimized by Jesse Millikan
  8
+# optimized by Reini Urban
  9
+
  10
+use constant PI            => 3.141592653589793;
  11
+use constant SOLAR_MASS    => (4 * PI * PI);
  12
+use constant DAYS_PER_YEAR => 365.24;
  13
+
  14
+sub energy;
  15
+sub advance($);
  16
+sub offset_momentum;
  17
+
  18
+my (@xs, @ys, @zs, @vxs, @vys, @vzs, @mass, $last);
  19
+my ($energy, $offset_momentum, $advance);
  20
+BEGIN {
  21
+# Global lexicals for arrays.
  22
+# Almost every iteration is a range, so I keep the last index rather than a count.
  23
+
  24
+# @ns = ( sun, jupiter, saturn, uranus, neptune )
  25
+@xs = (0, 4.84143144246472090e+00, 8.34336671824457987e+00, 1.28943695621391310e+01, 1.53796971148509165e+01);
  26
+@ys = (0, -1.16032004402742839e+00, 4.12479856412430479e+00, -1.51111514016986312e+01, -2.59193146099879641e+01);
  27
+@zs = (0, -1.03622044471123109e-01, -4.03523417114321381e-01, -2.23307578892655734e-01, 1.79258772950371181e-01);
  28
+@vxs = map {$_ * DAYS_PER_YEAR}
  29
+  (0, 1.66007664274403694e-03, -2.76742510726862411e-03, 2.96460137564761618e-03, 2.68067772490389322e-03);
  30
+@vys = map {$_ * DAYS_PER_YEAR}
  31
+  (0, 7.69901118419740425e-03, 4.99852801234917238e-03, 2.37847173959480950e-03, 1.62824170038242295e-03);
  32
+@vzs = map {$_ * DAYS_PER_YEAR}
  33
+  (0, -6.90460016972063023e-05, 2.30417297573763929e-05, -2.96589568540237556e-05, -9.51592254519715870e-05);
  34
+@mass = map {$_ * SOLAR_MASS}
  35
+  (1, 9.54791938424326609e-04, 2.85885980666130812e-04, 4.36624404335156298e-05, 5.15138902046611451e-05);
  36
+$last = $#xs;
  37
+
  38
+# Optimize array accesses: $a[const] are optimized to AELEMFAST, $a[$lexical] not.
  39
+# So unroll the loops in macro-like fashion (2x times faster). We do it in a BEGIN block,
  40
+# so perlcc can also benefit (again 2x faster).
  41
+$energy = '
  42
+sub energy
  43
+{
  44
+  my $e = 0.0;
  45
+  my ($dx, $dy, $dz, $distance);';
  46
+for my $i (0 .. $last) {
  47
+  $energy .= "
  48
+# loop $i..4
  49
+    \$e += 0.5 * \$mass[$i] *
  50
+          (\$vxs[$i] * \$vxs[$i] + \$vys[$i] * \$vys[$i] + \$vzs[$i] * \$vzs[$i]);
  51
+";
  52
+  for (my $j = $i + 1; $j < $last + 1; $j++) {
  53
+    $energy .= "
  54
+    # inner-loop $j..4
  55
+    \$dx = \$xs[$i] - \$xs[$j];
  56
+    \$dy = \$ys[$i] - \$ys[$j];
  57
+    \$dz = \$zs[$i] - \$zs[$j];
  58
+    \$distance = sqrt(\$dx * \$dx + \$dy * \$dy + \$dz * \$dz);
  59
+    \$e -= (\$mass[$i] * \$mass[$j]) / \$distance;";
  60
+  }
  61
+}
  62
+$energy .= '
  63
+  return $e;
  64
+}';
  65
+eval $energy; die if $@;
  66
+
  67
+$advance = '
  68
+sub advance($)
  69
+{
  70
+  my $dt = $_[0];
  71
+  my ($mm, $mm2, $j, $dx, $dy, $dz, $distance, $mag);
  72
+';
  73
+# This is faster in the outer loop...
  74
+for my $i (0..$last) {
  75
+  # But not in the inner loop. Strange.
  76
+  for ($j = $i + 1; $j < $last + 1; $j++) {
  77
+    $advance .= "
  78
+      # outer-loop $i..4
  79
+      # inner-loop $j..4
  80
+      \$dx = \$xs[$i] - \$xs[$j];
  81
+      \$dy = \$ys[$i] - \$ys[$j];
  82
+      \$dz = \$zs[$i] - \$zs[$j];
  83
+      \$distance = sqrt(\$dx * \$dx + \$dy * \$dy + \$dz * \$dz);
  84
+      \$mag = \$dt / (\$distance * \$distance * \$distance);
  85
+      \$mm  = \$mass[$i] * \$mag;
  86
+      \$mm2 = \$mass[$j] * \$mag;
  87
+      \$vxs[$i] -= \$dx * \$mm2;
  88
+      \$vxs[$j] += \$dx * \$mm;
  89
+      \$vys[$i] -= \$dy * \$mm2;
  90
+      \$vys[$j] += \$dy * \$mm;
  91
+      \$vzs[$i] -= \$dz * \$mm2;
  92
+      \$vzs[$j] += \$dz * \$mm;";
  93
+  }
  94
+}
  95
+# We're done with planet $i at this point
  96
+for my $i (0..$last) {
  97
+  $advance .= "
  98
+  \$xs[$i] += \$dt * \$vxs[$i];
  99
+  \$ys[$i] += \$dt * \$vys[$i];
  100
+  \$zs[$i] += \$dt * \$vzs[$i];";
  101
+}
  102
+  $advance .= '
  103
+}';
  104
+eval $advance; die if $@;
  105
+
  106
+$offset_momentum = ';
  107
+sub offset_momentum
  108
+{
  109
+  my $px = 0.0; 
  110
+  my $py = 0.0;
  111
+  my $pz = 0.0;
  112
+  my $mass;
  113
+';
  114
+for my $i (0 .. $last) {
  115
+  $offset_momentum .= "
  116
+    \$mass = \$mass[$i];
  117
+    \$px += \$vxs[$i] * \$mass;
  118
+    \$py += \$vys[$i] * \$mass;
  119
+    \$pz += \$vzs[$i] * \$mass;";
  120
+}
  121
+$offset_momentum .= '
  122
+  $vxs[0] = - $px / SOLAR_MASS;
  123
+  $vys[0] = - $py / SOLAR_MASS;
  124
+  $vzs[0] = - $pz / SOLAR_MASS;
  125
+}';
  126
+eval $offset_momentum; die if $@;
  127
+
  128
+} #BEGIN
  129
+
  130
+offset_momentum();
  131
+printf ("%.9f\n", energy());
  132
+
  133
+my $n = $ARGV[0];
  134
+$n =~ s/[,_]//g; # allow 50_000_000 or 50,000,000
  135
+
  136
+# This does not, in fact, consume N*4 bytes of memory
  137
+for (1 .. $n) {
  138
+  advance(0.01);
  139
+}
  140
+
  141
+printf ("%.9f\n", energy());

0 notes on commit 62b2167

Please sign in to comment.
Something went wrong with that request. Please try again.