Skip to content

Commit 69afd98

Browse files
authored
Merge pull request #65 from IgorErin/vector
Vector refactor
2 parents 4bafd2b + b38cc72 commit 69afd98

24 files changed

Lines changed: 443 additions & 222 deletions

File tree

src/GraphBLAS-sharp.Backend/Algorithms/BFS.fs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ open GraphBLAS.FSharp.Backend.Vector
1010
open GraphBLAS.FSharp.Backend.Vector.Dense
1111
open GraphBLAS.FSharp.Backend.Objects.ClContext
1212
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
13+
open GraphBLAS.FSharp.Backend.Objects.ClCell
1314

1415
module BFS =
1516
let singleSource
@@ -62,13 +63,9 @@ module BFS =
6263
maskComplementedTo queue front levels front
6364

6465
//Checking if front is empty
65-
let frontNotEmpty = Array.zeroCreate 1
66-
let sum = containsNonZero queue front
67-
68-
queue.PostAndReply(fun ch -> Msg.CreateToHostMsg(sum, frontNotEmpty, ch))
69-
|> ignore
70-
71-
stop <- not frontNotEmpty.[0]
66+
stop <-
67+
not
68+
<| (containsNonZero queue front).ToHostAndFree queue
7269

7370
front.Dispose queue
7471

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

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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
7+
open GraphBLAS.FSharp.Backend.Quotes
68

79
module ClArray =
810
let init (clContext: ClContext) workGroupSize (initializer: Expr<int -> 'a>) =
@@ -141,11 +143,7 @@ module ClArray =
141143
/// > val sum = [| 4 |]
142144
/// </code>
143145
/// </example>
144-
///<param name="clContext">.</param>
145146
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
146-
///<param name="processor">.</param>
147-
///<param name="inputArray">.</param>
148-
///<param name="totalSum">.</param>
149147
///<param name="plus">Associative binary operation.</param>
150148
///<param name="zero">Zero element for binary operation.</param>
151149
let prefixSumExcludeInplace = PrefixSum.runExcludeInplace
@@ -164,11 +162,7 @@ module ClArray =
164162
/// > val sum = [| 4 |]
165163
/// </code>
166164
/// </example>
167-
///<param name="clContext">.</param>
168165
///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param>
169-
///<param name="processor">.</param>
170-
///<param name="inputArray">.</param>
171-
///<param name="totalSum">.</param>
172166
///<param name="plus">Associative binary operation.</param>
173167
///<param name="zero">Zero element for binary operation.</param>
174168
let prefixSumIncludeInplace = PrefixSum.runIncludeInplace
@@ -180,11 +174,14 @@ module ClArray =
180174

181175
let copy = copy clContext workGroupSize
182176

183-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
177+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (zero: 'a) ->
184178

185179
let outputArray = copy processor allocationMode inputArray
186180

187-
runExcludeInplace processor outputArray totalSum zero
181+
let totalSum =
182+
runExcludeInplace processor outputArray zero
183+
184+
outputArray, totalSum
188185

189186
let prefixSumInclude plus (clContext: ClContext) workGroupSize =
190187

@@ -193,11 +190,14 @@ module ClArray =
193190

194191
let copy = copy clContext workGroupSize
195192

196-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
193+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<'a>) (zero: 'a) ->
197194

198195
let outputArray = copy processor allocationMode inputArray
199196

200-
runIncludeInplace processor outputArray totalSum zero
197+
let totalSum =
198+
runIncludeInplace processor outputArray zero
199+
200+
outputArray, totalSum
201201

202202
let prefixSumBackwardsExcludeInplace plus =
203203
PrefixSum.runBackwardsExcludeInplace plus
@@ -250,32 +250,23 @@ module ClArray =
250250
let getUniqueBitmap = getUniqueBitmap clContext workGroupSize
251251

252252
let prefixSumExclude =
253-
prefixSumExclude <@ (+) @> clContext workGroupSize
253+
prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
254254

255255
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) ->
256256

257257
let bitmap =
258258
getUniqueBitmap processor DeviceOnly inputArray
259259

260-
let sum = clContext.CreateClCell 0
261-
262-
let positions, sum =
263-
prefixSumExclude processor DeviceOnly bitmap sum 0
264-
265260
let resultLength =
266-
let a = [| 0 |]
267-
268-
processor.PostAndReply(fun ch -> Msg.CreateToHostMsg(sum, a, ch))
269-
|> ignore
270-
271-
processor.Post(Msg.CreateFreeMsg<_>(sum))
272-
273-
a.[0]
261+
(prefixSumExclude processor bitmap 0)
262+
.ToHostAndFree(processor)
274263

275264
let outputArray =
276265
clContext.CreateClArrayWithSpecificAllocationMode(DeviceOnly, resultLength)
277266

278-
scatter processor positions inputArray outputArray
267+
scatter processor bitmap inputArray outputArray
268+
269+
processor.Post <| Msg.CreateFreeMsg<_>(bitmap)
279270

280271
outputArray
281272

@@ -335,3 +326,82 @@ module ClArray =
335326
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
336327

337328
result
329+
330+
let map2Inplace<'a, 'b, 'c> (clContext: ClContext) workGroupSize (map: Expr<'a -> 'b -> 'c>) =
331+
332+
let kernel =
333+
<@ fun (ndRange: Range1D) length (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) (resultArray: ClArray<'c>) ->
334+
335+
let gid = ndRange.GlobalID0
336+
337+
if gid < length then
338+
339+
resultArray.[gid] <- (%map) leftArray.[gid] rightArray.[gid] @>
340+
341+
let kernel = clContext.Compile kernel
342+
343+
fun (processor: MailboxProcessor<_>) (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) (resultArray: ClArray<'c>) ->
344+
345+
let ndRange =
346+
Range1D.CreateValid(resultArray.Length, workGroupSize)
347+
348+
let kernel = kernel.GetKernel()
349+
350+
processor.Post(
351+
Msg.MsgSetArguments
352+
(fun () -> kernel.KernelFunc ndRange resultArray.Length leftArray rightArray resultArray)
353+
)
354+
355+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
356+
357+
let map2<'a, 'b, 'c> (clContext: ClContext) workGroupSize map =
358+
let map2 =
359+
map2Inplace<'a, 'b, 'c> clContext workGroupSize map
360+
361+
fun (processor: MailboxProcessor<_>) allocationMode (leftArray: ClArray<'a>) (rightArray: ClArray<'b>) ->
362+
363+
let resultArray =
364+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, leftArray.Length)
365+
366+
map2 processor leftArray rightArray resultArray
367+
368+
resultArray
369+
370+
let choose<'a, 'b> (clContext: ClContext) workGroupSize (predicate: Expr<'a -> 'b option>) =
371+
let getBitmap =
372+
map<'a, int> clContext workGroupSize
373+
<| Map.chooseBitmap predicate
374+
375+
let getOptionValues =
376+
map<'a, 'b option> clContext workGroupSize predicate
377+
378+
let getValues =
379+
map<'b option, 'b> clContext workGroupSize
380+
<| Map.optionToValueOrZero Unchecked.defaultof<'b>
381+
382+
let prefixSum =
383+
prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
384+
385+
let scatter =
386+
Scatter.runInplace clContext workGroupSize
387+
388+
fun (processor: MailboxProcessor<_>) allocationMode (array: ClArray<'a>) ->
389+
390+
let positions = getBitmap processor DeviceOnly array
391+
392+
let resultLength =
393+
(prefixSum processor positions 0)
394+
.ToHostAndFree(processor)
395+
396+
let optionValues =
397+
getOptionValues processor DeviceOnly array
398+
399+
let values =
400+
getValues processor DeviceOnly optionValues
401+
402+
let result =
403+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, resultLength)
404+
405+
scatter processor positions values result
406+
407+
result

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
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" />
2324
<Compile Include="Quotes/Mask.fs" />
2425
<Compile Include="Quotes/SubSum.fs" />
2526
<Compile Include="Quotes/PreparePositions.fs" />
2627
<Compile Include="Quotes/Predicates.fs" />
28+
<Compile Include="Quotes/Map.fs" />
2729
<Compile Include="Common/Scatter.fs" />
2830
<Compile Include="Common/Utils.fs" />
2931
<Compile Include="Common/Sum.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: 3 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,11 @@ 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).ToHostAndFree(queue)
174168

175169
let resultRows = context.CreateClArray<int> resultNNZ
176170
let resultCols = context.CreateClArray<int> resultNNZ

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

Lines changed: 2 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,14 @@ 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).ToHostAndFree(processor)
3627

3728
let resultRows =
3829
clContext.CreateClArrayWithSpecificAllocationMode<int>(allocationMode, resultLength)
3930

40-
4131
let resultColumns =
4232
clContext.CreateClArrayWithSpecificAllocationMode<int>(allocationMode, resultLength)
4333

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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@ module internal PrefixSum =
99
let scan =
1010
ClArray.prefixSumExcludeInplace <@ (+) @> clContext workGroupSize
1111

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

14-
scan processor inputArray totalSum 0
14+
scan processor inputArray 0
1515

1616
let standardIncludeInplace (clContext: ClContext) workGroupSize =
1717

1818
let scan =
1919
ClArray.prefixSumIncludeInplace <@ (+) @> clContext workGroupSize
2020

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

23-
scan processor inputArray totalSum 0
23+
scan processor inputArray 0
2424

2525
let standardInclude (clContext: ClContext) workGroupSize =
2626

2727
let scan =
2828
ClArray.prefixSumInclude <@ (+) @> clContext workGroupSize
2929

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

32-
scan processor allocationMode inputArray totalSum 0
32+
scan processor allocationMode inputArray 0
3333

3434
let standardExclude (clContext: ClContext) workGroupSize =
3535

3636
let scan =
3737
ClArray.prefixSumExclude <@ (+) @> clContext workGroupSize
3838

39-
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<int>) (totalSum: ClCell<int>) ->
39+
fun (processor: MailboxProcessor<_>) allocationMode (inputArray: ClArray<int>) ->
4040

41-
scan processor allocationMode inputArray totalSum 0
41+
scan processor allocationMode inputArray 0

0 commit comments

Comments
 (0)