spectral-norm F# .NET Core #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 Core SDK 3.1.201
Host Version: 3.1.3; Commit: 4a9f85e9f8
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
Thu, 07 May 2020 20:12:09 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
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
tmp -> /home/dunham/benchmarksgame_quadcore/spectralnorm/tmp/bin/Release/netcoreapp3.1/tmp.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:11.36
14.08s to complete and log all make actions
COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp3.1/tmp.dll 5500
PROGRAM OUTPUT:
1.274224153