Skip to content

Commit 1264c88

Browse files
authored
Merge pull request #41 from kirillgarbar/net5
EWiseAdd type generalization
2 parents 21fd3ad + 2b2feb3 commit 1264c88

21 files changed

Lines changed: 1377 additions & 817 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 112 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ open GraphBLAS.FSharp.IO
77
open BenchmarkDotNet.Attributes
88
open BenchmarkDotNet.Configs
99
open BenchmarkDotNet.Columns
10-
open Brahma.FSharp.OpenCL
10+
open Brahma.FSharp
1111
open OpenCL.Net
1212

1313
type Config() =
@@ -54,22 +54,22 @@ type EWiseAddBenchmarks<'matrixT, 'elem when 'matrixT :> Backend.IDeviceMemObjec
5454
let mutable secondMatrix = Unchecked.defaultof<'matrixT>
5555
let mutable firstMatrixHost = Unchecked.defaultof<_>
5656
let mutable secondMatrixHost = Unchecked.defaultof<_>
57-
57+
5858
member val ResultMatrix = Unchecked.defaultof<'matrixT> with get,set
59-
60-
[<ParamsSource("AvaliableContexts")>]
61-
member val OclContextInfo = Unchecked.defaultof<_> with get, set
59+
60+
[<ParamsSource("AvaliableContexts")>]
61+
member val OclContextInfo = Unchecked.defaultof<Utils.BenchmarkContext * int> with get, set
6262

6363
[<ParamsSource("InputMatricesProvider")>]
6464
member val InputMatrixReader = Unchecked.defaultof<MtxReader*MtxReader> with get, set
65-
66-
member this.OclContext:ClContext = fst this.OclContextInfo
65+
66+
member this.OclContext:ClContext = (fst this.OclContextInfo).ClContext
6767
member this.WorkGroupSize = snd this.OclContextInfo
6868

69-
member this.Processor =
70-
let p = this.OclContext.Provider.CommandQueue
69+
member this.Processor =
70+
let p = (fst this.OclContextInfo).Queue
7171
p.Error.Add(fun e -> failwithf "%A" e)
72-
p
72+
p
7373

7474
static member AvaliableContexts = Utils.avaliableContexts
7575

@@ -82,54 +82,54 @@ type EWiseAddBenchmarks<'matrixT, 'elem when 'matrixT :> Backend.IDeviceMemObjec
8282
printfn "%A" matrixFilename
8383

8484
match Path.GetExtension matrixFilename with
85-
| ".mtx" ->
85+
| ".mtx" ->
8686
MtxReader(Utils.getFullPathToMatrix datasetFolder matrixFilename)
8787
, MtxReader(Utils.getFullPathToMatrix datasetFolder ("squared_" + matrixFilename))
8888
| _ -> failwith "Unsupported matrix format")
8989

90-
member this.FunToBenchmark =
90+
member this.FunToBenchmark =
9191
match funToBenchmark with
92-
| None ->
92+
| None ->
9393
let x = buildFunToBenchmark this.OclContext this.WorkGroupSize
9494
funToBenchmark <- Some x
9595
x
9696
| Some x -> x
9797

98-
member this.ReadMatrix (reader:MtxReader) =
98+
member this.ReadMatrix (reader:MtxReader) =
9999
let converter =
100100
match reader.Field with
101101
| Pattern -> converterBool
102102
| _ -> converter
103-
103+
104104
reader.ReadMatrix converter
105-
105+
106106
member this.EWiseAddition() =
107107
this.ResultMatrix <- this.FunToBenchmark this.Processor firstMatrix secondMatrix
108108

109109
member this.ClearInputMatrices() =
110-
(firstMatrix :> Backend.IDeviceMemObject).Dispose()
111-
(secondMatrix :> Backend.IDeviceMemObject).Dispose()
110+
(firstMatrix :> Backend.IDeviceMemObject).Dispose this.Processor
111+
(secondMatrix :> Backend.IDeviceMemObject).Dispose this.Processor
112112

113113
member this.ClearResult() =
114-
(this.ResultMatrix :> Backend.IDeviceMemObject).Dispose()
114+
(this.ResultMatrix :> Backend.IDeviceMemObject).Dispose this.Processor
115115

116116
member this.ReadMatrices() =
117117
let leftMatrixReader = fst this.InputMatrixReader
118118
let rightMatrixReader = snd this.InputMatrixReader
119-
firstMatrixHost <- this.ReadMatrix leftMatrixReader
120-
secondMatrixHost <- this.ReadMatrix rightMatrixReader
119+
firstMatrixHost <- this.ReadMatrix leftMatrixReader
120+
secondMatrixHost <- this.ReadMatrix rightMatrixReader
121121

122-
member this.LoadMatricesToGPU () =
122+
member this.LoadMatricesToGPU () =
123123
firstMatrix <- buildMatrix this.OclContext firstMatrixHost
124-
secondMatrix <- buildMatrix this.OclContext secondMatrixHost
124+
secondMatrix <- buildMatrix this.OclContext secondMatrixHost
125125

126-
abstract member GlobalSetup : unit -> unit
126+
abstract member GlobalSetup : unit -> unit
127127

128-
abstract member IterationCleanup : unit -> unit
128+
abstract member IterationCleanup : unit -> unit
129129

130-
abstract member GlobalCleanup : unit -> unit
130+
abstract member GlobalCleanup : unit -> unit
131131

132-
abstract member Benchmark : unit -> unit
132+
abstract member Benchmark : unit -> unit
133133

134134
type EWiseAddBenchmarksWithoutDataTransfer<'matrixT, 'elem when 'matrixT :> Backend.IDeviceMemObject and 'elem : struct>(
135135
buildFunToBenchmark,
@@ -147,13 +147,13 @@ type EWiseAddBenchmarksWithoutDataTransfer<'matrixT, 'elem when 'matrixT :> Back
147147
override this.GlobalSetup() =
148148
this.ReadMatrices ()
149149
this.LoadMatricesToGPU ()
150-
150+
151151
[<IterationCleanup>]
152-
override this.IterationCleanup () =
152+
override this.IterationCleanup () =
153153
this.ClearResult()
154154

155155
[<GlobalCleanup>]
156-
override this.GlobalCleanup () =
156+
override this.GlobalCleanup () =
157157
this.ClearInputMatrices()
158158

159159
[<Benchmark>]
@@ -175,26 +175,26 @@ type EWiseAddBenchmarksWithDataTransfer<'matrixT, 'elem when 'matrixT :> Backend
175175
buildMatrix)
176176

177177
[<GlobalSetup>]
178-
override this.GlobalSetup () =
179-
this.ReadMatrices ()
178+
override this.GlobalSetup () =
179+
this.ReadMatrices ()
180180

181181
[<GlobalCleanup>]
182182
override this.GlobalCleanup () = ()
183-
183+
184184
[<IterationCleanup>]
185-
override this.IterationCleanup () =
185+
override this.IterationCleanup () =
186186
this.ClearInputMatrices()
187187
this.ClearResult()
188188

189189
[<Benchmark>]
190-
override this.Benchmark () =
190+
override this.Benchmark () =
191191
this.LoadMatricesToGPU()
192192
this.EWiseAddition()
193193
this.Processor.PostAndReply Msg.MsgNotifyMe
194194
let res = resultToHost this.ResultMatrix this.Processor
195195
this.Processor.PostAndReply Msg.MsgNotifyMe
196196

197-
module M =
197+
module M =
198198
let inline buildCooMatrix (context:ClContext) matrix =
199199
match matrix with
200200
| MatrixCOO m ->
@@ -206,16 +206,16 @@ module M =
206206

207207
let vals =
208208
context.CreateClArray (m.Values, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
209-
209+
210210
{ Backend.COOMatrix.Context = context
211211
Backend.COOMatrix.RowCount = m.RowCount
212212
Backend.COOMatrix.ColumnCount = m.ColumnCount
213213
Backend.COOMatrix.Rows = rows
214214
Backend.COOMatrix.Columns = cols
215-
Backend.COOMatrix.Values = vals }
215+
Backend.COOMatrix.Values = vals }
216216

217217
| x -> failwith "Unsupported matrix format: %A"
218-
218+
219219
let inline buildCsrMatrix (context:ClContext) matrix =
220220
match matrix with
221221
| MatrixCOO m ->
@@ -239,13 +239,13 @@ module M =
239239
,hostAccessMode = HostAccessMode.ReadOnly
240240
,deviceAccessMode = DeviceAccessMode.ReadOnly
241241
,allocationMode = AllocationMode.CopyHostPtr)
242-
242+
243243
{ Backend.CSRMatrix.Context = context
244244
Backend.CSRMatrix.RowCount = m.RowCount
245245
Backend.CSRMatrix.ColumnCount = m.ColumnCount
246246
Backend.CSRMatrix.RowPointers = rowPointers
247247
Backend.CSRMatrix.Columns = cols
248-
Backend.CSRMatrix.Values = vals }
248+
Backend.CSRMatrix.Values = vals }
249249

250250
| x -> failwith "Unsupported matrix format: %A"
251251

@@ -261,7 +261,7 @@ module M =
261261
let vals =
262262
let a = Array.zeroCreate resultMatrix.Values.Length
263263
procesor.Post(Msg.CreateToHostMsg(resultMatrix.Values,a))
264-
a
264+
a
265265
{
266266
RowCount = resultMatrix.RowCount
267267
ColumnCount = resultMatrix.ColumnCount
@@ -272,22 +272,22 @@ module M =
272272

273273

274274
type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
275-
275+
276276
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<float32>,float32>(
277-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context <@ (+) @> wgSize),
278-
float32,
277+
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
278+
float32,
279279
(fun _ -> Utils.nextSingle (System.Random())),
280-
M.buildCooMatrix
280+
M.buildCooMatrix
281281
)
282282

283283
static member InputMatricesProvider =
284284
EWiseAddBenchmarks<_,_>.InputMatricesProviderBuilder "EWiseAddBenchmarks4Float32COO.txt"
285285

286286
type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
287-
287+
288288
inherit EWiseAddBenchmarksWithDataTransfer<Backend.COOMatrix<float32>,float32>(
289-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context <@ (+) @> wgSize),
290-
float32,
289+
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
290+
float32,
291291
(fun _ -> Utils.nextSingle (System.Random())),
292292
M.buildCooMatrix,
293293
M.resultToHostCOO
@@ -298,39 +298,89 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
298298

299299

300300
type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
301-
301+
302302
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<bool>,bool>(
303-
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context <@ (||) @> wgSize),
304-
(fun _ -> true),
303+
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
305304
(fun _ -> true),
306-
M.buildCooMatrix
305+
(fun _ -> true),
306+
M.buildCooMatrix
307307
)
308308

309309
static member InputMatricesProvider =
310310
EWiseAddBenchmarks<_, _>.InputMatricesProviderBuilder "EWiseAddBenchmarks4BoolCOO.txt"
311311

312312

313313
type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
314-
314+
315315
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<float32>,float32>(
316-
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context <@ (+) @> wgSize),
317-
float32,
316+
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
317+
float32,
318318
(fun _ -> Utils.nextSingle (System.Random())),
319-
M.buildCsrMatrix
319+
M.buildCsrMatrix
320320
)
321321

322322
static member InputMatricesProvider =
323323
EWiseAddBenchmarks<_, _>.InputMatricesProviderBuilder "EWiseAddBenchmarks4Float32CSR.txt"
324324

325325

326326
type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
327-
327+
328328
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<bool>,bool>(
329-
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context <@ (||) @> wgSize),
330-
(fun _ -> true),
329+
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
330+
(fun _ -> true),
331331
(fun _ -> true),
332-
M.buildCsrMatrix
332+
M.buildCsrMatrix
333333
)
334334

335335
static member InputMatricesProvider =
336-
EWiseAddBenchmarks<_, _>.InputMatricesProviderBuilder "EWiseAddBenchmarks4BoolCSR.txt"
336+
EWiseAddBenchmarks<_, _>.InputMatricesProviderBuilder "EWiseAddBenchmarks4BoolCSR.txt"
337+
338+
// With AtLeastOne
339+
340+
type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
341+
342+
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<bool>,bool>(
343+
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
344+
(fun _ -> true),
345+
(fun _ -> true),
346+
M.buildCooMatrix
347+
)
348+
349+
static member InputMatricesProvider =
350+
EWiseAddBenchmarks<_, _>.InputMatricesProviderBuilder "EWiseAddBenchmarks4BoolCSR.txt"
351+
352+
type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
353+
354+
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<bool>,bool>(
355+
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
356+
(fun _ -> true),
357+
(fun _ -> true),
358+
M.buildCsrMatrix
359+
)
360+
361+
static member InputMatricesProvider =
362+
EWiseAddBenchmarks<_, _>.InputMatricesProviderBuilder "EWiseAddBenchmarks4BoolCSR.txt"
363+
364+
type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
365+
366+
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.COOMatrix<float32>,float32>(
367+
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
368+
float32,
369+
(fun _ -> Utils.nextSingle (System.Random())),
370+
M.buildCooMatrix
371+
)
372+
373+
static member InputMatricesProvider =
374+
EWiseAddBenchmarks<_,_>.InputMatricesProviderBuilder "EWiseAddBenchmarks4Float32COO.txt"
375+
376+
type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
377+
378+
inherit EWiseAddBenchmarksWithoutDataTransfer<Backend.CSRMatrix<float32>,float32>(
379+
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
380+
float32,
381+
(fun _ -> Utils.nextSingle (System.Random())),
382+
M.buildCsrMatrix
383+
)
384+
385+
static member InputMatricesProvider =
386+
EWiseAddBenchmarks<_,_>.InputMatricesProviderBuilder "EWiseAddBenchmarks4Float32COO.txt"

0 commit comments

Comments
 (0)