spectral-norm Haskell GHC #2 program
source code
--
-- The Computer Language Benchmarks Game
-- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
-- Contributed by Greg Buchholz
import Data.Array.Base
import System.Environment
import Numeric
main = do
[arg] <- getArgs
let n = (read arg) - 1
let init = listArray (0,n) (repeat 1.0)
let (v:u:rest) = drop 19 $ iterate (eval_AtA_times_u n) init
let vBv = sum [(u!i)*(v!i) |i<-[0..n]]
let vv = sum [(v!i)*(v!i) |i<-[0..n]]
putStrLn $ showFFloat (Just 9) (sqrt (vBv/vv)) ""
eval_AtA_times_u n u = eval_At_times_u n v
where v = eval_A_times_u n u
eval_A x y = 1.0/((i+j)*(i+j+1)/2+i+1)
where i = fromIntegral x
j = fromIntegral y
eval_A_times_u :: Int -> UArray Int Double -> UArray Int Double
eval_A_times_u n u = unsafeAccumArray (+) 0 (0,n)
[(i,(eval_A i j) * u!j)|i<-[0..n], j<-[0..n]]
eval_At_times_u :: Int -> UArray Int Double -> UArray Int Double
eval_At_times_u n u = unsafeAccumArray (+) 0 (0,n)
[(i,(eval_A j i) * u!j)|i<-[0..n], j<-[0..n]]
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
The Glorious Glasgow Haskell Compilation System,
version 8.8.1
Tue, 05 May 2020 23:20:07 GMT
MAKE:
mv spectralnorm.ghc-2.ghc spectralnorm.ghc-2.hs
/opt/src/ghc-8.8.1/bin/ghc --make -fllvm -O2 -XBangPatterns -threaded -rtsopts -XMagicHash spectralnorm.ghc-2.hs -o spectralnorm.ghc-2.ghc_run
Loaded package environment from /home/dunham/.ghc/x86_64-linux-8.8.1/environments/default
[1 of 1] Compiling Main ( spectralnorm.ghc-2.hs, spectralnorm.ghc-2.o )
You are using an unsupported version of LLVM!
Currently only 7 is supported.
We will try though...
Linking spectralnorm.ghc-2.ghc_run ...
rm spectralnorm.ghc-2.hs
23.13s to complete and log all make actions
COMMAND LINE:
./spectralnorm.ghc-2.ghc_run +RTS -N4 -RTS 5500
PROGRAM OUTPUT:
1.274224153