@@ -127,7 +127,12 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
127127 override this.Copy () = failwith " Not Implemented"
128128 override this.Resize a = failwith " Not Implemented"
129129 override this.GetNNZ () = failwith " Not Implemented"
130- override this.GetTuples () = failwith " Not Implemented"
130+
131+ override this.GetTuples () =
132+ opencl {
133+ return {| Indices = this.Indices; Values = this.Values |}
134+ }
135+
131136 override this.GetMask (? isComplemented : bool ) =
132137 let isComplemented = defaultArg isComplemented false
133138 failwith " Not Implemented"
@@ -151,7 +156,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
151156 member internal this.CalcPrefixSum
152157 ( inputArray : int []) =
153158
154- let outputArray = Array.copy inputArray
159+ let outputArray = Array.zeroCreate inputArray.Length
155160
156161 if inputArray.Length = 1
157162 then
@@ -177,6 +182,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
177182 }
178183 else
179184 let intermediateArray = Array.zeroCreate (( inputArray.Length + 1 ) / 2 )
185+ let inputArrayLength = inputArray.Length
180186
181187 let fillIntermediateArray =
182188 <@
@@ -185,7 +191,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
185191 ( intermediateArrayBuffer : int []) ->
186192
187193 let i = ndRange.GlobalID0
188- if 2 * i + 1 < inputArrayBuffer.Length
194+ if 2 * i + 1 < inputArrayLength
189195 then intermediateArrayBuffer.[ i] <- inputArrayBuffer.[ 2 * i] + inputArrayBuffer.[ 2 * i + 1 ]
190196 else intermediateArrayBuffer.[ i] <- inputArrayBuffer.[ 2 * i]
191197 @>
@@ -208,10 +214,13 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
208214 ( inputArrayBuffer : int [])
209215 ( outputArrayBuffer : int []) ->
210216
211- let i = ndRange.GlobalID0 + 1
217+ let i = ndRange.GlobalID0
212218 let j = ( i - 1 ) / 2
213219 if i % 2 = 0
214- then outputArrayBuffer.[ i] <- auxiliaryPrefixSumArrayBuffer.[ j] + inputArrayBuffer.[ i]
220+ then
221+ if i = 0
222+ then outputArrayBuffer.[ i] <- inputArrayBuffer.[ i]
223+ else outputArrayBuffer.[ i] <- auxiliaryPrefixSumArrayBuffer.[ j] + inputArrayBuffer.[ i]
215224 else outputArrayBuffer.[ i] <- auxiliaryPrefixSumArrayBuffer.[ j]
216225 @>
217226
@@ -220,7 +229,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
220229 let! auxiliaryPrefixSumArray = this.CalcPrefixSum intermediateArray
221230
222231 let binder kernelP =
223- let ndRange = _ 1D( inputArray.Length - 1 )
232+ let ndRange = _ 1D( inputArray.Length)
224233 kernelP
225234 ndRange
226235 auxiliaryPrefixSumArray
@@ -245,9 +254,6 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
245254 then this.Indices, this.Values, vector.Indices, vector.Values, append
246255 else vector.Indices, vector.Values, this.Indices, this.Values, <@ fun x y -> (% append) y x @>
247256
248- let longSide = firstIndices.Length
249- let shortSide = secondIndices.Length
250-
251257 let filterThroughMask =
252258 opencl {
253259 //TODO
@@ -257,6 +263,9 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
257263 let allIndices = Array.zeroCreate <| firstIndices.Length + secondIndices.Length
258264 let allValues = Array.init ( firstIndices.Length + secondIndices.Length) ( fun _ -> zero)
259265
266+ let longSide = firstIndices.Length
267+ let shortSide = secondIndices.Length
268+
260269 let createSortedConcatenation =
261270 <@
262271 fun ( ndRange : _1D )
@@ -269,8 +278,23 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
269278
270279 let i = ndRange.GlobalID0
271280
272- let mutable leftEdge = max 0 ( i + 1 - shortSide)
273- let mutable rightEdge = min i ( longSide - 1 )
281+ let f n =
282+ if 0 > n + 1 - shortSide
283+ then 0
284+ else n + 1 - shortSide
285+ let mutable leftEdge = f i
286+ // if 0 > i + 1 - shortSide
287+ // then 0
288+ // else i + 1 - shortSide
289+
290+ let g n =
291+ if n > longSide - 1
292+ then longSide - 1
293+ else n
294+ let mutable rightEdge = g i
295+ // if i > longSide - 1
296+ // then longSide - 1
297+ // else i
274298
275299 while leftEdge <= rightEdge do
276300 let middleIdx = ( leftEdge + rightEdge) / 2
@@ -304,7 +328,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
304328 do ! RunCommand createSortedConcatenation binder
305329 }
306330
307- let auxiliaryArray = Array.zeroCreate allIndices.Length
331+ let auxiliaryArray = Array.init allIndices.Length ( fun _ -> 1 )
308332
309333 let fillAuxiliaryArray =
310334 <@
@@ -320,7 +344,6 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
320344 auxiliaryArrayBuffer.[ i + 1 ] <- 0
321345 //Prepare to drop explicit zeroes
322346 allValuesBuffer.[ i] <- (% plus) allValuesBuffer.[ i] allValuesBuffer.[ i + 1 ]
323- else auxiliaryArrayBuffer.[ i + 1 ] <- 1
324347 @>
325348
326349 let fillAuxiliaryArray =
@@ -372,7 +395,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
372395
373396 if auxiliaryArrayBuffer.[ i] = 1
374397 then
375- let index = prefixSumArrayBuffer.[ i]
398+ let index = prefixSumArrayBuffer.[ i] - 1
376399
377400 resultIndicesBuffer.[ index] <- allIndicesBuffer.[ i]
378401 resultValuesBuffer.[ index] <- allValuesBuffer.[ i]
@@ -381,24 +404,28 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
381404 let resultIndices = Array.zeroCreate allIndices.Length
382405 let resultValues = Array.init allValues.Length ( fun _ -> zero)
383406
407+ let createUnion =
408+ opencl {
409+ let! prefixSumArray = this.CalcPrefixSum auxiliaryArray
410+ let binder kernelP =
411+ let ndRange = _ 1D( auxiliaryArray.Length)
412+ kernelP
413+ ndRange
414+ allIndices
415+ allValues
416+ auxiliaryArray
417+ prefixSumArray
418+ resultIndices
419+ resultValues
420+ do ! RunCommand createUnion binder
421+ }
422+
384423 opencl {
385424 do ! createSortedConcatenation
386425 do ! filterThroughMask
387426 do ! fillAuxiliaryArray
388427 do ! dropExplicitZeroes
389-
390- let! prefixSumArray = this.CalcPrefixSum auxiliaryArray
391- let binder kernelP =
392- let ndRange = _ 1D( auxiliaryArray.Length)
393- kernelP
394- ndRange
395- allIndices
396- allValues
397- auxiliaryArray
398- prefixSumArray
399- resultIndices
400- resultValues
401- do ! RunCommand createUnion binder
428+ do ! createUnion
402429
403430 return upcast SparseVector< 'a>( this.Size, resultIndices, resultValues)
404431 }
0 commit comments