forked from SparseLinearAlgebra/GraphBLAS-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBFS.fs
217 lines (171 loc) · 6.33 KB
/
BFS.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
namespace GraphBLAS.FSharp.Benchmarks.Algorithms.BFS
open System.IO
open BenchmarkDotNet.Attributes
open Microsoft.FSharp.Core
open Brahma.FSharp
open GraphBLAS.FSharp
open GraphBLAS.FSharp.IO
open GraphBLAS.FSharp.Benchmarks
open GraphBLAS.FSharp.Objects
open GraphBLAS.FSharp.Objects.ArraysExtensions
open GraphBLAS.FSharp.Backend.Quotes
[<AbstractClass>]
[<IterationCount(10)>]
[<WarmupCount(3)>]
[<Config(typeof<Configs.Matrix>)>]
type Benchmarks<'elem when 'elem : struct>(
buildFunToBenchmark,
converter: string -> 'elem,
binaryConverter,
vertex: int,
buildMatrix)
=
let mutable funToBenchmark = None
let mutable matrix = Unchecked.defaultof<ClMatrix<'elem>>
let mutable matrixHost = Unchecked.defaultof<_>
member val ResultLevels = Unchecked.defaultof<ClVector<int>> with get,set
[<ParamsSource("AvailableContexts")>]
member val OclContextInfo = Unchecked.defaultof<Utils.BenchmarkContext * int> with get, set
[<ParamsSource("InputMatrixProvider")>]
member val InputMatrixReader = Unchecked.defaultof<MtxReader> with get, set
member this.OclContext = (fst this.OclContextInfo).ClContext
member this.WorkGroupSize = snd this.OclContextInfo
member this.Processor =
let p = (fst this.OclContextInfo).Queue
//p.Error.Add(fun e -> failwithf "%A" e)
p
static member AvailableContexts = Utils.availableContexts
static member InputMatrixProviderBuilder pathToConfig =
let datasetFolder = ""
pathToConfig
|> Utils.getMatricesFilenames
|> Seq.map
(fun matrixFilename ->
printfn "%A" matrixFilename
match Path.GetExtension matrixFilename with
| ".mtx" -> MtxReader(Utils.getFullPathToMatrix datasetFolder matrixFilename)
| _ -> failwith "Unsupported matrix format")
member this.FunToBenchmark =
match funToBenchmark with
| None ->
let x = buildFunToBenchmark this.OclContext this.WorkGroupSize
funToBenchmark <- Some x
x
| Some x -> x
member this.BFS() =
this.ResultLevels <- this.FunToBenchmark this.Processor matrix vertex
member this.ClearInputMatrix() =
matrix.Dispose()
member this.ClearResult() =
match this.ResultLevels with
| ClVector.Dense result -> result.FreeAndWait this.Processor
| _ -> failwith "Impossible"
member this.ReadMatrix() =
let converter =
match this.InputMatrixReader.Field with
| Pattern -> binaryConverter
| _ -> converter
matrixHost <- this.InputMatrixReader.ReadMatrix converter
member this.LoadMatrixToGPU() =
matrix <- buildMatrix this.OclContext matrixHost
abstract member GlobalSetup : unit -> unit
abstract member IterationCleanup : unit -> unit
abstract member GlobalCleanup : unit -> unit
abstract member Benchmark : unit -> unit
type WithoutTransferBenchmark<'elem when 'elem : struct>(
buildFunToBenchmark,
converter: string -> 'elem,
boolConverter,
vertex,
buildMatrix) =
inherit Benchmarks<'elem>(
buildFunToBenchmark,
converter,
boolConverter,
vertex,
buildMatrix)
[<GlobalSetup>]
override this.GlobalSetup() =
this.ReadMatrix()
this.LoadMatrixToGPU()
this.Processor.Synchronize()
[<IterationCleanup>]
override this.IterationCleanup() =
this.ClearResult()
this.Processor.Synchronize()
[<GlobalCleanup>]
override this.GlobalCleanup() =
this.ClearInputMatrix()
[<Benchmark>]
override this.Benchmark() =
this.BFS()
this.Processor.Synchronize()
type BFSWithoutTransferBenchmarkBool() =
inherit WithoutTransferBenchmark<bool>(
(Algorithms.BFS.singleSource ArithmeticOperations.boolSumOption ArithmeticOperations.boolMulOption),
(fun _ -> true),
(fun _ -> true),
0,
(fun context matrix -> ClMatrix.CSR <| matrix.ToCSR.ToDevice context))
static member InputMatrixProvider =
Benchmarks<_>.InputMatrixProviderBuilder "BFSBenchmarks.txt"
type BFSPushPullWithoutTransferBenchmarkBool() =
inherit WithoutTransferBenchmark<bool>(
(Algorithms.BFS.singleSourcePushPull ArithmeticOperations.boolSumOption ArithmeticOperations.boolMulOption),
(fun _ -> true),
(fun _ -> true),
0,
(fun context matrix -> ClMatrix.CSR <| matrix.ToCSR.ToDevice context))
static member InputMatrixProvider =
Benchmarks<_>.InputMatrixProviderBuilder "BFSBenchmarks.txt"
type SSSPWithoutTransferBenchmarkInt32() =
inherit WithoutTransferBenchmark<int>(
Algorithms.SSSP.run,
int32,
(fun _ -> Utils.nextInt (System.Random())),
0,
(fun context matrix -> ClMatrix.CSR <| matrix.ToCSR.ToDevice context))
static member InputMatrixProvider =
Benchmarks<_>.InputMatrixProviderBuilder "BFSBenchmarks.txt"
type WithTransferBenchmark<'elem when 'elem : struct>(
buildFunToBenchmark,
converter: string -> 'elem,
boolConverter,
vertex,
buildMatrix) =
inherit Benchmarks<'elem>(
buildFunToBenchmark,
converter,
boolConverter,
vertex,
buildMatrix)
[<GlobalSetup>]
override this.GlobalSetup() =
this.ReadMatrix()
this.Processor.Synchronize()
[<GlobalCleanup>]
override this.GlobalCleanup() =
this.ClearResult()
[<IterationCleanup>]
override this.IterationCleanup() =
this.ClearInputMatrix()
this.ClearResult()
this.Processor.Synchronize()
[<Benchmark>]
override this.Benchmark() =
this.LoadMatrixToGPU()
this.BFS()
match this.ResultLevels with
| ClVector.Dense result ->
result.ToHost this.Processor |> ignore
this.Processor.Synchronize()
| _ -> failwith "Impossible"
type BFSWithTransferBenchmarkBool() =
inherit WithTransferBenchmark<bool>(
(Algorithms.BFS.singleSource ArithmeticOperations.boolSumOption ArithmeticOperations.boolMulOption),
(fun _ -> true),
(fun _ -> true),
0,
(fun context matrix -> ClMatrix.CSR <| matrix.ToCSR.ToDevice context))
static member InputMatrixProvider =
Benchmarks<_>.InputMatrixProviderBuilder "BFSBenchmarks.txt"