Skip to content

Commit 690496c

Browse files
committed
Moved matrix building from helpers, added benchmark with zeros filter
1 parent eccc4ee commit 690496c

4 files changed

Lines changed: 129 additions & 89 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
223223
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
224224
float32,
225225
(fun _ -> Utils.nextSingle (System.Random())),
226-
Utils.buildCooMatrix
226+
COOMatrix<float32>.ToBackend
227227
)
228228

229229
static member InputMatricesProvider =
@@ -235,7 +235,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
235235
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
236236
float32,
237237
(fun _ -> Utils.nextSingle (System.Random())),
238-
Utils.buildCooMatrix,
238+
COOMatrix<float32>.ToBackend,
239239
M.resultToHostCOO
240240
)
241241

@@ -249,7 +249,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
249249
(fun context wgSize -> Backend.COOMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
250250
(fun _ -> true),
251251
(fun _ -> true),
252-
Utils.buildCooMatrix
252+
COOMatrix<bool>.ToBackend
253253
)
254254

255255
static member InputMatricesProvider =
@@ -262,7 +262,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
262262
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.float32Sum wgSize),
263263
float32,
264264
(fun _ -> Utils.nextSingle (System.Random())),
265-
Utils.buildCsrMatrix
265+
CSRMatrix<float32>.ToBackend
266266
)
267267

268268
static member InputMatricesProvider =
@@ -275,7 +275,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
275275
(fun context wgSize -> Backend.CSRMatrix.eWiseAdd context Backend.Common.StandardOperations.boolSum wgSize),
276276
(fun _ -> true),
277277
(fun _ -> true),
278-
Utils.buildCsrMatrix
278+
CSRMatrix<bool>.ToBackend
279279
)
280280

281281
static member InputMatricesProvider =
@@ -289,7 +289,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
289289
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
290290
(fun _ -> true),
291291
(fun _ -> true),
292-
Utils.buildCooMatrix
292+
COOMatrix<bool>.ToBackend
293293
)
294294

295295
static member InputMatricesProvider =
@@ -301,7 +301,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
301301
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.boolSumAtLeastOne wgSize),
302302
(fun _ -> true),
303303
(fun _ -> true),
304-
Utils.buildCsrMatrix
304+
CSRMatrix<bool>.ToBackend
305305
)
306306

307307
static member InputMatricesProvider =
@@ -313,7 +313,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
313313
(fun context wgSize -> Backend.COOMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
314314
float32,
315315
(fun _ -> Utils.nextSingle (System.Random())),
316-
Utils.buildCooMatrix
316+
COOMatrix<float32>.ToBackend
317317
)
318318

319319
static member InputMatricesProvider =
@@ -325,7 +325,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
325325
(fun context wgSize -> Backend.CSRMatrix.eWiseAddAtLeastOne context Backend.Common.StandardOperations.float32SumAtLeastOne wgSize),
326326
float32,
327327
(fun _ -> Utils.nextSingle (System.Random())),
328-
Utils.buildCsrMatrix
328+
CSRMatrix<float32>.ToBackend
329329
)
330330

331331
static member InputMatricesProvider =

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxm.fs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ type MxmBenchmarksWithTransposing<'elem when 'elem : struct>(
205205
module Operations =
206206
let add = <@ fun x y -> Some (x + y) @>
207207

208+
let addWithFilter = <@ fun x y ->
209+
let res = x + y
210+
if abs res < 1e-8f then None else Some res
211+
@>
212+
208213
let mult = <@ fun x y -> Some (x * y) @>
209214

210215
let logicalOr = <@ fun x y ->
@@ -231,7 +236,7 @@ type MxmBenchmarks4Float32MultiplicationOnly() =
231236
(Backend.Matrix.mxm Operations.add Operations.mult),
232237
float32,
233238
(fun _ -> Utils.nextSingle (System.Random())),
234-
(fun context matrix -> Backend.MatrixCSR (Utils.buildCsrMatrix context matrix))
239+
(fun context matrix -> Backend.MatrixCSR (CSRMatrix<float32>.ToBackend context matrix))
235240
)
236241

237242
static member InputMatrixProvider =
@@ -243,7 +248,7 @@ type MxmBenchmarks4Float32WithTransposing() =
243248
(Backend.Matrix.mxm Operations.add Operations.mult),
244249
float32,
245250
(fun _ -> Utils.nextSingle (System.Random())),
246-
(fun context matrix -> Backend.MatrixCSR (Utils.buildCsrMatrix context matrix))
251+
(fun context matrix -> Backend.MatrixCSR (CSRMatrix<float32>.ToBackend context matrix))
247252
)
248253

249254
static member InputMatrixProvider =
@@ -255,7 +260,7 @@ type MxmBenchmarks4BoolMultiplicationOnly() =
255260
(Backend.Matrix.mxm Operations.logicalOr Operations.logicalAnd),
256261
(fun _ -> true),
257262
(fun _ -> true),
258-
(fun context matrix -> Backend.MatrixCSR (Utils.buildCsrMatrix context matrix))
263+
(fun context matrix -> Backend.MatrixCSR (CSRMatrix<bool>.ToBackend context matrix))
259264
)
260265

261266
static member InputMatrixProvider =
@@ -267,8 +272,32 @@ type MxmBenchmarks4BoolWithTransposing() =
267272
(Backend.Matrix.mxm Operations.logicalOr Operations.logicalAnd),
268273
(fun _ -> true),
269274
(fun _ -> true),
270-
(fun context matrix -> Backend.MatrixCSR (Utils.buildCsrMatrix context matrix))
275+
(fun context matrix -> Backend.MatrixCSR (CSRMatrix<bool>.ToBackend context matrix))
271276
)
272277

273278
static member InputMatrixProvider =
274279
MxmBenchmarks<_>.InputMatrixProviderBuilder "MxmBenchmarks4Bool.txt"
280+
281+
type MxmBenchmarks4Float32MultiplicationOnlyWithZerosFilter() =
282+
283+
inherit MxmBenchmarksMultiplicationOnly<float32>(
284+
(Backend.Matrix.mxm Operations.addWithFilter Operations.mult),
285+
float32,
286+
(fun _ -> Utils.nextSingle (System.Random())),
287+
(fun context matrix -> Backend.MatrixCSR (CSRMatrix<float32>.ToBackend context matrix))
288+
)
289+
290+
static member InputMatrixProvider =
291+
MxmBenchmarks<_>.InputMatrixProviderBuilder "MxmBenchmarks4Float32.txt"
292+
293+
type MxmBenchmarks4Float32WithTransposingWithZerosFilter() =
294+
295+
inherit MxmBenchmarksWithTransposing<float32>(
296+
(Backend.Matrix.mxm Operations.addWithFilter Operations.mult),
297+
float32,
298+
(fun _ -> Utils.nextSingle (System.Random())),
299+
(fun context matrix -> Backend.MatrixCSR (CSRMatrix<float32>.ToBackend context matrix))
300+
)
301+
302+
static member InputMatrixProvider =
303+
MxmBenchmarks<_>.InputMatrixProviderBuilder "MxmBenchmarks4Float32.txt"

benchmarks/GraphBLAS-sharp.Benchmarks/Helpers.fs

Lines changed: 22 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -229,79 +229,25 @@ module Utils =
229229
random.NextBytes buffer
230230
System.BitConverter.ToSingle(buffer, 0)
231231

232-
let rowPointers2rowIndices (rowPointers: int []) =
233-
let rowIndices =
234-
Array.zeroCreate rowPointers.[rowPointers.Length - 1]
235-
236-
[| 0 .. rowPointers.Length - 2 |]
237-
|> Array.Parallel.iter
238-
(fun i ->
239-
[| rowPointers.[i] .. rowPointers.[i + 1] - 1 |]
240-
|> Array.Parallel.iter (fun j -> rowIndices.[j] <- i))
241-
242-
rowIndices
243-
244-
let rowIndices2rowPointers (rowIndices: int []) rowCount =
245-
let nnzPerRow = Array.zeroCreate rowCount
246-
let rowPointers = Array.zeroCreate rowCount
247-
248-
Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
249-
250-
for i in 1 .. rowCount - 1 do
251-
rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
252-
253-
rowPointers
254-
255-
let inline buildCooMatrix (context:ClContext) matrix =
256-
match matrix with
257-
| MatrixCOO m ->
258-
let rows =
259-
context.CreateClArray (m.Rows, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
260-
261-
let cols =
262-
context.CreateClArray (m.Columns, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
263-
264-
let vals =
265-
context.CreateClArray (m.Values, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
266-
267-
{ Backend.COOMatrix.Context = context
268-
Backend.COOMatrix.RowCount = m.RowCount
269-
Backend.COOMatrix.ColumnCount = m.ColumnCount
270-
Backend.COOMatrix.Rows = rows
271-
Backend.COOMatrix.Columns = cols
272-
Backend.COOMatrix.Values = vals }
273-
274-
| x -> failwith "Unsupported matrix format: %A"
275-
276-
let inline buildCsrMatrix (context:ClContext) matrix =
277-
match matrix with
278-
| MatrixCOO m ->
279-
let rowPointers =
280-
context.CreateClArray(
281-
Utils.rowIndices2rowPointers m.Rows m.RowCount
282-
,hostAccessMode = HostAccessMode.ReadOnly
283-
,deviceAccessMode = DeviceAccessMode.ReadOnly
284-
,allocationMode = AllocationMode.CopyHostPtr)
285-
286-
let cols =
287-
context.CreateClArray (
288-
m.Columns
289-
,hostAccessMode = HostAccessMode.ReadOnly
290-
,deviceAccessMode = DeviceAccessMode.ReadOnly
291-
,allocationMode = AllocationMode.CopyHostPtr)
292-
293-
let vals =
294-
context.CreateClArray (
295-
m.Values
296-
,hostAccessMode = HostAccessMode.ReadOnly
297-
,deviceAccessMode = DeviceAccessMode.ReadOnly
298-
,allocationMode = AllocationMode.CopyHostPtr)
299-
300-
{ Backend.CSRMatrix.Context = context
301-
Backend.CSRMatrix.RowCount = m.RowCount
302-
Backend.CSRMatrix.ColumnCount = m.ColumnCount
303-
Backend.CSRMatrix.RowPointers = rowPointers
304-
Backend.CSRMatrix.Columns = cols
305-
Backend.CSRMatrix.Values = vals }
306-
307-
| x -> failwith "Unsupported matrix format: %A"
232+
// let rowPointers2rowIndices (rowPointers: int []) =
233+
// let rowIndices =
234+
// Array.zeroCreate rowPointers.[rowPointers.Length - 1]
235+
236+
// [| 0 .. rowPointers.Length - 2 |]
237+
// |> Array.Parallel.iter
238+
// (fun i ->
239+
// [| rowPointers.[i] .. rowPointers.[i + 1] - 1 |]
240+
// |> Array.Parallel.iter (fun j -> rowIndices.[j] <- i))
241+
242+
// rowIndices
243+
244+
// let rowIndices2rowPointers (rowIndices: int []) rowCount =
245+
// let nnzPerRow = Array.zeroCreate rowCount
246+
// let rowPointers = Array.zeroCreate rowCount
247+
248+
// Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
249+
250+
// for i in 1 .. rowCount - 1 do
251+
// rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
252+
253+
// rowPointers

src/GraphBLAS-sharp/Objects/Matrix.fs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,50 @@ and CSRMatrix<'a> =
208208
RowCount = rowsCount
209209
ColumnCount = columnsCount }
210210

211+
static member ToBackend (context:ClContext) matrix =
212+
let rowIndices2rowPointers (rowIndices: int []) rowCount =
213+
let nnzPerRow = Array.zeroCreate rowCount
214+
let rowPointers = Array.zeroCreate rowCount
215+
216+
Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
217+
218+
for i in 1 .. rowCount - 1 do
219+
rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
220+
221+
rowPointers
222+
223+
match matrix with
224+
| MatrixCOO m ->
225+
let rowPointers =
226+
context.CreateClArray(
227+
rowIndices2rowPointers m.Rows m.RowCount
228+
,hostAccessMode = HostAccessMode.ReadOnly
229+
,deviceAccessMode = DeviceAccessMode.ReadOnly
230+
,allocationMode = AllocationMode.CopyHostPtr)
231+
232+
let cols =
233+
context.CreateClArray (
234+
m.Columns
235+
,hostAccessMode = HostAccessMode.ReadOnly
236+
,deviceAccessMode = DeviceAccessMode.ReadOnly
237+
,allocationMode = AllocationMode.CopyHostPtr)
238+
239+
let vals =
240+
context.CreateClArray (
241+
m.Values
242+
,hostAccessMode = HostAccessMode.ReadOnly
243+
,deviceAccessMode = DeviceAccessMode.ReadOnly
244+
,allocationMode = AllocationMode.CopyHostPtr)
245+
246+
{ Backend.CSRMatrix.Context = context
247+
Backend.CSRMatrix.RowCount = m.RowCount
248+
Backend.CSRMatrix.ColumnCount = m.ColumnCount
249+
Backend.CSRMatrix.RowPointers = rowPointers
250+
Backend.CSRMatrix.Columns = cols
251+
Backend.CSRMatrix.Values = vals }
252+
253+
| x -> failwith "Unsupported matrix format: %A"
254+
211255
and COOMatrix<'a> =
212256
{ RowCount: int
213257
ColumnCount: int
@@ -240,6 +284,27 @@ and COOMatrix<'a> =
240284

241285
COOMatrix.FromTuples(Array2D.length1 array, Array2D.length2 array, rows, cols, vals)
242286

287+
static member ToBackend (context:ClContext) matrix =
288+
match matrix with
289+
| MatrixCOO m ->
290+
let rows =
291+
context.CreateClArray (m.Rows, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
292+
293+
let cols =
294+
context.CreateClArray (m.Columns, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
295+
296+
let vals =
297+
context.CreateClArray (m.Values, hostAccessMode = HostAccessMode.ReadOnly, deviceAccessMode = DeviceAccessMode.ReadOnly, allocationMode = AllocationMode.CopyHostPtr)
298+
299+
{ Backend.COOMatrix.Context = context
300+
Backend.COOMatrix.RowCount = m.RowCount
301+
Backend.COOMatrix.ColumnCount = m.ColumnCount
302+
Backend.COOMatrix.Rows = rows
303+
Backend.COOMatrix.Columns = cols
304+
Backend.COOMatrix.Values = vals }
305+
306+
| x -> failwith "Unsupported matrix format: %A"
307+
243308
and CSCMatrix<'a> =
244309
{ RowCount: int
245310
ColumnCount: int

0 commit comments

Comments
 (0)