Skip to content

Commit cf2dfe6

Browse files
committed
refactor: Vector.tests, SpMV types parameters
1 parent 4c1a4bc commit cf2dfe6

10 files changed

Lines changed: 42 additions & 45 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module SpMV =
9595
let multiplyValues = clContext.Compile multiplyValues
9696
let reduceValuesByRows = clContext.Compile reduceValuesByRows
9797

98-
fun (queue: MailboxProcessor<_>) (matrix: ClMatrix.CSR<'a>) (vector: ClArray<'b option>) (result: ClArray<'b option>) ->
98+
fun (queue: MailboxProcessor<_>) (matrix: ClMatrix.CSR<'a>) (vector: ClArray<'b option>) (result: ClArray<'c option>) ->
9999

100100
let matrixLength = matrix.Values.Length
101101

@@ -152,7 +152,7 @@ module SpMV =
152152
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) (vector: ClArray<'b option>) ->
153153

154154
let result =
155-
clContext.CreateClArrayWithSpecificAllocationMode<'b option>(allocationMode, matrix.RowCount)
155+
clContext.CreateClArrayWithSpecificAllocationMode<'c option>(allocationMode, matrix.RowCount)
156156

157157
runTo queue matrix vector result
158158

tests/GraphBLAS-sharp.Tests/Helpers.fs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module Generators =
7878
type SingleMatrix() =
7979
static let matrixGenerator (valuesGenerator: Gen<'a>) =
8080
gen {
81-
let! (nrows, ncols) = dimension2DGenerator
81+
let! nrows, ncols = dimension2DGenerator
8282
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
8383
return matrix
8484
}
@@ -125,7 +125,7 @@ module Generators =
125125
type SingleSymmetricalMatrix() =
126126
static let matrixGenerator (valuesGenerator: Gen<'a>) =
127127
gen {
128-
let! (nrows, _) = dimension2DGenerator
128+
let! nrows, _ = dimension2DGenerator
129129
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, nrows)
130130

131131
for row in 1 .. nrows - 1 do
@@ -177,7 +177,7 @@ module Generators =
177177
type PairOfSparseMatricesOfEqualSize() =
178178
static let pairOfMatricesOfEqualSizeGenerator (valuesGenerator: Gen<'a>) =
179179
gen {
180-
let! (nrows, ncols) = dimension2DGenerator
180+
let! nrows, ncols = dimension2DGenerator
181181
let! matrixA = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
182182
let! matrixB = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
183183
return (matrixA, matrixB)
@@ -225,7 +225,7 @@ module Generators =
225225
type PairOfSparseMatrixOAndVectorfCompatibleSize() =
226226
static let pairOfMatrixAndVectorOfCompatibleSizeGenerator (valuesGenerator: Gen<'a>) =
227227
gen {
228-
let! (nrows, ncols) = dimension2DGenerator
228+
let! nrows, ncols = dimension2DGenerator
229229
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
230230
let! vector = valuesGenerator |> Gen.arrayOfLength ncols
231231
let! mask = Arb.generate<bool> |> Gen.arrayOfLength nrows
@@ -281,7 +281,7 @@ module Generators =
281281
type PairOfSparseVectorAndMatrixOfCompatibleSize() =
282282
static let pairOfVectorAndMatrixOfCompatibleSizeGenerator (valuesGenerator: Gen<'a>) =
283283
gen {
284-
let! (nrows, ncols) = dimension2DGenerator
284+
let! nrows, ncols = dimension2DGenerator
285285
let! vector = valuesGenerator |> Gen.arrayOfLength nrows
286286
let! matrix = valuesGenerator |> Gen.array2DOfDim (nrows, ncols)
287287
let! mask = Arb.generate<bool> |> Gen.arrayOfLength ncols
@@ -330,7 +330,7 @@ module Generators =
330330
type PairOfMatricesOfCompatibleSize() =
331331
static let pairOfMatricesOfCompatibleSizeGenerator (valuesGenerator: Gen<'a>) =
332332
gen {
333-
let! (nrowsA, ncolsA, ncolsB) = dimension3DGenerator
333+
let! nrowsA, ncolsA, ncolsB = dimension3DGenerator
334334

335335
let! matrixA =
336336
valuesGenerator
@@ -385,7 +385,7 @@ module Generators =
385385
type PairOfMatricesOfCompatibleSizeWithMask() =
386386
static let pairOfMatricesOfCompatibleSizeWithMaskGenerator (valuesGenerator: Gen<'a>) =
387387
gen {
388-
let! (nrowsA, ncolsA, ncolsB) = dimension3DGenerator
388+
let! nrowsA, ncolsA, ncolsB = dimension3DGenerator
389389

390390
let! matrixA =
391391
valuesGenerator
@@ -702,6 +702,14 @@ module Utils =
702702
typeof<Generators.BufferCompatibleVector>
703703
typeof<Generators.PairOfVectorsOfEqualSize> ] }
704704

705+
let floatIsEqual x y =
706+
abs (x - y) < Accuracy.medium.absolute
707+
|| x.Equals y
708+
709+
let vectorToDenseVector = function
710+
| Vector.Dense vector -> vector
711+
| _ -> failwith "Vector format must be Dense."
712+
705713
let undirectedAlgoConfig =
706714
{ FsCheckConfig.defaultConfig with
707715
maxTest = 10
@@ -830,7 +838,7 @@ module Context =
830838
.GetDeviceInfo(device, DeviceInfo.Type, &e)
831839
.CastTo<DeviceType>()
832840

833-
let clDeviceType =
841+
let _ =
834842
match deviceType with
835843
| DeviceType.Cpu -> ClDeviceType.Cpu
836844
| DeviceType.Gpu -> ClDeviceType.Gpu

tests/GraphBLAS-sharp.Tests/Vector/AssignByMask.fs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ let checkResult isZero isComplemented (actual: Vector<'a>) (vector: 'a []) (mask
2525

2626
let expectedArray = Array.zeroCreate vector.Length
2727

28-
let (Vector.Dense vector) =
28+
let vector =
2929
createVectorFromArray Dense vector isZero
30+
|> vectorToDenseVector
3031

31-
let (Vector.Dense mask) = createVectorFromArray Dense mask isZero
32+
let mask =
33+
createVectorFromArray Dense mask isZero
34+
|> vectorToDenseVector
3235

3336
for i in 0 .. vector.Length - 1 do
3437
expectedArray.[i] <-
@@ -101,10 +104,6 @@ let testFixtures case =
101104
let wgSize = 32
102105
let context = case.TestContext.ClContext
103106

104-
let floatIsEqual x y =
105-
abs (x - y) < Accuracy.medium.absolute
106-
|| x.Equals(y)
107-
108107
let isComplemented = false
109108

110109
[ let intFill =
@@ -155,9 +154,6 @@ let testFixturesComplemented case =
155154
let wgSize = 32
156155
let context = case.TestContext.ClContext
157156

158-
let floatIsEqual x y =
159-
abs (x - y) < Accuracy.medium.absolute || x = y
160-
161157
let isComplemented = true
162158

163159
[ let intFill =

tests/GraphBLAS-sharp.Tests/Vector/Convert.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let makeTest
3030
let vector =
3131
createVectorFromArray formatFrom array isZero
3232

33-
if array.Length > 0 && vector.NNZ > 0 then
33+
if vector.NNZ > 0 then
3434

3535
let context = case.TestContext.ClContext
3636
let q = case.TestContext.Queue
@@ -47,7 +47,7 @@ let makeTest
4747
res
4848

4949
logger.debug (
50-
eventX "Actual is {actual}"
50+
eventX $"Actual is {actual}"
5151
>> setField "actual" (sprintf "%A" actual)
5252
)
5353

tests/GraphBLAS-sharp.Tests/Vector/Copy.fs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ let checkResult (isEqual: 'a -> 'a -> bool) (actual: Vector<'a>) (expected: Vect
3030
| Vector.Sparse actual, Vector.Sparse expected ->
3131
compareArrays isEqual actual.Values expected.Values "The values array must contain the default value"
3232
compareArrays (=) actual.Indices expected.Indices "The index array must contain the 0"
33-
| _, _ -> failwith "Copy format must be the same"
33+
| _ -> failwith "Copy format must be the same"
3434

3535
let correctnessGenericTest<'a when 'a: struct>
3636
isEqual
37-
(isZero: 'a -> bool)
37+
zero
3838
(copy: MailboxProcessor<Brahma.FSharp.Msg> -> AllocationFlag -> ClVector<'a> -> ClVector<'a>)
3939
(case: OperationCase<VectorFormat>)
4040
(array: 'a [])
4141
=
4242

4343
let expected =
44-
createVectorFromArray case.Format array isZero
44+
createVectorFromArray case.Format array (isEqual zero)
4545

4646
if array.Length > 0 && expected.NNZ > 0 then
4747

@@ -58,9 +58,6 @@ let correctnessGenericTest<'a when 'a: struct>
5858
checkResult isEqual actual expected
5959

6060
let testFixtures (case: OperationCase<VectorFormat>) =
61-
let filterFloats =
62-
Array.filter (System.Double.IsNaN >> not)
63-
6461
let config = defaultConfig
6562

6663
let getCorrectnessTestName datatype =
@@ -73,28 +70,26 @@ let testFixtures (case: OperationCase<VectorFormat>) =
7370
let isZero item = item = 0
7471

7572
case
76-
|> correctnessGenericTest<int> (=) isZero intCopy
73+
|> correctnessGenericTest<int> (=) 0 intCopy
7774
|> testPropertyWithConfig config (getCorrectnessTestName "int")
7875

7976
let floatCopy = Vector.copy context wgSize
80-
let isZero item = item = 0.0
8177

8278
case
83-
|> correctnessGenericTest<float> (=) isZero floatCopy
79+
|> correctnessGenericTest<float> floatIsEqual 0.0 floatCopy
8480
|> testPropertyWithConfig config (getCorrectnessTestName "float")
8581

8682
let boolCopy = Vector.copy context wgSize
87-
let isZero item = item = true
8883

8984
case
90-
|> correctnessGenericTest<bool> (=) isZero boolCopy
85+
|> correctnessGenericTest<bool> (=) false boolCopy
9186
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
9287

9388
let floatCopy = Vector.copy context wgSize
9489
let isZero item = item = 0uy
9590

9691
case
97-
|> correctnessGenericTest<byte> (=) isZero floatCopy
92+
|> correctnessGenericTest<byte> (=) 0uy floatCopy
9893
|> testPropertyWithConfig config (getCorrectnessTestName "byte") ]
9994

10095
let tests =

tests/GraphBLAS-sharp.Tests/Vector/Map2.fs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ let logger = Log.create "Vector.ElementWise.Tests"
1616

1717
let config = defaultConfig
1818

19-
let floatIsEqual =
20-
fun x y ->
21-
abs (x - y) < Accuracy.medium.absolute
22-
|| x.Equals y
23-
2419
let getCorrectnessTestName<'a> (case: OperationCase<'a>) fstType sndType thrType =
2520
$"Correctness on '{fstType} option -> '{sndType} option -> '{thrType} option, {case.Format}"
2621

@@ -34,8 +29,9 @@ let checkResult isEqual resultZero (op: 'a -> 'b -> 'c) (actual: Vector<'c>) (le
3429
for i in 0 .. expectedArrayLength - 1 do
3530
expectedArray.[i] <- op leftArray.[i] rightArray.[i]
3631

37-
let (Vector.Dense expected) =
32+
let expected =
3833
createVectorFromArray Dense expectedArray (isEqual resultZero)
34+
|> vectorToDenseVector
3935

4036
match actual with
4137
| Vector.Dense actual ->

tests/GraphBLAS-sharp.Tests/Vector/OfList.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let testFixtures (case: OperationCase<VectorFormat>) =
109109
let toCoo = Vector.toSparse context wgSize
110110

111111
case
112-
|> correctnessGenericTest<byte> (=) floatOfList toCoo
112+
|> correctnessGenericTest<float> floatIsEqual floatOfList toCoo
113113
|> testPropertyWithConfig config (getCorrectnessTestName "float") ]
114114

115115
let tests =

tests/GraphBLAS-sharp.Tests/Vector/Reduce.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ let testFixtures (case: OperationCase<VectorFormat>) =
8585
let floatMaxReduce = Vector.reduce context wgSize
8686

8787
case
88-
|> correctnessGenericTest (=) System.Double.MinValue max <@ max @> floatMaxReduce
88+
|> correctnessGenericTest floatIsEqual System.Double.MinValue max <@ max @> floatMaxReduce
8989
|> testPropertyWithConfig config (getCorrectnessTestName "float max")
9090

9191
let byteMaxReduce = Vector.reduce context wgSize
@@ -103,7 +103,7 @@ let testFixtures (case: OperationCase<VectorFormat>) =
103103
let floatMinReduce = Vector.reduce context wgSize
104104

105105
case
106-
|> correctnessGenericTest (=) System.Double.MaxValue min <@ min @> floatMinReduce
106+
|> correctnessGenericTest floatIsEqual System.Double.MaxValue min <@ min @> floatMinReduce
107107
|> testPropertyWithConfig config (getCorrectnessTestName "float min")
108108

109109
let byteMinReduce = Vector.reduce context wgSize

tests/GraphBLAS-sharp.Tests/Vector/SpMV.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open GraphBLAS.FSharp.Backend.Vector
1414
open GraphBLAS.FSharp.Objects
1515
open GraphBLAS.FSharp.Backend.Objects.ClContext
1616

17-
let checkResult isEqual sumOp mulOp zero (baseMtx: 'a [,]) (baseVtr: 'b []) (actual: 'c array) =
17+
let checkResult isEqual sumOp mulOp zero (baseMtx: 'a [,]) (baseVtr: 'a []) (actual: 'a option []) =
1818
let rows = Array2D.length1 baseMtx
1919
let columns = Array2D.length2 baseMtx
2020

@@ -48,11 +48,11 @@ let correctnessGenericTest
4848
zero
4949
sumOp
5050
mulOp
51-
(spMV: MailboxProcessor<_> -> AllocationFlag -> ClMatrix.CSR<'a> -> ClArray<'b option> -> ClArray<'c option>)
51+
(spMV: MailboxProcessor<_> -> AllocationFlag -> ClMatrix.CSR<'a> -> ClArray<'a option> -> ClArray<'a option>)
5252
(isEqual: 'a -> 'a -> bool)
5353
q
5454
(testContext: TestContext)
55-
(matrix: 'a [,], vector: 'a [], mask: bool [])
55+
(matrix: 'a [,], vector: 'a [], _: bool [])
5656
=
5757

5858
let mtx =

tests/GraphBLAS-sharp.Tests/Vector/ZeroCreate.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ let correctnessGenericTest<'a when 'a: struct and 'a: equality>
3333
(vectorSize: int)
3434
=
3535

36+
let vectorSize = abs vectorSize
37+
3638
if vectorSize > 0 then
3739
let q = case.TestContext.Queue
3840

0 commit comments

Comments
 (0)