@@ -119,82 +119,6 @@ module ElementwiseConstructor =
119119 firstResultValues.[ i] <- firstValuesBuffer.[ beginIdx + boundaryX]
120120 isLeftBitMap.[ i] <- 1 @>
121121
122- module FillSubVectorRead =
123- let both ( opAdd : Expr < 'a option -> 'b option -> 'a -> 'a option >) =
124- <@
125- fun gid ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ( value : 'a ) ->
126- (% opAdd) ( Some leftValues.[ gid]) ( Some rightValues.[ gid + 1 ]) value
127- @>
128-
129- let left ( opAdd : Expr < 'a option -> 'b option -> 'a -> 'a option >) =
130- <@
131- fun gid ( leftValues : ClArray < 'a >) ( value : 'a ) ->
132- (% opAdd) ( Some leftValues.[ gid]) None value
133- @>
134-
135- let right ( opAdd : Expr < 'a option -> 'b option -> 'a -> 'a option >) =
136- <@
137- fun gid ( rightValues : ClArray < 'b >) ( value : 'a ) ->
138- (% opAdd) None ( Some rightValues.[ gid + 1 ]) value
139- @>
140-
141- module FillSubVectorAtLeasOneRead =
142- let both ( opAdd : Expr < AtLeastOne < 'a , 'b > -> 'a -> 'a option >) =
143- <@
144- fun gid ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ( value : 'a ) ->
145- (% opAdd) ( Both( leftValues.[ gid], rightValues.[ gid + 1 ])) value
146- @>
147-
148- let left ( opAdd : Expr < AtLeastOne < 'a , 'b > -> 'a -> 'a option >) =
149- <@
150- fun gid ( leftValues : ClArray < 'a >) ( value : 'a ) ->
151- (% opAdd) ( Left( leftValues.[ gid])) value
152- @>
153-
154- let right ( opAdd : Expr < AtLeastOne < 'a , 'b > -> 'a -> 'a option >) =
155- <@
156- fun gid ( rightValues : ClArray < 'b >) ( value : 'a ) ->
157- (% opAdd) ( Right( rightValues.[ gid])) value
158- @>
159-
160- module ElementWiseRead =
161- let both ( opAdd : Expr < 'a option -> 'b option -> 'c option >) =
162- <@
163- fun gid ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ->
164- (% opAdd) ( Some leftValues.[ gid]) ( Some rightValues.[ gid + 1 ])
165- @>
166-
167- let left ( opAdd : Expr < 'a option -> 'b option -> 'c option >) =
168- <@
169- fun gid ( leftValues : ClArray < 'a >) ->
170- (% opAdd) ( Some leftValues.[ gid]) None
171- @>
172-
173- let right ( opAdd : Expr < 'a option -> 'b option -> 'c option >) =
174- <@
175- fun gid ( rightValues : ClArray < 'b >) ->
176- (% opAdd) None ( Some rightValues.[ gid + 1 ])
177- @>
178-
179- module ElementWiseAtLeasOneRead =
180- let both ( opAdd : Expr < AtLeastOne < 'a , 'b > -> 'c option >) =
181- <@
182- fun gid ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ->
183- (% opAdd) ( Both( leftValues.[ gid], rightValues.[ gid + 1 ]))
184- @>
185-
186- let left ( opAdd : Expr < AtLeastOne < 'a , 'b > -> 'c option >) =
187- <@
188- fun gid ( leftValues : ClArray < 'a >) ->
189- (% opAdd) ( Left( leftValues.[ gid]))
190- @>
191-
192- let right ( opAdd : Expr < AtLeastOne < 'a , 'b > -> 'c option >) =
193- <@
194- fun gid ( rightValues : ClArray < 'b >) ->
195- (% opAdd) ( Right( rightValues.[ gid]))
196- @>
197-
198122 let private both < 'c > =
199123 <@ fun index ( result : 'c option ) ( rawPositionsBuffer : ClArray < int >) ( allValuesBuffer : ClArray < 'c >) ->
200124 rawPositionsBuffer.[ index] <- 0
@@ -220,60 +144,70 @@ module ElementwiseConstructor =
220144 rawPositionsBuffer.[ index] <- 1
221145 | None -> rawPositionsBuffer.[ index] <- 0 @>
222146
223- let private preparePositionsGeneral
224- bothRead
225- leftRead
226- rightRead
227- =
228-
229- <@ fun ( ndRange : Range1D ) length ( allIndices : ClArray < int >) ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ( isLeft : ClArray < int >) ( allValues : ClArray < 'c >) ( positions : ClArray < int >) ->
147+ let prepareFillVector opAdd =
148+ <@ fun ( ndRange : Range1D ) length ( allIndices : ClArray < int >) ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ( value : ClCell < 'a >) ( isLeft : ClArray < int >) ( allValues : ClArray < 'a >) ( positions : ClArray < int >) ->
230149
231150 let gid = ndRange.GlobalID0
232151
152+ let value = value.Value
153+
233154 if gid < length - 1
234155 && allIndices.[ gid] = allIndices.[ gid + 1 ] then
235- let ( result : 'c option ) = (% bothRead ) gid leftValues rightValues
156+ let result = (% opAdd ) ( Some leftValues[ gid ]) ( Some rightValues[ gid + 1 ]) value
236157
237158 (% both) gid result positions allValues
238159 elif ( gid < length
239160 && gid > 0
240161 && allIndices.[ gid - 1 ] <> allIndices.[ gid])
241- || gid = 0 then
242-
243- let leftResult = (% leftRead) gid leftValues
244- let rightResult = (% rightRead) gid rightValues
162+ || gid = 0 then
163+ let leftResult = (% opAdd) ( Some leftValues.[ gid]) None value
164+ let rightResult = (% opAdd) None ( Some rightValues.[ gid]) value
245165
246166 (% leftRight) gid leftResult rightResult isLeft allValues positions @>
247167
248- let private prepareFillVectorGeneral bothRead leftRead rightRead =
249- <@ fun ( ndRange : Range1D ) length ( allIndices : ClArray < int >) ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ( value : ClCell < 'a >) ( isLeft : ClArray < int >) ( allValues : ClArray < 'a >) ( positions : ClArray < int >) ->
168+ let preparePositions opAdd =
169+ <@ fun ( ndRange : Range1D ) length ( allIndices : ClArray < int >) ( leftValues : ClArray < 'a >) ( rightValues : ClArray < 'b >) ( isLeft : ClArray < int >) ( allValues : ClArray < 'c >) ( positions : ClArray < int >) ->
250170
251171 let gid = ndRange.GlobalID0
252172
253- let value = value.Value
254-
255173 if gid < length - 1
256174 && allIndices.[ gid] = allIndices.[ gid + 1 ] then
257- let ( result : 'a option ) = (% bothRead ) gid leftValues rightValues value
175+ let result = (% opAdd ) ( Some leftValues[ gid ]) ( Some rightValues[ gid + 1 ])
258176
259177 (% both) gid result positions allValues
260178 elif ( gid < length
261179 && gid > 0
262180 && allIndices.[ gid - 1 ] <> allIndices.[ gid])
263181 || gid = 0 then
264- let leftResult = (% leftRead ) gid leftValues value
265- let rightResult = (% rightRead ) gid rightValues value
182+ let leftResult = (% opAdd ) ( Some leftValues.[ gid ]) None
183+ let rightResult = (% opAdd ) None ( Some rightValues .[ gid ])
266184
267185 (% leftRight) gid leftResult rightResult isLeft allValues positions @>
268186
269- let preparePositions opAdd =
270- preparePositionsGeneral ( ElementWiseRead.both opAdd) ( ElementWiseRead.left opAdd) ( ElementWiseRead.right opAdd)
271-
272- let preparePositionsAtLeastOne opAdd =
273- preparePositionsGeneral ( ElementWiseAtLeasOneRead.both opAdd) ( ElementWiseAtLeasOneRead.left opAdd) ( ElementWiseAtLeasOneRead.right opAdd)
274-
275- let prepareFillVector opAdd =
276- prepareFillVectorGeneral ( FillSubVectorRead.both opAdd) ( FillSubVectorRead.left opAdd) ( FillSubVectorRead.right opAdd)
277-
278- let prepareFillVectorAtLeastOne opAdd =
279- prepareFillVectorGeneral ( FillSubVectorAtLeasOneRead.both opAdd) ( FillSubVectorAtLeasOneRead.left opAdd) ( FillSubVectorAtLeasOneRead.right opAdd)
187+ let atLeastOneToNormalForm ( op : Expr < AtLeastOne < 'a , 'b > -> 'c option >) =
188+ <@
189+ fun ( leftItem : 'a option ) ( rightItem : 'b option ) ->
190+ match leftItem, rightItem with
191+ | Some left, Some right ->
192+ (% op) ( Both( left, right))
193+ | None, Some right ->
194+ (% op) ( Right right)
195+ | Some left, None ->
196+ (% op) ( Left left)
197+ | None, None ->
198+ None
199+ @>
200+
201+ let fillSubVectorAtLeastOneToNormalForm ( op : Expr < AtLeastOne < 'a , 'b > -> 'a -> 'a option >) =
202+ <@
203+ fun ( leftItem : 'a option ) ( rightItem : 'b option ) ( value : 'a ) ->
204+ match leftItem, rightItem with
205+ | Some left, Some right ->
206+ (% op) ( Both( left, right)) value
207+ | None, Some right ->
208+ (% op) ( Right right) value
209+ | Some left, None ->
210+ (% op) ( Left left) value
211+ | None, None ->
212+ None
213+ @>
0 commit comments