Skip to content

Commit b330686

Browse files
committed
Add eWiseAdd benchmarks on bool
1 parent add9637 commit b330686

8 files changed

Lines changed: 238 additions & 231 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksBFS.fs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ open System.IO
99
open System
1010
open MatrixBackend
1111

12-
[<Config(typeof<Config>)>]
1312
[<SimpleJob(targetCount=10)>]
1413
type BFSBenchmark4CSRMatrix() =
1514
let random = Random()
Lines changed: 145 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,63 @@
11
namespace GraphBLAS.FSharp.Benchmarks
22

3+
open System.IO
34
open GraphBLAS.FSharp
5+
open GraphBLAS.FSharp.Predefined
6+
open GraphBLAS.FSharp.MatrixBackend
47
open BenchmarkDotNet.Attributes
58
open BenchmarkDotNet.Engines
6-
open System.IO
7-
open MatrixBackend
8-
open GraphBLAS.FSharp.Predefined
9-
open MathNet.Numerics
9+
open BenchmarkDotNet.Configs
10+
open BenchmarkDotNet.Columns
11+
open BenchmarkDotNet.Filters
12+
open BenchmarkDotNet.Jobs
13+
open BenchmarkDotNet.Order
1014
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
1115
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
1216
open OpenCL.Net
1317

1418
module Float32Monoid =
1519
let add: Monoid<float32> = {
1620
Zero = 0.f
17-
Append = BinaryOp <@ ( + ) @>
21+
Append = BinaryOp <@ (+) @>
1822
}
1923

2024
module Float32Semiring =
2125
let addMult: Semiring<float32> = {
2226
PlusMonoid = Float32Monoid.add
23-
Times = BinaryOp <@ ( * ) @>
27+
Times = BinaryOp <@ (*) @>
2428
}
2529

26-
type ClContext = ClContext of OpenCLEvaluationContext
27-
with
28-
override this.ToString() =
29-
let mutable e = ErrorCode.Unknown
30-
let (ClContext context) = this
31-
let device = context.Device
32-
let deviceName = Cl.GetDeviceInfo(device, DeviceInfo.Name, &e).ToString()
33-
if deviceName.Length < 20 then
34-
sprintf "%s" deviceName
35-
else
36-
let platform = Cl.GetDeviceInfo(device, DeviceInfo.Platform, &e).CastTo<Platform>()
37-
let platformName = Cl.GetPlatformInfo(platform, PlatformInfo.Name, &e).ToString()
38-
let deviceType =
39-
match Cl.GetDeviceInfo(device, DeviceInfo.Type, &e).CastTo<DeviceType>() with
40-
| DeviceType.Cpu -> "CPU"
41-
| DeviceType.Gpu -> "GPU"
42-
| DeviceType.Accelerator -> "Accelerator"
43-
| _ -> "another"
44-
45-
sprintf "%s, %s" platformName deviceType
46-
47-
[<MinColumn; MaxColumn>]
48-
[<Config(typeof<Config>)>]
49-
[<SimpleJob(RunStrategy.Monitoring, targetCount=1)>]
50-
type EWiseAddBenchmarks4Float32() =
51-
let mutable leftCOO = Unchecked.defaultof<Matrix<float32>>
52-
let mutable rightCOO = Unchecked.defaultof<Matrix<float32>>
53-
54-
let mutable leftCSR = Unchecked.defaultof<Matrix<float32>>
55-
let mutable rightCSR = Unchecked.defaultof<Matrix<float32>>
56-
57-
let mutable leftMathNetSparse =
58-
Unchecked.defaultof<LinearAlgebra.Matrix<float32>>
59-
60-
let mutable rightMathNetSparse =
61-
Unchecked.defaultof<LinearAlgebra.Matrix<float32>>
62-
63-
let mutable firstMatrix = Unchecked.defaultof<COOFormat<float32>>
64-
let mutable secondMatrix = Unchecked.defaultof<COOFormat<float32>>
30+
type Config() =
31+
inherit ManualConfig()
6532

6633
do
67-
Control.UseNativeMKL()
68-
Control.UseMultiThreading()
34+
base.AddColumn(
35+
MatrixShapeColumn("RowCount", fun matrix -> matrix.MatrixStructure.RowCount) :> IColumn,
36+
MatrixShapeColumn("ColumnCount", fun matrix -> matrix.MatrixStructure.ColumnCount) :> IColumn,
37+
MatrixShapeColumn("NNZ", fun matrix -> matrix.MatrixStructure.Values.Length) :> IColumn,
38+
TEPSColumn() :> IColumn,
39+
StatisticColumn.Min,
40+
StatisticColumn.Max
41+
) |> ignore
42+
43+
base.AddFilter(
44+
DisjunctionFilter(
45+
NameFilter(fun name -> name.Contains "COO") :> IFilter
46+
)
47+
) |> ignore
48+
49+
[<Config(typeof<Config>)>]
50+
[<SimpleJob(RunStrategy.Monitoring, targetCount=2)>]
51+
type EWiseAddBenchmarks() =
52+
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
53+
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
6954

7055
[<ParamsSource("InputMatricesProvider")>]
7156
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat> with get, set
7257

58+
[<ParamsSource("AvaliableContexts")>]
59+
member val OclContext = Unchecked.defaultof<ClContext> with get, set
60+
7361
[<GlobalSetup>]
7462
member this.FormInputData() =
7563
let transposeCOO (matrix: COOFormat<float32>) =
@@ -87,97 +75,13 @@ type EWiseAddBenchmarks4Float32() =
8775
ColumnCount = matrix.RowCount
8876
}
8977

90-
firstMatrix <- this.InputMatrix.MatrixStructure
91-
secondMatrix <- transposeCOO this.InputMatrix.MatrixStructure
92-
93-
[<IterationSetup(Target="EWiseAdditionCOO")>]
94-
member this.BuildCOO() =
95-
let leftRows = Array.zeroCreate<int> firstMatrix.Rows.Length
96-
let leftCols = Array.zeroCreate<int> firstMatrix.Columns.Length
97-
let leftVals = Array.zeroCreate<float32> firstMatrix.Values.Length
98-
Array.blit firstMatrix.Rows 0 leftRows 0 firstMatrix.Rows.Length
99-
Array.blit firstMatrix.Columns 0 leftCols 0 firstMatrix.Columns.Length
100-
Array.blit firstMatrix.Values 0 leftVals 0 firstMatrix.Values.Length
101-
102-
leftCOO <-
103-
Matrix.Build<float32>(
104-
firstMatrix.RowCount,
105-
firstMatrix.ColumnCount,
106-
leftRows,
107-
leftCols,
108-
leftVals,
109-
COO
110-
)
78+
this.FirstMatrix <- this.InputMatrix.MatrixStructure
79+
this.SecondMatrix <- this.InputMatrix.MatrixStructure |> transposeCOO
11180

112-
let rightRows = Array.zeroCreate<int> secondMatrix.Rows.Length
113-
let rightCols = Array.zeroCreate<int> secondMatrix.Columns.Length
114-
let rightVals = Array.zeroCreate<float32> secondMatrix.Values.Length
115-
Array.blit secondMatrix.Rows 0 rightRows 0 secondMatrix.Rows.Length
116-
Array.blit secondMatrix.Columns 0 rightCols 0 secondMatrix.Columns.Length
117-
Array.blit secondMatrix.Values 0 rightVals 0 secondMatrix.Values.Length
118-
119-
rightCOO <-
120-
Matrix.Build<float32>(
121-
secondMatrix.RowCount,
122-
secondMatrix.ColumnCount,
123-
rightRows,
124-
rightCols,
125-
rightVals,
126-
COO
127-
)
128-
129-
[<IterationSetup(Target="EWiseAdditionCSR")>]
130-
member this.BuildCSR() =
131-
leftCSR <-
132-
Matrix.Build<float32>(
133-
firstMatrix.RowCount,
134-
firstMatrix.ColumnCount,
135-
firstMatrix.Rows,
136-
firstMatrix.Columns,
137-
firstMatrix.Values,
138-
CSR
139-
)
140-
141-
rightCSR <-
142-
Matrix.Build<float32>(
143-
secondMatrix.RowCount,
144-
secondMatrix.ColumnCount,
145-
secondMatrix.Rows,
146-
secondMatrix.Columns,
147-
secondMatrix.Values,
148-
CSR
149-
)
150-
151-
[<IterationSetup(Target="EWiseAdditionMathNetSparse")>]
152-
member this.BuildMathNetSparse() =
153-
leftMathNetSparse <-
154-
LinearAlgebra.SparseMatrix.ofListi
155-
firstMatrix.RowCount
156-
firstMatrix.ColumnCount
157-
(List.ofArray <| Array.zip3 firstMatrix.Rows firstMatrix.Columns firstMatrix.Values)
158-
159-
rightMathNetSparse <-
160-
LinearAlgebra.SparseMatrix.ofListi
161-
secondMatrix.RowCount
162-
secondMatrix.ColumnCount
163-
(List.ofArray <| Array.zip3 secondMatrix.Rows secondMatrix.Columns secondMatrix.Values)
164-
165-
[<Benchmark>]
166-
[<ArgumentsSource("AvaliableContextsProvider")>]
167-
member this.EWiseAdditionCOO(clContext: ClContext) =
168-
let (ClContext context) = clContext
169-
leftCOO.EWiseAdd rightCOO None Float32Semiring.addMult
170-
|> context.RunSync
171-
172-
[<Benchmark>]
173-
[<ArgumentsSource("AvaliableContextsProvider")>]
174-
member this.EWiseAdditionCSR(clContext: ClContext) =
175-
let (ClContext context) = clContext
176-
leftCSR.EWiseAdd rightCOO None Float32Semiring.addMult
177-
|> context.RunSync
178-
179-
[<Benchmark(Baseline=true)>]
180-
member this.EWiseAdditionMathNetSparse() = leftMathNetSparse + rightMathNetSparse
81+
[<IterationCleanup>]
82+
member this.ClearBuffers() =
83+
let (ClContext context) = this.OclContext
84+
context.Provider.CloseAllBuffers()
18185

18286
/// Sequence of paths to files where data for benchmarking will be taken from
18387
static member InputMatricesProvider =
@@ -188,59 +92,27 @@ type EWiseAddBenchmarks4Float32() =
18892
"webbase-1M.mtx"
18993
}
19094

191-
let getFullPathToMatrix filename =
192-
Path.Join [| __SOURCE_DIRECTORY__
193-
"Datasets"
194-
"EWiseAddDatasets"
195-
filename |]
196-
197-
let getCOO (pathToGraph: string) =
198-
use streamReader = new StreamReader(pathToGraph)
199-
200-
while streamReader.Peek() = int '%' do
201-
streamReader.ReadLine() |> ignore
202-
203-
let matrixInfo =
204-
streamReader.ReadLine().Split(' ')
205-
|> Array.map int
206-
207-
let (nrows, ncols, nnz) =
208-
matrixInfo.[0], matrixInfo.[1], matrixInfo.[2]
209-
210-
[0 .. nnz - 1]
211-
|> List.map
212-
(fun _ ->
213-
streamReader.ReadLine().Split(' ')
214-
|> (fun line -> int line.[0], int line.[1], float32 line.[2]))
215-
|> List.toArray
216-
|> Array.sortBy (fun (row, _, _) -> row)
217-
|> Array.unzip3
218-
|>
219-
fun (rows, cols, values) ->
220-
let c f x y = f y x
221-
let rows = rows |> Array.map (c (-) 1)
222-
let cols = cols |> Array.map (c (-) 1)
223-
{
224-
Rows = rows
225-
Columns = cols
226-
Values = values
227-
RowCount = nrows
228-
ColumnCount = ncols
229-
}
230-
23195
matricesFilenames
23296
|> Seq.map
23397
(fun matrixFilename ->
98+
let getFullPathToMatrix filename =
99+
Path.Join [|
100+
__SOURCE_DIRECTORY__
101+
"Datasets"
102+
"EWiseAddDatasets"
103+
filename
104+
|]
105+
234106
let fullPath = getFullPathToMatrix matrixFilename
235107
let matrixName = Path.GetFileNameWithoutExtension matrixFilename
236-
let matrixStructure = getCOO fullPath
108+
let matrixStructure = GraphReader.readMtx fullPath
237109
{
238110
MatrixName = matrixName
239111
MatrixStructure = matrixStructure
240112
}
241113
)
242114

243-
static member AvaliableContextsProvider =
115+
static member AvaliableContexts =
244116
let mutable e = ErrorCode.Unknown
245117
Cl.GetPlatformIDs &e
246118
|> Array.collect (fun platform -> Cl.GetDeviceIDs(platform, DeviceType.All, &e))
@@ -252,3 +124,97 @@ type EWiseAddBenchmarks4Float32() =
252124
let deviceType = Cl.GetDeviceInfo(device, DeviceInfo.Type, &e).CastTo<DeviceType>()
253125
OpenCLEvaluationContext(platformName, deviceType) |> ClContext
254126
)
127+
128+
type EWiseAddBenchmarks4Float32() =
129+
inherit EWiseAddBenchmarks()
130+
131+
let mutable leftCOO = Unchecked.defaultof<Matrix<float32>>
132+
let mutable rightCOO = Unchecked.defaultof<Matrix<float32>>
133+
134+
[<IterationSetup>]
135+
member this.BuildCOO() =
136+
let leftRows = Array.zeroCreate<int> base.FirstMatrix.Rows.Length
137+
let leftCols = Array.zeroCreate<int> base.FirstMatrix.Columns.Length
138+
let leftVals = Array.zeroCreate<float32> base.FirstMatrix.Values.Length
139+
Array.blit base.FirstMatrix.Rows 0 leftRows 0 base.FirstMatrix.Rows.Length
140+
Array.blit base.FirstMatrix.Columns 0 leftCols 0 base.FirstMatrix.Columns.Length
141+
Array.blit base.FirstMatrix.Values 0 leftVals 0 base.FirstMatrix.Values.Length
142+
143+
leftCOO <-
144+
Matrix.Build<float32>(
145+
base.FirstMatrix.RowCount,
146+
base.FirstMatrix.ColumnCount,
147+
leftRows,
148+
leftCols,
149+
leftVals,
150+
COO
151+
)
152+
153+
let rightRows = Array.zeroCreate<int> base.SecondMatrix.Rows.Length
154+
let rightCols = Array.zeroCreate<int> base.SecondMatrix.Columns.Length
155+
let rightVals = Array.zeroCreate<float32> base.SecondMatrix.Values.Length
156+
Array.blit base.SecondMatrix.Rows 0 rightRows 0 base.SecondMatrix.Rows.Length
157+
Array.blit base.SecondMatrix.Columns 0 rightCols 0 base.SecondMatrix.Columns.Length
158+
Array.blit base.SecondMatrix.Values 0 rightVals 0 base.SecondMatrix.Values.Length
159+
160+
rightCOO <-
161+
Matrix.Build<float32>(
162+
base.SecondMatrix.RowCount,
163+
base.SecondMatrix.ColumnCount,
164+
rightRows,
165+
rightCols,
166+
rightVals,
167+
COO
168+
)
169+
170+
[<Benchmark>]
171+
member this.EWiseAdditionCOOFloat32() =
172+
let (ClContext context) = this.OclContext
173+
leftCOO.EWiseAdd rightCOO None Float32Semiring.addMult
174+
|> context.RunSync
175+
176+
type EWiseAddBenchmarks4Bool() =
177+
inherit EWiseAddBenchmarks()
178+
179+
let mutable leftCOO = Unchecked.defaultof<Matrix<bool>>
180+
let mutable rightCOO = Unchecked.defaultof<Matrix<bool>>
181+
182+
[<IterationSetup>]
183+
member this.BuildCOO() =
184+
let leftRows = Array.zeroCreate<int> base.FirstMatrix.Rows.Length
185+
let leftCols = Array.zeroCreate<int> base.FirstMatrix.Columns.Length
186+
let leftVals = Array.create<bool> base.FirstMatrix.Values.Length true
187+
Array.blit base.FirstMatrix.Rows 0 leftRows 0 base.FirstMatrix.Rows.Length
188+
Array.blit base.FirstMatrix.Columns 0 leftCols 0 base.FirstMatrix.Columns.Length
189+
190+
leftCOO <-
191+
Matrix.Build<bool>(
192+
base.FirstMatrix.RowCount,
193+
base.FirstMatrix.ColumnCount,
194+
leftRows,
195+
leftCols,
196+
leftVals,
197+
COO
198+
)
199+
200+
let rightRows = Array.zeroCreate<int> base.SecondMatrix.Rows.Length
201+
let rightCols = Array.zeroCreate<int> base.SecondMatrix.Columns.Length
202+
let rightVals = Array.create<bool> base.SecondMatrix.Values.Length true
203+
Array.blit base.SecondMatrix.Rows 0 rightRows 0 base.SecondMatrix.Rows.Length
204+
Array.blit base.SecondMatrix.Columns 0 rightCols 0 base.SecondMatrix.Columns.Length
205+
206+
rightCOO <-
207+
Matrix.Build<bool>(
208+
base.SecondMatrix.RowCount,
209+
base.SecondMatrix.ColumnCount,
210+
rightRows,
211+
rightCols,
212+
rightVals,
213+
COO
214+
)
215+
216+
[<Benchmark>]
217+
member this.EWiseAdditionCOOBool() =
218+
let (ClContext context) = this.OclContext
219+
leftCOO.EWiseAdd rightCOO None BooleanSemiring.anyAll
220+
|> context.RunSync

0 commit comments

Comments
 (0)