Skip to content

Commit ad9b528

Browse files
committed
add: CSR.byRows tests
1 parent f5c6fe6 commit ad9b528

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

src/GraphBLAS-sharp/Objects/Matrix.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Matrix =
2424
|> List.mapi (fun i x -> (x, i))
2525
|> List.filter (fun pair -> not <| isZero (fst pair)))
2626
|> List.fold
27-
(fun (rowPtrs, valueInx) row -> ((rowPtrs.Head + row.Length) :: rowPtrs), valueInx @ row)
27+
(fun (rowPointers, valueInx) row -> ((rowPointers.Head + row.Length) :: rowPointers), valueInx @ row)
2828
([ 0 ], [])
2929

3030
{ Values =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="Matrix/SpGeMM/Masked.fs" />
5454
<Compile Include="Matrix/SpGeMM/Expand.fs" />
5555
<Compile Include="Matrix/RowsLengths.fs" />
56+
<Compile Include="Matrix/ByRows.fs" />
5657
<Compile Include="Program.fs" />
5758
</ItemGroup>
5859
<Import Project="..\..\.paket\Paket.Restore.targets" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module GraphBLAS.FSharp.Tests.Matrix.ByRows
2+
3+
open Expecto
4+
open GraphBLAS.FSharp.Tests
5+
open GraphBLAS.FSharp.Backend
6+
open GraphBLAS.FSharp.Objects
7+
open GraphBLAS.FSharp.Backend.Matrix
8+
open GraphBLAS.FSharp.Backend.Objects
9+
open GraphBLAS.FSharp.Backend.Objects.ClContext
10+
open GraphBLAS.FSharp.Objects.ClVectorExtensions
11+
12+
let context = Context.defaultContext.ClContext
13+
14+
let processor = Context.defaultContext.Queue
15+
16+
let config = Utils.defaultConfig
17+
18+
let makeTest<'a when 'a : struct> isEqual zero testFun (array: 'a [,]) =
19+
20+
let matrix = Matrix.CSR.FromArray2D(array, isEqual zero)
21+
22+
if matrix.NNZ > 0 then
23+
24+
let clMatrix = matrix.ToDevice context
25+
26+
let rows = testFun processor HostInterop clMatrix
27+
28+
"Rows count must be the same"
29+
|> Expect.equal (Seq.length rows) (Array2D.length1 array)
30+
31+
rows
32+
|> Seq.iteri (fun index -> function
33+
| Some (actualRow: ClVector.Sparse<_>) ->
34+
let expectedRow = Vector.Sparse.FromArray(array.[index, *], (isEqual zero))
35+
let actualHost = actualRow.ToHost processor
36+
37+
Utils.compareSparseVectors isEqual actualHost expectedRow
38+
| None ->
39+
"Expected row must be None"
40+
|> Expect.isFalse (Array.exists ((<<) not <| isEqual zero) array.[index, *]))
41+
42+
let createTest isEqual (zero: 'a) =
43+
CSR.Matrix.byRows context Utils.defaultWorkGroupSize
44+
|> makeTest<'a> isEqual zero
45+
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
46+
47+
let tests =
48+
[ createTest (=) 0
49+
50+
if Utils.isFloat64Available context.ClDevice then
51+
createTest Utils.floatIsEqual 0.0
52+
53+
createTest Utils.float32IsEqual 0.0f
54+
createTest (=) false ]
55+
|> testList "CSR byRows"

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ let makeTest context q formatFrom formatTo convertFun isZero (array: 'a [,]) =
4545
let expected =
4646
Utils.createMatrixFromArray2D formatTo array isZero
4747

48+
"Row count should be the same"
49+
|> Expect.equal actual.RowCount (Array2D.length1 array)
50+
51+
"Column count should be the same"
52+
|> Expect.equal actual.ColumnCount (Array2D.length2 array)
53+
4854
"Matrices should be equal"
4955
|> Expect.equal actual expected
5056

@@ -56,7 +62,7 @@ let createTest<'a when 'a: struct and 'a: equality> convertFun formatTo (isZero:
5662
|> List.map
5763
(fun formatFrom ->
5864
makeTest context q formatFrom formatTo convertFun isZero
59-
|> testPropertyWithConfig { config with endSize = 10 } $"test on %A{typeof<'a>} from %A{formatFrom}")
65+
|> testPropertyWithConfig config $"test on %A{typeof<'a>} from %A{formatFrom}")
6066

6167
let testFixtures formatTo =
6268
match formatTo with

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ let matrixTests =
1616
Matrix.SpGeMM.Expand.generalTests
1717
Matrix.SpGeMM.Masked.tests
1818
Matrix.Transpose.tests
19-
Matrix.RowsLengths.tests ]
19+
Matrix.RowsLengths.tests
20+
Matrix.ByRows.tests ]
2021
|> testSequenced
2122

2223
let commonTests =

0 commit comments

Comments
 (0)