Skip to content

Commit a12d7df

Browse files
committed
Add vxm correctness test
1 parent 0e6e92e commit a12d7df

9 files changed

Lines changed: 193 additions & 100 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BfsBenchmark.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ open BenchmarkDotNet.Configs
77
open BenchmarkDotNet.Columns
88
open System.IO
99
open System
10+
open MatrixBackend
1011

1112
[<Config(typeof<Config>)>]
1213
[<SimpleJob(targetCount=10)>]
13-
type BfsBenchmark() =
14+
type BFSBenchmark4CSRMatrix() =
1415
let random = Random()
1516

1617
let mutable matrix = Unchecked.defaultof<Matrix<bool>>
@@ -20,12 +21,12 @@ type BfsBenchmark() =
2021
member val PathToGraph = "" with get, set
2122

2223
[<GlobalSetup>]
23-
member this.BuildMatrix () =
24-
matrix <- Matrix.Build<bool> this.PathToGraph
24+
member this.BuildMatrix() =
25+
matrix <- Matrix.Build<bool>(this.PathToGraph, CSR)
2526
source <- random.Next matrix.RowCount
2627

2728
[<Benchmark>]
28-
member this.LevelBFS () =
29+
member this.LevelBFS() =
2930
levelBFS matrix source
3031

3132
/// Sequence of paths to files where data for benchmarking will be taken from

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
<Compile Include="Program.fs" />
2020
</ItemGroup>
2121
<Import Project="..\..\.paket\Paket.Restore.targets" />
22-
</Project>
22+
</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 GraphBLAS.FSharp.Benchmarks
44
[<EntryPoint>]
55
let main argv =
66
let benchmarks = BenchmarkSwitcher [|
7-
typeof<BfsBenchmark>
7+
typeof<BFSBenchmark4CSRMatrix>
88
|]
99

1010
benchmarks.Run argv |> ignore

paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ nuget FSharpx.Collections 2.1.3
2020
nuget FSharpx.Text.StructuredFormat 2.3.0
2121
nuget Brahma.FSharp.OpenCL.WorkflowBuilder
2222
nuget BenchmarkDotNet
23+
nuget MathNet.Numerics.FSharp
2324

2425
// [ FAKE GROUP ]
2526
group Build

paket.lock

Lines changed: 149 additions & 80 deletions
Large diffs are not rendered by default.

src/GraphBLAS-sharp/Matrix.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ module MatrixBackend =
1313
failwith "Not Implemented"
1414

1515
[<AutoOpen>]
16-
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
17-
module Matrix =
16+
module MatrixExtensions =
1817
open MatrixBackend
1918
type Matrix<'a when 'a : struct and 'a : equality> with
2019
static member Build(denseMatrix: 'T[,], zero: 'T) : Matrix<'T> =
@@ -26,3 +25,6 @@ module Matrix =
2625
static member ZeroCreate(rowCount: int, columnCount: int) : Matrix<'T> =
2726
Matrix.ZeroCreate(rowCount, columnCount)
2827

28+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
29+
module Matrix =
30+
let toSeq (matrix: Matrix<'a>) = failwith "Not Implemented"

src/GraphBLAS-sharp/Vector.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
namespace GraphBLAS.FSharp
22

33
[<AutoOpen>]
4-
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
5-
module Vector =
4+
module VectorExtensions =
65
type Vector<'a when 'a : struct and 'a : equality> with
76
static member Sparse(denseVector: 'T[], zero: 'T) : Vector<'T> =
87
failwith "Not Implemented"
@@ -17,3 +16,7 @@ module Vector =
1716
failwith "Not Implemented"
1817
static member ZeroDense(length: int, monoid: Monoid<'T>) : Vector<'T> =
1918
failwith "Not Implemented"
19+
20+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
21+
module Vector =
22+
let toSeq (vector: Vector<'a>) = failwith "Not Implemented"

tests/GraphBLAS-sharp.Tests/OperationsTests/VxmTests.fs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace GraphBLAS.FSharp.Tests
33
open Expecto
44
open FsCheck
55
open GraphBLAS.FSharp
6+
open MathNet.Numerics
67

78
type OperationCase = {
89
VectorCase: VectorType
@@ -11,25 +12,25 @@ type OperationCase = {
1112
}
1213

1314
type MatrixMultiplicationPair =
14-
static member DimensionGen2 () =
15+
static member DimensionGen2() =
1516
fun size ->
1617
Gen.choose (0, size |> float |> sqrt |> int)
1718
|> Gen.two
1819
|> Gen.sized
1920
|> Arb.fromGen
2021

21-
static member DimensionGen3 () =
22+
static member DimensionGen3() =
2223
fun size ->
2324
Gen.choose (0, size |> float |> sqrt |> int)
2425
|> Gen.three
2526
|> Gen.sized
2627
|> Arb.fromGen
2728

28-
static member FloatSparseMatrixVectorPair () =
29+
static member FloatSparseMatrixVectorPair() =
2930
fun size ->
3031
let floatSparseGenerator =
3132
Gen.oneof [
32-
Arb.Default.NormalFloat () |> Arb.toGen |> Gen.map float
33+
Arb.Default.NormalFloat() |> Arb.toGen |> Gen.map float
3334
Gen.constant 0.
3435
]
3536

@@ -74,8 +75,9 @@ module VxmTests =
7475
})
7576

7677
[<Tests>]
77-
let vxmTestList =
78-
testList "Vector-matrix multiplication tests" (
78+
let vxmTestsInStandardSemiring =
79+
let stdSemiring = Predefined.FloatSemiring.addMult
80+
ptestList "Float vector-matrix multiplication tests" (
7981
List.collect (fun case ->
8082
let matrixBackend =
8183
match case.MatrixCase with
@@ -94,17 +96,31 @@ module VxmTests =
9496

9597
[
9698
testPropertyWithConfig config "Dimensional mismatch should raise an exception" <|
97-
// тут просто размерности генерить и создавать пустые объекты
9899
fun matrixRowCount matrixColumnCount vectorSize ->
99100
let emptyMatrix = Matrix.ZeroCreate(matrixRowCount, matrixColumnCount, matrixBackend)
100101
let emptyVector = zeroVectorConstructor vectorSize
101102

102103
Expect.throwsT<System.ArgumentException>
103-
(fun () -> (emptyVector @. emptyMatrix) Mask1D.none Predefined.FloatSemiring.addMult |> ignore)
104-
"sss???"
104+
(fun () -> (emptyVector @. emptyMatrix) Mask1D.none stdSemiring |> ignore)
105+
(sprintf "1x%i @ %ix%i" vectorSize matrixRowCount matrixColumnCount)
105106

106107
testPropertyWithConfig config "Operation should have correct semantic" <|
107-
fun (matrix: float[,]) (vector: float[,]) -> ()
108+
fun (denseMatrix: float[,]) (denseVector: float[]) ->
109+
let matrix = Matrix.Build(denseMatrix, 0., matrixBackend)
110+
let vector = vectorConstructor denseVector
111+
let result = (vector @. matrix) Mask1D.none stdSemiring
112+
let a = LinearAlgebra.DenseMatrix.ofArray2 denseMatrix
113+
let b = LinearAlgebra.DenseVector.ofArray denseVector
114+
let c = b * a
115+
let elementWiseDifference =
116+
(result |> Vector.toSeq, c.AsArray() |> Seq.ofArray)
117+
||> Seq.zip
118+
|> Seq.map (fun (a, b) -> a - b)
119+
120+
Expect.all
121+
elementWiseDifference
122+
(fun diff -> abs diff < Accuracy.medium.absolute)
123+
(sprintf "%A @ %A = %A" vector matrix result)
108124

109125
ptestPropertyWithConfig config "Explicit zeroes after operation should be dropped" <|
110126
fun a b -> a + b = b + a

tests/GraphBLAS-sharp.Tests/paket.references

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Expecto
55
Expecto.FsCheck
66
Microsoft.NET.Test.Sdk
77
YoloDev.Expecto.TestSdk
8+
MathNet.Numerics.FSharp
89

910
dotnet-mono
1011

0 commit comments

Comments
 (0)