Skip to content

Commit c8f3042

Browse files
committed
Constants module and signature for PageRank module
1 parent b82fe68 commit c8f3042

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+147
-135
lines changed

benchmarks/GraphBLAS-sharp.Benchmarks/Algorithms/PageRank.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ type Benchmarks(
2323

2424
let mutable funToBenchmark = None
2525
let mutable matrix = Unchecked.defaultof<ClMatrix<float32>>
26-
let mutable matrixPrepared = Unchecked.defaultof<PageRankMatrix<float32>>
26+
let mutable matrixPrepared = Unchecked.defaultof<Algorithms.PageRank.PageRankMatrix>
2727
let mutable matrixHost = Unchecked.defaultof<_>
2828

29-
let accuracy = 0.00000001f
30-
3129
member val Result = Unchecked.defaultof<ClVector<float32>> with get,set
3230

3331
[<ParamsSource("AvailableContexts")>]
@@ -67,7 +65,7 @@ type Benchmarks(
6765
| Some x -> x
6866

6967
member this.PageRank() =
70-
this.Result <- this.FunToBenchmark this.Processor matrixPrepared accuracy
68+
this.Result <- this.FunToBenchmark this.Processor matrixPrepared Constants.PageRank.accuracy
7169

7270
member this.ClearInputMatrix() =
7371
matrix.Dispose this.Processor

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ module Algorithms =
2121
let run = SSSP.run
2222

2323
module PageRank =
24+
type PageRankMatrix = PageRank.PageRankMatrix
25+
2426
/// <summary>
2527
/// Computes PageRank of the given matrix.
2628
/// Matrix should be prepared in advance using "PageRank.prepareMatrix" method.
2729
/// Accepts accuracy as a parameter which determines how many iterations will be performed.
28-
/// Values of accuracy lower than 1e-06 are not recommended since the process may never stop.
30+
/// Values of accuracy higher than 1e-06 are not recommended since the process may never stop.
2931
/// </summary>
3032
/// <example>
3133
/// <code>

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ open GraphBLAS.FSharp.Objects.ClContextExtensions
1111
open GraphBLAS.FSharp.Objects.ArraysExtensions
1212
open GraphBLAS.FSharp.Objects.ClCellExtensions
1313

14-
module internal PageRank =
15-
let alpha = 0.85f
14+
module PageRank =
15+
type PageRankMatrix =
16+
| PreparedMatrix of ClMatrix<float32>
17+
18+
member this.Dispose(processor: MailboxProcessor<Msg>) =
19+
match this with
20+
| PreparedMatrix matrix -> matrix.Dispose processor
1621

1722
let private countOutDegree (clContext: ClContext) workGroupSize =
1823

@@ -40,10 +45,10 @@ module internal PageRank =
4045

4146
outDegree
4247

43-
let prepareMatrix (clContext: ClContext) workGroupSize =
48+
let internal prepareMatrix (clContext: ClContext) workGroupSize =
4449

4550
//Passing global variable to kernel in Brahma is not possible
46-
let alpha = alpha
51+
let alpha = Constants.PageRank.alpha
4752

4853
let op =
4954
<@ fun (x: float32 option) y ->
@@ -131,10 +136,10 @@ module internal PageRank =
131136

132137
transposeInPlace queue DeviceOnly newMatrix
133138
|> ClMatrix.CSR
134-
|> PageRankMatrix
139+
|> PreparedMatrix
135140
| _ -> failwith "Not implemented"
136141

137-
let run (clContext: ClContext) workGroupSize =
142+
let internal run (clContext: ClContext) workGroupSize =
138143

139144
let squareOfDifference = ArithmeticOperations.squareOfDifference
140145
let plus = ArithmeticOperations.float32SumOption
@@ -155,7 +160,7 @@ module internal PageRank =
155160
let create =
156161
GraphBLAS.FSharp.Vector.create clContext workGroupSize
157162

158-
fun (queue: MailboxProcessor<Msg>) (PageRankMatrix matrix) accuracy ->
163+
fun (queue: MailboxProcessor<Msg>) (PreparedMatrix matrix) accuracy ->
159164
let vertexCount = matrix.RowCount
160165

161166
//None is 0
@@ -169,7 +174,15 @@ module internal PageRank =
169174
create queue DeviceOnly vertexCount Dense None
170175

171176
let addition =
172-
create queue DeviceOnly vertexCount Dense (Some((1.0f - alpha) / (float32 vertexCount)))
177+
create
178+
queue
179+
DeviceOnly
180+
vertexCount
181+
Dense
182+
(Some(
183+
(1.0f - Constants.PageRank.alpha)
184+
/ (float32 vertexCount)
185+
))
173186

174187
let mutable error = accuracy + 0.1f
175188

@@ -178,7 +191,7 @@ module internal PageRank =
178191
while error > accuracy do
179192
i <- i + 1
180193

181-
// rank = matrix*rank + (1 - alpha)/N
194+
// rank = matrix*rank + (1 - ALPHA)/N
182195
spMVTo queue matrix prevRank rank
183196
addToResult queue rank addition
184197

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GraphBLAS.FSharp.Backend.Algorithms
2+
3+
open Brahma.FSharp
4+
open GraphBLAS.FSharp.Objects
5+
6+
module PageRank =
7+
[<Sealed>]
8+
type PageRankMatrix =
9+
member Dispose : MailboxProcessor<Msg> -> unit
10+
11+
val internal prepareMatrix : ClContext -> int -> (MailboxProcessor<Msg> -> ClMatrix<float32> -> PageRankMatrix)
12+
13+
val internal run : ClContext -> int -> (MailboxProcessor<Msg> -> PageRankMatrix -> float32 -> ClVector<float32>)

src/GraphBLAS-sharp.Backend/Common/Utils.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace GraphBLAS.FSharp.Backend.Common
22

33
module internal Utils =
4-
let defaultWorkGroupSize = 32
54

65
let floorToPower2 =
76
fun x -> x ||| (x >>> 1)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace GraphBLAS.FSharp
2+
3+
[<RequireQualifiedAccess>]
4+
module Constants =
5+
module PageRank =
6+
/// <summary>
7+
/// PageRank algorithms will finish then
8+
/// difference of current and previous vectors
9+
/// is less than accuracy
10+
/// </summary>
11+
let accuracy = 1e-6f
12+
/// <summary>
13+
/// Damping factor for PageRank algorithm
14+
/// </summary>
15+
let alpha = 0.85f
16+
17+
module Common =
18+
let defaultWorkGroupSize = 32

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<PropertyGroup>
@@ -11,14 +11,14 @@
1111

1212
<ItemGroup>
1313
<Compile Include="AssemblyInfo.fs" />
14+
<Compile Include="Constants/Constants.fs" />
1415
<Compile Include="Objects/Common.fs" />
1516
<Compile Include="Objects/ArraysExtentions.fs" />
1617
<Compile Include="Objects/Vector.fs" />
1718
<Compile Include="Objects/Matrix.fs" />
1819
<Compile Include="Objects/AtLeastOne.fs" />
1920
<Compile Include="Objects/ClContextExtensions.fs" />
2021
<Compile Include="Objects/ClCellExtensions.fs" />
21-
2222
<Compile Include="Quotes/SubReduce.fs" />
2323
<Compile Include="Quotes/Arithmetic.fs" />
2424
<Compile Include="Quotes/Convert.fs" />
@@ -28,7 +28,6 @@
2828
<Compile Include="Quotes/Predicates.fs" />
2929
<Compile Include="Quotes/Map.fs" />
3030
<Compile Include="Quotes/Search.fs" />
31-
3231
<Compile Include="Common/Scatter.fs" />
3332
<Compile Include="Common/Utils.fs" />
3433
<Compile Include="Common/PrefixSum.fs" />
@@ -39,9 +38,7 @@
3938
<Compile Include="Common/ClArray.fs" />
4039
<Compile Include="Common/Sort/Radix.fs" />
4140
<Compile Include="Common/Sort/Bitonic.fs" />
42-
4341
<Compile Include="Common/Common.fs" />
44-
4542
<Compile Include="Vector/Dense/Vector.fs" />
4643
<Compile Include="Vector/Sparse/Common.fs" />
4744
<Compile Include="Vector/Sparse/Merge.fs" />
@@ -61,19 +58,18 @@
6158
<Compile Include="Matrix/CSR/Matrix.fs" />
6259
<Compile Include="Matrix/LIL/Matrix.fs" />
6360
<Compile Include="Matrix/Matrix.fs" />
64-
<Compile Include="Operations/SpGeMM/Expand.fs" />
61+
<Compile Include="Operations/SpGeMM/Expand.fs" />
6562
<Compile Include="Operations/SpGeMM/Masked.fs" />
6663
<Compile Include="Operations/SpMV.fs" />
67-
<Compile Include="Operations/SpMSpV.fs" />
64+
<Compile Include="Operations/SpMSpV.fs" />
6865
<Compile Include="Operations/Kronecker.fs" />
6966
<Compile Include="Operations/Operations.fs" />
70-
7167
<Compile Include="Algorithms/BFS.fs" />
7268
<Compile Include="Algorithms/MSBFS.fs" />
7369
<Compile Include="Algorithms/SSSP.fs" />
74-
<Compile Include="Algorithms/PageRank.fs" />
70+
<Compile Include="Algorithms/PageRank.fsi" />
71+
<Compile Include="Algorithms/PageRank.fs" />
7572
<Compile Include="Algorithms/Algorithms.fs" />
76-
7773
</ItemGroup>
7874
<Import Project="..\..\.paket\Paket.Restore.targets" />
7975
</Project>

src/GraphBLAS-sharp.Backend/Objects/Matrix.fs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -181,36 +181,3 @@ type ClMatrix<'a when 'a: struct> =
181181
| ClMatrix.COO matrix -> matrix.NNZ
182182
| ClMatrix.CSC matrix -> matrix.NNZ
183183
| ClMatrix.LIL matrix -> matrix.NNZ
184-
185-
/// <summary>
186-
/// Represents an abstraction over matrix, which is converted to correct format for PageRank algorithm
187-
/// </summary>
188-
type PageRankMatrix<'a when 'a: struct> =
189-
| PageRankMatrix of ClMatrix<'a>
190-
/// <summary>
191-
/// Gets the number of rows in matrix.
192-
/// </summary>
193-
member this.RowCount =
194-
match this with
195-
| PageRankMatrix matrix -> matrix.RowCount
196-
197-
/// <summary>
198-
/// Gets the number of columns in matrix.
199-
/// </summary>
200-
member this.ColumnCount =
201-
match this with
202-
| PageRankMatrix matrix -> matrix.ColumnCount
203-
204-
/// <summary>
205-
/// Release device resources allocated for the matrix.
206-
/// </summary>
207-
member this.Dispose q =
208-
match this with
209-
| PageRankMatrix matrix -> matrix.Dispose q
210-
211-
/// <summary>
212-
/// Gets the number of non-zero elements in matrix.
213-
/// </summary>
214-
member this.NNZ =
215-
match this with
216-
| PageRankMatrix matrix -> matrix.NNZ

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let testFixtures (testContext: TestContext) =
1414
[ let config = Utils.undirectedAlgoConfig
1515
let context = testContext.ClContext
1616
let queue = testContext.Queue
17-
let workGroupSize = Utils.defaultWorkGroupSize
17+
let workGroupSize = Constants.Common.defaultWorkGroupSize
1818

1919
let testName =
2020
sprintf "Test on %A" testContext.ClContext

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ open GraphBLAS.FSharp.Objects.MatrixExtensions
1212

1313
let config = Utils.undirectedAlgoConfig
1414

15-
let workGroupSize = Utils.defaultWorkGroupSize
15+
let workGroupSize = Constants.Common.defaultWorkGroupSize
1616

1717
let makeLevelsTest context queue bfs (matrix: int [,]) =
1818
let graph = undirectedFromArray2D matrix 0

0 commit comments

Comments
 (0)