|
| 1 | +module GraphBLAS.FSharp.Tests.Backend.Vector.Map |
| 2 | + |
| 3 | +open Expecto |
| 4 | +open Expecto.Logging |
| 5 | +open Expecto.Logging.Message |
| 6 | +open Microsoft.FSharp.Collections |
| 7 | +open GraphBLAS.FSharp |
| 8 | +open GraphBLAS.FSharp.Backend |
| 9 | +open GraphBLAS.FSharp.Backend.Quotes |
| 10 | +open GraphBLAS.FSharp.Tests |
| 11 | +open GraphBLAS.FSharp.Tests.Backend |
| 12 | +open GraphBLAS.FSharp.Tests.TestCases |
| 13 | +open GraphBLAS.FSharp.Objects |
| 14 | +open GraphBLAS.FSharp.Objects.ClContextExtensions |
| 15 | +open GraphBLAS.FSharp.Objects.ClVectorExtensions |
| 16 | +open Mono.CompilerServices.SymbolWriter |
| 17 | + |
| 18 | +let logger = Log.create "Vector.Map.Tests" |
| 19 | + |
| 20 | +let config = Utils.defaultConfig |
| 21 | +let wgSize = Utils.defaultWorkGroupSize |
| 22 | + |
| 23 | +let getCorrectnessTestName case datatype = |
| 24 | + $"Correctness on %s{datatype}, %A{case}" |
| 25 | + |
| 26 | +let checkResult isEqual op zero (baseVector: 'a []) (actual: Vector<'b>) = |
| 27 | + |
| 28 | + let expectedArrayLength = baseVector.Length |
| 29 | + |
| 30 | + let expectedArray = |
| 31 | + Array.create expectedArrayLength zero |
| 32 | + |
| 33 | + for i in 0 .. expectedArrayLength - 1 do |
| 34 | + expectedArray.[i] <- op baseVector.[i] |
| 35 | + |
| 36 | + let expected = |
| 37 | + Utils.createVectorFromArray Dense expectedArray (isEqual zero) |
| 38 | + |> Utils.vectorToDenseVector |
| 39 | + |
| 40 | + match actual with |
| 41 | + | Vector.Dense actual -> |
| 42 | + "arrays must have the same values" |
| 43 | + |> Expect.equal actual expected |
| 44 | + | _ -> failwith "Vector format must be Sparse." |
| 45 | + |
| 46 | +let correctnessGenericTest |
| 47 | + zero |
| 48 | + op |
| 49 | + (addFun: MailboxProcessor<_> -> AllocationFlag -> ClVector<'a> -> ClVector<'a>) |
| 50 | + (toDense: MailboxProcessor<_> -> AllocationFlag -> ClVector<'a> -> ClVector<'a>) |
| 51 | + (isEqual: 'a -> 'a -> bool) |
| 52 | + (case: OperationCase<VectorFormat>) |
| 53 | + (array: 'a []) |
| 54 | + = |
| 55 | + |
| 56 | + let isZero = (isEqual zero) |
| 57 | + |
| 58 | + let vectorHost = |
| 59 | + Utils.createVectorFromArray case.Format array isZero |
| 60 | + |
| 61 | + if vectorHost.NNZ > 0 then |
| 62 | + |
| 63 | + let context = case.TestContext.ClContext |
| 64 | + let q = case.TestContext.Queue |
| 65 | + |
| 66 | + let vector = vectorHost.ToDevice context |
| 67 | + |
| 68 | + try |
| 69 | + let res = |
| 70 | + addFun q HostInterop vector |
| 71 | + |
| 72 | + vector.Dispose q |
| 73 | + |
| 74 | + let denseActual = toDense q HostInterop res |
| 75 | + |
| 76 | + let actual = denseActual.ToHost q |
| 77 | + |
| 78 | + res.Dispose q |
| 79 | + denseActual.Dispose q |
| 80 | + |
| 81 | + checkResult isEqual op zero array actual |
| 82 | + with |
| 83 | + | ex when ex.Message = "InvalidBufferSize" -> () |
| 84 | + | ex -> raise ex |
| 85 | + |
| 86 | +let createTestMap case (zero: 'a) (constant: 'a) binOp isEqual opQ = |
| 87 | + let getCorrectnessTestName = getCorrectnessTestName case |
| 88 | + |
| 89 | + let context = case.TestContext.ClContext |
| 90 | + let q = case.TestContext.Queue |
| 91 | + |
| 92 | + let unaryOp = binOp constant |
| 93 | + let unaryOpQ = opQ zero constant |
| 94 | + |
| 95 | + let map = |
| 96 | + Operations.Vector.map unaryOpQ context wgSize |
| 97 | + |
| 98 | + let toDense = Vector.toDense context wgSize |
| 99 | + |
| 100 | + case |
| 101 | + |> correctnessGenericTest zero unaryOp map toDense isEqual |
| 102 | + |> testPropertyWithConfig config (getCorrectnessTestName $"{typeof<'a>}") |
| 103 | + |
| 104 | +let testFixturesMapNot case = |
| 105 | + [ let q = case.TestContext.Queue |
| 106 | + q.Error.Add(fun e -> failwithf "%A" e) |
| 107 | + |
| 108 | + createTestMap case false true (fun _ -> not) (=) (fun _ _ -> ArithmeticOperations.notOption) ] |
| 109 | + |
| 110 | +let notTests = |
| 111 | + operationGPUTests "not" testFixturesMapNot |
| 112 | + |
| 113 | +let testFixturesMapAdd case = |
| 114 | + [ let context = case.TestContext.ClContext |
| 115 | + let q = case.TestContext.Queue |
| 116 | + q.Error.Add(fun e -> failwithf "%A" e) |
| 117 | + |
| 118 | + createTestMap case 0 10 (+) (=) ArithmeticOperations.addLeftConst |
| 119 | + |
| 120 | + if Utils.isFloat64Available context.ClDevice then |
| 121 | + createTestMap case 0.0 10.0 (+) Utils.floatIsEqual ArithmeticOperations.addLeftConst |
| 122 | + |
| 123 | + createTestMap case 0.0f 10.0f (+) Utils.float32IsEqual ArithmeticOperations.addLeftConst |
| 124 | + |
| 125 | + createTestMap case 0uy 10uy (+) (=) ArithmeticOperations.addLeftConst ] |
| 126 | + |
| 127 | +let addTests = |
| 128 | + operationGPUTests "add" testFixturesMapAdd |
| 129 | + |
| 130 | +let testFixturesMapMul case = |
| 131 | + [ let context = case.TestContext.ClContext |
| 132 | + let q = case.TestContext.Queue |
| 133 | + q.Error.Add(fun e -> failwithf "%A" e) |
| 134 | + |
| 135 | + createTestMap case 0 10 (*) (=) ArithmeticOperations.mulLeftConst |
| 136 | + |
| 137 | + if Utils.isFloat64Available context.ClDevice then |
| 138 | + createTestMap case 0.0 10.0 (*) Utils.floatIsEqual ArithmeticOperations.mulLeftConst |
| 139 | + |
| 140 | + createTestMap case 0.0f 10.0f (*) Utils.float32IsEqual ArithmeticOperations.mulLeftConst |
| 141 | + |
| 142 | + createTestMap case 0uy 10uy (*) (=) ArithmeticOperations.mulLeftConst ] |
| 143 | + |
| 144 | +let mulTests = |
| 145 | + operationGPUTests "mul" testFixturesMapMul |
| 146 | + |
| 147 | +let allTests = |
| 148 | + testList "Map" [ addTests; mulTests; notTests ] |
0 commit comments