|
| 1 | +namespace GraphBLAS.FSharp.Backend.Common |
| 2 | + |
| 3 | +open Brahma.FSharp |
| 4 | +open GraphBLAS.FSharp.Backend.Objects |
| 5 | +open GraphBLAS.FSharp.Backend.Objects.ClContext |
| 6 | + |
| 7 | +module Merge = |
| 8 | + module Vector = |
| 9 | + let run<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) workGroupSize = |
| 10 | + |
| 11 | + let merge = |
| 12 | + <@ fun (ndRange: Range1D) (firstSide: int) (secondSide: int) (sumOfSides: int) (firstIndicesBuffer: ClArray<int>) (firstValuesBuffer: ClArray<'a>) (secondIndicesBuffer: ClArray<int>) (secondValuesBuffer: ClArray<'b>) (allIndicesBuffer: ClArray<int>) (firstResultValues: ClArray<'a>) (secondResultValues: ClArray<'b>) (isLeftBitMap: ClArray<int>) -> |
| 13 | + |
| 14 | + let gid = ndRange.GlobalID0 |
| 15 | + let lid = ndRange.LocalID0 |
| 16 | + |
| 17 | + let mutable beginIdxLocal = local () |
| 18 | + let mutable endIdxLocal = local () |
| 19 | + |
| 20 | + if lid < 2 then |
| 21 | + // (n - 1) * wgSize - 1 for lid = 0 |
| 22 | + // n * wgSize - 1 for lid = 1 |
| 23 | + // where n in 1 .. wgGroupCount |
| 24 | + let x = lid * (workGroupSize - 1) + gid - 1 |
| 25 | + |
| 26 | + let diagonalNumber = min (sumOfSides - 1) x |
| 27 | + |
| 28 | + let mutable leftEdge = max 0 (diagonalNumber + 1 - secondSide) |
| 29 | + let mutable rightEdge = min (firstSide - 1) diagonalNumber |
| 30 | + |
| 31 | + while leftEdge <= rightEdge do |
| 32 | + let middleIdx = (leftEdge + rightEdge) / 2 |
| 33 | + |
| 34 | + let firstIndex = firstIndicesBuffer.[middleIdx] |
| 35 | + |
| 36 | + let secondIndex = |
| 37 | + secondIndicesBuffer.[diagonalNumber - middleIdx] |
| 38 | + |
| 39 | + if firstIndex <= secondIndex then |
| 40 | + leftEdge <- middleIdx + 1 |
| 41 | + else |
| 42 | + rightEdge <- middleIdx - 1 |
| 43 | + |
| 44 | + // Here localID equals either 0 or 1 |
| 45 | + if lid = 0 then |
| 46 | + beginIdxLocal <- leftEdge |
| 47 | + else |
| 48 | + endIdxLocal <- leftEdge |
| 49 | + |
| 50 | + barrierLocal () |
| 51 | + |
| 52 | + let beginIdx = beginIdxLocal |
| 53 | + let endIdx = endIdxLocal |
| 54 | + let firstLocalLength = endIdx - beginIdx |
| 55 | + let mutable x = workGroupSize - firstLocalLength |
| 56 | + |
| 57 | + if endIdx = firstSide then |
| 58 | + x <- secondSide - gid + lid + beginIdx |
| 59 | + |
| 60 | + let secondLocalLength = x |
| 61 | + |
| 62 | + //First indices are from 0 to firstLocalLength - 1 inclusive |
| 63 | + //Second indices are from firstLocalLength to firstLocalLength + secondLocalLength - 1 inclusive |
| 64 | + let localIndices = localArray<int> workGroupSize |
| 65 | + |
| 66 | + if lid < firstLocalLength then |
| 67 | + localIndices.[lid] <- firstIndicesBuffer.[beginIdx + lid] |
| 68 | + |
| 69 | + if lid < secondLocalLength then |
| 70 | + localIndices.[firstLocalLength + lid] <- secondIndicesBuffer.[gid - beginIdx] |
| 71 | + |
| 72 | + barrierLocal () |
| 73 | + |
| 74 | + if gid < sumOfSides then |
| 75 | + let mutable leftEdge = lid + 1 - secondLocalLength |
| 76 | + if leftEdge < 0 then leftEdge <- 0 |
| 77 | + |
| 78 | + let mutable rightEdge = firstLocalLength - 1 |
| 79 | + |
| 80 | + rightEdge <- min rightEdge lid |
| 81 | + |
| 82 | + while leftEdge <= rightEdge do |
| 83 | + let middleIdx = (leftEdge + rightEdge) / 2 |
| 84 | + let firstIndex = localIndices.[middleIdx] |
| 85 | + |
| 86 | + let secondIndex = |
| 87 | + localIndices.[firstLocalLength + lid - middleIdx] |
| 88 | + |
| 89 | + if firstIndex <= secondIndex then |
| 90 | + leftEdge <- middleIdx + 1 |
| 91 | + else |
| 92 | + rightEdge <- middleIdx - 1 |
| 93 | + |
| 94 | + let boundaryX = rightEdge |
| 95 | + let boundaryY = lid - leftEdge |
| 96 | + |
| 97 | + // boundaryX and boundaryY can't be off the right edge of array (only off the left edge) |
| 98 | + let isValidX = boundaryX >= 0 |
| 99 | + let isValidY = boundaryY >= 0 |
| 100 | + |
| 101 | + let mutable fstIdx = 0 |
| 102 | + |
| 103 | + if isValidX then |
| 104 | + fstIdx <- localIndices.[boundaryX] |
| 105 | + |
| 106 | + let mutable sndIdx = 0 |
| 107 | + |
| 108 | + if isValidY then |
| 109 | + sndIdx <- localIndices.[firstLocalLength + boundaryY] |
| 110 | + |
| 111 | + if not isValidX || isValidY && fstIdx <= sndIdx then |
| 112 | + allIndicesBuffer.[gid] <- sndIdx |
| 113 | + secondResultValues.[gid] <- secondValuesBuffer.[gid - lid - beginIdx + boundaryY] |
| 114 | + isLeftBitMap.[gid] <- 0 |
| 115 | + else |
| 116 | + allIndicesBuffer.[gid] <- fstIdx |
| 117 | + firstResultValues.[gid] <- firstValuesBuffer.[beginIdx + boundaryX] |
| 118 | + isLeftBitMap.[gid] <- 1 @> |
| 119 | + |
| 120 | + let kernel = clContext.Compile merge |
| 121 | + |
| 122 | + fun (processor: MailboxProcessor<_>) (firstVector: ClVector.Sparse<'a>) (secondVector: ClVector.Sparse<'b>) -> |
| 123 | + |
| 124 | + let firstSide = firstVector.Indices.Length |
| 125 | + |
| 126 | + let secondSide = secondVector.Indices.Length |
| 127 | + |
| 128 | + let sumOfSides = firstSide + secondSide |
| 129 | + |
| 130 | + let allIndices = |
| 131 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, sumOfSides) |
| 132 | + |
| 133 | + let firstValues = |
| 134 | + clContext.CreateClArrayWithSpecificAllocationMode<'a>(DeviceOnly, sumOfSides) |
| 135 | + |
| 136 | + let secondValues = |
| 137 | + clContext.CreateClArrayWithSpecificAllocationMode<'b>(DeviceOnly, sumOfSides) |
| 138 | + |
| 139 | + let isLeftBitmap = |
| 140 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, sumOfSides) |
| 141 | + |
| 142 | + let ndRange = |
| 143 | + Range1D.CreateValid(sumOfSides, workGroupSize) |
| 144 | + |
| 145 | + let kernel = kernel.GetKernel() |
| 146 | + |
| 147 | + processor.Post( |
| 148 | + Msg.MsgSetArguments |
| 149 | + (fun () -> |
| 150 | + kernel.KernelFunc |
| 151 | + ndRange |
| 152 | + firstSide |
| 153 | + secondSide |
| 154 | + sumOfSides |
| 155 | + firstVector.Indices |
| 156 | + firstVector.Values |
| 157 | + secondVector.Indices |
| 158 | + secondVector.Values |
| 159 | + allIndices |
| 160 | + firstValues |
| 161 | + secondValues |
| 162 | + isLeftBitmap) |
| 163 | + ) |
| 164 | + |
| 165 | + processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
| 166 | + |
| 167 | + allIndices, firstValues, secondValues, isLeftBitmap |
| 168 | + |
0 commit comments