|
| 1 | +namespace GraphBLAS.FSharp.Benchmarks |
| 2 | + |
| 3 | +open System.IO |
| 4 | +open System.Text.RegularExpressions |
| 5 | +open GraphBLAS.FSharp |
| 6 | +open GraphBLAS.FSharp.IO |
| 7 | +open BenchmarkDotNet.Attributes |
| 8 | +open BenchmarkDotNet.Configs |
| 9 | +open BenchmarkDotNet.Columns |
| 10 | +open MathNet.Numerics.LinearAlgebra |
| 11 | +open MathNet.Numerics |
| 12 | +open Brahma.FSharp.OpenCL |
| 13 | +open Microsoft.FSharp.Core |
| 14 | +open OpenCL.Net |
| 15 | + |
| 16 | +[<AbstractClass>] |
| 17 | +[<IterationCount(100)>] |
| 18 | +[<WarmupCount(10)>] |
| 19 | +[<Config(typeof<Config>)>] |
| 20 | +type MathNETBenchmark<'elem when 'elem: struct and 'elem :> System.IEquatable<'elem> and 'elem :> System.IFormattable and 'elem :> System.ValueType and 'elem: (new : |
| 21 | + unit -> 'elem)>(converter: string -> 'elem, converterBool) = |
| 22 | + do Control.UseNativeMKL() |
| 23 | + |
| 24 | + static member COOMatrixToMathNETSparse matrix = |
| 25 | + match matrix with |
| 26 | + | MatrixCOO matrix -> |
| 27 | + Matrix.Build.SparseFromCoordinateFormat( |
| 28 | + matrix.RowCount, |
| 29 | + matrix.ColumnCount, |
| 30 | + matrix.Values.Length, |
| 31 | + matrix.Rows, |
| 32 | + matrix.Columns, |
| 33 | + matrix.Values |
| 34 | + ) |
| 35 | + | _ -> failwith "Unsupported" |
| 36 | + |
| 37 | + member this.ReadMatrix(reader: MtxReader) = |
| 38 | + let converter = |
| 39 | + match reader.Field with |
| 40 | + | Pattern -> converterBool |
| 41 | + | _ -> converter |
| 42 | + |
| 43 | + let gbMatrix = reader.ReadMatrix converter |
| 44 | + MathNETBenchmark<_>.COOMatrixToMathNETSparse gbMatrix |
| 45 | + |
| 46 | + abstract member GlobalSetup : unit -> unit |
| 47 | + |
| 48 | + abstract member IterationCleanup : unit -> unit |
| 49 | + |
| 50 | + abstract member Benchmark : unit -> unit |
| 51 | + |
| 52 | +type BinOpMathNETBenchmark<'elem when 'elem: struct and 'elem :> System.IEquatable<'elem> and 'elem :> System.IFormattable and 'elem :> System.ValueType and 'elem: (new : |
| 53 | + unit -> 'elem)>(funToBenchmark, converter: string -> 'elem, converterBool) = |
| 54 | + inherit MathNETBenchmark<'elem>(converter, converterBool) |
| 55 | + |
| 56 | + let mutable firstMatrix = Unchecked.defaultof<Matrix<'elem>> |
| 57 | + let mutable secondMatrix = Unchecked.defaultof<Matrix<'elem>> |
| 58 | + |
| 59 | + member val ResultMatrix = Unchecked.defaultof<Matrix<'elem>> with get, set |
| 60 | + |
| 61 | + [<ParamsSource("InputMatricesProvider")>] |
| 62 | + member val InputMatrixReader = Unchecked.defaultof<MtxReader * MtxReader> with get, set |
| 63 | + |
| 64 | + static member InputMatricesProviderBuilder pathToConfig = |
| 65 | + let datasetFolder = "MathNET" |
| 66 | + |
| 67 | + pathToConfig |
| 68 | + |> Utils.getMatricesFilenames |
| 69 | + |> Seq.map |
| 70 | + (fun matrixFilename -> |
| 71 | + printfn "%A" matrixFilename |
| 72 | + |
| 73 | + match Path.GetExtension matrixFilename with |
| 74 | + | ".mtx" -> |
| 75 | + MtxReader(Utils.getFullPathToMatrix datasetFolder matrixFilename), |
| 76 | + MtxReader(Utils.getFullPathToMatrix datasetFolder ("squared_" + matrixFilename)) |
| 77 | + | _ -> failwith "Unsupported matrix format") |
| 78 | + |
| 79 | + member this.ReadMatrices() = |
| 80 | + let leftMatrixReader = fst this.InputMatrixReader |
| 81 | + let rightMatrixReader = snd this.InputMatrixReader |
| 82 | + firstMatrix <- this.ReadMatrix leftMatrixReader |
| 83 | + secondMatrix <- this.ReadMatrix rightMatrixReader |
| 84 | + |
| 85 | + [<GlobalSetup>] |
| 86 | + override this.GlobalSetup() = this.ReadMatrices() |
| 87 | + |
| 88 | + [<IterationCleanup>] |
| 89 | + override this.IterationCleanup() = |
| 90 | + this.ResultMatrix <- Unchecked.defaultof<Matrix<'elem>> |
| 91 | + |
| 92 | + [<Benchmark>] |
| 93 | + override this.Benchmark() = |
| 94 | + this.ResultMatrix <- funToBenchmark firstMatrix secondMatrix |
| 95 | + |
| 96 | +type EWiseAddMathNETBenchmarkFloat32() = |
| 97 | + |
| 98 | + inherit BinOpMathNETBenchmark<float32>((+), float32, (fun _ -> Utils.nextSingle (System.Random()))) |
| 99 | + |
| 100 | + static member InputMatricesProvider = |
| 101 | + BinOpMathNETBenchmark<_>.InputMatricesProviderBuilder "EWiseAddMathNETBenchmarks4Float32.txt" |
0 commit comments