Skip to content

Commit 1558c6d

Browse files
committed
add: tests
1 parent cf10d92 commit 1558c6d

8 files changed

Lines changed: 252 additions & 9 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ module Algorithms =
1212

1313
let singleSourcePushPull = BFS.singleSourcePushPull
1414

15+
module MSBFS =
16+
let runLevels = MSBFS.Levels.run
17+
18+
let runParents = MSBFS.Parents.run
19+
1520
module SSSP =
1621
let run = SSSP.run
1722

src/GraphBLAS-sharp.Backend/Algorithms/MSBFS.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ module internal MSBFS =
115115
stop <- true
116116
newFrontier.Dispose queue
117117

118-
levels
118+
ClMatrix.COO levels
119119

120120
let runSingleSourceMultipleTimes<'a when 'a: struct>
121121
(add: Expr<int option -> int option -> int option>)
@@ -131,7 +131,7 @@ module internal MSBFS =
131131
|> List.map (SSBFS queue matrix)
132132

133133
module Parents =
134-
let updateFrontAndParents (clContext: ClContext) workGroupSize =
134+
let private updateFrontAndParents (clContext: ClContext) workGroupSize =
135135
// update parents same as levels
136136
// every front value should be equal to its column number
137137
let frontExclude = frontExclude clContext workGroupSize
@@ -165,7 +165,7 @@ module internal MSBFS =
165165
=
166166

167167
let spGeMM =
168-
Operations.SpGeMM.COO.expand (ArithmeticOperations.min 0) (ArithmeticOperations.fst 0) clContext workGroupSize
168+
Operations.SpGeMM.COO.expand (ArithmeticOperations.min -1) (ArithmeticOperations.fst -1) clContext workGroupSize
169169

170170
let updateFrontAndLevels = updateFrontAndParents clContext workGroupSize
171171

@@ -187,7 +187,7 @@ module internal MSBFS =
187187

188188
let mutable parents =
189189
source
190-
|> List.mapi (fun i vertex -> i, vertex, 0)
190+
|> List.mapi (fun i vertex -> i, vertex, -1)
191191
|> Matrix.ofList clContext DeviceOnly sourceVertexCount vertexCount
192192

193193
let mutable front =
@@ -216,4 +216,4 @@ module internal MSBFS =
216216
stop <- true
217217
newFrontier.Dispose queue
218218

219-
parents
219+
ClMatrix.COO parents

src/GraphBLAS-sharp/Objects/Matrix.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ module Matrix =
187187
{ Context = context
188188
RowCount = this.RowCount
189189
ColumnCount = this.ColumnCount
190-
Rows = rows
191-
NNZ = this.NNZ }
190+
Rows = rows }
192191

193192
type Tuples<'a> =
194193
{ RowIndices: int []
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
module GraphBLAS.FSharp.Tests.Backend.Algorithms.MSBFS
2+
3+
open Expecto
4+
open GraphBLAS.FSharp
5+
open GraphBLAS.FSharp.Backend.Quotes
6+
open GraphBLAS.FSharp.Tests
7+
open GraphBLAS.FSharp.Tests.Context
8+
open GraphBLAS.FSharp.Tests.Backend.QuickGraph.Algorithms
9+
open GraphBLAS.FSharp.Tests.Backend.QuickGraph.CreateGraph
10+
open GraphBLAS.FSharp.Objects
11+
open GraphBLAS.FSharp.Objects.MatrixExtensions
12+
13+
let config = Utils.undirectedAlgoConfig
14+
let workGroupSize = Utils.defaultWorkGroupSize
15+
16+
let makeLevelsTest context queue bfs (matrix: int [,]) =
17+
let graph = undirectedFromArray2D matrix 0
18+
19+
let largestComponent =
20+
ConnectedComponents.largestComponent graph
21+
22+
if largestComponent.Length > 1 then
23+
let sourceVertexCount = max 2 (largestComponent.Length / 10)
24+
let source = largestComponent.[0 .. sourceVertexCount] |> Array.toList
25+
26+
let matrixHost =
27+
Utils.createMatrixFromArray2D CSR matrix ((=) 0)
28+
let matrixDevice = matrixHost.ToDevice context
29+
30+
let expectedArray2D: int [,] = Array2D.zeroCreate sourceVertexCount (Array2D.length2 matrix)
31+
32+
source
33+
|> Seq.iteri (fun i vertex ->
34+
(snd (BFS.runUndirected graph vertex))
35+
|> Utils.createArrayFromDictionary (Array2D.length1 matrix) 0
36+
|> Array.iteri (fun col value -> expectedArray2D.[i, col] <- value))
37+
38+
let expected = Utils.createMatrixFromArray2D COO expectedArray2D ((=) 0)
39+
40+
let actual: ClMatrix<int> = bfs queue matrixDevice source
41+
let actual = actual.ToHostAndFree queue
42+
43+
matrixDevice.Dispose queue
44+
45+
match actual, expected with
46+
| Matrix.COO a, Matrix.COO e ->
47+
Utils.compareCOOMatrix (=) a e
48+
| _ -> failwith "Not implemented"
49+
50+
let createLevelsTest context queue testFun =
51+
testFun
52+
|> makeLevelsTest context queue
53+
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
54+
55+
let levelsTestFixtures (testContext: TestContext) =
56+
[ let context = testContext.ClContext
57+
let queue = testContext.Queue
58+
59+
let bfsLevels =
60+
Algorithms.MSBFS.runLevels
61+
(fst ArithmeticOperations.intAdd)
62+
(fst ArithmeticOperations.intMul)
63+
context
64+
workGroupSize
65+
66+
createLevelsTest context queue bfsLevels ]
67+
68+
let levelsTests =
69+
TestCases.gpuTests "MSBFS Levels tests" levelsTestFixtures
70+
71+
let makeParentsTest context queue bfs (matrix: int [,]) =
72+
let graph = undirectedFromArray2D matrix -1
73+
74+
let largestComponent =
75+
ConnectedComponents.largestComponent graph
76+
77+
if largestComponent.Length > 1 then
78+
let sourceVertexCount = max 2 (largestComponent.Length / 10)
79+
let source = largestComponent.[0 .. sourceVertexCount] |> Array.toList
80+
81+
let matrixHost =
82+
Utils.createMatrixFromArray2D CSR matrix ((=) -1)
83+
let matrixDevice = matrixHost.ToDevice context
84+
85+
let expectedArray2D = HostPrimitives.MSBFSParents matrix source
86+
let expected = Utils.createMatrixFromArray2D COO expectedArray2D ((=) -1)
87+
88+
let actual: ClMatrix<int> = bfs queue matrixDevice source
89+
let actual = actual.ToHostAndFree queue
90+
91+
matrixDevice.Dispose queue
92+
93+
match actual, expected with
94+
| Matrix.COO a, Matrix.COO e ->
95+
Utils.compareCOOMatrix (=) a e
96+
| _ -> failwith "Not implemented"
97+
98+
let createParentsTest context queue testFun =
99+
testFun
100+
|> makeLevelsTest context queue
101+
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
102+
103+
let parentsTestFixtures (testContext: TestContext) =
104+
[ let context = testContext.ClContext
105+
let queue = testContext.Queue
106+
107+
let bfsLevels =
108+
Algorithms.MSBFS.runParents
109+
context
110+
workGroupSize
111+
112+
createLevelsTest context queue bfsLevels ]
113+
114+
let parentsTests =
115+
TestCases.gpuTests "MSBFS Levels tests" parentsTestFixtures
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module GraphBLAS.FSharp.Tests.Backend.Matrix.SpGeMM.ExpandCOO
2+
3+
open Expecto
4+
open GraphBLAS.FSharp
5+
open GraphBLAS.FSharp.Backend.Quotes
6+
open GraphBLAS.FSharp.Objects
7+
open GraphBLAS.FSharp.Objects.ClContextExtensions
8+
open GraphBLAS.FSharp.Objects.MatrixExtensions
9+
open GraphBLAS.FSharp.Test
10+
open GraphBLAS.FSharp.Tests
11+
open GraphBLAS.FSharp.Tests.Backend
12+
13+
let context = Context.defaultContext.ClContext
14+
15+
let processor = Context.defaultContext.Queue
16+
17+
let config =
18+
{ Utils.defaultConfig with
19+
arbitrary = [ typeof<Generators.PairOfSparseMatricesWithCompatibleSizes> ] }
20+
21+
let makeGeneralTest zero isEqual opAdd opMul testFun (leftArray: 'a [,], rightArray: 'a [,]) =
22+
23+
let leftMatrix =
24+
Utils.createMatrixFromArray2D COO leftArray (isEqual zero)
25+
26+
let rightMatrix =
27+
Utils.createMatrixFromArray2D CSR rightArray (isEqual zero)
28+
29+
if leftMatrix.NNZ > 0 && rightMatrix.NNZ > 0 then
30+
let clLeftMatrix = leftMatrix.ToDevice context
31+
let clRightMatrix = rightMatrix.ToDevice context
32+
33+
let (clMatrixActual: ClMatrix.COO<_> option) =
34+
testFun processor HostInterop clLeftMatrix clRightMatrix
35+
36+
let expected =
37+
HostPrimitives.array2DMultiplication zero opMul opAdd leftArray rightArray
38+
|> fun array -> Matrix.COO.FromArray2D(array, isEqual zero)
39+
40+
match clMatrixActual with
41+
| Some clMatrixActual ->
42+
43+
let matrixActual = clMatrixActual.ToHost processor
44+
clMatrixActual.Dispose processor
45+
46+
Utils.compareCOOMatrix isEqual matrixActual expected
47+
| None ->
48+
"Expected should be empty"
49+
|> Expect.isTrue (expected.NNZ = 0)
50+
51+
let createGeneralTest (zero: 'a) isEqual (opAddQ, opAdd) (opMulQ, opMul) testFun =
52+
testFun opAddQ opMulQ context Utils.defaultWorkGroupSize
53+
|> makeGeneralTest zero isEqual opAdd opMul
54+
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
55+
56+
let generalTests =
57+
[ createGeneralTest 0 (=) ArithmeticOperations.intAdd ArithmeticOperations.intMul Operations.SpGeMM.COO.expand
58+
59+
if Utils.isFloat64Available context.ClDevice then
60+
createGeneralTest
61+
0.0
62+
Utils.floatIsEqual
63+
ArithmeticOperations.floatAdd
64+
ArithmeticOperations.floatMul
65+
Operations.SpGeMM.COO.expand
66+
67+
createGeneralTest
68+
0.0f
69+
Utils.float32IsEqual
70+
ArithmeticOperations.float32Add
71+
ArithmeticOperations.float32Mul
72+
Operations.SpGeMM.COO.expand
73+
createGeneralTest false (=) ArithmeticOperations.boolAdd ArithmeticOperations.boolMul Operations.SpGeMM.COO.expand ]
74+
|> testList "General"
75+
76+
let tests =
77+
testList "SpGeMM.Expand" [ generalTests ]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<Compile Include="Backend/Algorithms/BFS.fs" />
2121
<Compile Include="Backend/Algorithms/SSSP.fs" />
2222
<Compile Include="Backend/Algorithms/PageRank.fs" />
23+
<Compile Include="Backend/Algorithms/MSBFS.fs" />
2324
<Compile Include="Backend/Common/ClArray/Blit.fs" />
2425
<Compile Include="Backend/Common/ClArray/Choose.fs" />
2526
<Compile Include="Backend/Common/ClArray/ChunkBySize.fs" />
@@ -51,6 +52,7 @@
5152
<Compile Include="Backend/Matrix/RowsLengths.fs" />
5253
<Compile Include="Backend/Matrix/SpGeMM/Expand.fs" />
5354
<Compile Include="Backend/Matrix/SpGeMM/Masked.fs" />
55+
<Compile Include="Backend/Matrix/SpGeMM/ExpandCOO.fs" />
5456
<Compile Include="Backend/Matrix/Transpose.fs" />
5557
<Compile Include="Backend/Matrix/Merge.fs" />
5658
<Compile Include="Backend/Matrix/ExpandRows.fs" />

tests/GraphBLAS-sharp.Tests/Helpers.fs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,48 @@ module HostPrimitives =
337337

338338
op leftElement rightElement
339339

340+
let MSBFSParents matrix source =
341+
let opAdd a b =
342+
let result = min a b
343+
if result = -1 then None else Some result
344+
345+
let opMul (a: int) _ = if a = -1 then None else Some a
346+
347+
let array2DMultiplication = array2DMultiplication -1 opMul opAdd
348+
349+
let front =
350+
Array2D.create
351+
<| Seq.length source
352+
<| Array2D.length1 matrix
353+
<| -1
354+
355+
source
356+
|> Seq.iteri (fun row vertex ->
357+
front.[row, vertex] <- vertex)
358+
359+
let parents =
360+
Array2D.create
361+
<| Seq.length source
362+
<| Array2D.length1 matrix
363+
<| -1
364+
365+
let mutable stop = false
366+
367+
while not stop do
368+
let newFront = array2DMultiplication front matrix
369+
stop <- true
370+
371+
newFront
372+
|> Array2D.iteri (fun i j value ->
373+
if value <> -1 then
374+
if parents.[i, j] <> -1 then
375+
newFront.[i, j] <- -1
376+
else
377+
stop <- false
378+
parents.[i, j] <- value)
379+
380+
front
381+
340382
module Context =
341383
type TestContext =
342384
{ ClContext: ClContext

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
open Expecto
22
open GraphBLAS.FSharp.Tests
33
open GraphBLAS.FSharp.Tests.Backend
4-
open GraphBLAS.FSharp.Tests.Backend.Matrix
54

65
let matrixTests =
76
testList
@@ -18,6 +17,7 @@ let matrixTests =
1817
Matrix.Kronecker.tests
1918

2019
Matrix.SpGeMM.Expand.tests
20+
Matrix.SpGeMM.ExpandCOO.tests
2121
Matrix.SpGeMM.Masked.tests ]
2222
|> testSequenced
2323

@@ -92,7 +92,10 @@ let algorithmsTests =
9292
testList
9393
"Algorithms tests"
9494
[ Algorithms.BFS.tests
95-
Algorithms.SSSP.tests ]
95+
Algorithms.SSSP.tests
96+
97+
Algorithms.MSBFS.levelsTests
98+
Algorithms.MSBFS.parentsTests ]
9699
|> testSequenced
97100

98101
let deviceTests =

0 commit comments

Comments
 (0)