@@ -12,15 +12,7 @@ module DenseVector =
1212 ( workGroupSize : int )
1313 =
1414
15- let eWiseAdd =
16- <@ fun ( ndRange : Range1D ) resultLength ( leftVector : ClArray < 'a option >) ( rightVector : ClArray < 'b option >) ( resultVector : ClArray < 'c option >) ->
17-
18- let gid = ndRange.GlobalID0
19-
20- if gid < resultLength then
21- resultVector.[ gid] <- (% opAdd) leftVector.[ gid] rightVector.[ gid] @>
22-
23- let kernel = clContext.Compile( eWiseAdd)
15+ let kernel = clContext.Compile( ElementwiseQuotes.kernel opAdd)
2416
2517 fun ( processor : MailboxProcessor < _ >) ( leftVector : ClArray < 'a option >) ( rightVector : ClArray < 'b option >) ->
2618
@@ -52,19 +44,7 @@ module DenseVector =
5244 ( workGroupSize : int )
5345 =
5446
55- let eWiseAdd =
56- <@ fun ( ndRange : Range1D ) resultLength ( leftVector : ClArray < 'a option >) ( rightVector : ClArray < 'b option >) ( resultVector : ClArray < 'c option >) ->
57-
58- let gid = ndRange.GlobalID0
59-
60- if gid < resultLength then
61- match leftVector.[ gid], rightVector.[ gid] with
62- | Some left, Some right -> resultVector.[ gid] <- (% opAdd) ( Both( left, right))
63- | Some left, None -> resultVector.[ gid] <- (% opAdd) ( Left left)
64- | None, Some right -> resultVector.[ gid] <- (% opAdd) ( Right right)
65- | _ -> resultVector.[ gid] <- None @>
66-
67- let kernel = clContext.Compile( eWiseAdd)
47+ let kernel = clContext.Compile( ElementwiseQuotes.atLeastOneKernel opAdd)
6848
6949 fun ( processor : MailboxProcessor < _ >) ( leftVector : ClArray < 'a option >) ( rightVector : ClArray < 'b option >) ->
7050
@@ -90,66 +70,35 @@ module DenseVector =
9070
9171 resultVector
9272
93- let fillSubVector < 'a , 'b when 'a : struct and 'b : struct > ( clContext : ClContext ) ( workGroupSize : int ) ( scalar : 'a ) =
94-
95- let eWiseAdd =
96- elementWiseAtLeastOne clContext ( StandardOperations.maskAtLeastOne scalar) workGroupSize
97-
98- fun ( processor : MailboxProcessor < _ >) ( leftVector : ClArray < 'a option >) ( maskVector : ClArray < 'b option >) ->
99-
100- let clScalar = clContext.CreateClCell scalar
101-
102- let resultVector = eWiseAdd processor leftVector maskVector
103-
104- processor.Post( Msg.CreateFreeMsg<_>( maskVector))
105-
106- processor.Post( Msg.CreateFreeMsg<_>( clScalar))
107-
108- resultVector
109-
110- let complemented < 'a when 'a : struct > ( clContext : ClContext ) ( workGroupSize : int ) =
111-
112- let complemented =
113- <@ fun ( ndRange : Range1D ) length ( inputArray : ClArray < 'a option >) ( defaultValue : ClCell < 'a >) ( resultArray : ClArray < 'a option >) ->
114-
115- let gid = ndRange.GlobalID0
116-
117- if gid < length then
118- match inputArray.[ gid] with
119- | None -> resultArray.[ gid] <- Some defaultValue.Value
120- | _ -> () @>
121-
122-
123- let kernel = clContext.Compile( complemented)
124-
125- let create =
126- ClArray.zeroCreate clContext workGroupSize
127-
128- fun ( processor : MailboxProcessor < _ >) ( vector : ClArray < 'a option >) ->
129-
130- let length = vector.Length
73+ let fillSubVector < 'a , 'b when 'a : struct and 'b : struct >
74+ ( clContext : ClContext )
75+ ( maskOp : Expr < 'a option -> 'b option -> 'a -> 'a option >)
76+ ( workGroupSize : int ) =
13177
132- let resultArray = create processor length
78+ let kernel = clContext.Compile ( ElementwiseQuotes.fillSubVector maskOp )
13379
134- let defaultValue =
135- clContext.CreateClCell Unchecked.defaultof< 'a>
80+ fun ( processor : MailboxProcessor < _ >) ( leftVector : ClArray < 'a option >) ( maskVector : ClArray < 'b option >) ( value : ClCell < 'a >) ->
81+ let resultArray =
82+ clContext.CreateClArray(
83+ leftVector.Length,
84+ hostAccessMode = HostAccessMode.NotAccessible,
85+ deviceAccessMode = DeviceAccessMode.ReadWrite,
86+ allocationMode = AllocationMode.Default
87+ )
13688
13789 let ndRange =
138- Range1D.CreateValid( length , workGroupSize)
90+ Range1D.CreateValid( leftVector.Length , workGroupSize)
13991
14092 let kernel = kernel.GetKernel()
14193
14294 processor.Post(
143- Msg.MsgSetArguments( fun () -> kernel.KernelFunc ndRange length vector defaultValue resultArray)
95+ Msg.MsgSetArguments( fun () ->
96+ kernel.KernelFunc ndRange leftVector.Length leftVector maskVector value resultArray)
14497 )
14598
146- processor.Post( Msg.CreateRunMsg( kernel))
147-
148- processor.Post( Msg.CreateFreeMsg( defaultValue))
149-
15099 resultArray
151100
152- let getBitmap < 'a when 'a : struct > ( clContext : ClContext ) ( workGroupSize : int ) =
101+ let private getBitmap < 'a when 'a : struct > ( clContext : ClContext ) ( workGroupSize : int ) =
153102
154103 let getPositions =
155104 <@ fun ( ndRange : Range1D ) length ( vector : ClArray < 'a option >) ( positions : ClArray < int >) ->
@@ -202,7 +151,6 @@ module DenseVector =
202151 resultIndices.[ index] <- gid
203152 | None -> () @>
204153
205-
206154 let kernel = clContext.Compile( getValuesAndIndices)
207155
208156 let getPositions = getBitmap clContext workGroupSize
0 commit comments