Skip to content

Commit d83f4b5

Browse files
committed
refactor: Matrix module, ClMatrix extensions
1 parent b48d80e commit d83f4b5

15 files changed

Lines changed: 354 additions & 394 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ open GraphBLAS.FSharp.Objects
1111
open GraphBLAS.FSharp.Backend.Objects
1212
open GraphBLAS.FSharp.Backend.Matrix.COO
1313
open GraphBLAS.FSharp.Backend.Matrix.CSR
14+
open GraphBLAS.FSharp.Objects.Matrix
15+
open GraphBLAS.FSharp.Benchmarks.MatrixExtensions
1416

1517
type Config() =
1618
inherit ManualConfig()
@@ -225,7 +227,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
225227
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.float32Sum wgSize),
226228
float32,
227229
(fun _ -> Utils.nextSingle (System.Random())),
228-
COOMatrix<float32>.ToBackend
230+
Matrix.ToBackendCOO
229231
)
230232

231233
static member InputMatricesProvider =
@@ -237,7 +239,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
237239
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.float32Sum wgSize),
238240
float32,
239241
(fun _ -> Utils.nextSingle (System.Random())),
240-
COOMatrix<float32>.ToBackend,
242+
Matrix.ToBackendCOO<float32>,
241243
M.resultToHostCOO
242244
)
243245

@@ -251,7 +253,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
251253
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.boolSum wgSize),
252254
(fun _ -> true),
253255
(fun _ -> true),
254-
COOMatrix<bool>.ToBackend
256+
Matrix.ToBackendCOO<bool>
255257
)
256258

257259
static member InputMatricesProvider =
@@ -264,7 +266,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
264266
(fun context wgSize -> CSRMatrix.elementwise context ArithmeticOperations.float32Sum wgSize),
265267
float32,
266268
(fun _ -> Utils.nextSingle (System.Random())),
267-
CSRMatrix<float32>.ToBackend
269+
Matrix.ToBackendCSR
268270
)
269271

270272
static member InputMatricesProvider =
@@ -277,7 +279,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
277279
(fun context wgSize -> CSRMatrix.elementwise context ArithmeticOperations.boolSum wgSize),
278280
(fun _ -> true),
279281
(fun _ -> true),
280-
CSRMatrix<bool>.ToBackend
282+
Matrix.ToBackendCSR
281283
)
282284

283285
static member InputMatricesProvider =
@@ -291,7 +293,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
291293
(fun context wgSize -> COOMatrix.elementwiseAtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
292294
(fun _ -> true),
293295
(fun _ -> true),
294-
COOMatrix<bool>.ToBackend
296+
Matrix.ToBackendCOO<bool>
295297
)
296298

297299
static member InputMatricesProvider =
@@ -303,7 +305,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
303305
(fun context wgSize -> CSRMatrix.elementwiseAtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
304306
(fun _ -> true),
305307
(fun _ -> true),
306-
CSRMatrix<bool>.ToBackend
308+
Matrix.ToBackendCSR
307309
)
308310

309311
static member InputMatricesProvider =
@@ -315,7 +317,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
315317
(fun context wgSize -> COOMatrix.elementwiseAtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
316318
float32,
317319
(fun _ -> Utils.nextSingle (System.Random())),
318-
COOMatrix<float32>.ToBackend
320+
Matrix.ToBackendCOO<float32>
319321
)
320322

321323
static member InputMatricesProvider =
@@ -327,7 +329,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
327329
(fun context wgSize -> CSRMatrix.elementwiseAtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
328330
float32,
329331
(fun _ -> Utils.nextSingle (System.Random())),
330-
CSRMatrix<float32>.ToBackend
332+
Matrix.ToBackendCSR<float32>
331333
)
332334

333335
static member InputMatricesProvider =

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxm.fs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Brahma.FSharp
77
open GraphBLAS.FSharp.Objects
88
open GraphBLAS.FSharp.Backend.Objects
99
open GraphBLAS.FSharp.Backend.Matrix
10+
open GraphBLAS.FSharp.Benchmarks.MatrixExtensions
1011

1112
[<AbstractClass>]
1213
[<IterationCount(100)>]
@@ -238,7 +239,7 @@ type MxmBenchmarks4Float32MultiplicationOnly() =
238239
(Matrix.mxm Operations.add Operations.mult),
239240
float32,
240241
(fun _ -> Utils.nextSingle (System.Random())),
241-
(fun context matrix -> ClMatrix.CSR (CSRMatrix<float32>.ToBackend context matrix))
242+
(fun context matrix -> ClMatrix.CSR (Matrix.ToBackendCSR context matrix))
242243
)
243244

244245
static member InputMatrixProvider =
@@ -250,7 +251,7 @@ type MxmBenchmarks4Float32WithTransposing() =
250251
(Matrix.mxm Operations.add Operations.mult),
251252
float32,
252253
(fun _ -> Utils.nextSingle (System.Random())),
253-
(fun context matrix -> ClMatrix.CSR (CSRMatrix<float32>.ToBackend context matrix))
254+
(fun context matrix -> ClMatrix.CSR (Matrix.ToBackendCSR context matrix))
254255
)
255256

256257
static member InputMatrixProvider =
@@ -262,7 +263,7 @@ type MxmBenchmarks4BoolMultiplicationOnly() =
262263
(Matrix.mxm Operations.logicalOr Operations.logicalAnd),
263264
(fun _ -> true),
264265
(fun _ -> true),
265-
(fun context matrix -> ClMatrix.CSR (CSRMatrix<bool>.ToBackend context matrix))
266+
(fun context matrix -> ClMatrix.CSR (Matrix.ToBackendCSR context matrix))
266267
)
267268

268269
static member InputMatrixProvider =
@@ -274,7 +275,7 @@ type MxmBenchmarks4BoolWithTransposing() =
274275
(Matrix.mxm Operations.logicalOr Operations.logicalAnd),
275276
(fun _ -> true),
276277
(fun _ -> true),
277-
(fun context matrix -> ClMatrix.CSR (CSRMatrix<bool>.ToBackend context matrix))
278+
(fun context matrix -> ClMatrix.CSR (Matrix.ToBackendCSR context matrix))
278279
)
279280

280281
static member InputMatrixProvider =
@@ -286,7 +287,7 @@ type MxmBenchmarks4Float32MultiplicationOnlyWithZerosFilter() =
286287
(Matrix.mxm Operations.addWithFilter Operations.mult),
287288
float32,
288289
(fun _ -> Utils.nextSingle (System.Random())),
289-
(fun context matrix -> ClMatrix.CSR (CSRMatrix<float32>.ToBackend context matrix))
290+
(fun context matrix -> ClMatrix.CSR (Matrix.ToBackendCSR context matrix))
290291
)
291292

292293
static member InputMatrixProvider =
@@ -298,7 +299,7 @@ type MxmBenchmarks4Float32WithTransposingWithZerosFilter() =
298299
(Matrix.mxm Operations.addWithFilter Operations.mult),
299300
float32,
300301
(fun _ -> Utils.nextSingle (System.Random())),
301-
(fun context matrix -> ClMatrix.CSR (CSRMatrix<float32>.ToBackend context matrix))
302+
(fun context matrix -> ClMatrix.CSR (Matrix.ToBackendCSR context matrix))
302303
)
303304

304305
static member InputMatrixProvider =

benchmarks/GraphBLAS-sharp.Benchmarks/GraphBLAS-sharp.Benchmarks.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</ItemGroup>
1616
<ItemGroup>
1717
<Compile Include="AssemblyInfo.fs" />
18+
<Compile Include="MatrixExtensions.fs" />
1819
<Compile Include="Helpers.fs" />
1920
<!--Compile Include="BenchmarksBFS.fs" /-->
2021
<Compile Include="BenchmarksMxv.fs" />

benchmarks/GraphBLAS-sharp.Benchmarks/Helpers.fs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ open BenchmarkDotNet.Reports
55
open BenchmarkDotNet.Running
66
open Brahma.FSharp
77
open Brahma.FSharp.OpenCL.Translator
8+
open GraphBLAS.FSharp.Backend.Objects
89
open OpenCL.Net
910
open GraphBLAS.FSharp.IO
1011
open System.IO
1112
open System.Text.RegularExpressions
1213
open BenchmarkDotNet.Configs
1314
open BenchmarkDotNet.Jobs
15+
open GraphBLAS.FSharp.Objects
1416

1517
type CommonConfig() =
1618
inherit ManualConfig()
@@ -223,25 +225,3 @@ module Utils =
223225
random.NextBytes buffer
224226
System.BitConverter.ToSingle(buffer, 0)
225227

226-
// let rowPointers2rowIndices (rowPointers: int []) =
227-
// let rowIndices =
228-
// Array.zeroCreate rowPointers.[rowPointers.Length - 1]
229-
230-
// [| 0 .. rowPointers.Length - 2 |]
231-
// |> Array.Parallel.iter
232-
// (fun i ->
233-
// [| rowPointers.[i] .. rowPointers.[i + 1] - 1 |]
234-
// |> Array.Parallel.iter (fun j -> rowIndices.[j] <- i))
235-
236-
// rowIndices
237-
238-
// let rowIndices2rowPointers (rowIndices: int []) rowCount =
239-
// let nnzPerRow = Array.zeroCreate rowCount
240-
// let rowPointers = Array.zeroCreate rowCount
241-
242-
// Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
243-
244-
// for i in 1 .. rowCount - 1 do
245-
// rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
246-
247-
// rowPointers
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
namespace GraphBLAS.FSharp.Benchmarks
2+
3+
open GraphBLAS.FSharp.Objects
4+
open Brahma.FSharp
5+
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
6+
7+
module MatrixExtensions =
8+
type Matrix<'a when 'a : struct> with
9+
static member ToBackendCOO (context: ClContext) matrix =
10+
match matrix with
11+
| Matrix.COO m ->
12+
let rows =
13+
context.CreateClArray(
14+
m.Rows,
15+
hostAccessMode = HostAccessMode.ReadOnly,
16+
deviceAccessMode = DeviceAccessMode.ReadOnly,
17+
allocationMode = AllocationMode.CopyHostPtr
18+
)
19+
20+
let cols =
21+
context.CreateClArray(
22+
m.Columns,
23+
hostAccessMode = HostAccessMode.ReadOnly,
24+
deviceAccessMode = DeviceAccessMode.ReadOnly,
25+
allocationMode = AllocationMode.CopyHostPtr
26+
)
27+
28+
let vals =
29+
context.CreateClArray(
30+
m.Values,
31+
hostAccessMode = HostAccessMode.ReadOnly,
32+
deviceAccessMode = DeviceAccessMode.ReadOnly,
33+
allocationMode = AllocationMode.CopyHostPtr
34+
)
35+
36+
{ Context = context
37+
RowCount = m.RowCount
38+
ColumnCount = m.ColumnCount
39+
Rows = rows
40+
Columns = cols
41+
Values = vals }
42+
43+
| _ -> failwith "Unsupported matrix format: %A"
44+
45+
static member ToBackendCSR (context: ClContext) matrix =
46+
let rowIndices2rowPointers (rowIndices: int []) rowCount =
47+
let nnzPerRow = Array.zeroCreate rowCount
48+
let rowPointers = Array.zeroCreate rowCount
49+
50+
Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
51+
52+
for i in 1 .. rowCount - 1 do
53+
rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
54+
55+
rowPointers
56+
57+
match matrix with
58+
| Matrix.COO m ->
59+
let rowPointers =
60+
context.CreateClArray(
61+
rowIndices2rowPointers m.Rows m.RowCount,
62+
hostAccessMode = HostAccessMode.ReadOnly,
63+
deviceAccessMode = DeviceAccessMode.ReadOnly,
64+
allocationMode = AllocationMode.CopyHostPtr
65+
)
66+
67+
let cols =
68+
context.CreateClArray(
69+
m.Columns,
70+
hostAccessMode = HostAccessMode.ReadOnly,
71+
deviceAccessMode = DeviceAccessMode.ReadOnly,
72+
allocationMode = AllocationMode.CopyHostPtr
73+
)
74+
75+
let vals =
76+
context.CreateClArray(
77+
m.Values,
78+
hostAccessMode = HostAccessMode.ReadOnly,
79+
deviceAccessMode = DeviceAccessMode.ReadOnly,
80+
allocationMode = AllocationMode.CopyHostPtr
81+
)
82+
83+
{ Context = context
84+
RowCount = m.RowCount
85+
ColumnCount = m.ColumnCount
86+
RowPointers = rowPointers
87+
Columns = cols
88+
Values = vals }
89+
90+
| _ -> failwith "Unsupported matrix format: %A"
91+

src/GraphBLAS-sharp/GraphBLAS-sharp.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Compile Include="Objects/Masks.fs" />
2424
<Compile Include="Objects\Vector.fs" />
2525
<Compile Include="Objects\VectorExtensions.fs" />
26+
<Compile Include="Objects\MatrixExtensions.fs" />
2627
<Compile Include="Operations/Scalar.fs" />
2728
<Compile Include="IO/MtxReader.fs" />
2829
<Compile Include="Predefined/Monoids/Any.fs" />

0 commit comments

Comments
 (0)