22
33open Expecto
44open GraphBLAS.FSharp
5- open GraphBLAS.FSharp .Backend .Quotes
65open GraphBLAS.FSharp .Tests
76open GraphBLAS.FSharp .Tests .Context
87open GraphBLAS.FSharp .Objects .ClVectorExtensions
98open GraphBLAS.FSharp .Objects
10- open GraphBLAS.FSharp .Objects .MatrixExtensions
9+
10+ let private alpha = 0.85 f
11+ let private accuracy = 0.00001 f
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 <> 0 f then 1 f else 0 f)) matrix
20+
21+ //Set value
22+ Array2D.iteri
23+ ( fun r c v ->
24+ result.[ r, c] <-
25+ if v <> 0 f then
26+ alpha / outDegrees.[ r]
27+ else
28+ 0 f)
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 ( 1 f / ( float32 rowCount))
48+
49+ let mutable error = accuracy + 1 f
50+ let addConst = ( 1 f - alpha) / ( float32 rowCount)
51+
52+ while ( error > accuracy) do
53+ for r in 0 .. rowCount - 1 do
54+ result [ r ] <- 0 f
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)) 0 f result prev
64+
65+ let temp = result
66+ result <- prev
67+ prev <- temp
68+
69+ prev
1170
1271let 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 ] <- 1 f
29- matrix.[ 0 , 2 ] <- 1 f
30- matrix.[ 0 , 3 ] <- 1 f
31- matrix.[ 1 , 2 ] <- 1 f
32- matrix.[ 1 , 3 ] <- 1 f
33- matrix.[ 2 , 0 ] <- 1 f
34- matrix.[ 3 , 2 ] <- 1 f
35- matrix.[ 3 , 0 ] <- 1 f
36-
83+ testPropertyWithConfig config testName
84+ <| fun ( matrix : float32 [,]) ->
3785 let matrixHost =
3886 Utils.createMatrixFromArray2D CSR matrix ((=) 0 f)
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.3681506515 f
55- 0.1418093443 f
56- 0.2879616022 f
57- 0.2020783126 f |]
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 0 f
106+ match resHost with
107+ | Vector.Dense resHost ->
108+ let actual = resHost |> Utils.unwrapOptionArray 0 f
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
68117let tests =
69- TestCases.gpuTests " Bfs tests" testFixtures
118+ TestCases.gpuTests " PageRank tests" testFixtures
0 commit comments