|
1 | | -namespace GraphBLAS.FSharp.Benchmarks |
| 1 | +namespace GraphBLAS.FSharp.Benchmarks |
2 | 2 |
|
3 | | -open GraphBLAS.FSharp |
4 | | -open GraphBLAS.FSharp.Algorithms |
| 3 | +open System.IO |
| 4 | +open GraphBLAS.FSharp.Backend.Quotes |
| 5 | +open GraphBLAS.FSharp.IO |
5 | 6 | open BenchmarkDotNet.Attributes |
6 | 7 | open BenchmarkDotNet.Configs |
7 | 8 | open BenchmarkDotNet.Columns |
8 | | -open System.IO |
9 | | -open System |
10 | | -open System.Text.RegularExpressions |
11 | | -open Brahma.FSharp.OpenCL |
12 | | -open OpenCL.Net |
13 | | -open GraphBLAS.FSharp.IO |
14 | | -open QuickGraph |
| 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 |
15 | 36 |
|
16 | | -[<Config(typeof<CommonConfig>)>] |
17 | | -type BFSBenchmarks() = |
18 | | - let random = Random() |
| 37 | + [<ParamsSource("InputMatricesProvider")>] |
| 38 | + member val InputMatrixReader = Unchecked.defaultof<MtxReader> with get, set |
19 | 39 |
|
20 | | - let mutable source = 0 |
| 40 | + member this.OclContext:ClContext = (fst this.OclContextInfo).ClContext |
| 41 | + member this.WorkGroupSize = snd this.OclContextInfo |
21 | 42 |
|
22 | | - // gb |
23 | | - let mutable matrix = Unchecked.defaultof<Matrix<int>> |
| 43 | + member this.Processor = |
| 44 | + let p = (fst this.OclContextInfo).Queue |
| 45 | + p.Error.Add(fun e -> failwithf "%A" e) |
| 46 | + p |
24 | 47 |
|
25 | | - // qg |
26 | | - let graph = AdjacencyGraph<int, Edge<int>>(false) |
27 | | - let mutable bfs = Unchecked.defaultof<Algorithms.Search.BreadthFirstSearchAlgorithm<int, Edge<int>>> |
| 48 | + static member AvaliableContexts = Utils.avaliableContexts |
28 | 49 |
|
29 | | - [<ParamsSource("AvaliableContextsProvider")>] |
30 | | - member val OclContext = Unchecked.defaultof<ClContext> with get, set |
31 | | - member this.Context = |
32 | | - failwith "fix me" |
33 | | - //let (ClContext context) = this.OclContext |
34 | | - //context |
| 50 | + static member InputMatricesProviderBuilder pathToConfig = |
| 51 | + let datasetFolder = "" |
| 52 | + pathToConfig |
| 53 | + |> Utils.getMatricesFilenames |
| 54 | + |> Seq.map |
| 55 | + (fun matrixFilename -> |
| 56 | + printfn "%A" matrixFilename |
35 | 57 |
|
36 | | - [<ParamsSource("InputMatricesProvider")>] |
37 | | - member val InputMatrixReader = Unchecked.defaultof<MtxReader> with get, set |
| 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.Dispose 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.intSum ArithmeticOperations.intMul wgSize), |
| 107 | + int, |
| 108 | + (fun _ -> Utils.nextInt (System.Random())), |
| 109 | + Matrix.ToBackendCSR) |
| 110 | + |
| 111 | + static member InputMatricesProvider = |
| 112 | + BFSBenchmarks<_,_>.InputMatricesProviderBuilder "BFSBenchmarks.txt" |
38 | 113 |
|
39 | 114 | [<GlobalSetup>] |
40 | | - member this.BuildGraph() = |
41 | | - let inputMatrix = this.InputMatrixReader.ReadMatrix(fun _ -> 1) |
42 | | - |
43 | | - failwith "fix me" |
44 | | - (*matrix <- |
45 | | - graphblas { |
46 | | - failwith "fix me" |
47 | | - //return! Matrix.switch CSR inputMatrix |
48 | | - //>>= Matrix.synchronizeAndReturn |
49 | | - } |
50 | | - |> EvalGB.withClContext this.Context |
51 | | - |> EvalGB.runSync |
52 | | - *) |
53 | | - match inputMatrix with |
54 | | - | MatrixCSR csr -> failwith "Not implemented" |
55 | | - | MatrixCOO coo -> |
56 | | - for i = 0 to coo.Values.Length - 1 do |
57 | | - graph.AddVerticesAndEdge(Edge(coo.Rows.[i], coo.Columns.[i])) |> ignore |
58 | | - |
59 | | - bfs <- Algorithms.Search.BreadthFirstSearchAlgorithm(graph) |
60 | | - |
61 | | - [<IterationSetup>] |
62 | | - member this.SetSource() = |
63 | | - source <- random.Next <| Matrix.rowCount matrix |
| 115 | + override this.GlobalSetup() = |
| 116 | + this.ReadMatrix () |
| 117 | + this.LoadMatrixToGPU () |
64 | 118 |
|
65 | | - [<Benchmark>] |
66 | | - member this.GraphblasLevelBFS() = |
67 | | - BFS.levelSingleSource matrix source |
68 | | - |> EvalGB.withClContext this.Context |
69 | | - |> EvalGB.runSync |
| 119 | + [<IterationCleanup>] |
| 120 | + override this.IterationCleanup() = |
| 121 | + this.ClearResult() |
| 122 | + |
| 123 | + [<GlobalCleanup>] |
| 124 | + override this.GlobalCleanup() = |
| 125 | + this.ClearInputMatrix() |
70 | 126 |
|
71 | 127 | [<Benchmark>] |
72 | | - member this.QuickGraphBFS() = |
73 | | - bfs.Compute(source) |
| 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) |
74 | 144 |
|
75 | | - //TODO fix me |
76 | | - (*[<IterationCleanup>] |
77 | | - member this.ClearBuffers() = |
78 | | - this.Context.Provider.CloseAllBuffers() |
| 145 | + [<GlobalSetup>] |
| 146 | + override this.GlobalSetup() = |
| 147 | + this.ReadMatrix() |
79 | 148 |
|
80 | 149 | [<GlobalCleanup>] |
81 | | - member this.ClearContext() = |
82 | | - let (ClContext context) = this.OclContext |
83 | | - context.Provider.Dispose() |
| 150 | + override this.GlobalCleanup() = () |
84 | 151 |
|
85 | | - *) |
| 152 | + [<IterationCleanup>] |
| 153 | + override this.IterationCleanup() = |
| 154 | + this.ClearInputMatrix() |
| 155 | + this.ClearResult() |
86 | 156 |
|
87 | | - static member AvaliableContextsProvider = Utils.avaliableContexts |
| 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 |
88 | 164 |
|
89 | | - static member InputMatricesProvider = |
90 | | - "Common.txt" |
91 | | - |> Utils.getMatricesFilenames |
92 | | - |> Seq.map |
93 | | - (fun matrixFilename -> |
94 | | - match Path.GetExtension matrixFilename with |
95 | | - | ".mtx" -> MtxReader(Utils.getFullPathToMatrix "Common" matrixFilename) |
96 | | - | _ -> failwith "Unsupported matrix format" |
97 | | - ) |
|
0 commit comments