Skip to content

Commit 5551c57

Browse files
committed
add: ClArray.upperBound
1 parent 3d2cc7f commit 5551c57

7 files changed

Lines changed: 171 additions & 11 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,3 +694,33 @@ module ClArray =
694694
else
695695
None
696696

697+
let upperBound<'a when 'a : equality and 'a : comparison> (clContext: ClContext) workGroupSize =
698+
699+
let kernel =
700+
<@ fun (ndRange: Range1D) length (values: ClArray<'a>) (value: ClCell<'a>) (result: ClCell<int>) ->
701+
702+
let value = value.Value
703+
let gid = ndRange.GlobalID0
704+
705+
if gid = 0 then
706+
707+
result.Value <-
708+
(%Search.Bin.lowerBound 0) length value values @>
709+
710+
let program = clContext.Compile(kernel)
711+
712+
fun (processor: MailboxProcessor<_>) (values: ClArray<'a>) (value: ClCell<'a>) ->
713+
let result = clContext.CreateClCell 0
714+
715+
let kernel = program.GetKernel()
716+
717+
let ndRange =
718+
Range1D.CreateValid(1, workGroupSize)
719+
720+
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange values.Length values value result))
721+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
722+
723+
result
724+
725+
726+

src/GraphBLAS-sharp.Backend/Matrix/SpGeMM/Expand.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ module Expand =
7272
let segmentPointersByLeftMatrixRows =
7373
clContext.CreateClArrayWithSpecificAllocationMode(DeviceOnly, leftMatrix.RowPointers.Length)
7474

75+
gather processor segmentLengths leftMatrix.RowPointers segmentPointersByLeftMatrixRows
7576

77+
let segmentPointersByLeftMatrixRows = segmentPointersByLeftMatrixRows.ToHostAndFree processor
78+
79+
let beginRow = 0
80+
let totalWork = 0
81+
82+
while beginRow < leftMatrix.RowCount do
83+
84+
//let endRow =
7685

7786
()
7887

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,25 @@ module Search =
121121

122122
resultPosition @>
123123

124-
let lowerBound<'a when 'a : equality and 'a : comparison> startValue =
124+
let lowerBound<'a when 'a: comparison> startValue =
125125
<@ fun lenght sourceItem (keys: ClArray<'a>) ->
126126

127127
let mutable leftEdge = 0
128128
let mutable rightEdge = lenght - 1
129129

130130
let mutable resultPosition = startValue
131131

132-
while leftEdge <= rightEdge do
133-
let currentPosition = (leftEdge + rightEdge) / 2
134-
let currentKey = keys.[currentPosition]
132+
if sourceItem >= keys.[lenght - 1] then lenght - 1
133+
else
134+
while leftEdge <= rightEdge do
135+
let currentPosition = (leftEdge + rightEdge) / 2
136+
let currentKey = keys.[currentPosition]
135137

136-
if sourceItem < currentKey then
137-
resultPosition <- currentPosition
138+
if sourceItem < currentKey then
139+
resultPosition <- currentPosition
138140

139-
rightEdge <- currentPosition - 1
140-
else
141-
leftEdge <- currentPosition + 1
141+
rightEdge <- currentPosition - 1
142+
else
143+
leftEdge <- currentPosition + 1
142144

143-
resultPosition @>
145+
resultPosition @>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.UpperBound
2+
3+
open Expecto
4+
open Brahma.FSharp
5+
open GraphBLAS.FSharp.Backend.Common
6+
open GraphBLAS.FSharp.Test
7+
open GraphBLAS.FSharp.Tests
8+
open GraphBLAS.FSharp.Backend.Objects.ClCell
9+
10+
let context = Context.defaultContext.ClContext
11+
12+
let processor = Context.defaultContext.Queue
13+
14+
let config =
15+
{ Utils.defaultConfig with
16+
arbitrary = [ typeof<Generators.UpperBound> ] }
17+
18+
let makeTest testFun (array: 'a [], value: 'a) =
19+
20+
if array.Length > 0 then
21+
22+
let array = Array.sort array
23+
24+
let clArray = context.CreateClArray array
25+
let clValue = context.CreateClCell value
26+
27+
let actual =
28+
(testFun processor clArray clValue: ClCell<_>)
29+
.ToHostAndFree processor
30+
31+
let expected =
32+
let mutable expected = 0
33+
34+
let array = Array.rev array
35+
36+
for i in 0 .. array.Length - 1 do
37+
let currentValue = array.[i]
38+
39+
if value < currentValue then
40+
expected <- i
41+
42+
array.Length - expected - 1
43+
44+
"Results must be the same"
45+
|> Expect.equal actual expected
46+
47+
let createTest<'a when 'a : equality and 'a : comparison> =
48+
ClArray.upperBound<'a> context Utils.defaultWorkGroupSize
49+
|> makeTest
50+
|> testPropertyWithConfig { config with endSize = 10 } $"test on %A{typeof<'a>}"
51+
52+
let tests =
53+
[ createTest<int>
54+
55+
if Utils.isFloat64Available context.ClDevice then
56+
createTest<float>
57+
58+
createTest<float32>
59+
createTest<bool> ]
60+
|> testList "UpperBound"

tests/GraphBLAS-sharp.Tests/Generators.fs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,64 @@ module Generators =
11601160
pairOfVectorsOfEqualSize <| Arb.generate<bool>
11611161
|> Arb.fromGen
11621162

1163+
type UpperBound() =
1164+
static let arrayAndChunkPosition (valuesGenerator: Gen<'a>) =
1165+
gen {
1166+
let! size = Gen.sized <| fun size -> Gen.choose (1, size + 1)
1167+
1168+
let! array = Gen.arrayOfLength size valuesGenerator
1169+
1170+
let! valueIndex = Gen.choose (0, array.Length - 1)
1171+
1172+
let value = array.[valueIndex]
1173+
1174+
return (array, value)
1175+
}
1176+
1177+
static member IntType() =
1178+
arrayAndChunkPosition <| Arb.generate<int>
1179+
|> Arb.fromGen
1180+
1181+
static member FloatType() =
1182+
arrayAndChunkPosition
1183+
<| (Arb.Default.NormalFloat()
1184+
|> Arb.toGen
1185+
|> Gen.map float)
1186+
|> Arb.fromGen
1187+
1188+
static member Float32Type() =
1189+
arrayAndChunkPosition
1190+
<| (normalFloat32Generator <| System.Random())
1191+
|> Arb.fromGen
1192+
1193+
static member SByteType() =
1194+
arrayAndChunkPosition <| Arb.generate<sbyte>
1195+
|> Arb.fromGen
1196+
1197+
static member ByteType() =
1198+
arrayAndChunkPosition <| Arb.generate<byte>
1199+
|> Arb.fromGen
1200+
1201+
static member Int16Type() =
1202+
arrayAndChunkPosition <| Arb.generate<int16>
1203+
|> Arb.fromGen
1204+
1205+
static member UInt16Type() =
1206+
arrayAndChunkPosition <| Arb.generate<uint16>
1207+
|> Arb.fromGen
1208+
1209+
static member Int32Type() =
1210+
arrayAndChunkPosition <| Arb.generate<int32>
1211+
|> Arb.fromGen
1212+
1213+
static member UInt32Type() =
1214+
arrayAndChunkPosition <| Arb.generate<uint32>
1215+
|> Arb.fromGen
1216+
1217+
static member BoolType() =
1218+
arrayAndChunkPosition <| Arb.generate<bool>
1219+
|> Arb.fromGen
1220+
11631221
module Matrix =
11641222
type Sub() =
11651223
static let arrayAndChunkPosition (valuesGenerator: Gen<'a>) =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<Compile Include="Backend/Common/ClArray/Pairwise.fs" />
3030
<Compile Include="Backend/Common/ClArray/RemoveDuplicates.fs" />
3131
<Compile Include="Backend/Common/ClArray/Replicate.fs" />
32+
<Compile Include="Backend\Common\ClArray\UpperBound.fs" />
3233
<Compile Include="Backend/Common/Gather.fs" />
3334
<Compile Include="Backend/Common/Reduce/Reduce.fs" />
3435
<Compile Include="Backend/Common/Reduce/ReduceByKey.fs" />

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ open GraphBLAS.FSharp.Tests
44

55

66
[<EntryPoint>]
7-
let main argv = Backend.Matrix.SubRows.tests |> testSequenced |> runTestsWithCLIArgs [] argv
7+
let main argv = Common.ClArray.UpperBound.tests |> testSequenced |> runTestsWithCLIArgs [] argv

0 commit comments

Comments
 (0)