@@ -132,208 +132,207 @@ module ClArray =
132132
133133 outputArray
134134
135- let private getUniqueBitmapGeneral predicate ( clContext : ClContext ) workGroupSize =
135+ let map < 'a , 'b > ( op : Expr < 'a -> 'b >) ( clContext : ClContext ) workGroupSize =
136136
137- let getUniqueBitmap =
138- <@ fun ( ndRange : Range1D ) ( inputArray : ClArray < 'a >) inputLength ( isUniqueBitmap : ClArray < int >) ->
137+ let map =
138+ <@ fun ( ndRange : Range1D ) lenght ( inputArray : ClArray < 'a >) ( result : ClArray < 'b >) ->
139139
140140 let gid = ndRange.GlobalID0
141141
142- if gid < inputLength then
143- let isUnique = (% predicate) gid inputLength inputArray // brahma error
144-
145- if isUnique then
146- isUniqueBitmap.[ gid] <- 1
147- else
148- isUniqueBitmap.[ gid] <- 0 @>
142+ if gid < lenght then
143+ result.[ gid] <- (% op) inputArray.[ gid] @>
149144
150- let kernel = clContext.Compile( getUniqueBitmap )
145+ let kernel = clContext.Compile map
151146
152147 fun ( processor : MailboxProcessor < _ >) allocationMode ( inputArray : ClArray < 'a >) ->
153148
154- let inputLength = inputArray.Length
149+ let result =
150+ clContext.CreateClArrayWithSpecificAllocationMode( allocationMode, inputArray.Length)
155151
156152 let ndRange =
157- Range1D.CreateValid( inputLength, workGroupSize)
158-
159- let bitmap =
160- clContext.CreateClArrayWithSpecificAllocationMode( allocationMode, inputLength)
153+ Range1D.CreateValid( inputArray.Length, workGroupSize)
161154
162155 let kernel = kernel.GetKernel()
163156
164- processor.Post( Msg.MsgSetArguments( fun () -> kernel.KernelFunc ndRange inputArray inputLength bitmap ))
157+ processor.Post( Msg.MsgSetArguments( fun () -> kernel.KernelFunc ndRange inputArray.Length inputArray result ))
165158
166- processor.Post( Msg.CreateRunMsg<_, _> kernel)
159+ processor.Post( Msg.CreateRunMsg<_, _>( kernel) )
167160
168- bitmap
161+ result
169162
170- let getUniqueBitmapFirstOccurrence clContext =
171- getUniqueBitmapGeneral
172- <| Predicates.firstOccurrence ()
173- <| clContext
163+ let map2InPlace < 'a , 'b , 'c > ( map : Expr < 'a -> 'b -> 'c >) ( clContext : ClContext ) workGroupSize =
174164
175- let getUniqueBitmapLastOccurrence clContext =
176- getUniqueBitmapGeneral
177- <| Predicates.lastOccurrence ()
178- <| clContext
165+ let kernel =
166+ <@ fun ( ndRange : Range1D ) length ( leftArray : ClArray < 'a >) ( rightArray : ClArray < 'b >) ( resultArray : ClArray < 'c >) ->
179167
180- ///<description >Remove duplicates form the given array.</description >
181- ///<param name =" clContext " >Computational context</param >
182- ///<param name =" workGroupSize " >Should be a power of 2 and greater than 1.</param >
183- ///<param name =" inputArray " >Should be sorted.</param >
184- let removeDuplications ( clContext : ClContext ) workGroupSize =
168+ let gid = ndRange.GlobalID0
185169
186- let scatter =
187- Scatter.lastOccurrence clContext workGroupSize
170+ if gid < length then
188171
189- let getUniqueBitmap =
190- getUniqueBitmapLastOccurrence clContext workGroupSize
172+ resultArray.[ gid] <- (% map) leftArray.[ gid] rightArray.[ gid] @>
191173
192- let prefixSumExclude =
193- PrefixSum.runExcludeInPlace <@ (+) @> clContext workGroupSize
174+ let kernel = clContext.Compile kernel
194175
195- fun ( processor : MailboxProcessor < _ >) ( inputArray : ClArray < 'a >) ->
176+ fun ( processor : MailboxProcessor < _ >) ( leftArray : ClArray < 'a >) ( rightArray : ClArray < 'b >) ( resultArray : ClArray < 'c >) ->
196177
197- let bitmap =
198- getUniqueBitmap processor DeviceOnly inputArray
178+ let ndRange =
179+ Range1D.CreateValid ( resultArray.Length , workGroupSize )
199180
200- let resultLength =
201- ( prefixSumExclude processor bitmap 0 )
202- .ToHostAndFree( processor)
181+ let kernel = kernel.GetKernel()
203182
204- let outputArray =
205- clContext.CreateClArrayWithSpecificAllocationMode( DeviceOnly, resultLength)
183+ processor.Post(
184+ Msg.MsgSetArguments
185+ ( fun () -> kernel.KernelFunc ndRange resultArray.Length leftArray rightArray resultArray)
186+ )
206187
207- scatter processor bitmap inputArray outputArray
188+ processor.Post ( Msg.CreateRunMsg <_, _>( kernel ))
208189
209- processor.Post <| Msg.CreateFreeMsg<_>( bitmap)
190+ let map2 < 'a , 'b , 'c > map ( clContext : ClContext ) workGroupSize =
191+ let map2 =
192+ map2InPlace< 'a, 'b, 'c> map clContext workGroupSize
210193
211- outputArray
194+ fun ( processor : MailboxProcessor < _ >) allocationMode ( leftArray : ClArray < 'a >) ( rightArray : ClArray < 'b >) ->
212195
213- let exists ( predicate : Expr < 'a -> bool >) ( clContext : ClContext ) workGroupSize =
196+ let resultArray =
197+ clContext.CreateClArrayWithSpecificAllocationMode( allocationMode, leftArray.Length)
214198
215- let exists =
216- <@ fun ( ndRange : Range1D ) length ( vector : ClArray < 'a >) ( result : ClCell < bool >) ->
199+ map2 processor leftArray rightArray resultArray
217200
218- let gid = ndRange.GlobalID0
201+ resultArray
219202
220- if gid < length then
221- let isExist = (% predicate ) vector .[ gid ]
203+ module Bitmap =
204+ let private getUniqueBitmapGeneral predicate ( clContext : ClContext ) workGroupSize =
222205
223- if isExist then result.Value <- true @>
206+ let getUniqueBitmap =
207+ <@ fun ( ndRange : Range1D ) ( inputArray : ClArray < 'a >) inputLength ( isUniqueBitmap : ClArray < int >) ->
224208
225- let kernel = clContext.Compile exists
209+ let gid = ndRange.GlobalID0
226210
227- fun ( processor : MailboxProcessor < _ >) ( vector : ClArray < 'a >) ->
211+ if gid < inputLength then
212+ let isUnique = (% predicate) gid inputLength inputArray // brahma error
228213
229- let result = clContext.CreateClCell false
214+ if isUnique then
215+ isUniqueBitmap.[ gid] <- 1
216+ else
217+ isUniqueBitmap.[ gid] <- 0 @>
230218
231- let ndRange =
232- Range1D.CreateValid( vector.Length, workGroupSize)
219+ let kernel = clContext.Compile( getUniqueBitmap)
233220
234- let kernel = kernel.GetKernel ()
221+ fun ( processor : MailboxProcessor < _ >) allocationMode ( inputArray : ClArray < 'a >) ->
235222
236- processor.Post ( Msg.MsgSetArguments ( fun () -> kernel.KernelFunc ndRange vector.Length vector result ))
223+ let inputLength = inputArray.Length
237224
238- processor.Post( Msg.CreateRunMsg<_, _>( kernel))
225+ let ndRange =
226+ Range1D.CreateValid( inputLength, workGroupSize)
239227
240- result
228+ let bitmap =
229+ clContext.CreateClArrayWithSpecificAllocationMode( allocationMode, inputLength)
241230
242- let map < 'a , 'b > ( op : Expr < 'a -> 'b >) ( clContext : ClContext ) workGroupSize =
231+ let kernel = kernel.GetKernel ()
243232
244- let map =
245- <@ fun ( ndRange : Range1D ) lenght ( inputArray : ClArray < 'a >) ( result : ClArray < 'b >) ->
233+ processor.Post( Msg.MsgSetArguments( fun () -> kernel.KernelFunc ndRange inputArray inputLength bitmap))
246234
247- let gid = ndRange.GlobalID0
235+ processor.Post ( Msg.CreateRunMsg <_, _> kernel )
248236
249- if gid < lenght then
250- result.[ gid] <- (% op) inputArray.[ gid] @>
237+ bitmap
251238
252- let kernel = clContext.Compile map
239+ let firstOccurrence clContext =
240+ getUniqueBitmapGeneral
241+ <| Predicates.firstOccurrence ()
242+ <| clContext
253243
254- fun ( processor : MailboxProcessor < _ >) allocationMode ( inputArray : ClArray < 'a >) ->
244+ let lastOccurrence clContext =
245+ getUniqueBitmapGeneral
246+ <| Predicates.lastOccurrence ()
247+ <| clContext
255248
256- let result =
257- clContext.CreateClArrayWithSpecificAllocationMode( allocationMode, inputArray.Length)
249+ let private getUniqueBitmap2General < 'a when 'a : equality > getUniqueBitmap ( clContext : ClContext ) workGroupSize =
258250
259- let ndRange =
260- Range1D.CreateValid ( inputArray.Length , workGroupSize)
251+ let map =
252+ map2 <@ fun x y -> x ||| y @> clContext workGroupSize
261253
262- let kernel = kernel.GetKernel ()
254+ let firstGetBitmap = getUniqueBitmap clContext workGroupSize
263255
264- processor.Post( Msg.MsgSetArguments( fun () -> kernel.KernelFunc ndRange inputArray.Length inputArray result))
256+ fun ( processor : MailboxProcessor < _ >) allocationMode ( firstArray : ClArray < 'a >) ( secondArray : ClArray < 'a >) ->
257+ let firstBitmap =
258+ firstGetBitmap processor DeviceOnly firstArray
265259
266- processor.Post( Msg.CreateRunMsg<_, _>( kernel))
260+ let secondBitmap =
261+ firstGetBitmap processor DeviceOnly secondArray
267262
268- result
263+ let result =
264+ map processor allocationMode firstBitmap secondBitmap
269265
270- let map2InPlace < 'a , 'b , 'c > ( map : Expr < 'a -> 'b -> 'c >) ( clContext : ClContext ) workGroupSize =
266+ firstBitmap.Free processor
267+ secondBitmap.Free processor
271268
272- let kernel =
273- <@ fun ( ndRange : Range1D ) length ( leftArray : ClArray < 'a >) ( rightArray : ClArray < 'b >) ( resultArray : ClArray < 'c >) ->
269+ result
274270
275- let gid = ndRange.GlobalID0
271+ let firstOccurrence2 clContext = getUniqueBitmap2General firstOccurrence clContext
276272
277- if gid < length then
273+ let lastOccurrence2 clContext = getUniqueBitmap2General lastOccurrence clContext
278274
279- resultArray.[ gid] <- (% map) leftArray.[ gid] rightArray.[ gid] @>
275+ ///<description >Remove duplicates form the given array.</description >
276+ ///<param name =" clContext " >Computational context</param >
277+ ///<param name =" workGroupSize " >Should be a power of 2 and greater than 1.</param >
278+ ///<param name =" inputArray " >Should be sorted.</param >
279+ let removeDuplications ( clContext : ClContext ) workGroupSize =
280280
281- let kernel = clContext.Compile kernel
281+ let scatter =
282+ Scatter.lastOccurrence clContext workGroupSize
282283
283- fun ( processor : MailboxProcessor < _ >) ( leftArray : ClArray < 'a >) ( rightArray : ClArray < 'b >) ( resultArray : ClArray < 'c >) ->
284+ let getUniqueBitmap =
285+ Bitmap.lastOccurrence clContext workGroupSize
284286
285- let ndRange =
286- Range1D.CreateValid ( resultArray.Length , workGroupSize)
287+ let prefixSumExclude =
288+ PrefixSum.runExcludeInPlace <@ (+) @> clContext workGroupSize
287289
288- let kernel = kernel.GetKernel ()
290+ fun ( processor : MailboxProcessor < _ >) ( inputArray : ClArray < 'a >) ->
289291
290- processor.Post(
291- Msg.MsgSetArguments
292- ( fun () -> kernel.KernelFunc ndRange resultArray.Length leftArray rightArray resultArray)
293- )
292+ let bitmap =
293+ getUniqueBitmap processor DeviceOnly inputArray
294294
295- processor.Post( Msg.CreateRunMsg<_, _>( kernel))
295+ let resultLength =
296+ ( prefixSumExclude processor bitmap 0 )
297+ .ToHostAndFree( processor)
296298
297- let map2 < 'a , 'b , 'c > map ( clContext : ClContext ) workGroupSize =
298- let map2 =
299- map2InPlace< 'a, 'b, 'c> map clContext workGroupSize
299+ let outputArray =
300+ clContext.CreateClArrayWithSpecificAllocationMode( DeviceOnly, resultLength)
300301
301- fun ( processor : MailboxProcessor < _ >) allocationMode ( leftArray : ClArray < 'a >) ( rightArray : ClArray < 'b >) ->
302+ scatter processor bitmap inputArray outputArray
302303
303- let resultArray =
304- clContext.CreateClArrayWithSpecificAllocationMode( allocationMode, leftArray.Length)
304+ processor.Post <| Msg.CreateFreeMsg<_>( bitmap)
305305
306- map2 processor leftArray rightArray resultArray
306+ outputArray
307307
308- resultArray
308+ let exists ( predicate : Expr < 'a -> bool >) ( clContext : ClContext ) workGroupSize =
309309
310- let getUniqueBitmap2General < 'a when 'a : equality > getUniqueBitmap ( clContext : ClContext ) workGroupSize =
310+ let exists =
311+ <@ fun ( ndRange : Range1D ) length ( vector : ClArray < 'a >) ( result : ClCell < bool >) ->
311312
312- let map =
313- map2 <@ fun x y -> x ||| y @> clContext workGroupSize
313+ let gid = ndRange.GlobalID0
314+
315+ if gid < length then
316+ let isExist = (% predicate) vector.[ gid]
314317
315- let firstGetBitmap = getUniqueBitmap clContext workGroupSize
318+ if isExist then result.Value <- true @>
316319
317- fun ( processor : MailboxProcessor < _ >) allocationMode ( firstArray : ClArray < 'a >) ( secondArray : ClArray < 'a >) ->
318- let firstBitmap =
319- firstGetBitmap processor DeviceOnly firstArray
320+ let kernel = clContext.Compile exists
320321
321- let secondBitmap =
322- firstGetBitmap processor DeviceOnly secondArray
322+ fun ( processor : MailboxProcessor < _ >) ( vector : ClArray < 'a >) ->
323323
324- let result =
325- map processor allocationMode firstBitmap secondBitmap
324+ let result = clContext.CreateClCell false
326325
327- firstBitmap.Free processor
328- secondBitmap.Free processor
326+ let ndRange =
327+ Range1D.CreateValid ( vector.Length , workGroupSize )
329328
330- result
329+ let kernel = kernel.GetKernel ()
331330
332- let getUniqueBitmap2FirstOccurrence clContext =
333- getUniqueBitmap2General getUniqueBitmapFirstOccurrence clContext
331+ processor.Post( Msg.MsgSetArguments( fun () -> kernel.KernelFunc ndRange vector.Length vector result))
334332
335- let getUniqueBitmap2LastOccurrence clContext =
336- getUniqueBitmap2General getUniqueBitmapLastOccurrence clContext
333+ processor.Post( Msg.CreateRunMsg<_, _>( kernel))
334+
335+ result
337336
338337 let assignOption ( op : Expr < 'a -> 'b option >) ( clContext : ClContext ) workGroupSize =
339338
@@ -694,3 +693,4 @@ module ClArray =
694693 Some result
695694 else
696695 None
696+
0 commit comments