Skip to content

Commit bd67a41

Browse files
committed
Add toHost bench
1 parent b400b84 commit bd67a41

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,102 @@ type EWiseAddBenchmarks4Float32() =
187187
| _ -> failwith "Unsupported matrix format"
188188
)
189189

190+
type ToHostBenchmarks4Float32() =
191+
inherit EWiseAddBenchmarks()
192+
193+
let mutable leftCOO = Unchecked.defaultof<Matrix<float32>>
194+
let mutable rightCOO = Unchecked.defaultof<Matrix<float32>>
195+
let mutable resultCOO = Unchecked.defaultof<Matrix<float32>>
196+
197+
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
198+
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
199+
200+
[<GlobalSetup>]
201+
member this.FormInputData() =
202+
let mtxFormat = GraphReader.readMtx <| Utils.getFullPathToMatrix this.InputMatrix.Filename
203+
let cooMatrix =
204+
match mtxFormat.Shape.Format, mtxFormat.Shape.Field with
205+
| "coordinate", "real" -> Utils.makeCOO mtxFormat <| FromString float32
206+
| "coordinate", "integer" -> Utils.makeCOO mtxFormat <| FromString float32
207+
| "coordinate", "pattern" ->
208+
let rand = System.Random()
209+
let nextSingle (random: System.Random) =
210+
let buffer = Array.zeroCreate<byte> 4
211+
random.NextBytes buffer
212+
System.BitConverter.ToSingle(buffer, 0)
213+
214+
Utils.makeCOO mtxFormat <| FromUnit (fun () -> nextSingle rand)
215+
| _ -> failwith "Unsupported matrix format"
216+
217+
this.FirstMatrix <- cooMatrix
218+
this.SecondMatrix <- cooMatrix |> Utils.transposeCOO
219+
220+
[<IterationSetup>]
221+
member this.BuildCOO() =
222+
let leftRows = Array.zeroCreate<int> this.FirstMatrix.Rows.Length
223+
let leftCols = Array.zeroCreate<int> this.FirstMatrix.Columns.Length
224+
let leftVals = Array.zeroCreate<float32> this.FirstMatrix.Values.Length
225+
Array.blit this.FirstMatrix.Rows 0 leftRows 0 this.FirstMatrix.Rows.Length
226+
Array.blit this.FirstMatrix.Columns 0 leftCols 0 this.FirstMatrix.Columns.Length
227+
Array.blit this.FirstMatrix.Values 0 leftVals 0 this.FirstMatrix.Values.Length
228+
229+
leftCOO <-
230+
Matrix.Build<float32>(
231+
this.FirstMatrix.RowCount,
232+
this.FirstMatrix.ColumnCount,
233+
leftRows,
234+
leftCols,
235+
leftVals,
236+
COO
237+
)
238+
239+
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
240+
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
241+
let rightVals = Array.zeroCreate<float32> this.SecondMatrix.Values.Length
242+
Array.blit this.SecondMatrix.Rows 0 rightRows 0 this.SecondMatrix.Rows.Length
243+
Array.blit this.SecondMatrix.Columns 0 rightCols 0 this.SecondMatrix.Columns.Length
244+
Array.blit this.SecondMatrix.Values 0 rightVals 0 this.SecondMatrix.Values.Length
245+
246+
rightCOO <-
247+
Matrix.Build<float32>(
248+
this.SecondMatrix.RowCount,
249+
this.SecondMatrix.ColumnCount,
250+
rightRows,
251+
rightCols,
252+
rightVals,
253+
COO
254+
)
255+
256+
let (ClContext context) = this.OclContext
257+
resultCOO <-
258+
leftCOO.EWiseAdd rightCOO None Float32Semiring.addMult
259+
|> context.RunSync
260+
261+
[<Benchmark>]
262+
member this.ToHostFloat32() =
263+
resultCOO.ToHost()
264+
265+
static member InputMatricesProvider =
266+
let matricesFilenames =
267+
let pathToConfig =
268+
Path.Combine [|
269+
__SOURCE_DIRECTORY__
270+
"Configs"
271+
"EWiseAddBenchmarks4Float32.txt"
272+
|] |> Path.GetFullPath
273+
274+
File.ReadAllLines pathToConfig
275+
|> Seq.ofArray
276+
|> Seq.filter (fun line -> not <| line.StartsWith "!")
277+
278+
matricesFilenames
279+
|> Seq.map
280+
(fun matrixFilename ->
281+
match Path.GetExtension matrixFilename with
282+
| ".mtx" -> GraphReader.readShapeMtx <| Utils.getFullPathToMatrix matrixFilename
283+
| _ -> failwith "Unsupported matrix format"
284+
)
285+
190286
type EWiseAddBenchmarks4Bool() =
191287
inherit EWiseAddBenchmarks()
192288

benchmarks/GraphBLAS-sharp.Benchmarks/Program.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let main argv =
88
let benchmarks = BenchmarkSwitcher [|
99
typeof<EWiseAddBenchmarks4Float32>
1010
typeof<EWiseAddBenchmarks4Bool>
11+
typeof<ToHostBenchmarks4Float32>
1112
|]
1213

1314
benchmarks.Run argv |> ignore

0 commit comments

Comments
 (0)