|
| 1 | +namespace GraphBLAS.FSharp.Backend.Matrix.CSR |
| 2 | + |
| 3 | +open Brahma.FSharp |
| 4 | +open Microsoft.FSharp.Quotations |
| 5 | +open GraphBLAS.FSharp.Backend |
| 6 | +open GraphBLAS.FSharp.Backend.Matrix |
| 7 | +open GraphBLAS.FSharp.Backend.Quotes |
| 8 | +open GraphBLAS.FSharp.Backend.Objects |
| 9 | +open GraphBLAS.FSharp.Backend.Objects.ClMatrix |
| 10 | +open GraphBLAS.FSharp.Backend.Objects.ClContext |
| 11 | +open GraphBLAS.FSharp.Backend.Matrix.COO |
| 12 | + |
| 13 | +module internal Map2 = |
| 14 | + let preparePositions<'a, 'b, 'c> (clContext: ClContext) workGroupSize opAdd = |
| 15 | + |
| 16 | + let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) = |
| 17 | + <@ fun (ndRange: Range1D) rowCount columnCount (leftValues: ClArray<'a>) (leftRowPointers: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRowPointers: ClArray<int>) (rightColumn: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'c>) (resultRows: ClArray<int>) (resultColumns: ClArray<int>) -> |
| 18 | + |
| 19 | + let gid = ndRange.GlobalID0 |
| 20 | + |
| 21 | + if gid < rowCount * columnCount then |
| 22 | + |
| 23 | + let columnIndex = gid % columnCount |
| 24 | + let rowIndex = gid / columnCount |
| 25 | + |
| 26 | + let leftStartIndex = leftRowPointers.[rowIndex] |
| 27 | + let leftLastIndex = leftRowPointers.[rowIndex + 1] - 1 |
| 28 | + |
| 29 | + let rightStartIndex = rightRowPointers.[rowIndex] |
| 30 | + let rightLastIndex = rightRowPointers.[rowIndex + 1] - 1 |
| 31 | + |
| 32 | + let leftValue = |
| 33 | + (%BinSearch.searchInRange) leftStartIndex leftLastIndex columnIndex leftColumns leftValues |
| 34 | + |
| 35 | + let rightValue = |
| 36 | + (%BinSearch.searchInRange) rightStartIndex rightLastIndex columnIndex rightColumn rightValues |
| 37 | + |
| 38 | + match (%op) leftValue rightValue with |
| 39 | + | Some value -> |
| 40 | + resultValues.[gid] <- value |
| 41 | + resultRows.[gid] <- rowIndex |
| 42 | + resultColumns.[gid] <- columnIndex |
| 43 | + |
| 44 | + resultBitmap.[gid] <- 1 |
| 45 | + | None -> resultBitmap.[gid] <- 0 @> |
| 46 | + |
| 47 | + let kernel = |
| 48 | + clContext.Compile <| preparePositions opAdd |
| 49 | + |
| 50 | + fun (processor: MailboxProcessor<_>) rowCount columnCount (leftValues: ClArray<'a>) (leftRows: ClArray<int>) (leftColumns: ClArray<int>) (rightValues: ClArray<'b>) (rightRows: ClArray<int>) (rightColumns: ClArray<int>) -> |
| 51 | + |
| 52 | + let (resultLength: int) = columnCount * rowCount |
| 53 | + |
| 54 | + let resultBitmap = |
| 55 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength) |
| 56 | + |
| 57 | + let resultRows = |
| 58 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength) |
| 59 | + |
| 60 | + let resultColumns = |
| 61 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength) |
| 62 | + |
| 63 | + let resultValues = |
| 64 | + clContext.CreateClArrayWithSpecificAllocationMode<'c>(DeviceOnly, resultLength) |
| 65 | + |
| 66 | + let ndRange = |
| 67 | + Range1D.CreateValid(resultLength, workGroupSize) |
| 68 | + |
| 69 | + let kernel = kernel.GetKernel() |
| 70 | + |
| 71 | + processor.Post( |
| 72 | + Msg.MsgSetArguments |
| 73 | + (fun () -> |
| 74 | + kernel.KernelFunc |
| 75 | + ndRange |
| 76 | + rowCount |
| 77 | + columnCount |
| 78 | + leftValues |
| 79 | + leftRows |
| 80 | + leftColumns |
| 81 | + rightValues |
| 82 | + rightRows |
| 83 | + rightColumns |
| 84 | + resultBitmap |
| 85 | + resultValues |
| 86 | + resultRows |
| 87 | + resultColumns) |
| 88 | + ) |
| 89 | + |
| 90 | + processor.Post(Msg.CreateRunMsg<_, _> kernel) |
| 91 | + |
| 92 | + resultBitmap, resultValues, resultRows, resultColumns |
| 93 | + |
| 94 | + ///<param name="clContext">.</param> |
| 95 | + ///<param name="opAdd">.</param> |
| 96 | + ///<param name="workGroupSize">Should be a power of 2 and greater than 1.</param> |
| 97 | + let runToCOO<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality> |
| 98 | + (clContext: ClContext) |
| 99 | + (opAdd: Expr<'a option -> 'b option -> 'c option>) |
| 100 | + workGroupSize |
| 101 | + = |
| 102 | + |
| 103 | + let map2 = |
| 104 | + preparePositions clContext workGroupSize opAdd |
| 105 | + |
| 106 | + let setPositions = |
| 107 | + Common.setPositions<'c> clContext workGroupSize |
| 108 | + |
| 109 | + fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSR<'b>) -> |
| 110 | + |
| 111 | + let bitmap, values, rows, columns = |
| 112 | + map2 |
| 113 | + queue |
| 114 | + matrixLeft.RowCount |
| 115 | + matrixLeft.ColumnCount |
| 116 | + matrixLeft.Values |
| 117 | + matrixLeft.RowPointers |
| 118 | + matrixLeft.Columns |
| 119 | + matrixRight.Values |
| 120 | + matrixRight.RowPointers |
| 121 | + matrixRight.Columns |
| 122 | + |
| 123 | + let resultRows, resultColumns, resultValues, _ = |
| 124 | + setPositions queue allocationMode rows columns values bitmap |
| 125 | + |
| 126 | + queue.Post(Msg.CreateFreeMsg<_>(bitmap)) |
| 127 | + queue.Post(Msg.CreateFreeMsg<_>(values)) |
| 128 | + queue.Post(Msg.CreateFreeMsg<_>(rows)) |
| 129 | + queue.Post(Msg.CreateFreeMsg<_>(columns)) |
| 130 | + |
| 131 | + { Context = clContext |
| 132 | + RowCount = matrixLeft.RowCount |
| 133 | + ColumnCount = matrixLeft.ColumnCount |
| 134 | + Rows = resultRows |
| 135 | + Columns = resultColumns |
| 136 | + Values = resultValues } |
| 137 | + |
| 138 | + let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct and 'c: equality> |
| 139 | + (clContext: ClContext) |
| 140 | + (opAdd: Expr<'a option -> 'b option -> 'c option>) |
| 141 | + workGroupSize |
| 142 | + = |
| 143 | + |
| 144 | + let map2ToCOO = runToCOO clContext opAdd workGroupSize |
| 145 | + |
| 146 | + let toCSRInplace = |
| 147 | + Matrix.toCSRInplace clContext workGroupSize |
| 148 | + |
| 149 | + fun (queue: MailboxProcessor<_>) allocationMode (matrixLeft: ClMatrix.CSR<'a>) (matrixRight: ClMatrix.CSR<'b>) -> |
| 150 | + map2ToCOO queue allocationMode matrixLeft matrixRight |
| 151 | + |> toCSRInplace queue allocationMode |
0 commit comments