Skip to content

Commit b38cc72

Browse files
committed
refactor: DenseVector.reduce
1 parent 253e52b commit b38cc72

13 files changed

Lines changed: 72 additions & 50 deletions

File tree

src/GraphBLAS-sharp.Backend/Common/ClArray.fs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,15 +368,22 @@ module ClArray =
368368
resultArray
369369

370370
let choose<'a, 'b> (clContext: ClContext) workGroupSize (predicate: Expr<'a -> 'b option>) =
371-
let getBitmap = map<'a, int> clContext workGroupSize <| Map.chooseBitmap predicate
371+
let getBitmap =
372+
map<'a, int> clContext workGroupSize
373+
<| Map.chooseBitmap predicate
372374

373-
let getOptionValues = map<'a, 'b option> clContext workGroupSize predicate
375+
let getOptionValues =
376+
map<'a, 'b option> clContext workGroupSize predicate
374377

375-
let getValues = map<'b option, 'b> clContext workGroupSize <| Map.optionToValueOrZero Unchecked.defaultof<'b>
378+
let getValues =
379+
map<'b option, 'b> clContext workGroupSize
380+
<| Map.optionToValueOrZero Unchecked.defaultof<'b>
376381

377-
let prefixSum = prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
382+
let prefixSum =
383+
prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
378384

379-
let scatter = Scatter.runInplace clContext workGroupSize
385+
let scatter =
386+
Scatter.runInplace clContext workGroupSize
380387

381388
fun (processor: MailboxProcessor<_>) allocationMode (array: ClArray<'a>) ->
382389

@@ -386,15 +393,15 @@ module ClArray =
386393
(prefixSum processor positions 0)
387394
.ToHostAndFree(processor)
388395

389-
let optionValues = getOptionValues processor DeviceOnly array
396+
let optionValues =
397+
getOptionValues processor DeviceOnly array
390398

391-
let values = getValues processor DeviceOnly optionValues
399+
let values =
400+
getValues processor DeviceOnly optionValues
392401

393402
let result =
394403
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
395404

396405
scatter processor positions values result
397406

398407
result
399-
400-

src/GraphBLAS-sharp.Backend/Quotes/Map.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ module Map =
2121
match (%map) item with
2222
| Some _ -> 1
2323
| None -> 0 @>
24-

src/GraphBLAS-sharp.Backend/Vector/DenseVector/DenseVector.fs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,29 @@ module DenseVector =
148148

149149
let reduce<'a when 'a: struct> (clContext: ClContext) workGroupSize (opAdd: Expr<'a -> 'a -> 'a>) =
150150

151-
let map =
152-
ClArray.map clContext workGroupSize
153-
<| Map.optionToValueOrZero Unchecked.defaultof<'a>
151+
let choose =
152+
ClArray.choose clContext workGroupSize Map.id
154153

155154
let reduce =
156155
Reduce.reduce clContext workGroupSize opAdd
157156

157+
let containsNonZero =
158+
ClArray.exists clContext workGroupSize Predicates.isSome
159+
158160
fun (processor: MailboxProcessor<_>) (vector: ClArray<'a option>) ->
159161

160-
try
161-
let values = map processor DeviceOnly vector
162+
let notEmpty =
163+
(containsNonZero processor vector)
164+
.ToHostAndFree processor
165+
166+
if notEmpty then
167+
let values = choose processor DeviceOnly vector
162168

163169
let result = reduce processor values
164170

165171
processor.Post(Msg.CreateFreeMsg<_>(values))
166172

167173
result
168-
with
169-
| ex when ex.Message = "InvalidBufferSize" -> clContext.CreateClCell Unchecked.defaultof<'a>
170-
| ex -> raise ex
174+
175+
else
176+
clContext.CreateClCell Unchecked.defaultof<'a>

tests/GraphBLAS-sharp.Tests/Common/Choose.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/Choose.fs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.Choose
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.Choose
22

33
open GraphBLAS.FSharp.Backend.Common
44
open Expecto
@@ -17,12 +17,15 @@ let makeTest<'a, 'b> testContext choose mapFun isEqual (array: 'a []) =
1717
let context = testContext.ClContext
1818
let q = testContext.Queue
1919

20-
let clArray = context.CreateClArrayWithSpecificAllocationMode(DeviceOnly, array)
20+
let clArray =
21+
context.CreateClArrayWithSpecificAllocationMode(DeviceOnly, array)
2122

2223
let (clResult: ClArray<'b>) = choose q HostInterop clArray
2324

2425
let hostResult = Array.zeroCreate clResult.Length
25-
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(clResult, hostResult, ch)) |> ignore
26+
27+
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(clResult, hostResult, ch))
28+
|> ignore
2629

2730
let expectedResult = Array.choose mapFun array
2831

@@ -32,7 +35,8 @@ let makeTest<'a, 'b> testContext choose mapFun isEqual (array: 'a []) =
3235
let createTest<'a, 'b> testContext mapFun mapFunQ isEqual =
3336
let context = testContext.ClContext
3437

35-
let choose = ClArray.choose context workGroupSize mapFunQ
38+
let choose =
39+
ClArray.choose context workGroupSize mapFunQ
3640

3741
makeTest<'a, 'b> testContext choose mapFun isEqual
3842
|> testPropertyWithConfig config $"Correctness on %A{typeof<'a>} -> %A{typeof<'b>}"
@@ -45,8 +49,9 @@ let testFixtures testContext =
4549
createTest<bool option, bool> testContext id Map.id (=)
4650

4751
if Utils.isFloat64Available device then
48-
createTest<float option, float> testContext id Map.id Utils.floatIsEqual
52+
createTest<float option, float> testContext id Map.id Utils.floatIsEqual
4953

5054
createTest<float32 option, float32> testContext id Map.id Utils.float32IsEqual ]
5155

52-
let tests = TestCases.gpuTests "ClArray.choose id tests" testFixtures
56+
let tests =
57+
TestCases.gpuTests "ClArray.choose id tests" testFixtures

tests/GraphBLAS-sharp.Tests/Common/Copy.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/Copy.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.Copy
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.Copy
22

33
open Expecto
44
open Expecto.Logging

tests/GraphBLAS-sharp.Tests/Common/Exists.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/Exists.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.Exists
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.Exists
22

33
open Expecto
44
open Expecto.Logging

tests/GraphBLAS-sharp.Tests/Common/Map.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/Map.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.Map
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.Map
22

33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Tests

tests/GraphBLAS-sharp.Tests/Common/Map2.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/Map2.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.Map2
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.Map2
22

33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Tests

tests/GraphBLAS-sharp.Tests/Common/PrefixSum.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/PrefixSum.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.PrefixSum
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.PrefixSum
22

33
open Expecto
44
open Expecto.Logging

tests/GraphBLAS-sharp.Tests/Common/RemoveDuplicates.fs renamed to tests/GraphBLAS-sharp.Tests/Common/ClArray/RemoveDuplicates.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Common.RemoveDuplicates
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.RemoveDuplicates
22

33
open Expecto
44
open Expecto.Logging

0 commit comments

Comments
 (0)