Skip to content

Commit 682c194

Browse files
committed
add: ClArray.exists
1 parent c4ebf2a commit 682c194

7 files changed

Lines changed: 52 additions & 44 deletions

File tree

src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module BFS =
3232
DenseVector.standardFillSubVectorTo<int, int> clContext workGroupSize
3333

3434
let containsNonZero =
35-
DenseVector.containsNonZero clContext workGroupSize
35+
ClArray.exists clContext workGroupSize <@ fun (item: int option) -> match item with Some _ -> true | _ -> false @>
3636

3737
fun (queue: MailboxProcessor<Msg>) (matrix: ClCSRMatrix<'a>) (source: int) ->
3838
let vertexCount = matrix.RowCount

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,33 @@ module ClArray =
292292
scatter processor positions inputArray outputArray
293293

294294
outputArray
295+
296+
let exists<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) (predicate: Expr<'a option -> bool>) =
297+
298+
let exists =
299+
<@ fun (ndRange: Range1D) length (vector: ClArray<'a option>) (result: ClCell<bool>) ->
300+
301+
let gid = ndRange.GlobalID0
302+
303+
if gid < length then
304+
let isExist = (%predicate) vector.[gid]
305+
306+
if isExist then
307+
result.Value <- true @>
308+
309+
let kernel = clContext.Compile exists
310+
311+
fun (processor: MailboxProcessor<_>) (vector: ClArray<'a option>) ->
312+
313+
let result = clContext.CreateClCell false
314+
315+
let ndRange =
316+
Range1D.CreateValid(vector.Length, workGroupSize)
317+
318+
let kernel = kernel.GetKernel()
319+
320+
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange vector.Length vector result))
321+
322+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
323+
324+
result

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
<ItemGroup>
1313
<Compile Include="AssemblyInfo.fs" />
14+
<Compile Include="Common\Quotes.fs" />
1415
<Compile Include="Common\Scatter.fs" />
15-
<Compile Include="Common/Quotes.fs" />
1616
<Compile Include="Common/Utils.fs" />
1717
<Compile Include="Common/Sum.fs" />
1818
<Compile Include="Common/PrefixSum.fs" />

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,6 @@ open GraphBLAS.FSharp.Backend.Predefined
77
open GraphBLAS.FSharp.Backend.Objects
88

99
module DenseVector =
10-
let containsNonZero<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) =
11-
12-
let containsNonZero =
13-
<@ fun (ndRange: Range1D) length (vector: ClArray<'a option>) (result: ClCell<bool>) ->
14-
15-
let gid = ndRange.GlobalID0
16-
17-
if gid < length then
18-
match vector.[gid] with
19-
| Some _ -> result.Value <- true
20-
| _ -> () @>
21-
22-
let kernel = clContext.Compile containsNonZero
23-
24-
fun (processor: MailboxProcessor<_>) (vector: ClArray<'a option>) ->
25-
26-
let result = clContext.CreateClCell false
27-
28-
let ndRange =
29-
Range1D.CreateValid(vector.Length, workGroupSize)
30-
31-
let kernel = kernel.GetKernel()
32-
33-
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange vector.Length vector result))
34-
35-
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
36-
37-
result
38-
3910
let elementWiseTo<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
4011
(clContext: ClContext)
4112
(opAdd: Expr<'a option -> 'b option -> 'c option>)

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
module GraphBLAS.FSharp.Tests.Backend.Vector.ContainNonZero
1+
module GraphBLAS.FSharp.Tests.Backend.Common.Exists
22

33
open Expecto
44
open Expecto.Logging
5+
open GraphBLAS.FSharp.Backend.Common
56
open GraphBLAS.FSharp.Tests
67
open GraphBLAS.FSharp.Tests.Utils
78
open Context
@@ -37,6 +38,12 @@ let correctnessGenericTest<'a when 'a: struct and 'a: equality> isZero containsN
3738
$"The results should be the same, vector : {vector}"
3839
|> Expect.equal result (Array.exists (not << isZero) array)
3940

41+
let predicate<'a when 'a : struct> =
42+
<@ fun (item: 'a option) ->
43+
match item with
44+
Some _ -> true
45+
| _ -> false @>
46+
4047
let testFixtures =
4148
let config = defaultConfig
4249

@@ -46,49 +53,49 @@ let testFixtures =
4653
sprintf "Correctness on %s, %A" datatype Dense
4754

4855
[ let containsNonZeroInt =
49-
DenseVector.containsNonZero context wgSize
56+
ClArray.exists context wgSize predicate
5057

5158
correctnessGenericTest<int> ((=) 0) containsNonZeroInt
5259
|> testPropertyWithConfig config (getCorrectnessTestName "int")
5360

5461
let containsNonZeroByte =
55-
DenseVector.containsNonZero context wgSize
62+
ClArray.exists context wgSize predicate
5663

5764
correctnessGenericTest<byte> ((=) 0uy) containsNonZeroByte
5865
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
5966

6067
let containsNonZeroFloat =
61-
DenseVector.containsNonZero context wgSize
68+
ClArray.exists context wgSize predicate
6269

6370
correctnessGenericTest<float> ((=) 0.0) containsNonZeroFloat
6471
|> testPropertyWithConfig config (getCorrectnessTestName "float")
6572

6673
let containsNonZeroBool =
67-
DenseVector.containsNonZero context wgSize
74+
ClArray.exists context wgSize predicate
6875

6976
correctnessGenericTest<bool> ((=) false) containsNonZeroBool
7077
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
7178

7279
let containsNonZeroInt =
73-
DenseVector.containsNonZero context wgSize
80+
ClArray.exists context wgSize predicate
7481

7582
correctnessGenericTest<int> ((=) 0) containsNonZeroInt (Array.create 1000 0)
7683
|> testPropertyWithConfig config (getCorrectnessTestName "int zeros")
7784

7885
let containsNonZeroByte =
79-
DenseVector.containsNonZero context wgSize
86+
ClArray.exists context wgSize predicate
8087

8188
correctnessGenericTest<byte> ((=) 0uy) containsNonZeroByte (Array.create 1000 0uy)
8289
|> testPropertyWithConfig config (getCorrectnessTestName "byte zeros")
8390

8491
let containsNonZeroFloat =
85-
DenseVector.containsNonZero context wgSize
92+
ClArray.exists context wgSize predicate
8693

8794
correctnessGenericTest<float> ((=) 0.0) containsNonZeroFloat (Array.create 1000 0.0)
8895
|> testPropertyWithConfig config (getCorrectnessTestName "float zeros")
8996

9097
let containsNonZeroBool =
91-
DenseVector.containsNonZero context wgSize
98+
ClArray.exists context wgSize predicate
9299

93100
correctnessGenericTest<bool> ((=) false) containsNonZeroBool (Array.create 1000 false)
94101
|> testPropertyWithConfig config (getCorrectnessTestName "bool zeros") ]

tests/GraphBLAS-sharp.Tests/GraphBLAS-sharp.Tests.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Compile Include="Common/Scatter.fs" />
2424
<Compile Include="Common/Sum.fs" />
2525
<Compile Include="Common/Reduce.fs" />
26+
<Compile Include="Common\Exists.fs" />
2627
<!--Compile Include="MatrixOperationsTests/GetTuplesTests.fs" /-->
2728
<!--Compile Include="MatrixOperationsTests/MxvTests.fs" /-->
2829
<!--Compile Include="MatrixOperationsTests/VxmTests.fs" /-->
@@ -35,7 +36,6 @@
3536
<Compile Include="Vector/Convert.fs" />
3637
<Compile Include="Vector/Reduce.fs" />
3738
<Compile Include="Vector/Elementwise.fs" />
38-
<Compile Include="Vector/ContainsNonZero.fs" />
3939
<Compile Include="Vector/SpMV.fs" />
4040
<Compile Include="Algorithms/BFS.fs" />
4141
<Compile Include="Matrix/Convert.fs" />

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ let commonTests =
2323
Common.Copy.tests
2424
Common.Replicate.tests
2525
Common.Reduce.tests
26-
Common.Sum.tests ]
26+
Common.Sum.tests
27+
Common.Exists.tests ]
2728
|> testSequenced
2829

2930
let vectorTests =
@@ -40,8 +41,7 @@ let vectorTests =
4041
Vector.Elementwise.mulTests
4142
Vector.FillSubVector.tests
4243
Vector.FillSubVector.complementedTests
43-
Vector.Reduce.tests
44-
Vector.ContainNonZero.tests ]
44+
Vector.Reduce.tests ]
4545
|> testSequenced
4646

4747
let algorithmsTests =

0 commit comments

Comments
 (0)