The Computer Language
Benchmarks Game

spectral-norm F# Mono LLVM #3 program

source code

// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// Based on C version by Ledrug and F# version by Don Syme
// Contributed by Simon Dickson

open System
open System.Threading

let nthread = Environment.ProcessorCount

let inline A i j = 1. / float((i + j) * (i + j + 1) / 2 + i + 1)

let inline dot (v : float array) (u : float array) n =
   let mutable sum = 0.
   for i = 0 to n - 1 do
      sum <- sum + v.[i] * u.[i]
   sum

let inline multiplyAv (v : double[]) (Av : double[]) r1 r2 =
    for i = r1 to r2 - 1 do 
        let mutable sum = 0.
        for j = 0 to v.Length - 1 do 
            sum <- sum + A i j * v.[j]
        Av.[i] <- sum

let inline multiplyAtv (v : double[]) (atv : double[]) r1 r2 =
    for i = r1 to r2 - 1 do
        let mutable sum = 0.
        for j = 0 to v.Length - 1 do 
            sum <- sum + A j i * v.[j]
        atv.[i] <- sum

let current = ref nthread
let mutable handle = new ManualResetEvent (false)
let inline barrierHandle () =
    let h = handle
    if Interlocked.Decrement current > 0 then
        h.WaitOne() |> ignore
    else
        handle <- new ManualResetEvent (false)
        Interlocked.Exchange (current, nthread) |> ignore
        h.Set () |> ignore
        h.Dispose ()

let mutable tmp = null
let inline multiplyAtAv (v : double[]) (out : double[]) r1 r2 =
    multiplyAv v tmp r1 r2
    barrierHandle ()
    multiplyAtv tmp out r1 r2
    barrierHandle ()

[<EntryPoint>]
let main args =
    let n = if args.Length >= 1 then int args.[0] else 5500
    let u = Array.create n 1.0
    tmp <- Array.zeroCreate n 
    let v = Array.zeroCreate n
    let chunk = n / nthread

    [for i in 0 .. nthread - 1 do
        let r1 = i * chunk
        let r2 = if (i < (nthread - 1)) then r1 + chunk else n
        yield async {
            for i = 0 to 10 do
                multiplyAtAv u v r1 r2
                multiplyAtAv v u r1 r2
        }]
    |> Async.Parallel
    |> Async.Ignore
    |> Async.RunSynchronously

    let result = Math.Sqrt(dot u v n / dot v v n)

    Console.WriteLine("{0:f9}", result);
    0
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
F# Compiler for F# 4.0 (Open Source Edition)
Mono JIT compiler version 4.5.1 (master/3e844dd Fri May  6 19:24:07 PDT 2016)
	LLVM:          yes(3.6.0svn-mono-master/9f79399)
	GC:            sgen



Wed, 21 Sep 2016 17:09:58 GMT

MAKE:
mv spectralnorm.fsharp-3.fsharp spectralnorm.fsharp-3.fs
/usr/local/bin/fsharpc --target:exe --platform:x86 -O  -o spectralnorm.fsharp-3.fsharp_run.exe spectralnorm.fsharp-3.fs
F# Compiler for F# 4.0 (Open Source Edition)
Freely distributed under the Apache 2.0 Open Source License

/home/dunham/benchmarksgame_onecore/spectralnorm/tmp/spectralnorm.fsharp-3.fs(7,1): warning FS0221: The declarations in this file will be placed in an implicit module 'Spectralnorm.fsharp-3' based on the file name 'spectralnorm.fsharp-3.fs'. However this is not a valid F# identifier, so the contents will not be accessible from other files. Consider renaming the file or adding a 'module' or 'namespace' declaration at the top of the file.
rm spectralnorm.fsharp-3.fs
4.60s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm --gc=sgen spectralnorm.fsharp-3.fsharp_run.exe 5500

PROGRAM OUTPUT:
1.274224153