Skip to content

Commit 1a6a787

Browse files
authored
Merge pull request #14 from dpanfilyonok/benchs-and-optimization
Latest version of benchmarks and eWiseAdd implementation
2 parents 5a42da3 + 551e83b commit 1a6a787

8 files changed

Lines changed: 223 additions & 173 deletions

File tree

Lines changed: 78 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
namespace GraphBLAS.FSharp.Benchmarks
22

33
open System.IO
4+
open System.Text.RegularExpressions
45
open GraphBLAS.FSharp
56
open GraphBLAS.FSharp.Predefined
67
open GraphBLAS.FSharp.MatrixBackend
78
open BenchmarkDotNet.Attributes
8-
open BenchmarkDotNet.Engines
99
open BenchmarkDotNet.Configs
1010
open BenchmarkDotNet.Columns
1111
open BenchmarkDotNet.Filters
12-
open BenchmarkDotNet.Jobs
13-
open BenchmarkDotNet.Order
1412
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
1513
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
1614
open OpenCL.Net
17-
open Brahma.OpenCL
18-
open System.Text.RegularExpressions
1915

20-
type Config<'a>() =
16+
type Config() =
2117
inherit ManualConfig()
2218

2319
do
2420
base.AddColumn(
25-
MatrixShapeColumn<'a>("RowCount", fun matrix -> matrix.MatrixStructure.RowCount) :> IColumn,
26-
MatrixShapeColumn<'a>("ColumnCount", fun matrix -> matrix.MatrixStructure.ColumnCount) :> IColumn,
27-
MatrixShapeColumn<'a>("NNZ", fun matrix -> matrix.MatrixStructure.Values.Length) :> IColumn,
28-
TEPSColumn<'a>() :> IColumn,
21+
MatrixShapeColumn("RowCount", fun matrix -> matrix.RowCount) :> IColumn,
22+
MatrixShapeColumn("ColumnCount", fun matrix -> matrix.ColumnCount) :> IColumn,
23+
MatrixShapeColumn("NNZ", fun matrix ->
24+
match matrix.Format with
25+
| "coordinate" -> matrix.Size.[2]
26+
| "array" -> 0
27+
| _ -> failwith "Unsupported") :> IColumn,
28+
TEPSColumn() :> IColumn,
2929
StatisticColumn.Min,
3030
StatisticColumn.Max
3131
) |> ignore
@@ -36,19 +36,26 @@ type Config<'a>() =
3636
)
3737
) |> ignore
3838

39-
type Float32Config = Config<float32>
40-
type BoolConfig = Config<bool>
41-
42-
[<SimpleJob(RunStrategy.Monitoring, targetCount=2)>]
39+
[<IterationCount(5)>]
40+
[<WarmupCount(3)>]
41+
[<Config(typeof<Config>)>]
4342
type EWiseAddBenchmarks() =
4443
[<ParamsSource("AvaliableContexts")>]
4544
member val OclContext = Unchecked.defaultof<ClContext> with get, set
46-
45+
46+
[<ParamsSource("InputMatricesProvider")>]
47+
member val InputMatrix = Unchecked.defaultof<MtxShape> with get, set
48+
4749
[<IterationCleanup>]
4850
member this.ClearBuffers() =
4951
let (ClContext context) = this.OclContext
5052
context.Provider.CloseAllBuffers()
5153

54+
[<GlobalCleanup>]
55+
member this.ClearContext() =
56+
let (ClContext context) = this.OclContext
57+
context.Provider.Dispose()
58+
5259
static member AvaliableContexts =
5360
let pathToConfig =
5461
Path.Combine [|
@@ -70,6 +77,7 @@ type EWiseAddBenchmarks() =
7077
Cl.GetPlatformIDs &e
7178
|> Array.collect (fun platform -> Cl.GetDeviceIDs(platform, deviceType, &e))
7279
|> Seq.ofArray
80+
|> Seq.distinctBy (fun device -> Cl.GetDeviceInfo(device, DeviceInfo.Name, &e).ToString())
7381
|> Seq.filter
7482
(fun device ->
7583
let platform = Cl.GetDeviceInfo(device, DeviceInfo.Platform, &e).CastTo<Platform>()
@@ -84,7 +92,6 @@ type EWiseAddBenchmarks() =
8492
OpenCLEvaluationContext(platformName, deviceType) |> ClContext
8593
)
8694

87-
[<Config(typeof<Float32Config>)>]
8895
type EWiseAddBenchmarks4Float32() =
8996
inherit EWiseAddBenchmarks()
9097

@@ -94,13 +101,25 @@ type EWiseAddBenchmarks4Float32() =
94101
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
95102
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
96103

97-
[<ParamsSource("InputMatricesProvider")>]
98-
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat<float32>> with get, set
99-
100104
[<GlobalSetup>]
101105
member this.FormInputData() =
102-
this.FirstMatrix <- this.InputMatrix.MatrixStructure
103-
this.SecondMatrix <- this.InputMatrix.MatrixStructure |> Utils.transposeCOO
106+
let mtxFormat = MtxReader.readMtx <| Utils.getFullPathToMatrix this.InputMatrix.Filename
107+
let cooMatrix =
108+
match mtxFormat.Shape.Format, mtxFormat.Shape.Field with
109+
| "coordinate", "real" -> Utils.makeCOO mtxFormat <| FromString float32
110+
| "coordinate", "integer" -> Utils.makeCOO mtxFormat <| FromString float32
111+
| "coordinate", "pattern" ->
112+
let rand = System.Random()
113+
let nextSingle (random: System.Random) =
114+
let buffer = Array.zeroCreate<byte> 4
115+
random.NextBytes buffer
116+
System.BitConverter.ToSingle(buffer, 0)
117+
118+
Utils.makeCOO mtxFormat <| FromUnit (fun () -> nextSingle rand)
119+
| _ -> failwith "Unsupported matrix format"
120+
121+
this.FirstMatrix <- cooMatrix
122+
this.SecondMatrix <- cooMatrix |> Utils.transposeCOO
104123

105124
[<IterationSetup>]
106125
member this.BuildCOO() =
@@ -115,9 +134,9 @@ type EWiseAddBenchmarks4Float32() =
115134
Matrix.Build<float32>(
116135
this.FirstMatrix.RowCount,
117136
this.FirstMatrix.ColumnCount,
118-
this.FirstMatrix.Rows,
119-
this.FirstMatrix.Columns,
120-
this.FirstMatrix.Values,
137+
leftRows,
138+
leftCols,
139+
leftVals,
121140
COO
122141
)
123142

@@ -132,9 +151,9 @@ type EWiseAddBenchmarks4Float32() =
132151
Matrix.Build<float32>(
133152
this.SecondMatrix.RowCount,
134153
this.SecondMatrix.ColumnCount,
135-
this.SecondMatrix.Rows,
136-
this.SecondMatrix.Columns,
137-
this.SecondMatrix.Values,
154+
rightRows,
155+
rightCols,
156+
rightVals,
138157
COO
139158
)
140159

@@ -145,44 +164,15 @@ type EWiseAddBenchmarks4Float32() =
145164
|> context.RunSync
146165

147166
static member InputMatricesProvider =
148-
let matricesFilenames =
149-
let pathToConfig =
150-
Path.Combine [|
151-
__SOURCE_DIRECTORY__
152-
"Configs"
153-
"EWiseAddBenchmarks4Float32.txt"
154-
|] |> Path.GetFullPath
155-
156-
File.ReadAllLines pathToConfig
157-
|> Seq.ofArray
158-
|> Seq.filter (fun line -> not <| line.StartsWith "!")
159-
160-
let matrixHandler (matrixFilename: string) =
161-
match Path.GetExtension matrixFilename with
162-
| ".mtx" ->
163-
let mtx = GraphReader.readMtx <| Utils.getFullPathToMatrix matrixFilename
164-
match mtx.Format, mtx.Field with
165-
| "coordinate", "real" -> Utils.makeCOO mtx <| FromString float32
166-
| "coordinate", "integer" -> Utils.makeCOO mtx <| FromString float32
167-
| "coordinate", "pattern" ->
168-
let rand = System.Random()
169-
let nextSingle (random: System.Random) =
170-
let buffer = Array.zeroCreate<byte> 4
171-
random.NextBytes buffer
172-
System.BitConverter.ToSingle(buffer, 0)
173-
174-
Utils.makeCOO mtx <| FromUnit (fun () -> nextSingle rand)
167+
"EWiseAddBenchmarks4Float32.txt"
168+
|> Utils.getMatricesFilenames
169+
|> Seq.map
170+
(fun matrixFilename ->
171+
match Path.GetExtension matrixFilename with
172+
| ".mtx" -> MtxReader.readShape <| Utils.getFullPathToMatrix matrixFilename
175173
| _ -> failwith "Unsupported matrix format"
176-
| _ -> failwith "Unsupported matrix format"
177-
178-
matricesFilenames
179-
|> Seq.map (fun filename ->
180-
{
181-
MatrixName = Path.GetFileNameWithoutExtension filename
182-
MatrixStructure = matrixHandler filename
183-
})
174+
)
184175

185-
[<Config(typeof<BoolConfig>)>]
186176
type EWiseAddBenchmarks4Bool() =
187177
inherit EWiseAddBenchmarks()
188178

@@ -192,8 +182,18 @@ type EWiseAddBenchmarks4Bool() =
192182
member val FirstMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
193183
member val SecondMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
194184

195-
[<ParamsSource("InputMatricesProvider")>]
196-
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat<bool>> with get, set
185+
[<GlobalSetup>]
186+
member this.FormInputData() =
187+
let mtxFormat = MtxReader.readMtx <| Utils.getFullPathToMatrix this.InputMatrix.Filename
188+
let cooMatrix =
189+
match mtxFormat.Shape.Format, mtxFormat.Shape.Field with
190+
| "coordinate", "real" -> Utils.makeCOO mtxFormat <| FromString (fun _ -> true)
191+
| "coordinate", "integer" -> Utils.makeCOO mtxFormat <| FromString (fun _ -> true)
192+
| "coordinate", "pattern" -> Utils.makeCOO mtxFormat <| FromUnit (fun _ -> true)
193+
| _ -> failwith "Unsupported matrix format"
194+
195+
this.FirstMatrix <- cooMatrix
196+
this.SecondMatrix <- cooMatrix |> Utils.transposeCOO
197197

198198
[<IterationSetup>]
199199
member this.BuildCOO() =
@@ -207,9 +207,9 @@ type EWiseAddBenchmarks4Bool() =
207207
Matrix.Build<bool>(
208208
this.FirstMatrix.RowCount,
209209
this.FirstMatrix.ColumnCount,
210-
this.FirstMatrix.Rows,
211-
this.FirstMatrix.Columns,
212-
this.FirstMatrix.Values,
210+
leftRows,
211+
leftCols,
212+
leftVals,
213213
COO
214214
)
215215

@@ -223,9 +223,9 @@ type EWiseAddBenchmarks4Bool() =
223223
Matrix.Build<bool>(
224224
this.SecondMatrix.RowCount,
225225
this.SecondMatrix.ColumnCount,
226-
this.SecondMatrix.Rows,
227-
this.SecondMatrix.Columns,
228-
this.SecondMatrix.Values,
226+
rightRows,
227+
rightCols,
228+
rightVals,
229229
COO
230230
)
231231

@@ -236,32 +236,11 @@ type EWiseAddBenchmarks4Bool() =
236236
|> context.RunSync
237237

238238
static member InputMatricesProvider =
239-
let matricesFilenames =
240-
let pathToConfig =
241-
Path.Combine [|
242-
__SOURCE_DIRECTORY__
243-
"Configs"
244-
"EWiseAddBenchmarks4Bool.txt"
245-
|] |> Path.GetFullPath
246-
247-
File.ReadAllLines pathToConfig
248-
|> Seq.ofArray
249-
|> Seq.filter (fun line -> not <| line.StartsWith "!")
250-
251-
let matrixHandler (matrixFilename: string) =
252-
match Path.GetExtension matrixFilename with
253-
| ".mtx" ->
254-
let mtx = GraphReader.readMtx <| Utils.getFullPathToMatrix matrixFilename
255-
match mtx.Format, mtx.Field with
256-
| "coordinate", "real" -> Utils.makeCOO mtx <| FromString (fun _ -> true)
257-
| "coordinate", "integer" -> Utils.makeCOO mtx <| FromString (fun _ -> true)
258-
| "coordinate", "pattern" -> Utils.makeCOO mtx <| FromUnit (fun _ -> true)
239+
"EWiseAddBenchmarks4Bool.txt"
240+
|> Utils.getMatricesFilenames
241+
|> Seq.map
242+
(fun matrixFilename ->
243+
match Path.GetExtension matrixFilename with
244+
| ".mtx" -> MtxReader.readShape <| Utils.getFullPathToMatrix matrixFilename
259245
| _ -> failwith "Unsupported matrix format"
260-
| _ -> failwith "Unsupported matrix format"
261-
262-
matricesFilenames
263-
|> Seq.map (fun filename ->
264-
{
265-
MatrixName = Path.GetFileNameWithoutExtension filename
266-
MatrixStructure = matrixHandler filename
267-
})
246+
)

benchmarks/GraphBLAS-sharp.Benchmarks/Common.fs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace GraphBLAS.FSharp.Benchmarks
22

3-
open GraphBLAS.FSharp
43
open BenchmarkDotNet.Columns
54
open BenchmarkDotNet.Reports
65
open BenchmarkDotNet.Running
@@ -28,24 +27,19 @@ with
2827

2928
sprintf "%s, %s" platformName deviceType
3029

31-
type InputMatrixFormat<'a> = {
32-
MatrixName: string
33-
MatrixStructure: COOFormat<'a>
34-
}
35-
with
36-
override this.ToString() =
37-
sprintf "%s" this.MatrixName
38-
39-
type MatrixShapeColumn<'a>(columnName: string, getShape: InputMatrixFormat<'a> -> int) =
30+
type MatrixShapeColumn(columnName: string, getShape: MtxShape -> int) =
4031
interface IColumn with
4132
member this.AlwaysShow: bool = true
4233
member this.Category: ColumnCategory = ColumnCategory.Params
4334
member this.ColumnName: string = columnName
35+
4436
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase): string =
45-
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat<'a>
37+
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> MtxShape
4638
sprintf "%i" <| getShape inputMatrix
39+
4740
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase, style: SummaryStyle): string =
4841
(this :> IColumn).GetValue(summary, benchmarkCase)
42+
4943
member this.Id: string = sprintf "%s.%s" "MatrixShapeColumn" columnName
5044
member this.IsAvailable(summary: Summary): bool = true
5145
member this.IsDefault(summary: Summary, benchmarkCase: BenchmarkCase): bool = false
@@ -54,25 +48,29 @@ type MatrixShapeColumn<'a>(columnName: string, getShape: InputMatrixFormat<'a> -
5448
member this.PriorityInCategory: int = 1
5549
member this.UnitType: UnitType = UnitType.Size
5650

57-
type TEPSColumn<'a>() =
51+
type TEPSColumn() =
5852
interface IColumn with
5953
member this.AlwaysShow: bool = true
6054
member this.Category: ColumnCategory = ColumnCategory.Statistics
6155
member this.ColumnName: string = "TEPS"
56+
6257
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase): string =
63-
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat<'a>
64-
let (nrows, ncols, nnz) =
65-
inputMatrix.MatrixStructure.RowCount,
66-
inputMatrix.MatrixStructure.ColumnCount,
67-
inputMatrix.MatrixStructure.Values.Length
68-
let (vertices, edges) = if nrows = ncols then (nrows, nnz) else (ncols, nrows)
58+
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> MtxShape
59+
let (nrows, ncols) = inputMatrix.RowCount, inputMatrix.ColumnCount
60+
let (vertices, edges) =
61+
match inputMatrix.Format with
62+
| "coordinate" -> if nrows = ncols then (nrows, inputMatrix.Size.[2]) else (ncols, nrows)
63+
| _ -> failwith "Unsupported"
64+
6965
if isNull summary.[benchmarkCase].ResultStatistics then
7066
"NA"
7167
else
7268
let meanTime = summary.[benchmarkCase].ResultStatistics.Mean
7369
sprintf "%f" <| float edges / (meanTime * 1e-6)
70+
7471
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase, style: SummaryStyle): string =
7572
(this :> IColumn).GetValue(summary, benchmarkCase)
73+
7674
member this.Id: string = "TEPSColumn"
7775
member this.IsAvailable(summary: Summary): bool = true
7876
member this.IsDefault(summary: Summary, benchmarkCase: BenchmarkCase): bool = false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Intel*
2-
All
2+
Cpu

benchmarks/GraphBLAS-sharp.Benchmarks/Program.fs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
open BenchmarkDotNet.Running
21
open GraphBLAS.FSharp.Benchmarks
3-
open BenchmarkDotNet.Configs
4-
open BenchmarkDotNet.Order
2+
open BenchmarkDotNet.Running
53

64
[<EntryPoint>]
75
let main argv =

0 commit comments

Comments
 (0)