-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebdb250
commit 6f3819b
Showing
13 changed files
with
164 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/GraphBLAS-sharp.Tests/Backend/Algorithms/PageRank.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
module GraphBLAS.FSharp.Tests.Backend.Algorithms.PageRank | ||
|
||
open Expecto | ||
open GraphBLAS.FSharp | ||
open GraphBLAS.FSharp.Tests | ||
open GraphBLAS.FSharp.Tests.Context | ||
open GraphBLAS.FSharp.Objects.ClVectorExtensions | ||
open GraphBLAS.FSharp.Objects | ||
|
||
let private alpha = 0.85f | ||
let private accuracy = 0.00001f | ||
|
||
let prepareNaive (matrix: float32 [,]) = | ||
let result = Array2D.copy matrix | ||
let rowCount = Array2D.length1 matrix | ||
let outDegrees = Array.zeroCreate rowCount | ||
|
||
//Count degree | ||
Array2D.iteri (fun r c v -> outDegrees.[r] <- outDegrees.[r] + (if v <> 0f then 1f else 0f)) matrix | ||
|
||
//Set value | ||
Array2D.iteri | ||
(fun r c v -> | ||
result.[r, c] <- | ||
if v <> 0f then | ||
alpha / outDegrees.[r] | ||
else | ||
0f) | ||
matrix | ||
|
||
//Transpose | ||
Array2D.iteri | ||
(fun r c _ -> | ||
if r > c then | ||
let temp = result.[r, c] | ||
result.[r, c] <- result.[c, r] | ||
result.[c, r] <- temp) | ||
matrix | ||
|
||
result | ||
|
||
let pageRankNaive (matrix: float32 [,]) = | ||
let rowCount = Array2D.length1 matrix | ||
let mutable result = Array.zeroCreate rowCount | ||
|
||
let mutable prev = | ||
Array.create rowCount (1f / (float32 rowCount)) | ||
|
||
let mutable error = accuracy + 1f | ||
let addConst = (1f - alpha) / (float32 rowCount) | ||
|
||
while (error > accuracy) do | ||
for r in 0 .. rowCount - 1 do | ||
result.[r] <- 0f | ||
|
||
for c in 0 .. rowCount - 1 do | ||
result.[r] <- result.[r] + matrix.[r, c] * prev.[c] | ||
|
||
result.[r] <- result.[r] + addConst | ||
|
||
error <- | ||
sqrt | ||
<| Array.fold2 (fun e x1 x2 -> e + (x1 - x2) * (x1 - x2)) 0f result prev | ||
|
||
let temp = result | ||
result <- prev | ||
prev <- temp | ||
|
||
prev | ||
|
||
let testFixtures (testContext: TestContext) = | ||
[ let config = Utils.undirectedAlgoConfig | ||
let context = testContext.ClContext | ||
let queue = testContext.Queue | ||
let workGroupSize = Utils.defaultWorkGroupSize | ||
|
||
let testName = | ||
sprintf "Test on %A" testContext.ClContext | ||
|
||
let pageRank = | ||
Algorithms.PageRank.run context workGroupSize | ||
|
||
testPropertyWithConfig config testName | ||
<| fun (matrix: float32 [,]) -> | ||
let matrixHost = | ||
Utils.createMatrixFromArray2D CSR matrix ((=) 0f) | ||
|
||
if matrixHost.NNZ > 0 then | ||
let preparedMatrixExpected = prepareNaive matrix | ||
|
||
let expected = pageRankNaive preparedMatrixExpected | ||
|
||
let matrix = matrixHost.ToDevice context | ||
|
||
let preparedMatrix = | ||
Algorithms.PageRank.prepareMatrix context workGroupSize queue matrix | ||
|
||
let res = pageRank queue preparedMatrix accuracy | ||
|
||
let resHost = res.ToHost queue | ||
|
||
preparedMatrix.Dispose queue | ||
matrix.Dispose queue | ||
res.Dispose queue | ||
|
||
match resHost with | ||
| Vector.Dense resHost -> | ||
let actual = resHost |> Utils.unwrapOptionArray 0f | ||
|
||
for i in 0 .. actual.Length - 1 do | ||
Expect.isTrue | ||
((abs (actual.[i] - expected.[i])) < accuracy) | ||
(sprintf "Values should be equal. Expected %A, actual %A" expected.[i] actual.[i]) | ||
|
||
| _ -> failwith "Not implemented" ] | ||
|
||
let tests = | ||
TestCases.gpuTests "PageRank tests" testFixtures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters