Skip to content

Commit e33db1a

Browse files
committed
fix: mergeDisjoint, msbfs, intersect tests bugs
1 parent 3d9654d commit e33db1a

File tree

7 files changed

+228
-167
lines changed

7 files changed

+228
-167
lines changed

tests/GraphBLAS-sharp.Tests/Backend/Algorithms/MSBFS.fs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,26 @@ let levelsTests =
7979
TestCases.gpuTests "MSBFS Levels tests" levelsTestFixtures
8080

8181
let makeParentsTest context queue bfs (matrix: int [,]) =
82-
let graph = undirectedFromArray2D matrix -1
82+
83+
let graph = undirectedFromArray2D matrix 0
8384

8485
let largestComponent =
8586
ConnectedComponents.largestComponent graph
8687

8788
if largestComponent.Length > 1 then
8889
let sourceVertexCount = max 2 (largestComponent.Length / 10)
8990

90-
let source =
91-
largestComponent.[0..sourceVertexCount]
92-
|> Array.toList
91+
let source = largestComponent.[0..sourceVertexCount]
92+
source |> Array.sortInPlace
93+
let source = source |> Array.toList
9394

9495
let matrixHost =
95-
Utils.createMatrixFromArray2D CSR matrix ((=) -1)
96+
Utils.createMatrixFromArray2D CSR matrix ((=) 0)
9697

9798
let matrixDevice = matrixHost.ToDevice context
9899

99-
let expectedArray2D =
100-
HostPrimitives.MSBFSParents matrix source
101-
102100
let expected =
103-
Utils.createMatrixFromArray2D COO expectedArray2D ((=) -1)
101+
HostPrimitives.MSBFSParents matrix source
104102

105103
let actual: ClMatrix<int> = bfs queue matrixDevice source
106104
let actual = actual.ToHostAndFree queue
@@ -111,7 +109,7 @@ let makeParentsTest context queue bfs (matrix: int [,]) =
111109
| Matrix.COO a, Matrix.COO e -> Utils.compareCOOMatrix (=) a e
112110
| _ -> failwith "Not implemented"
113111

114-
let createParentsTest context queue testFun =
112+
let createParentsTest<'a> context queue testFun =
115113
testFun
116114
|> makeParentsTest context queue
117115
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
@@ -120,10 +118,10 @@ let parentsTestFixtures (testContext: TestContext) =
120118
[ let context = testContext.ClContext
121119
let queue = testContext.Queue
122120

123-
let bfsLevels =
121+
let bfsParents =
124122
Algorithms.MSBFS.runParents context workGroupSize
125123

126-
createParentsTest context queue bfsLevels ]
124+
createParentsTest context queue bfsParents ]
127125

128126
let parentsTests =
129127
TestCases.gpuTests "MSBFS Parents tests" parentsTestFixtures

tests/GraphBLAS-sharp.Tests/Backend/Common/ClArray/ExcludeElements.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ let makeTest<'a> isEqual (zero: 'a) testFun ((array, bitmap): 'a array * int arr
2222
let arrayCl = context.CreateClArray array
2323
let bitmapCl = context.CreateClArray bitmap
2424

25-
let actual: ClArray<'a> option = testFun processor HostInterop bitmapCl arrayCl
25+
let actual: ClArray<'a> option =
26+
testFun processor HostInterop bitmapCl arrayCl
27+
2628
let actual =
2729
actual
2830
|> Option.map (fun a -> a.ToHostAndFree processor)

tests/GraphBLAS-sharp.Tests/Backend/Matrix/Intersect.fs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,39 @@ open GraphBLAS.FSharp.Objects.ArraysExtensions
1212

1313
let config =
1414
{ Utils.defaultConfig with
15-
arbitrary = [ typeof<Generators.PairOfSparseMatrices> ] }
15+
arbitrary = [ typeof<Generators.PairOfSparseMatrices> ] }
1616

1717
let workGroupSize = Utils.defaultWorkGroupSize
1818

19-
let context = Context.defaultContext.ClContext
20-
let processor = Context.defaultContext.Queue
19+
let context = defaultContext.ClContext
20+
let processor = defaultContext.Queue
2121

22-
let makeTest isZero testFun (leftMatrix: 'a [,], rightMatrix: 'a [,]) =
22+
let makeTest<'a when 'a: struct> isZero testFun (leftMatrix: 'a [,], rightMatrix: 'a [,]) =
2323

24-
let m1 = Matrix.COO.FromArray2D(leftMatrix, isZero)
25-
let m2 = Matrix.COO.FromArray2D(rightMatrix, isZero)
24+
let m1 =
25+
Matrix.COO.FromArray2D(leftMatrix, isZero)
26+
27+
let m2 =
28+
Matrix.COO.FromArray2D(rightMatrix, isZero)
2629

2730
if m1.NNZ > 0 && m2.NNZ > 0 then
31+
2832
let expected =
29-
let leftIndices =
30-
(m1.Rows, m1.Columns)
31-
||> Array.zip
33+
let mutable index = 0
34+
let bitmap = Array.zeroCreate m1.NNZ
35+
36+
leftMatrix
37+
|> Array2D.iteri (fun row col value ->
38+
if row < m2.RowCount
39+
&& col < m2.ColumnCount
40+
&& not <| isZero rightMatrix.[row, col]
41+
&& not <| isZero value then
42+
bitmap.[index] <- 1
3243

33-
let rightIndices =
34-
(m2.Rows, m2.Columns)
35-
||> Array.zip
44+
if not <| isZero value then
45+
index <- index + 1)
3646

37-
Array.init
38-
<| m1.NNZ
39-
<| fun i ->
40-
let index = leftIndices.[i]
41-
if Array.exists ((=) index) rightIndices then 1 else 0
47+
bitmap
4248

4349
let m1 = m1.ToDevice context
4450
let m2 = m2.ToDevice context
@@ -55,9 +61,9 @@ let makeTest isZero testFun (leftMatrix: 'a [,], rightMatrix: 'a [,]) =
5561
"Matrices should be equal"
5662
|> Expect.equal actual expected
5763

58-
let createTest isZero =
64+
let inline createTest<'a when 'a: struct> (isZero: 'a -> bool) =
5965
Matrix.COO.Matrix.findKeysIntersection context workGroupSize
60-
|> makeTest isZero
66+
|> makeTest<'a> isZero
6167
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
6268

6369
let tests =

tests/GraphBLAS-sharp.Tests/Backend/Matrix/Merge.fs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ let testsCOO =
110110
createTestCOO (=) false ]
111111
|> testList "COO"
112112

113-
let makeTestCOODisjoint isEqual zero testFun (leftArray: 'a [,], rightArray: 'a [,]) =
113+
let makeTestCOODisjoint isEqual zero testFun (array: ('a * 'a) [,]) =
114+
115+
let leftArray = Array2D.map fst array
116+
let rightArray = Array2D.map snd array
114117

115118
let leftMatrix =
116119
Matrix.COO.FromArray2D(leftArray, isEqual zero)
@@ -123,24 +126,29 @@ let makeTestCOODisjoint isEqual zero testFun (leftArray: 'a [,], rightArray: 'a
123126
let clLeftMatrix = leftMatrix.ToDevice context
124127
let clRightMatrix = rightMatrix.ToDevice context
125128

126-
let actual: ClMatrix.COO<'a> = testFun processor clLeftMatrix clRightMatrix
129+
let actual: ClMatrix.COO<'a> =
130+
testFun processor clLeftMatrix clRightMatrix
131+
127132
let actual = actual.ToHostAndFree processor
128133

129134
clLeftMatrix.Dispose processor
130135
clRightMatrix.Dispose processor
131136

132137
rightArray
133-
|> Array2D.iteri (fun row column value -> leftArray.[row, column] <- value)
138+
|> Array2D.iteri
139+
(fun row column value ->
140+
if value <> zero then
141+
leftArray.[row, column] <- value)
134142

135-
let expected = Matrix.COO.FromArray2D(leftArray, isEqual zero)
143+
let expected =
144+
Matrix.COO.FromArray2D(leftArray, isEqual zero)
136145

137146
Utils.compareCOOMatrix isEqual actual expected
138147

139148
let createTestCOODisjoint isEqual (zero: 'a) =
140149
let configDisjoint =
141-
{Utils.defaultConfig with
142-
endSize = 10
143-
arbitrary = [ typeof<Generators.PairOfDisjointMatricesOfTheSameSize> ]}
150+
{ Utils.defaultConfig with
151+
arbitrary = [ typeof<Generators.PairOfDisjointMatricesOfTheSameSize> ] }
144152

145153
Matrix.COO.Merge.runDisjoint context Utils.defaultWorkGroupSize
146154
|> makeTestCOODisjoint isEqual zero
@@ -221,4 +229,5 @@ let testsCSR =
221229
|> testList "CSR"
222230

223231
let allTests =
224-
[ testsCSR; testsCOO; testsCOODisjoint ] |> testList "Merge"
232+
[ testsCSR; testsCOO; testsCOODisjoint ]
233+
|> testList "Merge"

tests/GraphBLAS-sharp.Tests/Generators.fs

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ module Generators =
3434
}
3535

3636
let genericSparseGenerator zero valuesGen handler =
37-
let maxSparsity = 10
37+
let minSparsity = 10
38+
let maxSparsity = 50
3839
let upperBound = 100
39-
let sparsityGen = Gen.choose (1, maxSparsity)
40+
let sparsityGen = Gen.choose (minSparsity, maxSparsity)
4041

4142
let genWithSparsity sparseValuesGenProvider =
4243
gen {
@@ -469,44 +470,22 @@ module Generators =
469470
gen {
470471
let! rowCount, columnCount = dimension2DGenerator
471472

472-
let! matrixA =
473-
valuesGenerator
473+
let! pairs =
474+
Gen.two valuesGenerator
474475
|> Gen.array2DOfDim (rowCount, columnCount)
475476

476-
let! matrixB =
477-
valuesGenerator
478-
|> Gen.array2DOfDim (rowCount, columnCount)
477+
let isZero = (=) zero
479478

480-
for row in 0 .. rowCount - 1 do
481-
for col in 0 .. columnCount - 1 do
479+
let pairs =
480+
pairs
481+
|> Array2D.map
482+
(fun (fst, snd) ->
483+
match () with
484+
| () when isZero fst && not <| isZero snd -> (zero, snd)
485+
| () when not <| isZero fst && isZero snd -> (fst, zero)
486+
| () -> (fst, zero))
482487

483-
if matrixA.[row, col] <> zero then
484-
matrixB.[row, col] <- matrixA.[row, col]
485-
486-
return (matrixA, matrixB)
487-
488-
// let! rowCount, columnCount = dimension2DGenerator
489-
//
490-
// let! arrayA =
491-
// valuesGenerator
492-
// |> Gen.arrayOfLength (rowCount * columnCount)
493-
//
494-
// let! arrayB =
495-
// arrayA
496-
// |> Gen.collectToArr (fun v -> if v = zero then valuesGenerator else Gen.constant zero)
497-
//
498-
// let matrixA = Array2D.zeroCreate rowCount columnCount
499-
// let matrixB = Array2D.zeroCreate rowCount columnCount
500-
//
501-
// (arrayA, arrayB)
502-
// ||> Array.iteri2 (fun i valueA valueB ->
503-
// let row = i / columnCount
504-
// let column = i % columnCount
505-
//
506-
// matrixA.[row, column] <- valueA
507-
// matrixB.[row, column] <- valueB)
508-
//
509-
// return (matrixA, matrixB)
488+
return pairs
510489
}
511490

512491
static member IntType() =
@@ -1435,15 +1414,20 @@ module Generators =
14351414

14361415
let! array = Gen.arrayOfLength length valuesGenerator
14371416

1438-
let! bitmap = Gen.collectToArr (fun value -> if value = zero then Gen.constant 0 else Gen.choose (0, 1)) array
1417+
let! bitmap =
1418+
Gen.collectToArr
1419+
(fun value ->
1420+
if value = zero then
1421+
Gen.constant 0
1422+
else
1423+
Gen.choose (0, 1))
1424+
array
14391425

14401426
return (array, bitmap)
14411427
}
14421428

14431429
static member IntType() =
1444-
arrayAndBitmap
1445-
<| Arb.generate<int>
1446-
<| 0
1430+
arrayAndBitmap <| Arb.generate<int> <| 0
14471431
|> Arb.fromGen
14481432

14491433
static member FloatType() =
@@ -1461,44 +1445,31 @@ module Generators =
14611445
|> Arb.fromGen
14621446

14631447
static member SByteType() =
1464-
arrayAndBitmap
1465-
<| Arb.generate<sbyte>
1466-
<| 0y
1448+
arrayAndBitmap <| Arb.generate<sbyte> <| 0y
14671449
|> Arb.fromGen
14681450

14691451
static member ByteType() =
1470-
arrayAndBitmap
1471-
<| Arb.generate<byte>
1472-
<| 0uy
1452+
arrayAndBitmap <| Arb.generate<byte> <| 0uy
14731453
|> Arb.fromGen
14741454

14751455
static member Int16Type() =
1476-
arrayAndBitmap
1477-
<| Arb.generate<int16>
1478-
<| 0s
1456+
arrayAndBitmap <| Arb.generate<int16> <| 0s
14791457
|> Arb.fromGen
14801458

14811459
static member UInt16Type() =
1482-
arrayAndBitmap
1483-
<| Arb.generate<uint16>
1484-
<| 0us
1460+
arrayAndBitmap <| Arb.generate<uint16> <| 0us
14851461
|> Arb.fromGen
14861462

14871463
static member Int32Type() =
1488-
arrayAndBitmap
1489-
<| Arb.generate<int32>
1490-
<| 0
1464+
arrayAndBitmap <| Arb.generate<int32> <| 0
14911465
|> Arb.fromGen
14921466

14931467
static member UInt32Type() =
1494-
arrayAndBitmap
1495-
<| Arb.generate<uint32>
1496-
<| 0u
1468+
arrayAndBitmap <| Arb.generate<uint32> <| 0u
14971469
|> Arb.fromGen
1470+
14981471
static member BoolType() =
1499-
arrayAndBitmap
1500-
<| Arb.generate<bool>
1501-
<| false
1472+
arrayAndBitmap <| Arb.generate<bool> <| false
15021473
|> Arb.fromGen
15031474

15041475
type Set() =
@@ -1673,4 +1644,3 @@ module Generators =
16731644
static member BoolType() =
16741645
arrayAndChunkPosition <| Arb.generate<bool>
16751646
|> Arb.fromGen
1676-

0 commit comments

Comments
 (0)