Skip to content

Commit 98d41dd

Browse files
committed
add: ClArray.assign tests
1 parent 7d1ee0e commit 98d41dd

5 files changed

Lines changed: 79 additions & 20 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -562,34 +562,34 @@ module ClArray =
562562
let assign<'a> (clContext: ClContext) workGroupSize =
563563

564564
let assign =
565-
<@ fun (ndRange: Range1D) startPosition appendedArrayLength (inputArray: ClArray<'a>) (result: ClArray<'a>) ->
565+
<@ fun (ndRange: Range1D) targetPosition sourceArrayLength (sourceArray: ClArray<'a>) (targetArray: ClArray<'a>) ->
566566

567567
let gid = ndRange.GlobalID0
568568

569-
let resultPosition = gid + startPosition
569+
let resultPosition = gid + targetPosition
570570

571-
if gid < appendedArrayLength then
571+
if gid < sourceArrayLength then
572572

573-
result.[resultPosition] <- inputArray.[gid] @>
573+
targetArray.[resultPosition] <- sourceArray.[gid] @>
574574

575575
let kernel = clContext.Compile assign
576576

577-
fun (processor: MailboxProcessor<_>) (targetArray: ClArray<'a>) startPosition (appendedArray: ClArray<'a>) ->
578-
if startPosition < 0 then
577+
fun (processor: MailboxProcessor<_>) (sourceArray: ClArray<'a>) targetPosition (targetArray: ClArray<'a>) ->
578+
if targetPosition < 0 then
579579
failwith "The starting position cannot be less than zero"
580580

581-
if startPosition + appendedArray.Length > targetArray.Length then
581+
if targetPosition + sourceArray.Length > targetArray.Length then
582582
failwith "The array should fit completely"
583583

584584
let ndRange =
585-
Range1D.CreateValid(appendedArray.Length, workGroupSize)
585+
Range1D.CreateValid(targetArray.Length, workGroupSize)
586586

587587
let kernel = kernel.GetKernel()
588588

589589
processor.Post(
590590
Msg.MsgSetArguments
591591
(fun () ->
592-
kernel.KernelFunc ndRange appendedArray.Length appendedArray.Length appendedArray targetArray)
592+
kernel.KernelFunc ndRange targetPosition sourceArray.Length sourceArray targetArray)
593593
)
594594

595595
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module GraphBLAS.FSharp.Tests.Backned.Common.ClArray.Assign
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.Assign
22

33
open Expecto
44
open Brahma.FSharp
@@ -13,21 +13,20 @@ let processor = Context.defaultContext.Queue
1313

1414
let config =
1515
{ Utils.defaultConfig with
16-
arbitrary = [ typeof<Generators.ArrayAndChunkPositions> ] }
16+
arbitrary = [ typeof<Generators.AssignArray> ] }
1717

18-
let makeTest<'a> isEqual testFun (source: 'a []) (target: 'a []) =
18+
let makeTest<'a> isEqual testFun (source: 'a [], target: 'a [], targetPosition: int) =
1919

2020
if source.Length > 0
2121
&& target.Length > 0 then
2222

2323
let clSource = context.CreateClArray source
2424
let clTarget = context.CreateClArray target
25-
let targetPosition = 0
2625

2726
testFun processor clSource targetPosition clTarget
2827

29-
let actual = clSource.ToHostAndFree processor
30-
clTarget.Free processor
28+
clSource.Free processor
29+
let actual = clTarget.ToHostAndFree processor
3130

3231
// write to target --- target expected
3332
Array.blit source 0 target targetPosition source.Length
@@ -37,15 +36,15 @@ let makeTest<'a> isEqual testFun (source: 'a []) (target: 'a []) =
3736

3837
let createTest<'a when 'a : equality> isEqual =
3938
ClArray.assign context Utils.defaultWorkGroupSize
40-
|> makeTest isEqual
39+
|> makeTest<'a> isEqual
4140
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
4241

4342
let tests =
4443
[ createTest<int> (=)
4544

4645
if Utils.isFloat64Available context.ClDevice then
47-
createTest<float> (=)
46+
createTest<float> Utils.floatIsEqual
4847

49-
createTest<float32> (=)
48+
createTest<float32> Utils.float32IsEqual
5049
createTest<bool> (=) ]
5150
|> testList "Assign"

tests/GraphBLAS-sharp.Tests/Common/ClArray/ChunkBySize.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.ClArray.chunkBySize
1+
module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.ChunkBySize
22

33
open Expecto
44
open Brahma.FSharp

tests/GraphBLAS-sharp.Tests/Generators.fs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,3 +956,63 @@ module Generators =
956956
static member BoolType() =
957957
arrayAndChunkPosition <| Arb.generate<bool>
958958
|> Arb.fromGen
959+
960+
type AssignArray() =
961+
static let pairOfVectorsOfEqualSize (valuesGenerator: Gen<'a>) =
962+
gen {
963+
let! targetArrayLength = Gen.sized <| fun size -> Gen.choose (2, size)
964+
965+
let! targetArray = Gen.arrayOfLength targetArrayLength valuesGenerator
966+
967+
let! sourceArrayLength = Gen.choose (1, targetArrayLength)
968+
969+
let! sourceArray = Gen.arrayOfLength sourceArrayLength valuesGenerator
970+
971+
let! startPosition = Gen.choose (0, targetArrayLength - sourceArrayLength)
972+
973+
return (sourceArray, targetArray, startPosition)
974+
}
975+
976+
static member IntType() =
977+
pairOfVectorsOfEqualSize <| Arb.generate<int>
978+
|> Arb.fromGen
979+
980+
static member FloatType() =
981+
pairOfVectorsOfEqualSize
982+
<| (Arb.Default.NormalFloat()
983+
|> Arb.toGen
984+
|> Gen.map float)
985+
|> Arb.fromGen
986+
987+
static member Float32Type() =
988+
pairOfVectorsOfEqualSize
989+
<| (normalFloat32Generator <| System.Random())
990+
|> Arb.fromGen
991+
992+
static member SByteType() =
993+
pairOfVectorsOfEqualSize <| Arb.generate<sbyte>
994+
|> Arb.fromGen
995+
996+
static member ByteType() =
997+
pairOfVectorsOfEqualSize <| Arb.generate<byte>
998+
|> Arb.fromGen
999+
1000+
static member Int16Type() =
1001+
pairOfVectorsOfEqualSize <| Arb.generate<int16>
1002+
|> Arb.fromGen
1003+
1004+
static member UInt16Type() =
1005+
pairOfVectorsOfEqualSize <| Arb.generate<uint16>
1006+
|> Arb.fromGen
1007+
1008+
static member Int32Type() =
1009+
pairOfVectorsOfEqualSize <| Arb.generate<int32>
1010+
|> Arb.fromGen
1011+
1012+
static member UInt32Type() =
1013+
pairOfVectorsOfEqualSize <| Arb.generate<uint32>
1014+
|> Arb.fromGen
1015+
1016+
static member BoolType() =
1017+
pairOfVectorsOfEqualSize <| Arb.generate<bool>
1018+
|> Arb.fromGen

tests/GraphBLAS-sharp.Tests/Program.fs

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

9696
[<EntryPoint>]
9797
let main argv =
98-
testList "lol" [ Common.Reduce.ByKey.testsSegmentsSequentialOption ]
98+
testList "lol" [ Common.ClArray.Assign.tests ]
9999
|> runTestsWithCLIArgs [] argv

0 commit comments

Comments
 (0)