Skip to content

Commit e1a9e5b

Browse files
authored
Merge pull request #77 from IgorErin/splitbench
SpGeMM
2 parents fb1909c + e52847c commit e1a9e5b

File tree

21 files changed

+1543
-482
lines changed

21 files changed

+1543
-482
lines changed

benchmarks/GraphBLAS-sharp.Benchmarks/Matrix/SpGeMM/Expand.fs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Benchmarks<'elem when 'elem : struct>(
3030
let mutable firstMatrixHost = Unchecked.defaultof<_>
3131
let mutable secondMatrixHost = Unchecked.defaultof<_>
3232

33-
member val ResultMatrix = Unchecked.defaultof<ClMatrix<'elem>> with get, set
33+
member val ResultMatrix = Unchecked.defaultof<ClMatrix.COO<'elem> option> with get, set
3434

3535
[<ParamsSource("AvailableContexts")>]
3636
member val OclContextInfo = Unchecked.defaultof<Utils.BenchmarkContext * int> with get, set
@@ -85,7 +85,9 @@ type Benchmarks<'elem when 'elem : struct>(
8585
secondMatrix.Dispose this.Processor
8686

8787
member this.ClearResult() =
88-
this.ResultMatrix.Dispose this.Processor
88+
match this.ResultMatrix with
89+
| Some matrix -> matrix.Dispose this.Processor
90+
| None -> ()
8991

9092
member this.ReadMatrices() =
9193
firstMatrixHost <- this.ReadMatrix this.InputMatrixReader

src/GraphBLAS-sharp.Backend/Common/ClArray.fs

Lines changed: 216 additions & 123 deletions
Large diffs are not rendered by default.

src/GraphBLAS-sharp.Backend/Matrix/COO/Matrix.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open GraphBLAS.FSharp.Backend.Objects
88
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
99
open GraphBLAS.FSharp.Backend.Objects.ClCell
1010
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
11+
open GraphBLAS.FSharp.Backend.Objects.ClContext
1112

1213
module Matrix =
1314
let map = Map.run

src/GraphBLAS-sharp.Backend/Matrix/CSR/Matrix.fs

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,138 @@ open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
1313

1414

1515
module Matrix =
16+
let expandRowPointers (clContext: ClContext) workGroupSize =
17+
18+
let kernel =
19+
<@ fun (ndRange: Range1D) columnsLength pointersLength (pointers: ClArray<int>) (results: ClArray<int>) ->
20+
21+
let gid = ndRange.GlobalID0
22+
23+
if gid < columnsLength then
24+
let result =
25+
(%Search.Bin.lowerBound) pointersLength gid pointers
26+
27+
results.[gid] <- result - 1 @>
28+
29+
let program = clContext.Compile kernel
30+
31+
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
32+
33+
let rows =
34+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, matrix.Columns.Length)
35+
36+
let kernel = program.GetKernel()
37+
38+
let ndRange =
39+
Range1D.CreateValid(matrix.Columns.Length, workGroupSize)
40+
41+
processor.Post(
42+
Msg.MsgSetArguments
43+
(fun () ->
44+
kernel.KernelFunc
45+
ndRange
46+
matrix.Columns.Length
47+
matrix.RowPointers.Length
48+
matrix.RowPointers
49+
rows)
50+
)
51+
52+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
53+
54+
rows
55+
56+
let subRows (clContext: ClContext) workGroupSize =
57+
58+
let kernel =
59+
<@ fun (ndRange: Range1D) resultLength sourceRow pointersLength (pointers: ClArray<int>) (results: ClArray<int>) ->
60+
61+
let gid = ndRange.GlobalID0
62+
63+
let shift = pointers.[sourceRow]
64+
let shiftedId = gid + shift
65+
66+
if gid < resultLength then
67+
let result =
68+
(%Search.Bin.lowerBound) pointersLength shiftedId pointers
69+
70+
results.[gid] <- result - 1 @>
71+
72+
let program = clContext.Compile kernel
73+
74+
let blit = ClArray.blit clContext workGroupSize
75+
76+
let blitData = ClArray.blit clContext workGroupSize
77+
78+
fun (processor: MailboxProcessor<_>) allocationMode startIndex count (matrix: ClMatrix.CSR<'a>) ->
79+
if count <= 0 then
80+
failwith "Count must be greater than zero"
81+
82+
if startIndex < 0 then
83+
failwith "startIndex must be greater then zero"
84+
85+
if startIndex + count > matrix.RowCount then
86+
failwith "startIndex and count sum is larger than the matrix row count"
87+
88+
// extract rows
89+
let rowPointers = matrix.RowPointers.ToHost processor
90+
91+
let resultLength =
92+
rowPointers.[startIndex + count]
93+
- rowPointers.[startIndex]
94+
95+
let rows =
96+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
97+
98+
let kernel = program.GetKernel()
99+
100+
let ndRange =
101+
Range1D.CreateValid(matrix.Columns.Length, workGroupSize)
102+
103+
processor.Post(
104+
Msg.MsgSetArguments
105+
(fun () ->
106+
kernel.KernelFunc
107+
ndRange
108+
resultLength
109+
startIndex
110+
matrix.RowPointers.Length
111+
matrix.RowPointers
112+
rows)
113+
)
114+
115+
processor.Post(Msg.CreateRunMsg<_, _> kernel)
116+
117+
let startPosition = rowPointers.[startIndex]
118+
119+
// extract values
120+
let values =
121+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
122+
123+
blitData processor matrix.Values startPosition values 0 resultLength
124+
125+
// extract indices
126+
let columns =
127+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
128+
129+
blit processor matrix.Columns startPosition columns 0 resultLength
130+
131+
{ Context = clContext
132+
RowCount = matrix.RowCount
133+
ColumnCount = matrix.ColumnCount
134+
Rows = rows
135+
Columns = columns
136+
Values = values }
137+
16138
let toCOO (clContext: ClContext) workGroupSize =
17139
let prepare =
18-
Common.expandRowPointers clContext workGroupSize
140+
expandRowPointers clContext workGroupSize
19141

20142
let copy = ClArray.copy clContext workGroupSize
21143

22144
let copyData = ClArray.copy clContext workGroupSize
23145

24146
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
25-
let rows =
26-
prepare processor allocationMode matrix.RowPointers matrix.Columns.Length matrix.RowCount
147+
let rows = prepare processor allocationMode matrix
27148

28149
let cols =
29150
copy processor allocationMode matrix.Columns
@@ -40,11 +161,10 @@ module Matrix =
40161

41162
let toCOOInPlace (clContext: ClContext) workGroupSize =
42163
let prepare =
43-
Common.expandRowPointers clContext workGroupSize
164+
expandRowPointers clContext workGroupSize
44165

45166
fun (processor: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
46-
let rows =
47-
prepare processor allocationMode matrix.RowPointers matrix.Columns.Length matrix.RowCount
167+
let rows = prepare processor allocationMode matrix
48168

49169
processor.Post(Msg.CreateFreeMsg(matrix.RowPointers))
50170

@@ -92,7 +212,6 @@ module Matrix =
92212
let toCSRInPlace =
93213
COO.Matrix.toCSRInPlace clContext workGroupSize
94214

95-
96215
fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) ->
97216
toCOO queue allocationMode matrix
98217
|> transposeInPlace queue

src/GraphBLAS-sharp.Backend/Matrix/Common.fs

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend.Common
55
open GraphBLAS.FSharp.Backend.Objects.ClContext
66
open GraphBLAS.FSharp.Backend.Objects.ClCell
7+
open GraphBLAS.FSharp.Backend.Objects
8+
open GraphBLAS.FSharp.Backend.Quotes
79

8-
module Common =
10+
module internal Common =
911
///<param name="clContext">.</param>
1012
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
1113
let setPositions<'a when 'a: struct> (clContext: ClContext) workGroupSize =
@@ -40,40 +42,3 @@ module Common =
4042
valuesScatter processor positions allValues resultValues
4143

4244
resultRows, resultColumns, resultValues, resultLength
43-
44-
let expandRowPointers (clContext: ClContext) workGroupSize =
45-
46-
let expandRowPointers =
47-
<@ fun (ndRange: Range1D) (rowPointers: ClArray<int>) (rowCount: int) (rows: ClArray<int>) ->
48-
49-
let i = ndRange.GlobalID0
50-
51-
if i < rowCount then
52-
let rowPointer = rowPointers.[i]
53-
54-
if rowPointer <> rowPointers.[i + 1] then
55-
rows.[rowPointer] <- i @>
56-
57-
let program = clContext.Compile expandRowPointers
58-
59-
let create =
60-
ClArray.zeroCreate clContext workGroupSize
61-
62-
let scan =
63-
PrefixSum.runIncludeInPlace <@ max @> clContext workGroupSize
64-
65-
fun (processor: MailboxProcessor<_>) allocationMode (rowPointers: ClArray<int>) nnz rowCount ->
66-
67-
let rows = create processor allocationMode nnz
68-
69-
let kernel = program.GetKernel()
70-
71-
let ndRange =
72-
Range1D.CreateValid(rowCount, workGroupSize)
73-
74-
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange rowPointers rowCount rows))
75-
processor.Post(Msg.CreateRunMsg<_, _> kernel)
76-
77-
(scan processor rows 0).Free processor
78-
79-
rows

src/GraphBLAS-sharp.Backend/Matrix/Matrix.fs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open GraphBLAS.FSharp.Backend.Matrix
77
open GraphBLAS.FSharp.Backend.Objects
88
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
99
open GraphBLAS.FSharp.Backend.Vector
10+
open GraphBLAS.FSharp.Backend.Objects.ClContext
1011

1112
module Matrix =
1213
let copy (clContext: ClContext) workGroupSize =
@@ -413,11 +414,19 @@ module Matrix =
413414
=
414415

415416
let run =
416-
SpGeMM.Expand.run clContext workGroupSize opAdd opMul
417+
SpGeMM.Expand.run opAdd opMul clContext workGroupSize
417418

418419
fun (processor: MailboxProcessor<_>) allocationMode (leftMatrix: ClMatrix<'a>) (rightMatrix: ClMatrix<'b>) ->
419420
match leftMatrix, rightMatrix with
420421
| ClMatrix.CSR leftMatrix, ClMatrix.CSR rightMatrix ->
421-
ClMatrix.LIL
422-
<| run processor allocationMode leftMatrix rightMatrix
422+
let allocCapacity =
423+
List.max [ sizeof<'a>
424+
sizeof<'c>
425+
sizeof<'b> ]
426+
* 1<Byte>
427+
428+
let resultCapacity =
429+
(clContext.MaxMemAllocSize / allocCapacity) / 3
430+
431+
run processor allocationMode resultCapacity leftMatrix rightMatrix
423432
| _ -> failwith "Matrix formats are not matching"

0 commit comments

Comments
 (0)