@@ -3,6 +3,8 @@ namespace GraphBLAS.FSharp.Backend.Common
33open Brahma.FSharp
44open Microsoft.FSharp .Quotations
55open GraphBLAS.FSharp .Backend .Objects .ClContext
6+ open GraphBLAS.FSharp .Backend .Objects .ClCell
7+ open GraphBLAS.FSharp .Backend .Quotes
68
79module 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
0 commit comments