Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 29 additions & 0 deletions src/GraphBLAS-sharp.Backend/Vector/DenseVector/DenseVector.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ open Microsoft.FSharp.Quotations
open GraphBLAS.FSharp.Backend.Predefined

module DenseVector =
let containsNonZero<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) =

let containsNonZero =
<@ fun (ndRange: Range1D) length (vector: ClArray<'a option>) (result: ClCell<bool>) ->

let gid = ndRange.GlobalID0

if gid < length then
match vector.[gid] with
| Some _ -> result.Value <- true
| _ -> () @>

let kernel = clContext.Compile containsNonZero

fun (processor: MailboxProcessor<_>) (vector: ClArray<'a option>) ->

let result = clContext.CreateClCell false

let ndRange =
Range1D.CreateValid(vector.Length, workGroupSize)

let kernel = kernel.GetKernel()

processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange vector.Length vector result))

processor.Post(Msg.CreateRunMsg<_, _>(kernel))

result

let elementWise<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
(clContext: ClContext)
(opAdd: Expr<'a option -> 'b option -> 'c option>)
Expand Down
1 change: 1 addition & 0 deletions tests/GraphBLAS-sharp.Tests/GraphBLAS-sharp.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<Compile Include="Vector/Convert.fs" />
<Compile Include="Vector/Reduce.fs" />
<Compile Include="Vector/ElementWise.fs" />
<Compile Include="Vector/ContainsNonZero.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
Expand Down
3 changes: 2 additions & 1 deletion tests/GraphBLAS-sharp.Tests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ let allTests =
Backend.Vector.ElementWise.mulTests
Backend.Vector.FillSubVector.tests
Backend.Vector.FillSubVector.complementedTests
Backend.Vector.Reduce.tests ]
Backend.Vector.Reduce.tests
Backend.Vector.ContainNonZero.tests ]
|> testSequenced

[<EntryPoint>]
Expand Down
96 changes: 96 additions & 0 deletions tests/GraphBLAS-sharp.Tests/Vector/ContainsNonZero.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module Backend.Vector.ContainNonZero

open Expecto
open Expecto.Logging
open GraphBLAS.FSharp.Backend
open GraphBLAS.FSharp.Tests
open GraphBLAS.FSharp.Tests.Utils
open Context
open Brahma.FSharp

let logger =
Log.create "Vector.containsNonZero.Tests"

let context = defaultContext.ClContext

let q = defaultContext.Queue

let correctnessGenericTest<'a when 'a: struct and 'a: equality> isZero containsNonZero (array: 'a []) =

if array.Length > 0 then
let vector = createVectorFromArray Dense array isZero

let result =
match vector.ToDevice context with
| ClVectorDense clArray ->
let resultCell = containsNonZero q clArray
let result = Array.zeroCreate 1

let res =
q.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(resultCell, result, ch))

q.Post(Msg.CreateFreeMsg<_>(resultCell))

res.[0]

$"The results should be the same, vector : {vector}"
|> Expect.equal result (Array.exists (not << isZero) array)

let testFixtures =
let config = defaultConfig

let wgSize = 32

let getCorrectnessTestName datatype =
sprintf "Correctness on %s, %A" datatype Dense

[ let containsNonZeroInt =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroByte =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroFloat =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroBool =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroInt =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroByte =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroFloat =
DenseVector.DenseVector.containsNonZero context wgSize

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

let containsNonZeroBool =
DenseVector.DenseVector.containsNonZero context wgSize

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

let tests =
testList "Backend.Vector.containsNonZero tests" testFixtures