Skip to content

Commit ace945d

Browse files
committed
add: ClCell.ToHostAndFree
1 parent 087eef0 commit ace945d

11 files changed

Lines changed: 62 additions & 91 deletions

File tree

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace GraphBLAS.FSharp.Backend.Common
33
open Brahma.FSharp
44
open Microsoft.FSharp.Quotations
55
open GraphBLAS.FSharp.Backend.Objects.ClContext
6+
open GraphBLAS.FSharp.Backend.Objects.ClCell
67

78
module ClArray =
89
let init (clContext: ClContext) workGroupSize (initializer: Expr<int -> 'a>) =
@@ -172,11 +173,13 @@ module ClArray =
172173

173174
let copy = copy clContext workGroupSize
174175

175-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
176+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (zero: 'a) ->
176177

177178
let outputArray = copy processor allocationMode inputArray
178179

179-
runExcludeInplace processor outputArray totalSum zero
180+
let totalSum = runExcludeInplace processor outputArray zero
181+
182+
outputArray, totalSum
180183

181184
let prefixSumInclude plus (clContext: ClContext) workGroupSize =
182185

@@ -185,11 +188,13 @@ module ClArray =
185188

186189
let copy = copy clContext workGroupSize
187190

188-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
191+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (zero: 'a) ->
189192

190193
let outputArray = copy processor allocationMode inputArray
191194

192-
runIncludeInplace processor outputArray totalSum zero
195+
let totalSum = runIncludeInplace processor outputArray zero
196+
197+
outputArray, totalSum
193198

194199
let prefixSumBackwardsExcludeInplace plus =
195200
PrefixSum.runBackwardsExcludeInplace plus
@@ -242,32 +247,23 @@ module ClArray =
242247
let getUniqueBitmap = getUniqueBitmap clContext workGroupSize
243248

244249
let prefixSumExclude =
245-
prefixSumExclude <@ (+) @> clContext workGroupSize
250+
prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
246251

247252
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) ->
248253

249254
let bitmap =
250255
getUniqueBitmap processor DeviceOnly inputArray
251256

252-
let sum = clContext.CreateClCell 0
253-
254-
let positions, sum =
255-
prefixSumExclude processor DeviceOnly bitmap sum 0
256-
257257
let resultLength =
258-
let a = [| 0 |]
259-
260-
processor.PostAndReply(fun ch -> Msg.CreateToHostMsg(sum, a, ch))
261-
|> ignore
262-
263-
processor.Post(Msg.CreateFreeMsg<_>(sum))
264-
265-
a.[0]
258+
(prefixSumExclude processor bitmap 0)
259+
.ToHostAndFree(processor)
266260

267261
let outputArray =
268262
clContext.CreateClArrayWithSpecificAllocationMode(DeviceOnly, resultLength)
269263

270-
scatter processor positions inputArray outputArray
264+
scatter processor bitmap inputArray outputArray
265+
266+
processor.Post <| Msg.CreateFreeMsg<_>(bitmap)
271267

272268
outputArray
273269

@@ -360,3 +356,5 @@ module ClArray =
360356
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, leftArray.Length)
361357

362358
map2 processor leftArray rightArray resultArray
359+
360+
resultArray

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ module PrefixSum =
164164

165165
let update = update opAdd clContext workGroupSize
166166

167-
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
167+
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) (zero: 'a) ->
168168

169169
let firstVertices =
170170
clContext.CreateClArray<'a>(
@@ -178,6 +178,8 @@ module PrefixSum =
178178
hostAccessMode = HostAccessMode.NotAccessible
179179
)
180180

181+
let totalSum = clContext.CreateClCell<'a>()
182+
181183
let mutable verticesArrays = firstVertices, secondVertices
182184
let swap (a, b) = (b, a)
183185
let mutable verticesLength = firstVertices.Length
@@ -207,7 +209,7 @@ module PrefixSum =
207209
processor.Post(Msg.CreateFreeMsg(firstVertices))
208210
processor.Post(Msg.CreateFreeMsg(secondVertices))
209211

210-
inputArray, totalSum
212+
totalSum
211213

212214
let runExcludeInplace plus = runInplace false scanExclusive plus
213215

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="Objects/Matrix.fs" />
1818
<Compile Include="Objects/AtLeastOne.fs" />
1919
<Compile Include="Objects/ClContextExtensions.fs" />
20+
<Compile Include="Objects\ClCell.fs" />
2021
<Compile Include="Quotes/SubReduce.fs" />
2122
<Compile Include="Quotes/Arithmetic.fs" />
2223
<Compile Include="Quotes/Convert.fs" />

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,8 @@ module COOMatrix =
365365
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange rowIndices nnz rowPointers))
366366
processor.Post(Msg.CreateRunMsg<_, _> kernel)
367367

368-
let total = clContext.CreateClCell()
369-
let _ = scan processor rowPointers total nnz
370-
processor.Post(Msg.CreateFreeMsg(total))
368+
let result = scan processor rowPointers nnz
369+
processor.Post <| Msg.CreateFreeMsg(result)
371370

372371
rowPointers
373372

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ module CSRMatrix =
4343
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange rowPointers rowCount rows))
4444
processor.Post(Msg.CreateRunMsg<_, _> kernel)
4545

46-
let total = clContext.CreateClCell()
47-
ignore <| scan processor rows total 0
46+
let total = scan processor rows 0
4847
processor.Post(Msg.CreateFreeMsg(total))
4948

5049
rows

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.FSharp.Quotations
77
open GraphBLAS.FSharp.Backend.Objects
88
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
99
open GraphBLAS.FSharp.Backend.Objects.ClContext
10+
open GraphBLAS.FSharp.Backend.Objects.ClCell
1011

1112
module internal SpGEMM =
1213
let private calculate
@@ -159,18 +160,12 @@ module internal SpGEMM =
159160

160161
fun (queue: MailboxProcessor<_>) (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSC<'b>) (mask: ClMatrix.COO<_>) ->
161162

162-
let values, bitmap =
163+
let values, positions =
163164
calculate queue matrixLeft matrixRight mask
164165

165-
let total = context.CreateClCell 0
166-
let positions, total = scanInplace queue bitmap total
167-
168166
let resultNNZ =
169-
let res =
170-
queue.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(total, [| 0 |], ch))
171-
172-
queue.Post(Msg.CreateFreeMsg<_>(total))
173-
res.[0]
167+
(scanInplace queue positions)
168+
.ToHostAndFree(queue)
174169

175170
let resultRows = context.CreateClArray<int> resultNNZ
176171
let resultCols = context.CreateClArray<int> resultNNZ

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend.Common
55
open GraphBLAS.FSharp.Backend.Predefined
66
open GraphBLAS.FSharp.Backend.Objects.ClContext
7+
open GraphBLAS.FSharp.Backend.Objects.ClCell
78

89
module Common =
910
///<param name="clContext">.</param>
@@ -19,25 +20,15 @@ module Common =
1920
let sum =
2021
PrefixSum.standardExcludeInplace clContext workGroupSize
2122

22-
let resultLength = Array.zeroCreate<int> 1
23-
2423
fun (processor: MailboxProcessor<_>) allocationMode (allRows: ClArray<int>) (allColumns: ClArray<int>) (allValues: ClArray<'a>) (positions: ClArray<int>) ->
25-
let resultLengthGpu = clContext.CreateClCell 0
26-
27-
let _, r = sum processor positions resultLengthGpu
2824

2925
let resultLength =
30-
let res =
31-
processor.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(r, resultLength, ch))
32-
33-
processor.Post(Msg.CreateFreeMsg<_>(r))
34-
35-
res.[0]
26+
(sum processor positions)
27+
.ToHostAndFree(processor)
3628

3729
let resultRows =
3830
clContext.CreateClArrayWithSpecificAllocationMode<int>(allocationMode, resultLength)
3931

40-
4132
let resultColumns =
4233
clContext.CreateClArrayWithSpecificAllocationMode<int>(allocationMode, resultLength)
4334

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GraphBLAS.FSharp.Backend.Objects
2+
3+
open Brahma.FSharp
4+
5+
module ClCell =
6+
type ClCell<'a> with
7+
member this.ToHostAndFree(processor: MailboxProcessor<_>) =
8+
let res =
9+
processor.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(this, (Array.zeroCreate<'a> 1), ch))
10+
11+
processor.Post(Msg.CreateFreeMsg<_>(this))
12+
13+
res.[0]

src/GraphBLAS-sharp.Backend/Predefined/PrefixSum.fs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,40 @@ namespace GraphBLAS.FSharp.Backend.Predefined
22

33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend.Common
5+
56
module internal PrefixSum =
67
let standardExcludeInplace (clContext: ClContext) workGroupSize =
78

89
let scan =
910
ClArray.prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
1011

11-
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<int>) (totalSum: ClCell<int>) ->
12+
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<int>) ->
1213

13-
scan processor inputArray totalSum 0
14+
scan processor inputArray 0
1415

1516
let standardIncludeInplace (clContext: ClContext) workGroupSize =
1617

1718
let scan =
1819
ClArray.prefixSumIncludeInplace <@ (+) @> clContext workGroupSize
1920

20-
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<int>) (totalSum: ClCell<int>) ->
21+
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<int>) ->
2122

22-
scan processor inputArray totalSum 0
23+
scan processor inputArray 0
2324

2425
let standardInclude (clContext: ClContext) workGroupSize =
2526

2627
let scan =
2728
ClArray.prefixSumInclude <@ (+) @> clContext workGroupSize
2829

29-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<int>) (totalSum: ClCell<int>) ->
30+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<int>) ->
3031

31-
scan processor allocationMode inputArray totalSum 0
32+
scan processor allocationMode inputArray 0
3233

3334
let standardExclude (clContext: ClContext) workGroupSize =
3435

3536
let scan =
3637
ClArray.prefixSumExclude <@ (+) @> clContext workGroupSize
3738

38-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<int>) (totalSum: ClCell<int>) ->
39-
40-
scan processor allocationMode inputArray totalSum 0
41-
42-
let standardExcludeInplaceLengthToHost clContext workGroupSize =
43-
44-
let scan = standardExcludeInplace clContext workGroupSize
45-
46-
let resultLength = Array.zeroCreate<int> 1
47-
48-
fun (processor: MailboxProcessor<_>) (positions: ClArray<int>) ->
49-
50-
let resultLengthGpu = clContext.CreateClCell 0
51-
52-
let _, r =
53-
scan processor positions resultLengthGpu
54-
55-
let res =
56-
processor.PostAndReply(fun ch -> Msg.CreateToHostMsg<_>(r, resultLength, ch))
57-
58-
processor.Post(Msg.CreateFreeMsg<_>(r))
59-
60-
res.[0]
61-
39+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<int>) ->
6240

41+
scan processor allocationMode inputArray 0

src/GraphBLAS-sharp.Backend/Vector/DenseVector/DenseVector.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.FSharp.Quotations
77
open GraphBLAS.FSharp.Backend.Predefined
88
open GraphBLAS.FSharp.Backend.Objects.ClVector
99
open GraphBLAS.FSharp.Backend.Objects.ClContext
10+
open GraphBLAS.FSharp.Backend.Objects.ClCell
1011

1112
module DenseVector =
1213
let map2Inplace<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>
@@ -95,7 +96,7 @@ module DenseVector =
9596
let getBitmap = ClArray.map clContext workGroupSize <| Map.option 1 0
9697

9798
let prefixSum =
98-
PrefixSum.standardExcludeInplaceLengthToHost clContext workGroupSize
99+
PrefixSum.standardExcludeInplace clContext workGroupSize
99100

100101
let allIndices = ClArray.init clContext workGroupSize <@ id @>
101102

@@ -105,7 +106,9 @@ module DenseVector =
105106

106107
let positions = getBitmap processor DeviceOnly vector
107108

108-
let resultLength = prefixSum processor positions
109+
let resultLength =
110+
(prefixSum processor positions)
111+
.ToHostAndFree(processor)
109112

110113
// compute result indices
111114
let resultIndices =

0 commit comments

Comments
 (0)