[commit: nofib] master: Add the n-body shootout benchmark (a735c7a)

Johan Tibell johan.tibell at gmail.com
Tue Feb 5 22:23:45 CET 2013


Repository : ssh://darcs.haskell.org//srv/darcs/nofib

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/a735c7a1a6628ec6832eacf3798626c7132477fa

>---------------------------------------------------------------

commit a735c7a1a6628ec6832eacf3798626c7132477fa
Author: Johan Tibell <johan.tibell at gmail.com>
Date:   Tue Feb 5 13:23:30 2013 -0800

    Add the n-body shootout benchmark

>---------------------------------------------------------------

 .gitignore                                         |    1 +
 shootout/Makefile                                  |    2 +-
 .../Nbody/nbody.hs => shootout/n-body/Main.hs      |   51 +++++++++-----------
 shootout/{fannkuch-redux => n-body}/Makefile       |    8 ++--
 shootout/n-body/n-body.faststdout                  |    2 +
 shootout/n-body/n-body.slowstdout                  |    2 +
 shootout/n-body/n-body.stdout                      |    2 +
 7 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/.gitignore b/.gitignore
index 8d0effb..a778995 100644
--- a/.gitignore
+++ b/.gitignore
@@ -53,6 +53,7 @@ real/veritas/veritas
 
 shootout/binary-trees/binary-trees
 shootout/fannkuch-redux/fannkuch-redux
+shootout/n-body/n-body
 shootout/pidigits/pidigits
 shootout/spectral-norm/spectral-norm
 
diff --git a/shootout/Makefile b/shootout/Makefile
index 16733c9..0b039e5 100644
--- a/shootout/Makefile
+++ b/shootout/Makefile
@@ -1,7 +1,7 @@
 TOP = ..
 include $(TOP)/mk/boilerplate.mk
 
-SUBDIRS = binary-trees fannkuch-redux pidigits spectral-norm
+SUBDIRS = binary-trees fannkuch-redux n-body pidigits spectral-norm
 
 include $(TOP)/mk/target.mk
 
diff --git a/fibon/Shootout/Nbody/nbody.hs b/shootout/n-body/Main.hs
similarity index 82%
copy from fibon/Shootout/Nbody/nbody.hs
copy to shootout/n-body/Main.hs
index 6d76b44..59fcb5b 100644
--- a/fibon/Shootout/Nbody/nbody.hs
+++ b/shootout/n-body/Main.hs
@@ -1,14 +1,14 @@
-{-# OPTIONS_GHC -fexcess-precision #-}
 -- 
--- The Computer Language Shootout
--- http://shootout.alioth.debian.org/
+-- The Computer Language Benchmarks Game
+-- http://benchmarksgame.alioth.debian.org/
 --        
--- Contributed by Olof Kraigher, with help from Don Stewart.
+-- Contributed by Olof Kraigher and Don Stewart.
 --     
--- Compile with:
+-- To be compiled with:
 --
---  -funbox-strict-fields -fglasgow-exts -fbang-patterns -O3
---      -optc-O3 -optc-mfpmath=sse -optc-msse2 -optc-march=pentium4 
+--  -O2 -fglasgow-exts -funbox-strict-fields -fbang-patterns -optc-O 
+--
+-- Don't enable -optc-mfpmath=sse -optc-msse2, this triggers a gcc bug on x86
 -- 
 
 import Foreign
@@ -23,10 +23,9 @@ main = do
     n <- getArgs >>= readIO.head
     initialize
     offset_momentum
-    energy 0 planets >>= printf "%.2f\n"
+    energy 0 planets >>= printf "%.9f\n"
     replicateM_ n (advance planets)
-    energy 0 planets >>= printf "%.2f\n"
-    return ()
+    energy 0 planets >>= printf "%.9f\n"
 
 offset_momentum = do
     m <- foldr (.+.) (Vec 0 0 0)
@@ -36,11 +35,10 @@ offset_momentum = do
 
     setVec (vel planets) $ (-1/solar_mass) *. m
   where
-    momentum p = p `seq` liftM2 (*.) (mass p) (getVec (vel p))
+    momentum !p = liftM2 (*.) (mass p) (getVec (vel p))
 
 energy :: Double -> Ptr Double -> IO Double
-energy e p
-    | e `seq` p `seq` False = undefined
+energy !e !p
     | p == end = return e
     | otherwise      = do
         p1 <- getVec (pos p)
@@ -50,8 +48,7 @@ energy e p
         energy (e + 0.5 * m1 * magnitude2 v1) p2
     where p2 = next p
 
-energy2 p1 m1 e p
-    | p1 `seq` m1 `seq` e `seq` p `seq` False = undefined
+energy2 !p1 !m1 !e !p
     | p  == end = return e
     | otherwise = do
         p2 <- getVec (pos p)
@@ -61,12 +58,10 @@ energy2 p1 m1 e p
         energy2 p1 m1 (e - m1 * m2 / distance) (next p)
 
 advance :: Ptr Double -> IO ()
-advance p1   | p1 `seq` False = undefined
-advance p1 = when (p1 /= end) $ do
+advance !p1 = when (p1 /= end) $ do
     pos1 <- getVec $ pos p1
     m1   <- mass p1
-    let go p2
-            | p2 `seq` False = undefined
+    let go !p2
             | p2 /= end = do
                 pos2 <- getVec (pos p2)
                 m2   <- mass p2
@@ -131,20 +126,20 @@ initialize = mapM_ newPlanet planets
 
 data Vector3 = Vec !Double !Double !Double
 
-cursor :: IORef (Ptr Double)
-cursor = unsafePerformIO $ newIORef planets
-
 end :: Ptr Double
-end  = inc planets (nbodies * 7)
+end = inc planets $ nbodies * 7
 
 next  :: Ptr Double -> Ptr Double
-next = flip inc 7
+next p = inc p 7
+
+cursor :: IORef (Ptr Double)
+cursor = unsafePerformIO $ newIORef planets
 
 inc :: Ptr Double -> Int -> Ptr Double
-inc ptr n = ptr `seq` n `seq` plusPtr ptr (n * 8)
+inc ptr n = plusPtr ptr (n * 8)
 
 newPlanet :: Double -> IO ()
-newPlanet d = d `seq` do
+newPlanet !d = do
     ptr <- readIORef cursor
     pokeElemOff ptr 0 d
     writeIORef cursor (inc ptr 1)
@@ -156,7 +151,7 @@ vel :: Ptr Double -> Ptr Double
 vel ptr = inc ptr 3
 
 mass :: Ptr Double -> IO Double
-mass = flip peekElemOff 6
+mass ptr = peekElemOff ptr 6
 
 ------------------------------------------------------------------------
 
@@ -170,7 +165,7 @@ magnitude2 (Vec x y z) = x*x + y*y + z*z
 
 ------------------------------------------------------------------------
 
-getVec p = p `seq` liftM3 Vec (peek p) (f 1) (f 2)
+getVec !p = liftM3 Vec (peek p) (f 1) (f 2)
     where f = peekElemOff p
 
 setVec p (Vec x y z)= do
diff --git a/shootout/fannkuch-redux/Makefile b/shootout/n-body/Makefile
similarity index 58%
copy from shootout/fannkuch-redux/Makefile
copy to shootout/n-body/Makefile
index facb262..1ea0b09 100644
--- a/shootout/fannkuch-redux/Makefile
+++ b/shootout/n-body/Makefile
@@ -2,10 +2,10 @@ TOP = ../..
 include $(TOP)/mk/boilerplate.mk
 include $(TOP)/mk/target.mk
 
-FAST_OPTS = 10
-NORM_OPTS = 11
-SLOW_OPTS = 12  # official shootout setting
+FAST_OPTS = 500000
+NORM_OPTS = 5000000
+SLOW_OPTS = 50000000  # official shootout setting
 
 # The benchmark game also uses -fllvm, which we can't since it might
 # not be available on the developer's machine.
-HC_OPTS += -XBangPatterns -O2
+HC_OPTS += -O2 -XBangPatterns -fexcess-precision
diff --git a/shootout/n-body/n-body.faststdout b/shootout/n-body/n-body.faststdout
new file mode 100644
index 0000000..e624e02
--- /dev/null
+++ b/shootout/n-body/n-body.faststdout
@@ -0,0 +1,2 @@
+-0.169075164
+-0.169096567
diff --git a/shootout/n-body/n-body.slowstdout b/shootout/n-body/n-body.slowstdout
new file mode 100644
index 0000000..a6a8ff5
--- /dev/null
+++ b/shootout/n-body/n-body.slowstdout
@@ -0,0 +1,2 @@
+-0.169075164
+-0.169059907
diff --git a/shootout/n-body/n-body.stdout b/shootout/n-body/n-body.stdout
new file mode 100644
index 0000000..e1eb1f5
--- /dev/null
+++ b/shootout/n-body/n-body.stdout
@@ -0,0 +1,2 @@
+-0.169075164
+-0.169083134





More information about the ghc-commits mailing list