Skip to content

Commit 04a09cc

Browse files
committed
add: Vector.elementWise
1 parent 4281f95 commit 04a09cc

15 files changed

Lines changed: 209 additions & 186 deletions

File tree

src/GraphBLAS-sharp.Backend/Vector/Vector.fs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module Vector =
9292
| ClVectorSparse vector ->
9393
ClVectorDense <| toDense processor vector
9494

95-
let elementWiseAddAtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
95+
let elementWiseAtLeastOne (clContext: ClContext) (opAdd: Expr<AtLeastOne<'a, 'b> -> 'c option>) workGroupSize =
9696
let addCoo =
9797
SparseVector.elementWiseAtLeastOne clContext opAdd workGroupSize
9898

@@ -105,6 +105,14 @@ module Vector =
105105
| ClVectorDense left, ClVectorDense right -> ClVectorDense <| addDense processor left right
106106
| _ -> failwith "Vector formats are not matching."
107107

108+
let elementWise (clContext: ClContext) (opAdd: Expr<'a option -> 'b option -> 'c option>) (workGroupSize: int) =
109+
let addDense = DenseVector.elementWise clContext opAdd workGroupSize
110+
111+
fun (processor: MailboxProcessor<_>) (leftVector: ClVector<'a>) (rightVector: ClVector<'b>) ->
112+
match leftVector, rightVector with
113+
| ClVectorDense leftVector, ClVectorDense rightVector -> addDense processor leftVector rightVector
114+
| _ -> failwith "Vector formats are not matching."
115+
108116
let fillSubVector (clContext: ClContext) (workGroupSize: int) =
109117
let cooFillVector =
110118
SparseVector.fillSubVector clContext workGroupSize

tests/GraphBLAS-sharp.Tests/BackendCommonTests/MatrixElementwiseTests.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ let testFixturesEWiseAdd case =
135135
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
136136

137137
let elementwiseAddTests =
138-
testsWithFixtures testFixturesEWiseAdd "Backend.Matrix.EWiseAdd tests"
138+
testsWithOperationCase testFixturesEWiseAdd "Backend.Matrix.EWiseAdd tests"
139139

140140
let testFixturesEWiseAddAtLeastOne case =
141141
[ let config = defaultConfig
@@ -185,7 +185,7 @@ let testFixturesEWiseAddAtLeastOne case =
185185
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
186186

187187
let elementwiseAddAtLeastOneTests =
188-
testsWithFixtures testFixturesEWiseAddAtLeastOne "Backend.Matrix.EWiseAddAtLeastOne tests"
188+
testsWithOperationCase testFixturesEWiseAddAtLeastOne "Backend.Matrix.EWiseAddAtLeastOne tests"
189189

190190
let testFixturesEWiseAddAtLeastOneToCOO case =
191191
[ let config = defaultConfig
@@ -235,7 +235,7 @@ let testFixturesEWiseAddAtLeastOneToCOO case =
235235
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
236236

237237
let elementwiseAddAtLeastOneToCOOTests =
238-
testsWithFixtures testFixturesEWiseAddAtLeastOneToCOO "Backend.Matrix.EWiseAddAtLeastOneToCOO tests"
238+
testsWithOperationCase testFixturesEWiseAddAtLeastOneToCOO "Backend.Matrix.EWiseAddAtLeastOneToCOO tests"
239239

240240
let testFixturesEWiseMulAtLeastOne case =
241241
[ let config = defaultConfig
@@ -285,4 +285,4 @@ let testFixturesEWiseMulAtLeastOne case =
285285
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
286286

287287
let elementwiseMulAtLeastOneTests =
288-
testsWithFixtures testFixturesEWiseMulAtLeastOne "Backend.Matrix.eWiseMulAtLeastOne tests"
288+
testsWithOperationCase testFixturesEWiseMulAtLeastOne "Backend.Matrix.eWiseMulAtLeastOne tests"

tests/GraphBLAS-sharp.Tests/BackendCommonTests/TransposeTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,4 @@ let testFixtures case =
173173
|> testPropertyWithConfig config (getCorrectnessTestName "bool (twice transpose)") ]
174174

175175
let tests =
176-
testsWithFixtures testFixtures "Transpose tests"
176+
testsWithOperationCase testFixtures "Transpose tests"

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
<!--Compile Include="MatrixOperationsTests/MxvTests.fs" /-->
2828
<!--Compile Include="MatrixOperationsTests/VxmTests.fs" /-->
2929
<!--Compile Include="AlgorithmsTests/BfsTests.fs" /-->
30-
<Compile Include="VectorOperations/ZeroCreate.fs" />
31-
<Compile Include="VectorOperations/OfList.fs" />
32-
<Compile Include="VectorOperations/Copy.fs" />
33-
<Compile Include="VectorOperations/FillSubVector.fs" />
34-
<Compile Include="VectorOperations/ElementWiseAtLeastOne.fs" />
35-
<Compile Include="VectorOperations/Convert.fs" />
36-
<Compile Include="VectorOperations/Complemented.fs" />
37-
<Compile Include="VectorOperations/Reduce.fs" />
30+
<Compile Include="Vector\ZeroCreate.fs" />
31+
<Compile Include="Vector\OfList.fs" />
32+
<Compile Include="Vector\Copy.fs" />
33+
<Compile Include="Vector\FillSubVector.fs" />
34+
<Compile Include="Vector\ElementWiseAtLeasOne.fs" />
35+
<Compile Include="Vector\Convert.fs" />
36+
<Compile Include="Vector\Reduce.fs" />
37+
<Compile Include="Vector\ElementWise.fs" />
3838
<Compile Include="Program.fs" />
3939
</ItemGroup>
4040
<Import Project="..\..\.paket\Paket.Restore.targets" />

tests/GraphBLAS-sharp.Tests/Helpers.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ module Utils =
740740
<| expected.[i]
741741
|> failtestf "%s"
742742

743-
let testsWithFixtures<'a when 'a: equality> testFixtures name =
743+
let testsWithOperationCase<'a when 'a: equality> testFixtures name =
744744
testCases<'a>
745745
|> List.filter
746746
(fun case ->

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ let allTests =
3232
// Backend.Vector.ZeroCreate.tests
3333
// Backend.Vector.OfList.tests
3434
// Backend.Vector.Copy.tests
35-
Backend.Vector.Convert.tests
36-
// Backend.Vector.ElementWiseAddAtLeastOne.addTests
37-
// Backend.Vector.ElementWiseAddAtLeastOne.mulTests
35+
// Backend.Vector.Convert.tests
36+
// Backend.Vector.ElementWiseAtLeastOne.addTests
37+
// Backend.Vector.ElementWiseAtLeastOne.mulTests
38+
Backend.Vector.ElementWise.addTests
39+
Backend.Vector.ElementWise.mulTests
3840
// Backend.Vector.FillSubVector.tests
39-
// Backend.Vector.Complemented.tests
4041
// Backend.Vector.Reduce.tests ]
4142
]
4243
|> testSequenced

tests/GraphBLAS-sharp.Tests/VectorOperations/Convert.fs renamed to tests/GraphBLAS-sharp.Tests/Vector/Convert.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ let testFixtures case =
8888
|> List.concat
8989

9090
let tests =
91-
testsWithFixtures<VectorFormat> testFixtures "Backend.Vector.Convert tests"
91+
testsWithOperationCase<VectorFormat> testFixtures "Backend.Vector.Convert tests"

tests/GraphBLAS-sharp.Tests/VectorOperations/Copy.fs renamed to tests/GraphBLAS-sharp.Tests/Vector/Copy.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ let testFixtures (case: OperationCase<VectorFormat>) =
9595
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
9696

9797
let tests =
98-
testsWithFixtures<VectorFormat> testFixtures "Backend.Vector.copy tests"
98+
testsWithOperationCase<VectorFormat> testFixtures "Backend.Vector.copy tests"
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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

Comments
 (0)