|
| 1 | +namespace GraphBLAS.FSharp.Backend.Matrix.CSR |
| 2 | + |
| 3 | +open Brahma.FSharp |
| 4 | +open FSharp.Quotations |
| 5 | +open GraphBLAS.FSharp.Backend |
| 6 | +open GraphBLAS.FSharp.Backend.Quotes |
| 7 | +open GraphBLAS.FSharp.Backend.Matrix |
| 8 | +open GraphBLAS.FSharp.Backend.Matrix.COO |
| 9 | +open GraphBLAS.FSharp.Backend.Objects |
| 10 | +open GraphBLAS.FSharp.Backend.Objects.ClMatrix |
| 11 | +open GraphBLAS.FSharp.Backend.Objects.ClContext |
| 12 | + |
| 13 | +module internal Map = |
| 14 | + let preparePositions<'a, 'b> (clContext: ClContext) workGroupSize op = |
| 15 | + |
| 16 | + let preparePositions (op: Expr<'a option -> 'b option>) = |
| 17 | + <@ fun (ndRange: Range1D) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) (resultBitmap: ClArray<int>) (resultValues: ClArray<'b>) (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 startIndex = rowPointers.[rowIndex] |
| 27 | + let lastIndex = rowPointers.[rowIndex + 1] - 1 |
| 28 | + |
| 29 | + let value = |
| 30 | + (%Search.Bin.inRange) startIndex lastIndex columnIndex columns values |
| 31 | + |
| 32 | + match (%op) value with |
| 33 | + | Some resultValue -> |
| 34 | + resultValues.[gid] <- resultValue |
| 35 | + resultRows.[gid] <- rowIndex |
| 36 | + resultColumns.[gid] <- columnIndex |
| 37 | + |
| 38 | + resultBitmap.[gid] <- 1 |
| 39 | + | None -> resultBitmap.[gid] <- 0 @> |
| 40 | + |
| 41 | + let kernel = clContext.Compile <| preparePositions op |
| 42 | + |
| 43 | + fun (processor: MailboxProcessor<_>) rowCount columnCount (values: ClArray<'a>) (rowPointers: ClArray<int>) (columns: ClArray<int>) -> |
| 44 | + |
| 45 | + let (resultLength: int) = columnCount * rowCount |
| 46 | + |
| 47 | + let resultBitmap = |
| 48 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength) |
| 49 | + |
| 50 | + let resultRows = |
| 51 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength) |
| 52 | + |
| 53 | + let resultColumns = |
| 54 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, resultLength) |
| 55 | + |
| 56 | + let resultValues = |
| 57 | + clContext.CreateClArrayWithSpecificAllocationMode<'b>(DeviceOnly, resultLength) |
| 58 | + |
| 59 | + let ndRange = |
| 60 | + Range1D.CreateValid(resultLength, workGroupSize) |
| 61 | + |
| 62 | + let kernel = kernel.GetKernel() |
| 63 | + |
| 64 | + processor.Post( |
| 65 | + Msg.MsgSetArguments |
| 66 | + (fun () -> |
| 67 | + kernel.KernelFunc |
| 68 | + ndRange |
| 69 | + rowCount |
| 70 | + columnCount |
| 71 | + values |
| 72 | + rowPointers |
| 73 | + columns |
| 74 | + resultBitmap |
| 75 | + resultValues |
| 76 | + resultRows |
| 77 | + resultColumns) |
| 78 | + ) |
| 79 | + |
| 80 | + processor.Post(Msg.CreateRunMsg<_, _> kernel) |
| 81 | + |
| 82 | + resultBitmap, resultValues, resultRows, resultColumns |
| 83 | + |
| 84 | + let runToCOO<'a, 'b when 'a: struct and 'b: struct and 'b: equality> |
| 85 | + (clContext: ClContext) |
| 86 | + (opAdd: Expr<'a option -> 'b option>) |
| 87 | + workGroupSize |
| 88 | + = |
| 89 | + |
| 90 | + let map = |
| 91 | + preparePositions clContext workGroupSize opAdd |
| 92 | + |
| 93 | + let setPositions = |
| 94 | + Common.setPositions<'b> clContext workGroupSize |
| 95 | + |
| 96 | + fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) -> |
| 97 | + |
| 98 | + let bitmap, values, rows, columns = |
| 99 | + map queue matrix.RowCount matrix.ColumnCount matrix.Values matrix.RowPointers matrix.Columns |
| 100 | + |
| 101 | + let resultRows, resultColumns, resultValues, _ = |
| 102 | + setPositions queue allocationMode rows columns values bitmap |
| 103 | + |
| 104 | + queue.Post(Msg.CreateFreeMsg<_>(bitmap)) |
| 105 | + queue.Post(Msg.CreateFreeMsg<_>(values)) |
| 106 | + queue.Post(Msg.CreateFreeMsg<_>(rows)) |
| 107 | + queue.Post(Msg.CreateFreeMsg<_>(columns)) |
| 108 | + |
| 109 | + { Context = clContext |
| 110 | + RowCount = matrix.RowCount |
| 111 | + ColumnCount = matrix.ColumnCount |
| 112 | + Rows = resultRows |
| 113 | + Columns = resultColumns |
| 114 | + Values = resultValues } |
| 115 | + |
| 116 | + let run<'a, 'b when 'a: struct and 'b: struct and 'b: equality> |
| 117 | + (clContext: ClContext) |
| 118 | + (opAdd: Expr<'a option -> 'b option>) |
| 119 | + workGroupSize |
| 120 | + = |
| 121 | + |
| 122 | + let mapToCOO = runToCOO clContext opAdd workGroupSize |
| 123 | + |
| 124 | + let toCSRInplace = |
| 125 | + Matrix.toCSRInplace clContext workGroupSize |
| 126 | + |
| 127 | + fun (queue: MailboxProcessor<_>) allocationMode (matrix: ClMatrix.CSR<'a>) -> |
| 128 | + mapToCOO queue allocationMode matrix |
| 129 | + |> toCSRInplace queue allocationMode |
0 commit comments