Skip to content

Commit 1a3bac6

Browse files
authored
Merge branch 'benchs-and-optimization' into coo-element-wise-addition-optimization
2 parents 1af566d + 897fa4b commit 1a3bac6

8 files changed

Lines changed: 270 additions & 120 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 163 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ open BenchmarkDotNet.Order
1414
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
1515
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
1616
open OpenCL.Net
17+
open Brahma.OpenCL
18+
open System.Text.RegularExpressions
1719

18-
type Config() =
20+
type Config<'a>() =
1921
inherit ManualConfig()
2022

2123
do
2224
base.AddColumn(
23-
MatrixShapeColumn("RowCount", fun matrix -> matrix.MatrixStructure.RowCount) :> IColumn,
24-
MatrixShapeColumn("ColumnCount", fun matrix -> matrix.MatrixStructure.ColumnCount) :> IColumn,
25-
MatrixShapeColumn("NNZ", fun matrix -> matrix.MatrixStructure.Values.Length) :> IColumn,
26-
TEPSColumn() :> IColumn,
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,
2729
StatisticColumn.Min,
2830
StatisticColumn.Max
2931
) |> ignore
@@ -34,77 +36,46 @@ type Config() =
3436
)
3537
) |> ignore
3638

37-
[<Config(typeof<Config>)>]
39+
type Float32Config = Config<float32>
40+
type BoolConfig = Config<bool>
41+
3842
[<SimpleJob(RunStrategy.Monitoring, targetCount=2)>]
3943
type EWiseAddBenchmarks() =
40-
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
41-
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
42-
43-
[<ParamsSource("InputMatricesProvider")>]
44-
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat> with get, set
45-
4644
[<ParamsSource("AvaliableContexts")>]
4745
member val OclContext = Unchecked.defaultof<ClContext> with get, set
48-
49-
[<GlobalSetup>]
50-
member this.FormInputData() =
51-
let transposeCOO (matrix: COOFormat<float32>) =
52-
(matrix.Rows, matrix.Columns, matrix.Values)
53-
|||> Array.zip3
54-
|> Array.sortBy (fun (row, col, value) -> col, row)
55-
|> Array.unzip3
56-
|>
57-
fun (rows, cols, values) ->
58-
{
59-
Rows = cols
60-
Columns = rows
61-
Values = values
62-
RowCount = matrix.ColumnCount
63-
ColumnCount = matrix.RowCount
64-
}
65-
66-
this.FirstMatrix <- this.InputMatrix.MatrixStructure
67-
this.SecondMatrix <- this.InputMatrix.MatrixStructure |> transposeCOO
68-
46+
6947
[<IterationCleanup>]
7048
member this.ClearBuffers() =
7149
let (ClContext context) = this.OclContext
7250
context.Provider.CloseAllBuffers()
7351

74-
/// Sequence of paths to files where data for benchmarking will be taken from
75-
static member InputMatricesProvider =
76-
let matricesFilenames =
77-
seq {
78-
"arc130.mtx"
79-
"linux_call_graph.mtx"
80-
"webbase-1M.mtx"
81-
}
52+
static member AvaliableContexts =
53+
let pathToConfig =
54+
Path.Combine [|
55+
__SOURCE_DIRECTORY__
56+
"Configs"
57+
"Context.txt"
58+
|] |> Path.GetFullPath
8259

83-
matricesFilenames
84-
|> Seq.map
85-
(fun matrixFilename ->
86-
let getFullPathToMatrix filename =
87-
Path.Combine [|
88-
__SOURCE_DIRECTORY__
89-
"Datasets"
90-
"EWiseAddDatasets"
91-
filename
92-
|]
93-
94-
let fullPath = getFullPathToMatrix matrixFilename
95-
let matrixName = Path.GetFileNameWithoutExtension matrixFilename
96-
let matrixStructure = GraphReader.readMtx fullPath
97-
{
98-
MatrixName = matrixName
99-
MatrixStructure = matrixStructure
100-
}
101-
)
60+
use reader = new StreamReader(pathToConfig)
61+
let platformRegex = Regex <| reader.ReadLine()
62+
let deviceType =
63+
match reader.ReadLine() with
64+
| "Cpu" -> DeviceType.Cpu
65+
| "Gpu" -> DeviceType.Gpu
66+
| "All" -> DeviceType.All
67+
| _ -> failwith "Unsupported"
10268

103-
static member AvaliableContexts =
10469
let mutable e = ErrorCode.Unknown
10570
Cl.GetPlatformIDs &e
106-
|> Array.collect (fun platform -> Cl.GetDeviceIDs(platform, DeviceType.All, &e))
71+
|> Array.collect (fun platform -> Cl.GetDeviceIDs(platform, deviceType, &e))
10772
|> Seq.ofArray
73+
|> Seq.filter
74+
(fun device ->
75+
let platform = Cl.GetDeviceInfo(device, DeviceInfo.Platform, &e).CastTo<Platform>()
76+
let platformName = Cl.GetPlatformInfo(platform, PlatformInfo.Name, &e).ToString()
77+
platformRegex.IsMatch platformName
78+
)
10879
|> Seq.map
10980
(fun device ->
11081
let platform = Cl.GetDeviceInfo(device, DeviceInfo.Platform, &e).CastTo<Platform>()
@@ -113,45 +84,57 @@ type EWiseAddBenchmarks() =
11384
OpenCLEvaluationContext(platformName, deviceType) |> ClContext
11485
)
11586

87+
[<Config(typeof<Float32Config>)>]
11688
type EWiseAddBenchmarks4Float32() =
11789
inherit EWiseAddBenchmarks()
11890

11991
let mutable leftCOO = Unchecked.defaultof<Matrix<float32>>
12092
let mutable rightCOO = Unchecked.defaultof<Matrix<float32>>
12193

94+
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
95+
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
96+
97+
[<ParamsSource("InputMatricesProvider")>]
98+
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat<float32>> with get, set
99+
100+
[<GlobalSetup>]
101+
member this.FormInputData() =
102+
this.FirstMatrix <- this.InputMatrix.MatrixStructure
103+
this.SecondMatrix <- this.InputMatrix.MatrixStructure |> Utils.transposeCOO
104+
122105
[<IterationSetup>]
123106
member this.BuildCOO() =
124-
let leftRows = Array.zeroCreate<int> base.FirstMatrix.Rows.Length
125-
let leftCols = Array.zeroCreate<int> base.FirstMatrix.Columns.Length
126-
let leftVals = Array.zeroCreate<float32> base.FirstMatrix.Values.Length
127-
Array.blit base.FirstMatrix.Rows 0 leftRows 0 base.FirstMatrix.Rows.Length
128-
Array.blit base.FirstMatrix.Columns 0 leftCols 0 base.FirstMatrix.Columns.Length
129-
Array.blit base.FirstMatrix.Values 0 leftVals 0 base.FirstMatrix.Values.Length
107+
let leftRows = Array.zeroCreate<int> this.FirstMatrix.Rows.Length
108+
let leftCols = Array.zeroCreate<int> this.FirstMatrix.Columns.Length
109+
let leftVals = Array.zeroCreate<float32> this.FirstMatrix.Values.Length
110+
Array.blit this.FirstMatrix.Rows 0 leftRows 0 this.FirstMatrix.Rows.Length
111+
Array.blit this.FirstMatrix.Columns 0 leftCols 0 this.FirstMatrix.Columns.Length
112+
Array.blit this.FirstMatrix.Values 0 leftVals 0 this.FirstMatrix.Values.Length
130113

131114
leftCOO <-
132115
Matrix.Build<float32>(
133-
base.FirstMatrix.RowCount,
134-
base.FirstMatrix.ColumnCount,
135-
leftRows,
136-
leftCols,
137-
leftVals,
116+
this.FirstMatrix.RowCount,
117+
this.FirstMatrix.ColumnCount,
118+
this.FirstMatrix.Rows,
119+
this.FirstMatrix.Columns,
120+
this.FirstMatrix.Values,
138121
COO
139122
)
140123

141-
let rightRows = Array.zeroCreate<int> base.SecondMatrix.Rows.Length
142-
let rightCols = Array.zeroCreate<int> base.SecondMatrix.Columns.Length
143-
let rightVals = Array.zeroCreate<float32> base.SecondMatrix.Values.Length
144-
Array.blit base.SecondMatrix.Rows 0 rightRows 0 base.SecondMatrix.Rows.Length
145-
Array.blit base.SecondMatrix.Columns 0 rightCols 0 base.SecondMatrix.Columns.Length
146-
Array.blit base.SecondMatrix.Values 0 rightVals 0 base.SecondMatrix.Values.Length
124+
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
125+
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
126+
let rightVals = Array.zeroCreate<float32> this.SecondMatrix.Values.Length
127+
Array.blit this.SecondMatrix.Rows 0 rightRows 0 this.SecondMatrix.Rows.Length
128+
Array.blit this.SecondMatrix.Columns 0 rightCols 0 this.SecondMatrix.Columns.Length
129+
Array.blit this.SecondMatrix.Values 0 rightVals 0 this.SecondMatrix.Values.Length
147130

148131
rightCOO <-
149132
Matrix.Build<float32>(
150-
base.SecondMatrix.RowCount,
151-
base.SecondMatrix.ColumnCount,
152-
rightRows,
153-
rightCols,
154-
rightVals,
133+
this.SecondMatrix.RowCount,
134+
this.SecondMatrix.ColumnCount,
135+
this.SecondMatrix.Rows,
136+
this.SecondMatrix.Columns,
137+
this.SecondMatrix.Values,
155138
COO
156139
)
157140

@@ -161,43 +144,88 @@ type EWiseAddBenchmarks4Float32() =
161144
leftCOO.EWiseAdd rightCOO None Float32Semiring.addMult
162145
|> context.RunSync
163146

147+
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)
175+
| _ -> 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+
})
184+
185+
[<Config(typeof<BoolConfig>)>]
164186
type EWiseAddBenchmarks4Bool() =
165187
inherit EWiseAddBenchmarks()
166188

167189
let mutable leftCOO = Unchecked.defaultof<Matrix<bool>>
168190
let mutable rightCOO = Unchecked.defaultof<Matrix<bool>>
169191

192+
member val FirstMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
193+
member val SecondMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
194+
195+
[<ParamsSource("InputMatricesProvider")>]
196+
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat<bool>> with get, set
197+
170198
[<IterationSetup>]
171199
member this.BuildCOO() =
172-
let leftRows = Array.zeroCreate<int> base.FirstMatrix.Rows.Length
173-
let leftCols = Array.zeroCreate<int> base.FirstMatrix.Columns.Length
174-
let leftVals = Array.create<bool> base.FirstMatrix.Values.Length true
175-
Array.blit base.FirstMatrix.Rows 0 leftRows 0 base.FirstMatrix.Rows.Length
176-
Array.blit base.FirstMatrix.Columns 0 leftCols 0 base.FirstMatrix.Columns.Length
200+
let leftRows = Array.zeroCreate<int> this.FirstMatrix.Rows.Length
201+
let leftCols = Array.zeroCreate<int> this.FirstMatrix.Columns.Length
202+
let leftVals = Array.create<bool> this.FirstMatrix.Values.Length true
203+
Array.blit this.FirstMatrix.Rows 0 leftRows 0 this.FirstMatrix.Rows.Length
204+
Array.blit this.FirstMatrix.Columns 0 leftCols 0 this.FirstMatrix.Columns.Length
177205

178206
leftCOO <-
179207
Matrix.Build<bool>(
180-
base.FirstMatrix.RowCount,
181-
base.FirstMatrix.ColumnCount,
182-
leftRows,
183-
leftCols,
184-
leftVals,
208+
this.FirstMatrix.RowCount,
209+
this.FirstMatrix.ColumnCount,
210+
this.FirstMatrix.Rows,
211+
this.FirstMatrix.Columns,
212+
this.FirstMatrix.Values,
185213
COO
186214
)
187215

188-
let rightRows = Array.zeroCreate<int> base.SecondMatrix.Rows.Length
189-
let rightCols = Array.zeroCreate<int> base.SecondMatrix.Columns.Length
190-
let rightVals = Array.create<bool> base.SecondMatrix.Values.Length true
191-
Array.blit base.SecondMatrix.Rows 0 rightRows 0 base.SecondMatrix.Rows.Length
192-
Array.blit base.SecondMatrix.Columns 0 rightCols 0 base.SecondMatrix.Columns.Length
216+
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
217+
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
218+
let rightVals = Array.create<bool> this.SecondMatrix.Values.Length true
219+
Array.blit this.SecondMatrix.Rows 0 rightRows 0 this.SecondMatrix.Rows.Length
220+
Array.blit this.SecondMatrix.Columns 0 rightCols 0 this.SecondMatrix.Columns.Length
193221

194222
rightCOO <-
195223
Matrix.Build<bool>(
196-
base.SecondMatrix.RowCount,
197-
base.SecondMatrix.ColumnCount,
198-
rightRows,
199-
rightCols,
200-
rightVals,
224+
this.SecondMatrix.RowCount,
225+
this.SecondMatrix.ColumnCount,
226+
this.SecondMatrix.Rows,
227+
this.SecondMatrix.Columns,
228+
this.SecondMatrix.Values,
201229
COO
202230
)
203231

@@ -206,3 +234,34 @@ type EWiseAddBenchmarks4Bool() =
206234
let (ClContext context) = this.OclContext
207235
leftCOO.EWiseAdd rightCOO None BooleanSemiring.anyAll
208236
|> context.RunSync
237+
238+
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)
259+
| _ -> 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+
})

benchmarks/GraphBLAS-sharp.Benchmarks/Common.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ with
2828

2929
sprintf "%s, %s" platformName deviceType
3030

31-
type InputMatrixFormat = {
31+
type InputMatrixFormat<'a> = {
3232
MatrixName: string
33-
MatrixStructure: COOFormat<float32>
33+
MatrixStructure: COOFormat<'a>
3434
}
3535
with
3636
override this.ToString() =
3737
sprintf "%s" this.MatrixName
3838

39-
type MatrixShapeColumn(columnName: string, getShape: InputMatrixFormat -> int) =
39+
type MatrixShapeColumn<'a>(columnName: string, getShape: InputMatrixFormat<'a> -> int) =
4040
interface IColumn with
4141
member this.AlwaysShow: bool = true
4242
member this.Category: ColumnCategory = ColumnCategory.Params
4343
member this.ColumnName: string = columnName
4444
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase): string =
45-
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat
45+
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat<'a>
4646
sprintf "%i" <| getShape inputMatrix
4747
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase, style: SummaryStyle): string =
4848
(this :> IColumn).GetValue(summary, benchmarkCase)
@@ -54,13 +54,13 @@ type MatrixShapeColumn(columnName: string, getShape: InputMatrixFormat -> int) =
5454
member this.PriorityInCategory: int = 1
5555
member this.UnitType: UnitType = UnitType.Size
5656

57-
type TEPSColumn() =
57+
type TEPSColumn<'a>() =
5858
interface IColumn with
5959
member this.AlwaysShow: bool = true
6060
member this.Category: ColumnCategory = ColumnCategory.Statistics
6161
member this.ColumnName: string = "TEPS"
6262
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase): string =
63-
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat
63+
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat<'a>
6464
let (nrows, ncols, nnz) =
6565
inputMatrix.MatrixStructure.RowCount,
6666
inputMatrix.MatrixStructure.ColumnCount,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Intel*
2+
All

0 commit comments

Comments
 (0)