|
| 1 | +namespace GraphBLAS.FSharp.Backend.Common |
| 2 | + |
| 3 | +open Brahma.FSharp |
| 4 | +open GraphBLAS.FSharp.Backend.Objects.ClContext |
| 5 | + |
| 6 | +module Merge = |
| 7 | + let run<'a, 'b when 'a: struct and 'b: struct and 'a: comparison> (clContext: ClContext) workGroupSize = |
| 8 | + |
| 9 | + let defaultValue = Unchecked.defaultof<'a> |
| 10 | + |
| 11 | + let merge = |
| 12 | + <@ fun (ndRange: Range1D) (firstSide: int) (secondSide: int) (sumOfSides: int) (firstValues: ClArray<'a>) (secondValues: ClArray<'a>) (resultValues: ClArray<'a>) -> |
| 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 | + |
| 30 | + let mutable rightEdge = min (firstSide - 1) diagonalNumber |
| 31 | + |
| 32 | + while leftEdge <= rightEdge do |
| 33 | + let middleIdx = (leftEdge + rightEdge) / 2 |
| 34 | + |
| 35 | + let firstIndex = firstValues.[middleIdx] |
| 36 | + |
| 37 | + let secondIndex = |
| 38 | + secondValues.[diagonalNumber - middleIdx] |
| 39 | + |
| 40 | + if firstIndex <= secondIndex then |
| 41 | + leftEdge <- middleIdx + 1 |
| 42 | + else |
| 43 | + rightEdge <- middleIdx - 1 |
| 44 | + |
| 45 | + // Here localID equals either 0 or 1 |
| 46 | + if lid = 0 then |
| 47 | + beginIdxLocal <- leftEdge |
| 48 | + else |
| 49 | + endIdxLocal <- leftEdge |
| 50 | + |
| 51 | + barrierLocal () |
| 52 | + |
| 53 | + let beginIdx = beginIdxLocal |
| 54 | + let endIdx = endIdxLocal |
| 55 | + let firstLocalLength = endIdx - beginIdx |
| 56 | + let mutable x = workGroupSize - firstLocalLength |
| 57 | + |
| 58 | + if endIdx = firstSide then |
| 59 | + x <- secondSide - gid + lid + beginIdx |
| 60 | + |
| 61 | + let secondLocalLength = x |
| 62 | + |
| 63 | + //First indices are from 0 to firstLocalLength - 1 inclusive |
| 64 | + //Second indices are from firstLocalLength to firstLocalLength + secondLocalLength - 1 inclusive |
| 65 | + let localIndices = localArray<'a> workGroupSize |
| 66 | + |
| 67 | + if lid < firstLocalLength then |
| 68 | + localIndices.[lid] <- firstValues.[beginIdx + lid] |
| 69 | + |
| 70 | + if lid < secondLocalLength then |
| 71 | + localIndices.[firstLocalLength + lid] <- firstValues.[gid - beginIdx] |
| 72 | + |
| 73 | + barrierLocal () |
| 74 | + |
| 75 | + if gid < sumOfSides then |
| 76 | + let mutable leftEdge = lid + 1 - secondLocalLength |
| 77 | + if leftEdge < 0 then leftEdge <- 0 |
| 78 | + |
| 79 | + let mutable rightEdge = firstLocalLength - 1 |
| 80 | + |
| 81 | + rightEdge <- min rightEdge lid |
| 82 | + |
| 83 | + while leftEdge <= rightEdge do |
| 84 | + let middleIdx = (leftEdge + rightEdge) / 2 |
| 85 | + let firstIndex = localIndices.[middleIdx] |
| 86 | + |
| 87 | + let secondIndex = |
| 88 | + localIndices.[firstLocalLength + lid - middleIdx] |
| 89 | + |
| 90 | + if firstIndex <= secondIndex then |
| 91 | + leftEdge <- middleIdx + 1 |
| 92 | + else |
| 93 | + rightEdge <- middleIdx - 1 |
| 94 | + |
| 95 | + let boundaryX = rightEdge |
| 96 | + let boundaryY = lid - leftEdge |
| 97 | + |
| 98 | + // boundaryX and boundaryY can't be off the right edge of array (only off the left edge) |
| 99 | + let isValidX = boundaryX >= 0 |
| 100 | + let isValidY = boundaryY >= 0 |
| 101 | + |
| 102 | + let mutable fstIdx = defaultValue |
| 103 | + |
| 104 | + if isValidX then |
| 105 | + fstIdx <- localIndices.[boundaryX] |
| 106 | + |
| 107 | + let mutable sndIdx = defaultValue |
| 108 | + |
| 109 | + if isValidY then |
| 110 | + sndIdx <- localIndices.[firstLocalLength + boundaryY] |
| 111 | + |
| 112 | + if not isValidX || isValidY && fstIdx <= sndIdx then |
| 113 | + resultValues.[gid] <- sndIdx |
| 114 | + else |
| 115 | + resultValues.[gid] <- fstIdx @> |
| 116 | + |
| 117 | + let kernel = clContext.Compile merge |
| 118 | + |
| 119 | + fun (processor: MailboxProcessor<_>) (firstValues: ClArray<'a>) (secondValues: ClArray<'a>) -> |
| 120 | + |
| 121 | + let firstSide = firstValues.Length |
| 122 | + |
| 123 | + let secondSide = secondValues.Length |
| 124 | + |
| 125 | + let sumOfSides = firstSide + secondSide |
| 126 | + |
| 127 | + let resultValues = |
| 128 | + clContext.CreateClArrayWithSpecificAllocationMode<'a>(DeviceOnly, sumOfSides) |
| 129 | + |
| 130 | + let ndRange = |
| 131 | + Range1D.CreateValid(sumOfSides, workGroupSize) |
| 132 | + |
| 133 | + let kernel = kernel.GetKernel() |
| 134 | + |
| 135 | + processor.Post( |
| 136 | + Msg.MsgSetArguments |
| 137 | + (fun () -> |
| 138 | + kernel.KernelFunc ndRange firstSide secondSide sumOfSides firstValues secondValues resultValues) |
| 139 | + ) |
| 140 | + |
| 141 | + processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
| 142 | + |
| 143 | + resultValues |
0 commit comments