|
| 1 | +module GraphBLAS.FSharp.Tests.Backend.Common.ClArray.chunkBySize |
| 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.ClContext |
| 9 | +open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions |
| 10 | + |
| 11 | +let context = Context.defaultContext.ClContext |
| 12 | + |
| 13 | +let processor = Context.defaultContext.Queue |
| 14 | + |
| 15 | +let config = { Utils.defaultConfig with arbitrary = [ typeof<Generators.ArrayAndChunkPositions> ] } |
| 16 | + |
| 17 | +let makeTestGetChunk<'a when 'a : equality> testFun (array: 'a [], startPosition: int, endPosition: int) = |
| 18 | + |
| 19 | + if array.Length > 0 then |
| 20 | + |
| 21 | + let clArray = context.CreateClArray array |
| 22 | + |
| 23 | + let (clActual: ClArray<'a>) = |
| 24 | + testFun processor HostInterop clArray startPosition endPosition |
| 25 | + |
| 26 | + clArray.Free processor |
| 27 | + let actual = clActual.ToHostAndFree processor |
| 28 | + |
| 29 | + "Results must be the same" |
| 30 | + |> Expect.sequenceEqual actual array.[startPosition .. endPosition - 1] |
| 31 | + |
| 32 | +let creatTestGetChunk<'a when 'a : equality> = |
| 33 | + ClArray.getChunk context Utils.defaultWorkGroupSize |
| 34 | + |> makeTestGetChunk<'a> |
| 35 | + |> testPropertyWithConfig config $"test on %A{typeof<'a>}" |
| 36 | + |
| 37 | +let getChunkTests = |
| 38 | + [ creatTestGetChunk<int> |
| 39 | + |
| 40 | + if Utils.isFloat64Available context.ClDevice then |
| 41 | + creatTestGetChunk<float> |
| 42 | + |
| 43 | + creatTestGetChunk<float32> |
| 44 | + creatTestGetChunk<bool> |
| 45 | + creatTestGetChunk<byte> ] |
| 46 | + |> testList "getChunk" |
| 47 | + |
| 48 | +let makeTestChunkBySize<'a when 'a : equality> isEqual testFun (array: 'a [], chunkSize: uint) = |
| 49 | + |
| 50 | + let chunkSize = int chunkSize |
| 51 | + |
| 52 | + if chunkSize > 0 && array.Length > 0 then |
| 53 | + |
| 54 | + let clArray = context.CreateClArray array |
| 55 | + |
| 56 | + let clActual: ClArray<'a>[] = |
| 57 | + (testFun processor HostInterop chunkSize clArray) |
| 58 | + |
| 59 | + clArray.Free processor |
| 60 | + |
| 61 | + let actual = |
| 62 | + clActual |
| 63 | + |> Array.map (fun clArray -> clArray.ToHostAndFree processor) |
| 64 | + |
| 65 | + let expected = |
| 66 | + Array.chunkBySize chunkSize array |
| 67 | + |
| 68 | + "Results must be the same" |
| 69 | + |> Utils.compareChunksArrays isEqual actual expected |
| 70 | + |
| 71 | +let creatTestChunkBySize<'a when 'a : equality> isEqual = |
| 72 | + ClArray.chunkBySize context Utils.defaultWorkGroupSize |
| 73 | + |> makeTestChunkBySize<'a> isEqual |
| 74 | + |> testPropertyWithConfig config $"test on %A{typeof<'a>}" |
| 75 | + |
| 76 | +let chunkBySizeTests = |
| 77 | + [ creatTestChunkBySize<int> (=) |
| 78 | + |
| 79 | + if Utils.isFloat64Available context.ClDevice then |
| 80 | + creatTestChunkBySize<float> Utils.floatIsEqual |
| 81 | + |
| 82 | + creatTestChunkBySize<float32> Utils.float32IsEqual |
| 83 | + creatTestChunkBySize<bool> (=) |
| 84 | + creatTestChunkBySize<byte> (=) ] |
| 85 | + |> testList "chanBySize" |
| 86 | + |
| 87 | +let creatTestChunkBySizeLazy<'a when 'a : equality> isEqual = |
| 88 | + (fun processor allocationMode chunkSize array -> |
| 89 | + ClArray.lazyChunkBySize context Utils.defaultWorkGroupSize processor allocationMode chunkSize array |
| 90 | + |> Seq.map (fun lazyValue -> lazyValue.Value) |
| 91 | + |> Seq.toArray) |
| 92 | + |> makeTestChunkBySize<'a> isEqual |
| 93 | + |> testPropertyWithConfig config $"test on %A{typeof<'a>}" |
| 94 | + |
| 95 | +let lazyChunkBySizeTests = |
| 96 | + [ creatTestChunkBySizeLazy<int> (=) |
| 97 | + |
| 98 | + if Utils.isFloat64Available context.ClDevice then |
| 99 | + creatTestChunkBySizeLazy<float> Utils.floatIsEqual |
| 100 | + |
| 101 | + creatTestChunkBySizeLazy<float32> Utils.float32IsEqual |
| 102 | + creatTestChunkBySizeLazy<bool> (=) |
| 103 | + creatTestChunkBySizeLazy<byte> (=) ] |
| 104 | + |> testList "chunkBySize lazy" |
| 105 | + |
| 106 | +let allTests = |
| 107 | + testList "chunk" [ getChunkTests; chunkBySizeTests; lazyChunkBySizeTests ] |
0 commit comments