|
| 1 | +namespace GraphBLAS.FSharp.Benchmarks |
| 2 | + |
| 3 | +open System.IO |
| 4 | +open GraphBLAS.FSharp.Backend.Quotes |
| 5 | +open GraphBLAS.FSharp.IO |
| 6 | +open BenchmarkDotNet.Attributes |
| 7 | +open BenchmarkDotNet.Configs |
| 8 | +open BenchmarkDotNet.Columns |
| 9 | +open Brahma.FSharp |
| 10 | +open GraphBLAS.FSharp.Objects |
| 11 | +open GraphBLAS.FSharp.Backend.Objects |
| 12 | +open GraphBLAS.FSharp.Backend.Algorithms |
| 13 | +open MatrixExtensions |
| 14 | +open ArraysExtensions |
| 15 | + |
| 16 | +[<AbstractClass>] |
| 17 | +[<IterationCount(10)>] |
| 18 | +[<WarmupCount(5)>] |
| 19 | +[<Config(typeof<AlgorithmConfig>)>] |
| 20 | +type BFSBenchmarks<'matrixT, 'elem when 'matrixT :> IDeviceMemObject and 'elem : struct>( |
| 21 | + buildFunToBenchmark, |
| 22 | + converter: string -> 'elem, |
| 23 | + converterBool, |
| 24 | + buildMatrix) = |
| 25 | + |
| 26 | + let mutable funToBenchmark = None |
| 27 | + let mutable matrix = Unchecked.defaultof<'matrixT> |
| 28 | + let mutable matrixHost = Unchecked.defaultof<_> |
| 29 | + |
| 30 | + let source = 0 |
| 31 | + |
| 32 | + member val ResultVector = Unchecked.defaultof<ClArray<'elem option>> with get,set |
| 33 | + |
| 34 | + [<ParamsSource("AvaliableContexts")>] |
| 35 | + member val OclContextInfo = Unchecked.defaultof<Utils.BenchmarkContext * int> with get, set |
| 36 | + |
| 37 | + [<ParamsSource("InputMatricesProvider")>] |
| 38 | + member val InputMatrixReader = Unchecked.defaultof<MtxReader> with get, set |
| 39 | + |
| 40 | + member this.OclContext:ClContext = (fst this.OclContextInfo).ClContext |
| 41 | + member this.WorkGroupSize = snd this.OclContextInfo |
| 42 | + |
| 43 | + member this.Processor = |
| 44 | + let p = (fst this.OclContextInfo).Queue |
| 45 | + p.Error.Add(fun e -> failwithf "%A" e) |
| 46 | + p |
| 47 | + |
| 48 | + static member AvaliableContexts = Utils.avaliableContexts |
| 49 | + |
| 50 | + static member InputMatricesProviderBuilder pathToConfig = |
| 51 | + let datasetFolder = "" |
| 52 | + pathToConfig |
| 53 | + |> Utils.getMatricesFilenames |
| 54 | + |> Seq.map |
| 55 | + (fun matrixFilename -> |
| 56 | + printfn "%A" matrixFilename |
| 57 | + |
| 58 | + match Path.GetExtension matrixFilename with |
| 59 | + | ".mtx" -> |
| 60 | + MtxReader(Utils.getFullPathToMatrix datasetFolder matrixFilename) |
| 61 | + | _ -> failwith "Unsupported matrix format") |
| 62 | + |
| 63 | + member this.FunToBenchmark = |
| 64 | + match funToBenchmark with |
| 65 | + | None -> |
| 66 | + let x = buildFunToBenchmark this.OclContext this.WorkGroupSize |
| 67 | + funToBenchmark <- Some x |
| 68 | + x |
| 69 | + | Some x -> x |
| 70 | + |
| 71 | + member this.ReadMatrix (reader:MtxReader) = |
| 72 | + let converter = |
| 73 | + match reader.Field with |
| 74 | + | Pattern -> converterBool |
| 75 | + | _ -> converter |
| 76 | + |
| 77 | + reader.ReadMatrix converter |
| 78 | + |
| 79 | + member this.BFS() = |
| 80 | + this.ResultVector <- this.FunToBenchmark this.Processor matrix source |
| 81 | + |
| 82 | + member this.ClearInputMatrix() = |
| 83 | + (matrix :> IDeviceMemObject).Dispose this.Processor |
| 84 | + |
| 85 | + member this.ClearResult() = |
| 86 | + this.ResultVector.FreeAndWait this.Processor |
| 87 | + |
| 88 | + member this.ReadMatrix() = |
| 89 | + let matrixReader = this.InputMatrixReader |
| 90 | + matrixHost <- this.ReadMatrix matrixReader |
| 91 | + |
| 92 | + member this.LoadMatrixToGPU() = |
| 93 | + matrix <- buildMatrix this.OclContext matrixHost |
| 94 | + |
| 95 | + abstract member GlobalSetup : unit -> unit |
| 96 | + |
| 97 | + abstract member IterationCleanup : unit -> unit |
| 98 | + |
| 99 | + abstract member GlobalCleanup : unit -> unit |
| 100 | + |
| 101 | + abstract member Benchmark : unit -> unit |
| 102 | + |
| 103 | +type BFSBenchmarksWithoutDataTransfer() = |
| 104 | + |
| 105 | + inherit BFSBenchmarks<ClMatrix.CSR<int>, int>( |
| 106 | + (fun context wgSize -> BFS.singleSource context ArithmeticOperations.intSumOption ArithmeticOperations.intMulOption wgSize), |
| 107 | + int, |
| 108 | + (fun _ -> Utils.nextInt (System.Random())), |
| 109 | + Matrix.ToBackendCSR) |
| 110 | + |
| 111 | + static member InputMatricesProvider = |
| 112 | + BFSBenchmarks<_,_>.InputMatricesProviderBuilder "BFSBenchmarks.txt" |
| 113 | + |
| 114 | + [<GlobalSetup>] |
| 115 | + override this.GlobalSetup() = |
| 116 | + this.ReadMatrix () |
| 117 | + this.LoadMatrixToGPU () |
| 118 | + |
| 119 | + [<IterationCleanup>] |
| 120 | + override this.IterationCleanup() = |
| 121 | + this.ClearResult() |
| 122 | + |
| 123 | + [<GlobalCleanup>] |
| 124 | + override this.GlobalCleanup() = |
| 125 | + this.ClearInputMatrix() |
| 126 | + |
| 127 | + [<Benchmark>] |
| 128 | + override this.Benchmark() = |
| 129 | + this.BFS() |
| 130 | + this.Processor.PostAndReply(Msg.MsgNotifyMe) |
| 131 | + |
| 132 | +type BFSBenchmarksWithDataTransfer<'matrixT, 'elem when 'matrixT :> IDeviceMemObject and 'elem : struct>( |
| 133 | + buildFunToBenchmark, |
| 134 | + converter: string -> 'elem, |
| 135 | + converterBool, |
| 136 | + buildMatrix, |
| 137 | + resultToHost) = |
| 138 | + |
| 139 | + inherit BFSBenchmarks<'matrixT, 'elem>( |
| 140 | + buildFunToBenchmark, |
| 141 | + converter, |
| 142 | + converterBool, |
| 143 | + buildMatrix) |
| 144 | + |
| 145 | + [<GlobalSetup>] |
| 146 | + override this.GlobalSetup() = |
| 147 | + this.ReadMatrix() |
| 148 | + |
| 149 | + [<GlobalCleanup>] |
| 150 | + override this.GlobalCleanup() = () |
| 151 | + |
| 152 | + [<IterationCleanup>] |
| 153 | + override this.IterationCleanup() = |
| 154 | + this.ClearInputMatrix() |
| 155 | + this.ClearResult() |
| 156 | + |
| 157 | + [<Benchmark>] |
| 158 | + override this.Benchmark() = |
| 159 | + this.LoadMatrixToGPU() |
| 160 | + this.BFS() |
| 161 | + this.Processor.PostAndReply Msg.MsgNotifyMe |
| 162 | + let res = resultToHost this.ResultVector this.Processor |
| 163 | + this.Processor.PostAndReply Msg.MsgNotifyMe |
| 164 | + |
0 commit comments