@@ -2,26 +2,10 @@ namespace GraphBLAS.FSharp.Backend.Common
22
33open Brahma.FSharp
44open Microsoft.FSharp .Quotations
5+ open GraphBLAS.FSharp .Backend .Objects .ClContext
56
67module 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
0 commit comments