Skip to content

Commit b012269

Browse files
committed
add: DenseVector.ContainsNonZero
1 parent 617c402 commit b012269

4 files changed

Lines changed: 128 additions & 1 deletion

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@ open Microsoft.FSharp.Quotations
77
open GraphBLAS.FSharp.Backend.Predefined
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+
1039
let elementWise<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
1140
(clContext: ClContext)
1241
(opAdd: Expr<'a option -> 'b option -> 'c option>)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Compile Include="Vector/Convert.fs" />
3838
<Compile Include="Vector/Reduce.fs" />
3939
<Compile Include="Vector/ElementWise.fs" />
40+
<Compile Include="Vector/ContainsNonZero.fs" />
4041
<Compile Include="Program.fs" />
4142
</ItemGroup>
4243
<Import Project="..\..\.paket\Paket.Restore.targets" />

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ let allTests =
4040
Backend.Vector.ElementWise.mulTests
4141
Backend.Vector.FillSubVector.tests
4242
Backend.Vector.FillSubVector.complementedTests
43-
Backend.Vector.Reduce.tests ]
43+
Backend.Vector.Reduce.tests
44+
Backend.Vector.ContainNonZero.tests ]
4445
|> testSequenced
4546

4647
[<EntryPoint>]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
module Backend.Vector.ContainNonZero
2+
3+
open Expecto
4+
open Expecto.Logging
5+
open GraphBLAS.FSharp.Backend
6+
open GraphBLAS.FSharp.Tests
7+
open GraphBLAS.FSharp.Tests.Utils
8+
open Context
9+
open Brahma.FSharp
10+
11+
let logger =
12+
Log.create "Vector.containsNonZero.Tests"
13+
14+
let context = defaultContext.ClContext
15+
16+
let q = defaultContext.Queue
17+
18+
let correctnessGenericTest<'a when 'a: struct and 'a: equality> isZero containsNonZero (array: 'a []) =
19+
20+
if array.Length > 0 then
21+
let vector = createVectorFromArray Dense array isZero
22+
23+
let result =
24+
match vector.ToDevice context with
25+
| ClVectorDense clArray ->
26+
let resultCell = containsNonZero q clArray
27+
let result = Array.zeroCreate 1
28+
29+
let res =
30+
q.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(resultCell, result, ch))
31+
32+
q.Post(Msg.CreateFreeMsg<_>(resultCell))
33+
34+
res.[0]
35+
36+
$"The results should be the same, vector : {vector}"
37+
|> Expect.equal result (Array.exists (not << isZero) array)
38+
39+
let testFixtures =
40+
let config = defaultConfig
41+
42+
let wgSize = 32
43+
44+
let getCorrectnessTestName datatype =
45+
sprintf "Correctness on %s, %A" datatype Dense
46+
47+
[ let containsNonZeroInt =
48+
DenseVector.DenseVector.containsNonZero context wgSize
49+
50+
correctnessGenericTest<int> ((=) 0) containsNonZeroInt
51+
|> testPropertyWithConfig config (getCorrectnessTestName "int")
52+
53+
let containsNonZeroByte =
54+
DenseVector.DenseVector.containsNonZero context wgSize
55+
56+
correctnessGenericTest<byte> ((=) 0uy) containsNonZeroByte
57+
|> testPropertyWithConfig config (getCorrectnessTestName "byte")
58+
59+
let containsNonZeroFloat =
60+
DenseVector.DenseVector.containsNonZero context wgSize
61+
62+
correctnessGenericTest<float> ((=) 0.0) containsNonZeroFloat
63+
|> testPropertyWithConfig config (getCorrectnessTestName "float")
64+
65+
let containsNonZeroBool =
66+
DenseVector.DenseVector.containsNonZero context wgSize
67+
68+
correctnessGenericTest<bool> ((=) false) containsNonZeroBool
69+
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
70+
71+
let containsNonZeroInt =
72+
DenseVector.DenseVector.containsNonZero context wgSize
73+
74+
correctnessGenericTest<int> ((=) 0) containsNonZeroInt (Array.create 1000 0)
75+
|> testPropertyWithConfig config (getCorrectnessTestName "int zeros")
76+
77+
let containsNonZeroByte =
78+
DenseVector.DenseVector.containsNonZero context wgSize
79+
80+
correctnessGenericTest<byte> ((=) 0uy) containsNonZeroByte (Array.create 1000 0uy)
81+
|> testPropertyWithConfig config (getCorrectnessTestName "byte zeros")
82+
83+
let containsNonZeroFloat =
84+
DenseVector.DenseVector.containsNonZero context wgSize
85+
86+
correctnessGenericTest<float> ((=) 0.0) containsNonZeroFloat (Array.create 1000 0.0)
87+
|> testPropertyWithConfig config (getCorrectnessTestName "float zeros")
88+
89+
let containsNonZeroBool =
90+
DenseVector.DenseVector.containsNonZero context wgSize
91+
92+
correctnessGenericTest<bool> ((=) false) containsNonZeroBool (Array.create 1000 false)
93+
|> testPropertyWithConfig config (getCorrectnessTestName "bool zeros") ]
94+
95+
let tests =
96+
testList "Backend.Vector.containsNonZero tests" testFixtures

0 commit comments

Comments
 (0)