The Computer Language
22.05 Benchmarks Game

spectral-norm F# .NET #4 program

source code

// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// Based on C# version by Isaac Gouy, The Anh Tran, Alan McGovern
// Contributed by Don Syme
// Small optimisations by Anthony Lloyd

#nowarn "9"

open Microsoft.FSharp.NativeInterop

let approximate n1 u tmp v rbegin rend (barrier:System.Threading.Barrier) =

    let inline multiplyAv v Av A =
        for i = rbegin to rend do
            let mutable sum = A i 0 * NativePtr.read v
            for j = 1 to n1 do
                sum <- sum + A i j * NativePtr.get<float> v j
            NativePtr.set Av i sum

    let inline multiplyatAv v tmp atAv =
        let inline A i j = 1.0 / float((i + j) * (i + j + 1) / 2 + i + 1)
        multiplyAv v tmp A
        barrier.SignalAndWait()
        let inline At i j = A j i
        multiplyAv tmp atAv At
        barrier.SignalAndWait()

    for __ = 0 to 9 do
        multiplyatAv u tmp v
        multiplyatAv v tmp u

    let vbegin = NativePtr.get v rbegin
    let mutable vv = vbegin * vbegin
    let mutable vBv = vbegin * NativePtr.get u rbegin
    for i = rbegin+1 to rend do
        let vi = NativePtr.get v i
        vv <- vv + vi * vi
        vBv <- vBv + vi * NativePtr.get u i
    vBv, vv

[<EntryPoint>]
let main args =
    let n = try int args.[0] with _ -> 2500
    let u = fixed &(Array.create n 1.0).[0]
    let tmp = NativePtr.stackalloc n
    let v = NativePtr.stackalloc n
    let nthread = System.Environment.ProcessorCount
    let barrier = new System.Threading.Barrier(nthread)
    let chunk = n / nthread
    let aps =
        [ for i = 0 to nthread-1 do
            let r1 = i * chunk
            let r2 = if (i < (nthread - 1)) then r1 + chunk - 1 else n-1
            yield async { return approximate (n-1) u tmp v r1 r2 barrier } ]
        |> Async.Parallel
        |> Async.RunSynchronously
    sqrt(Array.sumBy fst aps/Array.sumBy snd aps).ToString("F9")
    |> stdout.WriteLine
    exit 0
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 6.0.101
Host Version: 6.0.1;
Commit: 3a25a7f1cc
<ServerGarbageCollection>true


Tue, 10 May 2022 04:03:26 GMT

MAKE:
cp spectralnorm.fsharpcore-4.fsharpcore Program.fs
cp Include/fsharpcore/tmp.fsproj .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
/usr/bin/dotnet build -c Release --no-restore --no-self-contained -r ubuntu-x64 
Microsoft (R) Build Engine version 17.1.1+a02f73656 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/bin/Release/net6.0/ubuntu-x64/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:07.41

9.04s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/net6.0/ubuntu-x64/tmp.dll 5500

PROGRAM OUTPUT:
1.274224153