Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions QuadTree.Benchmark/BFS.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ type Benchmark() =

let mutable matrix = Unchecked.defaultof<Matrix.SparseMatrix<double>>


[<Params("494_bus.mtx", "arc130.mtx")>]
member val MatrixName = "" with get, set

[<GlobalSetup>]
member this.LoadMatrix() =
matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false
matrix <-
match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with
| Ok m -> m
| Error msg -> failwith $"Failed to load matrix {this.MatrixName}: {msg}"

[<Benchmark>]
member this.BFS() =
let startVertices =
let startVerticesResult =
Vector.CoordinateList((uint64 matrix.ncols) * 1UL<Vector.dataLength>, [ 0UL<Vector.index>, 1UL ])
|> Vector.fromCoordinateList

let startVertices =
match startVerticesResult with
| Ok v -> v
| Error msg -> failwith $"Failed to create start vertices: {msg}"

Graph.BFS.bfs_level matrix startVertices
53 changes: 53 additions & 0 deletions QuadTree.Benchmark/Kronecker.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace QuadTree.Benchmarks.Kronecker

open System
open BenchmarkDotNet.Attributes
open QuadTree.Benchmarks.Utils

[<Config(typeof<MyConfig>)>]
[<MemoryDiagnoser>]
type Benchmark() =

[<Params(100, 150)>]
member val SizeA = 0 with get, set

[<Params(100, 150, 200)>]
member val SizeB = 0 with get, set

[<Params(0.005, 0.01, 0.05, 0.1)>]
member val DensityB = 0.0 with get, set

[<Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)>]
member val Seed = 0 with get, set

member val A = Unchecked.defaultof<Matrix.SparseMatrix<double>> with get, set
member val B = Unchecked.defaultof<Matrix.SparseMatrix<double>> with get, set

member private this.GenerateMatrix(size: int, density: float, rng: Random) =
let coords =
[ for i in 0 .. size - 1 do
for j in 0 .. size - 1 do
if rng.NextDouble() < density then
let value = double (rng.Next(1, 4))
yield (uint64 i * 1UL<Matrix.rowindex>, uint64 j * 1UL<Matrix.colindex>, value) ]

match
Matrix.fromCoordinateList (
Matrix.CoordinateList(uint64 size * 1UL<Matrix.nrows>, uint64 size * 1UL<Matrix.ncols>, coords)
)
with
| Ok m -> m
| Error msg -> failwithf "Failed to create matrix: %s" msg

[<GlobalSetup>]
member this.Setup() =
let rng = Random(this.Seed)
this.A <- this.GenerateMatrix(this.SizeA, 0.01, rng)
this.B <- this.GenerateMatrix(this.SizeB, this.DensityB, rng)

[<Benchmark>]
member this.Kronecker() =
match Matrix.kroneckerProduct this.A this.B (fun a b -> Some(a * b)) with
| Ok res -> res
| Error msg -> failwithf "Kronecker failed: %s" msg
|> ignore
6 changes: 5 additions & 1 deletion QuadTree.Benchmark/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ let main argv =
BenchmarkSwitcher
[| typeof<QuadTree.Benchmarks.BFS.Benchmark>
typeof<QuadTree.Benchmarks.SSSP.Benchmark>
typeof<QuadTree.Benchmarks.Triangles.Benchmark> |]
typeof<QuadTree.Benchmarks.Triangles.Benchmark>
typeof<QuadTree.Benchmarks.ReduceComparison.Benchmark>
typeof<QuadTree.Benchmarks.VectorSlice.Benchmark>
typeof<QuadTree.Benchmarks.MatrixSlice.Benchmark>
typeof<QuadTree.Benchmarks.Kronecker.Benchmark> |]

benchmarks.Run argv |> ignore
0
59 changes: 59 additions & 0 deletions QuadTree.Benchmark/MatrixSlice.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace QuadTree.Benchmarks.MatrixSlice

open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Jobs
open QuadTree.Benchmarks.Utils

type RealConfig() =
inherit ManualConfig()
do base.AddJob(Job.Default.WithWarmupCount(5).WithIterationCount(10)) |> ignore

[<Config(typeof<RealConfig>)>]
[<MemoryDiagnoser>]
type Benchmark() =

[<Params(1000, 2000, 3000, 4000, 5000)>]
member val Size = 0 with get, set

[<Params(0.001, 0.005, 0.01, 0.05, 0.1, 0.5)>]
member val Density = 0.0 with get, set

[<Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)>]
member val Seed = 0 with get, set

member val Matrix = Unchecked.defaultof<Matrix.SparseMatrix<double>> with get, set

member private this.GenerateMatrix(size: int, density: float, rng: Random) =
let coords =
[ for i in 0 .. size - 1 do
for j in 0 .. size - 1 do
if rng.NextDouble() < density then
let value = double (rng.Next(1, 4))
yield (uint64 i * 1UL<Matrix.rowindex>, uint64 j * 1UL<Matrix.colindex>, value) ]

match
Matrix.fromCoordinateList (
Matrix.CoordinateList(uint64 size * 1UL<Matrix.nrows>, uint64 size * 1UL<Matrix.ncols>, coords)
)
with
| Ok m -> m
| Error msg -> failwithf "Failed to create matrix: %s" msg

[<GlobalSetup>]
member this.Setup() =
let rng = Random(this.Seed)
this.Matrix <- this.GenerateMatrix(this.Size, this.Density, rng)

member private this.SliceMiddle(m: Matrix.SparseMatrix<double>) =
let n = int m.nrows
let start = n / 4
let last = 3 * n / 4 - 1

match Matrix.slice m start last start last with
| Ok res -> res
| Error msg -> failwithf "Slice failed: %s" msg

[<Benchmark>]
member this.Slice() = this.SliceMiddle(this.Matrix) |> ignore
6 changes: 5 additions & 1 deletion QuadTree.Benchmark/QuadTree.Benchmark.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<Compile Include="BFS.fs"/>
<Compile Include="SSSP.fs"/>
<Compile Include="Triangles.fs"/>
<Compile Include="ReduceComparison.fs"/>
<Compile Include="VectorSlice.fs"/>
<Compile Include="MatrixSlice.fs"/>
<Compile Include="Kronecker.fs"/>
<Compile Include="Main.fs"/>
</ItemGroup>

Expand All @@ -22,4 +26,4 @@
<ProjectReference Include="..\QuadTree\QuadTree.fsproj" />
</ItemGroup>

</Project>
</Project>
110 changes: 110 additions & 0 deletions QuadTree.Benchmark/ReduceComparison.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
namespace QuadTree.Benchmarks.ReduceComparison

open System
open System.IO
open BenchmarkDotNet.Attributes
open QuadTree.Benchmarks.Utils

[<Config(typeof<MyConfig>)>]
[<MemoryDiagnoser>]
type Benchmark() =

let add x y =
match x, y with
| Some a, Some b -> Some(a + b)
| Some a, None
| None, Some a -> Some a
| _ -> None

[<Params("g7jac010sc",
"g7jac020",
"g7jac020sc",
"g7jac040",
"g7jac040sc",
"g7jac050sc",
"g7jac060",
"g7jac060sc",
"g7jac080",
"g7jac100",
"g7jac100sc",
"g7jac120",
"g7jac120sc",
"g7jac140",
"g7jac140sc",
"g7jac160",
"jan99jac020",
"jan99jac020sc",
"mark3jac020",
"mark3jac020sc",
"mesh2e1",
"mesh3em5",
"pwt",
"shuttle_eddy",
"tandem_vtx",
"bcsstk01",
"cavity01",
"cavity05",
"cavity10",
"email-Eu-core")>]
member val MatrixName = "" with get, set

member val Matrix = Unchecked.defaultof<Matrix.SparseMatrix<double>> with get, set
member val Size = 0 with get, set
member val Density = 0.0 with get, set
member val IsSymmetric = false with get, set

member private this.CheckSymmetric(m: Matrix.SparseMatrix<double>) =
let coo = Matrix.toCoordinateList m
let dict = System.Collections.Generic.Dictionary<string, double>()

for (i, j, v) in coo.list do
let key = $"{uint64 i},{uint64 j}"
dict.[key] <- v

let mutable sym = true

for (i, j, v) in coo.list do
let key = $"{uint64 j},{uint64 i}"

match dict.TryGetValue(key) with
| true, v2 when v = v2 -> ()
| _ -> sym <- false

sym

[<GlobalSetup>]
member this.Setup() =
let rec findProjectRoot (dir: string) =
if Directory.Exists(Path.Combine(dir, "data")) then
dir
else
let parent = Directory.GetParent(dir)

if parent = null then
failwith "Не найден корень проекта (папка data)"
else
findProjectRoot parent.FullName

let projectRoot = findProjectRoot __SOURCE_DIRECTORY__

let path =
Path.Combine(projectRoot, "data", "Reduce_matrices", $"{this.MatrixName}.mtx")

if not (File.Exists path) then
failwithf "Файл не найден: %s\nИщем в: %s" path projectRoot

match QuadTree.Benchmarks.Utils.readMtx path false with
| Ok m ->
this.Matrix <- m
this.Size <- int m.nrows
this.Density <- float m.nvals / (float m.nrows * float m.ncols)
this.IsSymmetric <- this.CheckSymmetric(m)
| Error msg -> failwithf "Не удалось загрузить %s: %s" this.MatrixName msg

[<Benchmark>]
member this.ReduceCols_Original() = Matrix.reduceCols add this.Matrix

[<Benchmark>]
member this.ReduceCols_ViaTranspose() =
let transposed = Matrix.transpose this.Matrix
Matrix.reduceRows add transposed
6 changes: 4 additions & 2 deletions QuadTree.Benchmark/SSSP.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ open QuadTree.Benchmarks.Utils
type Benchmark() =
let mutable matrix = Unchecked.defaultof<Matrix.SparseMatrix<double>>


[<Params("494_bus.mtx", "arc130.mtx")>]
member val MatrixName = "" with get, set

[<GlobalSetup>]
member this.LoadMatrix() =
matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false
matrix <-
match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with
| Ok m -> m

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну и так про все бенчи. Тут уже лучше ронять, чтобы было видно, что что-то пошло не так.

| Error e -> failwith $"Failed to load matrix {this.MatrixName}: {e}"

[<Benchmark>]
member this.SSSP() = Graph.SSSP.sssp matrix 0UL
5 changes: 4 additions & 1 deletion QuadTree.Benchmark/Triangles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ type Benchmark() =

[<GlobalSetup>]
member this.LoadMatrix() =
matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false
matrix <-
match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with
| Ok m -> m
| Error msg -> failwith $"Failed to load matrix {this.MatrixName}: {msg}"

[<Benchmark>]
member this.TriangleCount() =
Expand Down
54 changes: 54 additions & 0 deletions QuadTree.Benchmark/VectorSlice.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace QuadTree.Benchmarks.VectorSlice

open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Jobs
open QuadTree.Benchmarks.Utils

type RealConfig() =
inherit ManualConfig()
do base.AddJob(Job.Default.WithWarmupCount(5).WithIterationCount(10)) |> ignore

[<Config(typeof<RealConfig>)>]
[<MemoryDiagnoser>]
type Benchmark() =

[<Params(1000000, 2000000, 2500000, 4000000, 5000000, 7500000)>]
member val Size = 0 with get, set

[<Params(0.005, 0.01, 0.05, 0.1, 0.5)>]
member val Density = 0.0 with get, set

[<Params(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)>]
member val Seed = 0 with get, set

member val Vector = Unchecked.defaultof<Vector.SparseVector<double>> with get, set

member private this.GenerateVector(size: int, density: float, rng: Random) =
let coords =
[ for i in 0 .. size - 1 do
if rng.NextDouble() < density then
let value = double (rng.Next(1, 4))
yield (uint64 i * 1UL<Vector.index>, value) ]

match Vector.fromCoordinateList (Vector.CoordinateList(uint64 size * 1UL<Vector.dataLength>, coords)) with
| Ok v -> v
| Error msg -> failwithf "Failed to create vector: %s" msg

[<GlobalSetup>]
member this.Setup() =
let rng = Random(this.Seed)
this.Vector <- this.GenerateVector(this.Size, this.Density, rng)

member private this.SliceMiddle(v: Vector.SparseVector<double>) =
let n = int v.length
let start = n / 4
let last = 3 * n / 4 - 1

match Vector.slice start last v with
| Ok res -> res
| Error msg -> failwithf "Slice failed: %s" msg

[<Benchmark>]
member this.Slice() = this.SliceMiddle(this.Vector) |> ignore
2 changes: 1 addition & 1 deletion QuadTree.Tests/QuadTree.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Compile Include="Tests.BFS.fs" />
<Compile Include="Tests.TriangleCount.fs" />
<Compile Include="Tests.SSSP.fs" />
<Compile Include="Tests.Boruvka.fs" />
<Compile Include="Tests.MST.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading