Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/command-queue'
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgarbar committed May 10, 2024
2 parents 54c0c00 + 108c45c commit b8c4244
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
15 changes: 9 additions & 6 deletions src/GraphBLAS-sharp.Backend/Common/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ module Common =
/// <param name="clContext">ClContext.</param>
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
/// <param name="zero">Zero element for binary operation.</param>
let runIncludeInPlace plus = PrefixSum.runIncludeInPlace plus
let runIncludeInPlace plus =
PrefixSumInternal.runIncludeInPlace plus

/// <summary>
/// Exclude in-place prefix sum. Array is scanned starting from the end.
Expand All @@ -260,7 +261,7 @@ module Common =
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
/// <param name="zero">Zero element for binary operation.</param>
let runBackwardsExcludeInPlace plus =
PrefixSum.runBackwardsExcludeInPlace plus
PrefixSumInternal.runBackwardsExcludeInPlace plus

/// <summary>
/// Include in-place prefix sum. Array is scanned starting from the end.
Expand All @@ -270,7 +271,7 @@ module Common =
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
/// <param name="zero">Zero element for binary operation.</param>
let runBackwardsIncludeInPlace plus =
PrefixSum.runBackwardsIncludeInPlace plus
PrefixSumInternal.runBackwardsIncludeInPlace plus

/// <summary>
/// Exclude in-place prefix sum of integer array with addition operation and start value that is equal to 0.
Expand Down Expand Up @@ -304,7 +305,7 @@ module Common =
/// </example>
/// <param name="clContext">ClContext.</param>
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
let standardIncludeInPlace = PrefixSum.standardIncludeInPlace
let standardIncludeInPlace = PrefixSumInternal.standardIncludeInPlace

module ByKey =
/// <summary>
Expand All @@ -318,7 +319,8 @@ module Common =
/// > val result = [| 0; 0; 1; 2; 0; 1 |]
/// </code>
/// </example>
let sequentialExclude op = PrefixSum.ByKey.sequentialExclude op
let sequentialExclude op =
PrefixSumInternal.ByKey.sequentialExclude op

/// <summary>
/// Include scan by key.
Expand All @@ -331,7 +333,8 @@ module Common =
/// > val result = [| 1; 1; 2; 3; 1; 2 |]
/// </code>
/// </example>
let sequentialInclude op = PrefixSum.ByKey.sequentialInclude op
let sequentialInclude op =
PrefixSumInternal.ByKey.sequentialInclude op

module Reduce =
/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/GraphBLAS-sharp.Backend/Common/PrefixSum.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ open GraphBLAS.FSharp.Backend.Quotes
open GraphBLAS.FSharp.Objects.ArraysExtensions
open GraphBLAS.FSharp.Objects.ClCellExtensions

module PrefixSum =
module internal PrefixSumInternal =
let private update (opAdd: Expr<'a -> 'a -> 'a>) (clContext: ClContext) workGroupSize =

let update =
Expand Down Expand Up @@ -209,6 +209,8 @@ module PrefixSum =
/// </example>
/// <param name="clContext">ClContext.</param>
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
[<System.ObsoleteAttribute("This method is deprecated due to bad perfomance. Use method from Scan module instead.",
false)>]
let standardExcludeInPlace (clContext: ClContext) workGroupSize =

let scan =
Expand Down
2 changes: 1 addition & 1 deletion src/GraphBLAS-sharp.Backend/Common/Sort/Radix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ module internal Radix =
let count = count clContext workGroupSize mask

let prefixSum =
PrefixSum.standardExcludeInPlace clContext workGroupSize
ScanInternal.standardExcludeInPlace clContext workGroupSize

let scatterByKey =
scatterByKey clContext workGroupSize mask
Expand Down
55 changes: 34 additions & 21 deletions tests/GraphBLAS-sharp.Tests/Backend/Common/Scan/PrefixSum.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,51 @@ let logger = Log.create "ClArray.PrefixSum.Tests"

let context = defaultContext.ClContext

let config = Tests.Utils.defaultConfig
let config =
{ Tests.Utils.defaultConfig with
maxTest = 20
startSize = 1
endSize = 1000000 }

let wgSize = 128

let q = defaultContext.Queue

let makeTest plus zero isEqual scan (array: 'a []) =
let makeTest plus zero isEqual scanInclude scanExclude (array: 'a []) =
if array.Length > 0 then
// Exclude
let actual, actualSum =
let clArray = context.CreateClArray array
let (total: ClCell<_>) = scanExclude q clArray

let actual = clArray.ToHostAndFree q
let actualSum = total.ToHostAndFree q

actual, actualSum

let expected, expectedSum =
array
|> Array.mapFold
(fun s t ->
let a = plus s t
s, a)
zero

logger.debug (
eventX $"Array is %A{array}\n"
>> setField "array" (sprintf "%A" array)
)
"Arrays for exclude should be the same"
|> Tests.Utils.compareArrays isEqual actual expected

"Total sums for exclude should be equal"
|> Expect.equal actualSum expectedSum

// Include
let actual, actualSum =
let clArray = context.CreateClArray array
let (total: ClCell<_>) = scan q clArray
let (total: ClCell<_>) = scanInclude q clArray zero

let actual = clArray.ToHostAndFree q
let actualSum = total.ToHostAndFree q
actual, actualSum

logger.debug (
eventX "Actual is {actual}\n"
>> setField "actual" (sprintf "%A" actual)
)

let expected, expectedSum =
array
|> Array.mapFold
Expand All @@ -48,20 +66,15 @@ let makeTest plus zero isEqual scan (array: 'a []) =
s, a)
zero

logger.debug (
eventX "Expected is {expected}\n"
>> setField "expected" (sprintf "%A" expected)
)

"Total sums should be equal"
"Total sums for include should be equal"
|> Expect.equal actualSum expectedSum

"Arrays should be the same"
"Arrays for include should be the same"
|> Tests.Utils.compareArrays isEqual actual expected

let testFixtures plus plusQ zero isEqual name =
Common.PrefixSum.runExcludeInPlace plusQ zero context wgSize
|> makeTest plus zero isEqual
(PrefixSum.runIncludeInPlace plusQ context wgSize, PrefixSum.runExcludeInPlace plusQ zero context wgSize)
||> makeTest plus zero isEqual
|> testPropertyWithConfig config $"Correctness on %s{name}"

let tests =
Expand Down

0 comments on commit b8c4244

Please sign in to comment.