|
| 1 | +namespace GraphBLAS.FSharp.Benchmarks |
| 2 | + |
| 3 | +open GraphBLAS.FSharp |
| 4 | +open GraphBLAS.FSharp.Algorithms |
| 5 | +open BenchmarkDotNet.Attributes |
| 6 | +open BenchmarkDotNet.Configs |
| 7 | +open BenchmarkDotNet.Columns |
| 8 | +open BenchmarkDotNet.Engines |
| 9 | +open System.IO |
| 10 | +open System |
| 11 | +open MatrixBackend |
| 12 | +open GraphBLAS.FSharp.Predefined |
| 13 | +open MathNet.Numerics |
| 14 | +open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic |
| 15 | +open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation |
| 16 | + |
| 17 | +type COOFormat<'a> = |
| 18 | + { Rows: int [] |
| 19 | + Columns: int [] |
| 20 | + Values: 'a [] |
| 21 | + RowCount: int |
| 22 | + ColumnCount: int } |
| 23 | + |
| 24 | +[<MinColumn; MaxColumn>] |
| 25 | +[<Config(typeof<Config>)>] |
| 26 | +[<SimpleJob(RunStrategy.Throughput)>] |
| 27 | +type EWiseAddBenchmarks() = |
| 28 | + let mutable leftCOO = Unchecked.defaultof<Matrix<float>> |
| 29 | + let mutable rightCOO = Unchecked.defaultof<Matrix<float>> |
| 30 | + |
| 31 | + let mutable leftCSR = Unchecked.defaultof<Matrix<float>> |
| 32 | + let mutable rightCSR = Unchecked.defaultof<Matrix<float>> |
| 33 | + |
| 34 | + let mutable leftMathNetSparse = |
| 35 | + Unchecked.defaultof<LinearAlgebra.Matrix<float>> |
| 36 | + |
| 37 | + let mutable rightMathNetSparse = |
| 38 | + Unchecked.defaultof<LinearAlgebra.Matrix<float>> |
| 39 | + |
| 40 | + let mutable firstGraph = Unchecked.defaultof<COOFormat<float>> |
| 41 | + let mutable secondGraph = Unchecked.defaultof<COOFormat<float>> |
| 42 | + |
| 43 | + [<ParamsSource("GraphPaths")>] |
| 44 | + member val PathToGraph = Unchecked.defaultof<string * string> with get, set |
| 45 | + |
| 46 | + [<GlobalSetup>] |
| 47 | + member this.ReadMatrices() = |
| 48 | + let getCOO(pathToGraph: string) = |
| 49 | + use streamReader = new StreamReader(pathToGraph) |
| 50 | + |
| 51 | + while streamReader.Peek() = int '%' do |
| 52 | + streamReader.ReadLine() |> ignore |
| 53 | + |
| 54 | + let matrixInfo = |
| 55 | + streamReader.ReadLine().Split(' ') |
| 56 | + |> Array.map int |
| 57 | + |
| 58 | + let (nrows, ncols, nnz) = |
| 59 | + matrixInfo.[0], matrixInfo.[1], matrixInfo.[2] |
| 60 | + |
| 61 | + [ 0 .. nnz - 1 ] |
| 62 | + |> List.map |
| 63 | + (fun _ -> |
| 64 | + streamReader.ReadLine().Split(' ') |
| 65 | + |> (fun line -> int line.[0], int line.[1], float line.[2])) |
| 66 | + |> List.toArray |
| 67 | + |> Array.unzip3 |
| 68 | + |> fun (rows, cols, values) -> |
| 69 | + { Rows = rows |
| 70 | + Columns = cols |
| 71 | + Values = values |
| 72 | + RowCount = nrows |
| 73 | + ColumnCount = ncols } |
| 74 | + |
| 75 | + firstGraph <- fst this.PathToGraph |> getCOO |
| 76 | + secondGraph <- snd this.PathToGraph |> getCOO |
| 77 | + |
| 78 | + [<IterationSetup(Target = "EWiseAdditionCOO")>] |
| 79 | + member this.BuildCOO() = |
| 80 | + leftCOO <- |
| 81 | + Matrix.Build<float>( |
| 82 | + firstGraph.RowCount, |
| 83 | + firstGraph.ColumnCount, |
| 84 | + firstGraph.Rows, |
| 85 | + firstGraph.Columns, |
| 86 | + firstGraph.Values, |
| 87 | + COO |
| 88 | + ) |
| 89 | + |
| 90 | + rightCOO <- |
| 91 | + Matrix.Build<float>( |
| 92 | + secondGraph.RowCount, |
| 93 | + secondGraph.ColumnCount, |
| 94 | + secondGraph.Rows, |
| 95 | + secondGraph.Columns, |
| 96 | + secondGraph.Values, |
| 97 | + COO |
| 98 | + ) |
| 99 | + |
| 100 | + [<IterationSetup(Target = "EWiseAdditionCSR")>] |
| 101 | + member this.BuildCSR() = |
| 102 | + leftCSR <- |
| 103 | + Matrix.Build<float>( |
| 104 | + firstGraph.RowCount, |
| 105 | + firstGraph.ColumnCount, |
| 106 | + firstGraph.Rows, |
| 107 | + firstGraph.Columns, |
| 108 | + firstGraph.Values, |
| 109 | + CSR |
| 110 | + ) |
| 111 | + |
| 112 | + rightCSR <- |
| 113 | + Matrix.Build<float>( |
| 114 | + secondGraph.RowCount, |
| 115 | + secondGraph.ColumnCount, |
| 116 | + secondGraph.Rows, |
| 117 | + secondGraph.Columns, |
| 118 | + secondGraph.Values, |
| 119 | + CSR |
| 120 | + ) |
| 121 | + |
| 122 | + [<IterationSetup(Target = "EWiseAdditionMathNetSparse")>] |
| 123 | + member this.BuildMathNetSparse() = |
| 124 | + leftMathNetSparse <- |
| 125 | + LinearAlgebra.SparseMatrix.ofListi |
| 126 | + firstGraph.RowCount |
| 127 | + firstGraph.ColumnCount |
| 128 | + (List.ofArray <| Array.zip3 firstGraph.Rows firstGraph.Columns firstGraph.Values) |
| 129 | + |
| 130 | + rightMathNetSparse <- |
| 131 | + LinearAlgebra.SparseMatrix.ofListi |
| 132 | + secondGraph.RowCount |
| 133 | + secondGraph.ColumnCount |
| 134 | + (List.ofArray <| Array.zip3 secondGraph.Rows secondGraph.Columns secondGraph.Values) |
| 135 | + |
| 136 | + [<Benchmark>] |
| 137 | + member this.EWiseAdditionCOO() = |
| 138 | + leftCOO.EWiseAdd rightCOO None FloatSemiring.addMult |
| 139 | + |> oclContext.RunSync |
| 140 | + |
| 141 | + [<Benchmark>] |
| 142 | + member this.EWiseAdditionCSR() = |
| 143 | + leftCSR.EWiseAdd rightCOO None FloatSemiring.addMult |
| 144 | + |> oclContext.RunSync |
| 145 | + |
| 146 | + [<Benchmark>] |
| 147 | + member this.EWiseAdditionMathNetSparse() = leftMathNetSparse + rightMathNetSparse |
| 148 | + |
| 149 | + /// Sequence of paths to files where data for benchmarking will be taken from |
| 150 | + static member GraphPaths = |
| 151 | + seq { |
| 152 | + let getPathToFile filename = |
| 153 | + Path.Join [| "Datasets" |
| 154 | + "EWiseAddDatasets" |
| 155 | + filename |] |
| 156 | + |
| 157 | + let map f (x, y) = f x, f y |
| 158 | + |
| 159 | + // data filenames |
| 160 | + yield ("", "") |> map getPathToFile |
| 161 | + } |
0 commit comments