The Computer Language
24.11 Benchmarks Game

mandelbrot F# .NET program

source code

// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// ported from C# version by Anthony Lloyd

#nowarn "9"

open System
open System.Numerics
open System.Runtime.CompilerServices
open System.Threading.Tasks
open Microsoft.FSharp.NativeInterop

[<EntryPoint>]
let main args =
    let inline padd p i = IntPtr.Add(p,8*i)
    let inline ptrGet p i = Unsafe.Read((padd p i).ToPointer())
    let inline ptrSet p i v = Unsafe.Write((padd p i).ToPointer(), v)
    
    let inline getByte (ciby:float) pcrbi =
        let rec calc i res =
            if i=8 then res
            else
                let vCrbx = ptrGet pcrbi i
                let vCiby = Vector ciby
                let mutable zr = vCrbx
                let mutable zi = vCiby
                let mutable j = 49
                let mutable b = 0
                while b<>3 && j>0 do
                    j <- j-1
                    let nZr = zr * zr - zi * zi + vCrbx
                    zi <- let zrzi = zr * zi in zrzi + zrzi + vCiby
                    zr <- nZr
                    let t = nZr * nZr + zi * zi
                    b <- b ||| if t.[0]>4.0 then 2 else 0
                           ||| if t.[1]>4.0 then 1 else 0
                calc (i+2) ((res <<< 2) + b)
        calc 0 0 ^^^ -1 |> byte
    
    let size = if args.Length=0 then 200 else int args.[0]
    let lineLength = size >>> 3
    let s = "P4\n"+string size+" "+string size+"\n"
    let data = Array.zeroCreate (size*lineLength+s.Length)
    Text.ASCIIEncoding.ASCII.GetBytes(s, 0, s.Length, data, 0) |> ignore
    let crb = Array.zeroCreate (size+2)
    use pdata = fixed &data.[s.Length]
    use pcrb = fixed &crb.[0]
    let pcrbi = NativePtr.toNativeInt pcrb
    let invN = Vector (2.0/float size)
    let onePtFive = Vector 1.5
    let step = Vector 2.0
    let rec loop i value =
        if i<size then
            ptrSet pcrbi i (value*invN-onePtFive)
            loop (i+2) (value+step)
    Vector<float> [|0.0;1.0;0.0;0.0;0.0;0.0;0.0;0.0|] |> loop 0
    Parallel.For(0, size, fun y ->
        let ciby = NativePtr.get pcrb y+0.5
        for x = 0 to lineLength-1 do
            x*8 |> padd pcrbi |> getByte ciby
            |> NativePtr.set pdata (y*lineLength+x)
    ) |> ignore
    Console.OpenStandardOutput().Write(data, 0, data.Length)
    exit 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


 Fri, 15 Nov 2024 02:10:42 GMT

MAKE:
cp mandelbrot.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/mandelbrot/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/mandelbrot/tmp/program.fsproj (in 6.39 sec).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/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/mandelbrot/tmp/bin/Release/net9.0/linux-x64/program.dll

Build succeeded.

/home/dunham/all-benchmarksgame/benchmarksgame_i53330/mandelbrot/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/mandelbrot/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:17.75

19.99s to complete and log all make actions

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

(BINARY) PROGRAM OUTPUT NOT SHOWN