Skip to content

Add SIMD [F#] #253

Description

@bloup1

Language

F#

Current Implementation

open System.IO

[<EntryPoint>]
let main _ =
    let rounds = File.ReadAllText("rounds.txt").Trim() |> int

    let mutable pi = 1.0
    let mutable x = 1.0

    for i in 2..rounds + 1 do
        x <- -x
        pi <- pi + x / float (2 * i - 1)

    pi <- pi * 4.0
    printfn $"{pi}"
    0

Proposed Implementation

open System.IO
open System.Runtime.Intrinsics

[<EntryPoint>]
let main _ =
    let mutable rounds  = File.ReadAllText("rounds.txt").Trim() |> uint
    let unroll          = uint Vector512<float>.Count
    let mutable den     = Vector512<float>.Zero;
    let inc             = Vector512.Create(float unroll);
    let two             = Vector512.Create(2.0);
    let mone            = Vector512.Create(-1.0);
    let xvec            = Vector512.Create(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
    let mutable ivec    = Vector512.Create(2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0);
    let mutable pivec   = Vector512<float>.Zero;
    rounds <- rounds + 2u;
    let vend = rounds - ((rounds - 2u) % unroll);
    let mutable i = 2u
    while i < vend do
        den     <- (two * ivec) + mone;
        ivec    <- ivec + inc
        pivec   <- pivec + xvec / den
        i       <- i + unroll
    let mutable x   = 1.00
    let mutable pi  = 1.00 + Vector512.Sum(pivec)
    let mutable j   = vend
    while j < rounds do
        x   <- -x
        pi  <- pi + x / (2.0*(float j) - 1.0)
        j   <- j + 1u
    pi <- pi * 4.0
    printfn $"{pi}"

Explanation

Translation of the C# SIMD avx512 code.

Expected Speedup

Similar speed-up as C#-SIMD vs C# (~4x faster)

Benchmark Results


Optimization Rules

  • Still single-threaded (no concurrency/parallelism)
  • Still uses the Leibniz formula
  • Produces the same (or more accurate) result
  • Uses standard language features (no inline assembly unless it's a SIMD variant)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions