Skip to content

Commit 15f415e

Browse files
committed
PageRank test property
1 parent baa0c0b commit 15f415e

1 file changed

Lines changed: 86 additions & 37 deletions

File tree

  • tests/GraphBLAS-sharp.Tests/Backend/Algorithms

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

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,75 @@
22

33
open Expecto
44
open GraphBLAS.FSharp
5-
open GraphBLAS.FSharp.Backend.Quotes
65
open GraphBLAS.FSharp.Tests
76
open GraphBLAS.FSharp.Tests.Context
87
open GraphBLAS.FSharp.Objects.ClVectorExtensions
98
open GraphBLAS.FSharp.Objects
10-
open GraphBLAS.FSharp.Objects.MatrixExtensions
9+
10+
let private alpha = 0.85f
11+
let private accuracy = 0.00001f
12+
13+
let prepareNaive (matrix: float32 [,]) =
14+
let result = Array2D.copy matrix
15+
let rowCount = Array2D.length1 matrix
16+
let outDegrees = Array.zeroCreate rowCount
17+
18+
//Count degree
19+
Array2D.iteri (fun r c v -> outDegrees.[r] <- outDegrees.[r] + (if v <> 0f then 1f else 0f)) matrix
20+
21+
//Set value
22+
Array2D.iteri
23+
(fun r c v ->
24+
result.[r, c] <-
25+
if v <> 0f then
26+
alpha / outDegrees.[r]
27+
else
28+
0f)
29+
matrix
30+
31+
//Transpose
32+
Array2D.iteri
33+
(fun r c _ ->
34+
if r > c then
35+
let temp = result.[r, c]
36+
result.[r, c] <- result.[c, r]
37+
result.[c, r] <- temp)
38+
matrix
39+
40+
result
41+
42+
let pageRankNaive (matrix: float32 [,]) =
43+
let rowCount = Array2D.length1 matrix
44+
let mutable result = Array.zeroCreate rowCount
45+
46+
let mutable prev =
47+
Array.create rowCount (1f / (float32 rowCount))
48+
49+
let mutable error = accuracy + 1f
50+
let addConst = (1f - alpha) / (float32 rowCount)
51+
52+
while (error > accuracy) do
53+
for r in 0 .. rowCount - 1 do
54+
result [ r ] <- 0f
55+
56+
for c in 0 .. rowCount - 1 do
57+
result.[r] <- result.[r] + matrix.[r, c] * prev.[c]
58+
59+
result.[r] <- result.[r] + addConst
60+
61+
error <-
62+
sqrt
63+
<| Array.fold2 (fun e x1 x2 -> e + (x1 - x2) * (x1 - x2)) 0f result prev
64+
65+
let temp = result
66+
result <- prev
67+
prev <- temp
68+
69+
prev
1170

1271
let testFixtures (testContext: TestContext) =
13-
[ let context = testContext.ClContext
72+
[ let config = Utils.undirectedAlgoConfig
73+
let context = testContext.ClContext
1474
let queue = testContext.Queue
1575
let workGroupSize = Utils.defaultWorkGroupSize
1676

@@ -20,50 +80,39 @@ let testFixtures (testContext: TestContext) =
2080
let pageRank =
2181
Algorithms.PageRank.run context workGroupSize
2282

23-
testCase testName
24-
<| fun () ->
25-
26-
let matrix = Array2D.zeroCreate 4 4
27-
28-
matrix.[0, 1] <- 1f
29-
matrix.[0, 2] <- 1f
30-
matrix.[0, 3] <- 1f
31-
matrix.[1, 2] <- 1f
32-
matrix.[1, 3] <- 1f
33-
matrix.[2, 0] <- 1f
34-
matrix.[3, 2] <- 1f
35-
matrix.[3, 0] <- 1f
36-
83+
testPropertyWithConfig config testName
84+
<| fun (matrix: float32 [,]) ->
3785
let matrixHost =
3886
Utils.createMatrixFromArray2D CSR matrix ((=) 0f)
3987

40-
let matrix = matrixHost.ToDevice context
88+
if matrixHost.NNZ > 0 then
89+
let preparedMatrixExpected = prepareNaive matrix
90+
91+
let expected = pageRankNaive preparedMatrixExpected
4192

42-
let preparedMatrix =
43-
Algorithms.PageRank.prepareMatrix context workGroupSize queue matrix
93+
let matrix = matrixHost.ToDevice context
4494

45-
let res = pageRank queue preparedMatrix
95+
let preparedMatrix =
96+
Algorithms.PageRank.prepareMatrix context workGroupSize queue matrix
4697

47-
let resHost = res.ToHost queue
98+
let res = pageRank queue preparedMatrix accuracy
4899

49-
preparedMatrix.Dispose queue
50-
matrix.Dispose queue
51-
res.Dispose queue
100+
let resHost = res.ToHost queue
52101

53-
let expected =
54-
[| 0.3681506515f
55-
0.1418093443f
56-
0.2879616022f
57-
0.2020783126f |]
102+
preparedMatrix.Dispose queue
103+
matrix.Dispose queue
104+
res.Dispose queue
58105

59-
match resHost with
60-
| Vector.Dense resHost ->
61-
let actual = resHost |> Utils.unwrapOptionArray 0f
106+
match resHost with
107+
| Vector.Dense resHost ->
108+
let actual = resHost |> Utils.unwrapOptionArray 0f
62109

63-
for i in 0 .. actual.Length - 1 do
64-
Expect.isTrue (Utils.float32IsEqual actual.[i] expected.[i]) "Values should be equal"
110+
for i in 0 .. actual.Length - 1 do
111+
Expect.isTrue
112+
((abs (actual.[i] - expected.[i])) < accuracy)
113+
(sprintf "Values should be equal. Expected %A, actual %A" expected.[i] actual.[i])
65114

66-
| _ -> failwith "Not implemented" ]
115+
| _ -> failwith "Not implemented" ]
67116

68117
let tests =
69-
TestCases.gpuTests "Bfs tests" testFixtures
118+
TestCases.gpuTests "PageRank tests" testFixtures

0 commit comments

Comments
 (0)