|
| 1 | +module Backend.Vector.ElementWise |
| 2 | + |
| 3 | +open Expecto |
| 4 | +open Expecto.Logging |
| 5 | +open GraphBLAS.FSharp.Backend |
| 6 | +open GraphBLAS.FSharp.Tests.Utils |
| 7 | +open GraphBLAS.FSharp.Backend.Common |
| 8 | +open StandardOperations |
| 9 | + |
| 10 | +let logger = |
| 11 | + Log.create "Vector.ElementWise.Tests" |
| 12 | + |
| 13 | +let context = defaultContext.ClContext |
| 14 | + |
| 15 | +let q = defaultContext.Queue |
| 16 | + |
| 17 | +let config = defaultConfig |
| 18 | + |
| 19 | +let NNZCountCount array isZero = |
| 20 | + Array.filter (fun item -> not <| isZero item) array |
| 21 | + |> Array.length |
| 22 | + |
| 23 | +let checkResult |
| 24 | + isEqual |
| 25 | + resultZero |
| 26 | + (op: 'a -> 'b -> 'c) |
| 27 | + (actual: Vector<'c>) |
| 28 | + (leftArray: 'a []) |
| 29 | + (rightArray: 'b []) |
| 30 | + = |
| 31 | + |
| 32 | + let expectedArrayLength = leftArray.Length |
| 33 | + |
| 34 | + let expectedArray = |
| 35 | + Array.create expectedArrayLength resultZero |
| 36 | + |
| 37 | + for i in 0 .. expectedArrayLength - 1 do |
| 38 | + expectedArray.[i] <- op leftArray.[i] rightArray.[i] |
| 39 | + |
| 40 | + let (VectorDense expected) = createVectorFromArray Dense expectedArray (isEqual resultZero) |
| 41 | + |
| 42 | + match actual with |
| 43 | + | VectorDense actual -> |
| 44 | + "arrays must have the same values" |
| 45 | + |> Expect.equal actual expected |
| 46 | + | _ -> failwith "Vector format must be Sparse." |
| 47 | + |
| 48 | +let correctnessGenericTest |
| 49 | + leftIsEqual |
| 50 | + rightIsEqual |
| 51 | + resultIsEqual |
| 52 | + leftZero |
| 53 | + rightZero |
| 54 | + resultZero |
| 55 | + op |
| 56 | + (addFun: MailboxProcessor<_> -> ClVector<'a> -> ClVector<'b> -> ClVector<'c>) |
| 57 | + (leftArray: 'a [], rightArray: 'b []) |
| 58 | + = |
| 59 | + |
| 60 | + let leftNNZCount = |
| 61 | + NNZCountCount leftArray (leftIsEqual leftZero) |
| 62 | + |
| 63 | + let rightNNZCount = |
| 64 | + NNZCountCount rightArray (rightIsEqual rightZero) |
| 65 | + |
| 66 | + if leftNNZCount > 0 && rightNNZCount > 0 then |
| 67 | + |
| 68 | + let firstVector = |
| 69 | + createVectorFromArray Dense leftArray (leftIsEqual leftZero) |
| 70 | + |
| 71 | + let secondVector = |
| 72 | + createVectorFromArray Dense rightArray (rightIsEqual rightZero) |
| 73 | + |
| 74 | + let v1 = firstVector.ToDevice context |
| 75 | + let v2 = secondVector.ToDevice context |
| 76 | + |
| 77 | + try |
| 78 | + let res = addFun q v1 v2 |
| 79 | + |
| 80 | + v1.Dispose q |
| 81 | + v2.Dispose q |
| 82 | + |
| 83 | + let actual = res.ToHost q |
| 84 | + |
| 85 | + res.Dispose q |
| 86 | + |
| 87 | + checkResult resultIsEqual resultZero op actual leftArray rightArray |
| 88 | + with |
| 89 | + | ex when ex.Message = "InvalidBufferSize" -> () |
| 90 | + | ex -> raise ex |
| 91 | + |
| 92 | +let addTestFixtures = |
| 93 | + let getCorrectnessTestName fstType sndType thrType = |
| 94 | + $"Correctness on AtLeastOne<{fstType}, {sndType}> -> {thrType} option, Dense" |
| 95 | + |
| 96 | + let wgSize = 32 |
| 97 | + |
| 98 | + [ let intAddFun = |
| 99 | + Vector.elementWiseAtLeastOne context intSumAtLeastOne wgSize |
| 100 | + |
| 101 | + correctnessGenericTest (=) (=) (=) 0 0 0 (+) intAddFun |
| 102 | + |> testPropertyWithConfig config (getCorrectnessTestName "int" "int" "int") |
| 103 | + |
| 104 | + let floatAddFun = |
| 105 | + Vector.elementWiseAtLeastOne context floatSumAtLeastOne wgSize |
| 106 | + |
| 107 | + let fIsEqual = |
| 108 | + fun x y -> abs (x - y) < Accuracy.medium.absolute || x = y |
| 109 | + |
| 110 | + correctnessGenericTest fIsEqual fIsEqual fIsEqual 0.0 0.0 0.0 (+) floatAddFun |
| 111 | + |> testPropertyWithConfig config (getCorrectnessTestName "float" "float" "float") |
| 112 | + |
| 113 | + let boolAddFun = |
| 114 | + Vector.elementWiseAtLeastOne context boolSumAtLeastOne wgSize |
| 115 | + |
| 116 | + correctnessGenericTest (=) (=) (=) false false false (||) boolAddFun |
| 117 | + |> testPropertyWithConfig config (getCorrectnessTestName "bool" "bool" "bool") |
| 118 | + |
| 119 | + let byteAddFun = |
| 120 | + Vector.elementWiseAtLeastOne context byteSumAtLeastOne wgSize |
| 121 | + |
| 122 | + correctnessGenericTest (=) (=) (=) 0uy 0uy 0uy (+) byteAddFun |
| 123 | + |> testPropertyWithConfig config (getCorrectnessTestName "byte" "byte" "byte") ] |
| 124 | + |
| 125 | +let addTests = testList "Backend.Vector.ElementWiseAdd tests" addTestFixtures |
| 126 | + |
| 127 | +let mulTestFixtures = |
| 128 | + let getCorrectnessTestName fstType sndType thrType = |
| 129 | + $"Correctness on AtLeastOne<{fstType}, {sndType}> -> {thrType} option, Dense" |
| 130 | + |
| 131 | + let wgSize = 32 |
| 132 | + |
| 133 | + [ let intMulFun = |
| 134 | + Vector.elementWiseAtLeastOne context intMulAtLeastOne wgSize |
| 135 | + |
| 136 | + correctnessGenericTest (=) (=) (=) 0 0 0 (*) intMulFun |
| 137 | + |> testPropertyWithConfig config (getCorrectnessTestName "int" "int" "int") |
| 138 | + |
| 139 | + let floatMulFun = |
| 140 | + Vector.elementWiseAtLeastOne context floatMulAtLeastOne wgSize |
| 141 | + |
| 142 | + let fIsEqual = |
| 143 | + fun x y -> abs (x - y) < Accuracy.medium.absolute || x = y |
| 144 | + |
| 145 | + correctnessGenericTest fIsEqual fIsEqual fIsEqual 0.0 0.0 0.0 (*) floatMulFun |
| 146 | + |> testPropertyWithConfig config (getCorrectnessTestName "float" "float" "float") |
| 147 | + |
| 148 | + let boolMulFun = |
| 149 | + Vector.elementWiseAtLeastOne context boolMulAtLeastOne wgSize |
| 150 | + |
| 151 | + correctnessGenericTest (=) (=) (=) false false false (&&) boolMulFun |
| 152 | + |> testPropertyWithConfig config (getCorrectnessTestName "bool" "bool" "bool") |
| 153 | + |
| 154 | + let byteMulFun = |
| 155 | + Vector.elementWiseAtLeastOne context byteMulAtLeastOne wgSize |
| 156 | + |
| 157 | + correctnessGenericTest (=) (=) (=) 0uy 0uy 0uy (*) byteMulFun |
| 158 | + |> testPropertyWithConfig config (getCorrectnessTestName "byte" "byte" "byte") ] |
| 159 | + |
| 160 | +let mulTests = testList "Backend.Vector.ElementWiseMul tests" mulTestFixtures |
0 commit comments