Skip to content

Commit d14ef08

Browse files
authored
Merge pull request #58 from IgorErin/flags
Flags
2 parents 8b2df22 + 0949c73 commit d14ef08

30 files changed

Lines changed: 556 additions & 553 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ open GraphBLAS.FSharp.Backend.Matrix.COO
1313
open GraphBLAS.FSharp.Backend.Matrix.CSR
1414
open GraphBLAS.FSharp.Objects.Matrix
1515
open GraphBLAS.FSharp.Benchmarks.MatrixExtensions
16+
open GraphBLAS.FSharp.Backend.Objects.ClContext
1617

1718
type Config() =
1819
inherit ManualConfig()
@@ -224,7 +225,7 @@ module M =
224225
type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
225226

226227
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<float32>,float32>(
227-
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.float32Sum wgSize),
228+
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.float32Sum wgSize HostInterop),
228229
float32,
229230
(fun _ -> Utils.nextSingle (System.Random())),
230231
Matrix.ToBackendCOO
@@ -236,7 +237,7 @@ type EWiseAddBenchmarks4Float32COOWithoutDataTransfer() =
236237
type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
237238

238239
inherit EWiseAddBenchmarksWithDataTransfer<ClMatrix.COO<float32>,float32>(
239-
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.float32Sum wgSize),
240+
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.float32Sum wgSize HostInterop),
240241
float32,
241242
(fun _ -> Utils.nextSingle (System.Random())),
242243
Matrix.ToBackendCOO<float32>,
@@ -250,7 +251,7 @@ type EWiseAddBenchmarks4Float32COOWithDataTransfer() =
250251
type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
251252

252253
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<bool>,bool>(
253-
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.boolSum wgSize),
254+
(fun context wgSize -> COOMatrix.elementwise context ArithmeticOperations.boolSum wgSize HostInterop),
254255
(fun _ -> true),
255256
(fun _ -> true),
256257
Matrix.ToBackendCOO<bool>
@@ -263,7 +264,7 @@ type EWiseAddBenchmarks4BoolCOOWithoutDataTransfer() =
263264
type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
264265

265266
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<float32>,float32>(
266-
(fun context wgSize -> CSRMatrix.elementwise context ArithmeticOperations.float32Sum wgSize),
267+
(fun context wgSize -> CSRMatrix.elementwise context ArithmeticOperations.float32Sum wgSize HostInterop),
267268
float32,
268269
(fun _ -> Utils.nextSingle (System.Random())),
269270
Matrix.ToBackendCSR
@@ -276,7 +277,7 @@ type EWiseAddBenchmarks4Float32CSRWithoutDataTransfer() =
276277
type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
277278

278279
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<bool>,bool>(
279-
(fun context wgSize -> CSRMatrix.elementwise context ArithmeticOperations.boolSum wgSize),
280+
(fun context wgSize -> CSRMatrix.elementwise context ArithmeticOperations.boolSum wgSize HostInterop),
280281
(fun _ -> true),
281282
(fun _ -> true),
282283
Matrix.ToBackendCSR
@@ -290,7 +291,7 @@ type EWiseAddBenchmarks4BoolCSRWithoutDataTransfer() =
290291
type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
291292

292293
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<bool>,bool>(
293-
(fun context wgSize -> COOMatrix.elementwiseAtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
294+
(fun context wgSize -> COOMatrix.elementwiseAtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize HostInterop),
294295
(fun _ -> true),
295296
(fun _ -> true),
296297
Matrix.ToBackendCOO<bool>
@@ -302,7 +303,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCOOWithoutDataTransfer() =
302303
type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
303304

304305
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<bool>,bool>(
305-
(fun context wgSize -> CSRMatrix.elementwiseAtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize),
306+
(fun context wgSize -> CSRMatrix.elementwiseAtLeastOne context ArithmeticOperations.boolSumAtLeastOne wgSize HostInterop),
306307
(fun _ -> true),
307308
(fun _ -> true),
308309
Matrix.ToBackendCSR
@@ -314,7 +315,7 @@ type EWiseAddAtLeastOneBenchmarks4BoolCSRWithoutDataTransfer() =
314315
type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
315316

316317
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.COO<float32>,float32>(
317-
(fun context wgSize -> COOMatrix.elementwiseAtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
318+
(fun context wgSize -> COOMatrix.elementwiseAtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize HostInterop),
318319
float32,
319320
(fun _ -> Utils.nextSingle (System.Random())),
320321
Matrix.ToBackendCOO<float32>
@@ -326,7 +327,7 @@ type EWiseAddAtLeastOneBenchmarks4Float32COOWithoutDataTransfer() =
326327
type EWiseAddAtLeastOneBenchmarks4Float32CSRWithoutDataTransfer() =
327328

328329
inherit EWiseAddBenchmarksWithoutDataTransfer<ClMatrix.CSR<float32>,float32>(
329-
(fun context wgSize -> CSRMatrix.elementwiseAtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize),
330+
(fun context wgSize -> CSRMatrix.elementwiseAtLeastOne context ArithmeticOperations.float32SumAtLeastOne wgSize HostInterop),
330331
float32,
331332
(fun _ -> Utils.nextSingle (System.Random())),
332333
Matrix.ToBackendCSR<float32>

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxm.fs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open GraphBLAS.FSharp.Objects
88
open GraphBLAS.FSharp.Backend.Objects
99
open GraphBLAS.FSharp.Backend.Matrix
1010
open GraphBLAS.FSharp.Benchmarks.MatrixExtensions
11+
open GraphBLAS.FSharp.Backend.Objects.ClContext
1112

1213
[<AbstractClass>]
1314
[<IterationCount(100)>]
@@ -74,15 +75,15 @@ type MxmBenchmarks<'elem when 'elem : struct>(
7475
member this.FunCSR2CSC =
7576
match funCSR2CSC with
7677
| None ->
77-
let x = Matrix.toCSCInplace this.OclContext this.WorkGroupSize
78+
let x = Matrix.toCSCInplace this.OclContext this.WorkGroupSize HostInterop
7879
funCSR2CSC <- Some x
7980
x
8081
| Some x -> x
8182

8283
member this.FunCSC2CSR =
8384
match funCSC2CSR with
8485
| None ->
85-
let x = Matrix.toCSRInplace this.OclContext this.WorkGroupSize
86+
let x = Matrix.toCSRInplace this.OclContext this.WorkGroupSize HostInterop
8687
funCSC2CSR <- Some x
8788
x
8889
| Some x -> x

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open GraphBLAS.FSharp.Backend.Common
88
open GraphBLAS.FSharp.Backend.Quotes
99
open GraphBLAS.FSharp.Backend.Vector
1010
open GraphBLAS.FSharp.Backend.Vector.Dense
11+
open GraphBLAS.FSharp.Backend.Objects.ClContext
1112
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
1213

1314
module BFS =
@@ -22,9 +23,10 @@ module BFS =
2223
SpMV.runTo clContext add mul workGroupSize
2324

2425
let zeroCreate =
25-
ClArray.zeroCreate clContext workGroupSize
26+
ClArray.zeroCreate clContext workGroupSize HostInterop
2627

27-
let ofList = Vector.ofList clContext Dense
28+
let ofList =
29+
Vector.ofList clContext workGroupSize DeviceOnly
2830

2931
let maskComplementedTo =
3032
DenseVector.elementWiseTo clContext Mask.complementedMaskOp workGroupSize
@@ -40,7 +42,8 @@ module BFS =
4042

4143
let levels = zeroCreate queue vertexCount
4244

43-
let frontier = ofList vertexCount [ source, 1 ]
45+
let frontier =
46+
ofList queue Dense vertexCount [ source, 1 ]
4447

4548
match frontier with
4649
| ClVector.Dense front ->

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

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,10 @@ namespace GraphBLAS.FSharp.Backend.Common
22

33
open Brahma.FSharp
44
open Microsoft.FSharp.Quotations
5+
open GraphBLAS.FSharp.Backend.Objects.ClContext
56

67
module ClArray =
7-
type ClContext with
8-
member this.CreateClArrayWithGPUOnlyFlags(size: int) =
9-
this.CreateClArray(
10-
size,
11-
deviceAccessMode = DeviceAccessMode.ReadWrite,
12-
hostAccessMode = HostAccessMode.NotAccessible,
13-
allocationMode = AllocationMode.Default
14-
)
15-
16-
member this.CreateClArrayWithGPUOnlyFlags(array: 'a []) =
17-
this.CreateClArray(
18-
array,
19-
deviceAccessMode = DeviceAccessMode.ReadWrite,
20-
hostAccessMode = HostAccessMode.NotAccessible,
21-
allocationMode = AllocationMode.CopyHostPtr
22-
)
23-
24-
let init (initializer: Expr<int -> 'a>) (clContext: ClContext) workGroupSize =
8+
let init (clContext: ClContext) workGroupSize flag (initializer: Expr<int -> 'a>) =
259

2610
let init =
2711
<@ fun (range: Range1D) (outputBuffer: ClArray<'a>) (length: int) ->
@@ -34,9 +18,8 @@ module ClArray =
3418
let program = clContext.Compile(init)
3519

3620
fun (processor: MailboxProcessor<_>) (length: int) ->
37-
// TODO: Выставить нужные флаги
3821
let outputArray =
39-
clContext.CreateClArrayWithGPUOnlyFlags(length)
22+
clContext.CreateClArrayWithFlag(flag, length)
4023

4124
let kernel = program.GetKernel()
4225

@@ -48,7 +31,7 @@ module ClArray =
4831

4932
outputArray
5033

51-
let create (clContext: ClContext) workGroupSize =
34+
let create (clContext: ClContext) workGroupSize flag =
5235

5336
let create =
5437
<@ fun (range: Range1D) (outputBuffer: ClArray<'a>) (length: int) (value: ClCell<'a>) ->
@@ -64,7 +47,7 @@ module ClArray =
6447
let value = clContext.CreateClCell(value)
6548

6649
let outputArray =
67-
clContext.CreateClArrayWithGPUOnlyFlags(length)
50+
clContext.CreateClArrayWithFlag(flag, length)
6851

6952
let kernel = program.GetKernel()
7053

@@ -77,15 +60,14 @@ module ClArray =
7760

7861
outputArray
7962

80-
let zeroCreate (clContext: ClContext) workGroupSize =
63+
let zeroCreate (clContext: ClContext) workGroupSize flag =
8164

82-
let create = create clContext workGroupSize
65+
let create = create clContext workGroupSize flag
8366

84-
fun (processor: MailboxProcessor<_>) (length: int) -> create processor length Unchecked.defaultof<'a>
67+
fun (processor: MailboxProcessor<_>) length -> create processor length Unchecked.defaultof<'a>
8568

86-
let copy (clContext: ClContext) workGroupSize =
69+
let copy (clContext: ClContext) workGroupSize flag =
8770
let copy =
88-
8971
<@ fun (ndRange: Range1D) (inputArrayBuffer: ClArray<'a>) (outputArrayBuffer: ClArray<'a>) inputArrayLength ->
9072

9173
let i = ndRange.GlobalID0
@@ -100,7 +82,7 @@ module ClArray =
10082
Range1D.CreateValid(inputArray.Length, workGroupSize)
10183

10284
let outputArray =
103-
clContext.CreateClArrayWithGPUOnlyFlags inputArray.Length
85+
clContext.CreateClArrayWithFlag(flag, inputArray.Length)
10486

10587
let kernel = program.GetKernel()
10688

@@ -112,7 +94,7 @@ module ClArray =
11294

11395
outputArray
11496

115-
let replicate (clContext: ClContext) =
97+
let replicate (clContext: ClContext) flag =
11698

11799
let replicate =
118100
<@ fun (ndRange: Range1D) (inputArrayBuffer: ClArray<'a>) (outputArrayBuffer: ClArray<'a>) inputArrayLength outputArrayLength ->
@@ -128,7 +110,7 @@ module ClArray =
128110
let outputArrayLength = inputArray.Length * count
129111

130112
let outputArray =
131-
clContext.CreateClArrayWithGPUOnlyFlags outputArrayLength
113+
clContext.CreateClArrayWithFlag(flag, outputArrayLength)
132114

133115
let ndRange =
134116
Range1D.CreateValid(outputArray.Length, workGroupSize)
@@ -190,25 +172,25 @@ module ClArray =
190172
///<param name="zero">Zero element for binary operation.</param>
191173
let prefixSumIncludeInplace = PrefixSum.runIncludeInplace
192174

193-
let prefixSumExclude plus (clContext: ClContext) workGroupSize =
175+
let prefixSumExclude plus (clContext: ClContext) workGroupSize flag =
194176

195177
let runExcludeInplace =
196178
prefixSumExcludeInplace plus clContext workGroupSize
197179

198-
let copy = copy clContext workGroupSize
180+
let copy = copy clContext workGroupSize flag
199181

200182
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
201183

202184
let outputArray = copy processor inputArray
203185

204186
runExcludeInplace processor outputArray totalSum zero
205187

206-
let prefixSumInclude plus (clContext: ClContext) workGroupSize =
188+
let prefixSumInclude plus (clContext: ClContext) workGroupSize flag =
207189

208190
let runIncludeInplace =
209191
prefixSumIncludeInplace plus clContext workGroupSize
210192

211-
let copy = copy clContext workGroupSize
193+
let copy = copy clContext workGroupSize flag
212194

213195
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) (totalSum: ClCell<'a>) (zero: 'a) ->
214196

@@ -222,7 +204,7 @@ module ClArray =
222204
let prefixSumBackwardsIncludeInplace plus =
223205
PrefixSum.runBackwardsIncludeInplace plus
224206

225-
let getUniqueBitmap (clContext: ClContext) =
207+
let getUniqueBitmap (clContext: ClContext) flag =
226208

227209
let getUniqueBitmap =
228210
<@ fun (ndRange: Range1D) (inputArray: ClArray<'a>) inputLength (isUniqueBitmap: ClArray<int>) ->
@@ -245,7 +227,7 @@ module ClArray =
245227
Range1D.CreateValid(inputLength, workGroupSize)
246228

247229
let bitmap =
248-
clContext.CreateClArrayWithGPUOnlyFlags inputLength
230+
clContext.CreateClArrayWithFlag(flag, inputLength)
249231

250232
let kernel = kernel.GetKernel()
251233

@@ -264,10 +246,10 @@ module ClArray =
264246
let scatter =
265247
Scatter.runInplace clContext workGroupSize
266248

267-
let getUniqueBitmap = getUniqueBitmap clContext
249+
let getUniqueBitmap = getUniqueBitmap clContext DeviceOnly
268250

269251
let prefixSumExclude =
270-
prefixSumExclude <@ (+) @> clContext workGroupSize
252+
prefixSumExclude <@ (+) @> clContext workGroupSize DeviceOnly
271253

272254
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) ->
273255

@@ -289,7 +271,7 @@ module ClArray =
289271
a.[0]
290272

291273
let outputArray =
292-
clContext.CreateClArrayWithGPUOnlyFlags resultLength
274+
clContext.CreateClArrayWithFlag(DeviceOnly, resultLength)
293275

294276
scatter processor positions inputArray outputArray
295277

@@ -323,3 +305,31 @@ module ClArray =
323305
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
324306

325307
result
308+
309+
let map<'a, 'b> (clContext: ClContext) workGroupSize flag (op: Expr<'a -> 'b>) =
310+
311+
let map =
312+
<@ fun (ndRange: Range1D) (lenght: int) (inputArray: ClArray<'a>) (result: ClArray<'b>) ->
313+
314+
let gid = ndRange.GlobalID0
315+
316+
if gid < lenght then
317+
result.[gid] <- (%op) inputArray.[gid] @>
318+
319+
let kernel = clContext.Compile map
320+
321+
fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) ->
322+
323+
let result =
324+
clContext.CreateClArrayWithFlag(flag, inputArray.Length)
325+
326+
let ndRange =
327+
Range1D.CreateValid(inputArray.Length, workGroupSize)
328+
329+
let kernel = kernel.GetKernel()
330+
331+
processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange inputArray.Length inputArray result))
332+
333+
processor.Post(Msg.CreateRunMsg<_, _>(kernel))
334+
335+
result

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/Masks.fs" />
1919
<Compile Include="Objects/AtLeastOne.fs" />
20+
<Compile Include="Objects/ClContextExtensions.fs" />
2021
<Compile Include="Quotes/SubReduce.fs" />
2122
<Compile Include="Quotes/Arithmetic.fs" />
2223
<Compile Include="Quotes/Convert.fs" />

0 commit comments

Comments
 (0)