The Computer Language
24.11 Benchmarks Game

spectral-norm F# .NET #6 program

source code

// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// Based on spectral-norm Rust #6 program
// Contributed by Arseniy Zlobintsev

open System
open System.Runtime.Intrinsics
open System.Runtime.Intrinsics.X86
open System.Threading.Tasks

type F64x2 = Vector128<float>

let inline hadd (x: F64x2) y =
    if Sse41.IsSupported then Sse41.HorizontalAdd(x, y)
    else Vector128.Create(Vector128.Sum x, Vector128.Sum y)

let inline a i j =
    let i0, i1 = i
    let j0, j1 = j
    Vector128.Create(
        (i0 + j0) * (i0 + j0 + 1.0) / 2.0 + i0 + 1.0,
        (i1 + j1) * (i1 + j1 + 1.0) / 2.0 + i1 + 1.0
    )

let inline mult (v: F64x2 array) (out: F64x2 array) ([<InlineIfLambda>] f) =
    Parallel.For(0, v.Length, fun i ->
        let fi = float (2 * i)
        let i0, i1 = (fi, fi), (fi + 1.0, fi + 1.0)
        
        let mutable sum0, sum1 = F64x2.Zero, F64x2.Zero
        for j in 0..v.Length-1 do
            let x = v[j]
            let j = float (2 * j), float (2 * j + 1)
            sum0 <- sum0 + x / f i0 j
            sum1 <- sum1 + x / f i1 j
        
        out[i] <- hadd sum0 sum1
    ) |> ignore

let multAtAv v out tmp =
    mult v tmp a
    mult tmp out (fun i j -> a j i)

let dot (v: F64x2 array) (u: F64x2 array) =
    let mutable acc = F64x2.Zero
    for i in 0..v.Length-1 do
        acc <- acc + v[i] * u[i]
    Vector128.Sum acc

[<EntryPoint>]
let main argv =
    let n = (try int argv[0] with _ -> 500) / 2
    let u = Array.create n (Vector128.Create 1.0)
    let v = Array.zeroCreate n
    let tmp = Array.zeroCreate n

    for _ in 0..9 do
        multAtAv u v tmp
        multAtAv v u tmp
    
    let answer = sqrt (dot u v / dot v v)

    Console.WriteLine(answer.ToString "F9")
    0
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 9.0.100
Host Version: 9.0.0
Commit: 9d5a6a9aa4

<OutputType>Exe
<TargetFramework>net9.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false


 Thu, 12 Dec 2024 21:09:55 GMT

MAKE:
cp spectralnorm.fsharpcore-6.fsharpcore Program.fs
cp Include/fsharpcore/program.fsproj .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
/opt/src/dotnet-sdk-9.0.100/dotnet build -c Release --use-current-runtime  	
  Determining projects to restore...
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.fsproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
  Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.fsproj (in 6.11 sec).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.fsproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
  program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/bin/Release/net9.0/linux-x64/program.dll

Build succeeded.

/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.fsproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/spectralnorm/tmp/program.fsproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
    2 Warning(s)
    0 Error(s)

Time Elapsed 00:00:11.51

11.94s to complete and log all make actions

COMMAND LINE:
 ./bin/Release/net9.0/linux-x64/program 5500

PROGRAM OUTPUT:
1.274224153