Skip to content

SIMD vectorization of Array.sum<int>, etc #18509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Core/9.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Support for `and!` in `TaskBuilder` ([LanguageSuggestion #1363](https://github.com/fsharp/fslang-suggestions/issues/1363), [PR #18451](https://github.com/dotnet/fsharp/pull/18451))

### Changed
* Array.sum and Seq.sum to call System.Linq.Enumerable methods on base-types (float/float32/int/int64) to utilize vectorization. [PR #18509](https://github.com/dotnet/fsharp/pull/18509)

### Breaking Changes
* Struct unions with overlapping fields now generate mappings needed for reading via reflection ([Issue #18121](https://github.com/dotnet/fsharp/issues/17797), [PR #18274](https://github.com/dotnet/fsharp/pull/18274)). Previous versions of FSharp.Core returned incomplete mapping between fields and cases, these older fslib versions will now report an exception.
27 changes: 25 additions & 2 deletions src/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1578,8 +1578,7 @@ module Array =
checkNonNull "array" array
Microsoft.FSharp.Primitives.Basics.Array.permute indexMap array

[<CompiledName("Sum")>]
let inline sum (array: ^T array) : ^T =
let inline private classicSum (array: ^T array) : ^T =
checkNonNull "array" array
let mutable acc = LanguagePrimitives.GenericZero< ^T>

Expand All @@ -1588,6 +1587,30 @@ module Array =

acc

[<CompiledName("Sum")>]
let inline sum (array: ^T array) : ^T =
classicSum array
when ^T : float =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<float> -> float) (# "" array : IEnumerable<float> #)
(# "" r : 'T #)
when ^T : float32 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<float32> -> float32) (# "" array : IEnumerable<float32> #)
(# "" r : 'T #)
when ^T : int =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<int> -> int) (# "" array : IEnumerable<int> #)
(# "" r : 'T #)
when ^T : int64 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum array
else
let r = (System.Linq.Enumerable.Sum : IEnumerable<int64> -> int64) (# "" array : IEnumerable<int64> #)
(# "" r : 'T #)

[<CompiledName("SumBy")>]
let inline sumBy ([<InlineIfLambda>] projection: 'T -> ^U) (array: 'T array) : ^U =
checkNonNull "array" array
Expand Down
32 changes: 28 additions & 4 deletions src/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ module Internal =
static member Bind(g: Generator<'T>, cont) =
match g with
| :? GenerateThen<'T> as g ->
GenerateThen<_>.Bind(g.Generator, (fun () -> GenerateThen<_>.Bind(g.Cont(), cont)))
GenerateThen<_>
.Bind(g.Generator, (fun () -> GenerateThen<_>.Bind(g.Cont(), cont)))
| g -> (new GenerateThen<'T>(g, cont) :> Generator<'T>)

let bindG g cont =
Expand Down Expand Up @@ -1463,15 +1464,38 @@ module Seq =
else
mkDelayedSeq (fun () -> countByRefType projection source)

[<CompiledName("Sum")>]
let inline sum (source: seq< ^a >) : ^a =
let inline private classicSum (source: seq< ^a >) : ^a =
use e = source.GetEnumerator()
let mutable acc = LanguagePrimitives.GenericZero< ^a>

while e.MoveNext() do
acc <- Checked.(+) acc e.Current

acc
acc

[<CompiledName("Sum")>]
let inline sum (source: seq< ^a >) : ^a =
classicSum source
when ^a: int64 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<int64> -> int64) (# "" source : IEnumerable<int64> #)
(# "" r : 'a #)
when ^a: int =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<int> -> int) (# "" source : IEnumerable<int> #)
(# "" r : 'a #)
when ^a: float32 =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<float32> -> float32) (# "" source : IEnumerable<float32> #)
(# "" r : 'a #)
when ^a: float =
if System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.StartsWith ".NET Framework" then classicSum source
else
let r = (System.Linq.Enumerable.Sum: IEnumerable<float> -> float) (# "" source : IEnumerable<float> #)
(# "" r : 'a #)

[<CompiledName("SumBy")>]
let inline sumBy ([<InlineIfLambda>] projection: 'T -> ^U) (source: seq<'T>) : ^U =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ type CollectionsBenchmark() =
|> Array.updateAt (x.Length - 1) 1
|> ignore

[<Benchmark>]
member x.ArraySum() =
array
|> Array.sum
|> ignore
/// Seq
[<Benchmark>]
member x.SeqBaseline() =
Expand Down
Loading