Skip to content

Commit 6f3819b

Browse files
committed
Fix merge
1 parent ebdb250 commit 6f3819b

File tree

13 files changed

+164
-27
lines changed

13 files changed

+164
-27
lines changed

benchmarks/GraphBLAS-sharp.Benchmarks/GraphBLAS-sharp.Benchmarks.fsproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
<Compile Include="Matrix/Map2/MathNET.fs" />
2626
<Compile Include="Vector/Map2.fs" />
2727
<Compile Include="Algorithms/BFS.fs" />
28+
<Compile Include="Algorithms/PageRank.fs" />
2829
<Compile Include="Program.fs" />
2930
<Folder Include="Datasets" />
3031
</ItemGroup>
3132
<Import Project="..\..\.paket\Paket.Restore.targets" />
32-
</Project>
33+
</Project>

benchmarks/GraphBLAS-sharp.Benchmarks/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open BenchmarkDotNet.Running
44
[<EntryPoint>]
55
let main argv =
66
let benchmarks =
7-
BenchmarkSwitcher [| typeof<Algorithms.BFS.BFSWithoutTransferBenchmarkBool>
7+
BenchmarkSwitcher [| typeof<Algorithms.BFS.BFSWithoutTransferBenchmarkBool> |]
88

99
benchmarks.Run argv |> ignore
1010
0

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ module Algorithms =
1919

2020
module SSSP =
2121
let run = SSSP.run
22+
23+
module PageRank =
24+
let run = PageRank.run
25+
26+
let prepareMatrix = PageRank.prepareMatrix

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module internal BFS =
3434
let containsNonZero =
3535
Vector.exists Predicates.isSome clContext workGroupSize
3636

37-
fun (queue: MailboxProcessor<Msg>) (matrix: ClMatrix<'a>) (source: int) ->
37+
fun (queue: MailboxProcessor<Msg>) (matrix: ClMatrix<bool>) (source: int) ->
3838
let vertexCount = matrix.RowCount
3939

4040
let levels =

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,4 @@ module SSSP =
7676
front1.Dispose queue
7777
front2.Dispose queue
7878

79-
match distance with
80-
| ClVector.Dense dist -> dist
81-
| _ -> failwith "not implemented"
79+
distance

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="Algorithms/BFS.fs" />
7272
<Compile Include="Algorithms/MSBFS.fs" />
7373
<Compile Include="Algorithms/SSSP.fs" />
74+
<Compile Include="Algorithms/PageRank.fs" />
7475
<Compile Include="Algorithms/Algorithms.fs" />
7576

7677
</ItemGroup>

src/GraphBLAS-sharp.Backend/Quotes/Arithmetic.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,16 @@ module ArithmeticOperations =
257257
<@ fun (x: 'a) (y: 'a) -> Some(min x y) @>
258258

259259
let fst<'a> = <@ fun (x: 'a) (_: 'a) -> Some x @>
260+
261+
//PageRank specific
262+
let squareOfDifference =
263+
<@ fun (x: float32 option) (y: float32 option) ->
264+
let mutable res = 0.0f
265+
266+
match x, y with
267+
| Some f, Some s -> res <- (f - s) * (f - s)
268+
| Some f, None -> res <- f * f
269+
| None, Some s -> res <- s * s
270+
| None, None -> ()
271+
272+
if res = 0.0f then None else Some res @>

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,30 @@ open GraphBLAS.FSharp.Backend.Vector
1515
[<RequireQualifiedAccess>]
1616
module Vector =
1717
/// <summary>
18-
/// Builds vector of given format with fixed size and fills it with the default values of desired type.
18+
/// Builds vector of given format with fixed size and fills it with the given value.
1919
/// </summary>
2020
/// <param name="clContext">OpenCL context.</param>
2121
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
22-
let zeroCreate (clContext: ClContext) workGroupSize =
23-
let zeroCreate =
24-
ClArray.zeroCreate clContext workGroupSize
22+
let create (clContext: ClContext) workGroupSize =
23+
let create = ClArray.create clContext workGroupSize
2524

26-
fun (processor: MailboxProcessor<_>) allocationMode size format ->
25+
fun (processor: MailboxProcessor<_>) allocationMode size format value ->
2726
match format with
28-
| Sparse ->
29-
ClVector.Sparse
30-
{ Context = clContext
31-
Indices = clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, [| 0 |])
32-
Values =
33-
clContext.CreateClArrayWithSpecificAllocationMode(
34-
allocationMode,
35-
[| Unchecked.defaultof<'a> |]
36-
) // TODO empty vector
37-
Size = size }
27+
| Sparse -> failwith "Attempting to create full sparse vector"
3828
| Dense ->
3929
ClVector.Dense
40-
<| zeroCreate processor allocationMode size
30+
<| create processor allocationMode size value
31+
32+
/// <summary>
33+
/// Builds vector of given format with fixed size and fills it with the default values of desired type.
34+
/// </summary>
35+
/// <param name="clContext">OpenCL context.</param>
36+
/// <param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
37+
let zeroCreate (clContext: ClContext) workGroupSize =
38+
let create = create clContext workGroupSize
39+
40+
fun (processor: MailboxProcessor<_>) allocationMode size format ->
41+
create processor allocationMode size format None
4142

4243
/// <summary>
4344
/// Builds vector of given format with fixed size and fills it with the values from the given list.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let makeLevelsTest context queue bfs (matrix: int [,]) =
6060
let createLevelsTest<'a> context queue testFun =
6161
testFun
6262
|> makeLevelsTest context queue
63-
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
63+
|> testPropertyWithConfig config $"test on %A{typeof<'a>}, %A{context}"
6464

6565
let levelsTestFixtures (testContext: TestContext) =
6666
[ let context = testContext.ClContext
@@ -112,7 +112,7 @@ let makeParentsTest context queue bfs (matrix: int [,]) =
112112
let createParentsTest<'a> context queue testFun =
113113
testFun
114114
|> makeParentsTest context queue
115-
|> testPropertyWithConfig config $"test on %A{typeof<'a>}"
115+
|> testPropertyWithConfig config $"test on %A{typeof<'a>}, %A{context}"
116116

117117
let parentsTestFixtures (testContext: TestContext) =
118118
[ let context = testContext.ClContext
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
module GraphBLAS.FSharp.Tests.Backend.Algorithms.PageRank
2+
3+
open Expecto
4+
open GraphBLAS.FSharp
5+
open GraphBLAS.FSharp.Tests
6+
open GraphBLAS.FSharp.Tests.Context
7+
open GraphBLAS.FSharp.Objects.ClVectorExtensions
8+
open GraphBLAS.FSharp.Objects
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
70+
71+
let testFixtures (testContext: TestContext) =
72+
[ let config = Utils.undirectedAlgoConfig
73+
let context = testContext.ClContext
74+
let queue = testContext.Queue
75+
let workGroupSize = Utils.defaultWorkGroupSize
76+
77+
let testName =
78+
sprintf "Test on %A" testContext.ClContext
79+
80+
let pageRank =
81+
Algorithms.PageRank.run context workGroupSize
82+
83+
testPropertyWithConfig config testName
84+
<| fun (matrix: float32 [,]) ->
85+
let matrixHost =
86+
Utils.createMatrixFromArray2D CSR matrix ((=) 0f)
87+
88+
if matrixHost.NNZ > 0 then
89+
let preparedMatrixExpected = prepareNaive matrix
90+
91+
let expected = pageRankNaive preparedMatrixExpected
92+
93+
let matrix = matrixHost.ToDevice context
94+
95+
let preparedMatrix =
96+
Algorithms.PageRank.prepareMatrix context workGroupSize queue matrix
97+
98+
let res = pageRank queue preparedMatrix accuracy
99+
100+
let resHost = res.ToHost queue
101+
102+
preparedMatrix.Dispose queue
103+
matrix.Dispose queue
104+
res.Dispose queue
105+
106+
match resHost with
107+
| Vector.Dense resHost ->
108+
let actual = resHost |> Utils.unwrapOptionArray 0f
109+
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])
114+
115+
| _ -> failwith "Not implemented" ]
116+
117+
let tests =
118+
TestCases.gpuTests "PageRank tests" testFixtures

0 commit comments

Comments
 (0)