|
| 1 | +namespace GraphBLAS.FSharp.Backend.Vector.Sparse |
| 2 | + |
| 3 | +open FSharp.Quotations.Evaluator |
| 4 | +open Microsoft.FSharp.Quotations |
| 5 | +open Brahma.FSharp |
| 6 | +open GraphBLAS.FSharp.Backend |
| 7 | +open GraphBLAS.FSharp.Backend.Quotes |
| 8 | +open GraphBLAS.FSharp.Backend.Vector.Sparse |
| 9 | +open GraphBLAS.FSharp.Backend.Objects.ClVector |
| 10 | +open GraphBLAS.FSharp.Backend.Common.ClArray |
| 11 | +open GraphBLAS.FSharp.Backend.Objects.ClCell |
| 12 | +open GraphBLAS.FSharp.Backend.Objects.ClContext |
| 13 | + |
| 14 | +module Map = |
| 15 | + module WithValueOption = |
| 16 | + let preparePositions<'a, 'b, 'c> opAdd (clContext: ClContext) workGroupSize = |
| 17 | + |
| 18 | + let preparePositions (op: Expr<'a option -> 'b option -> 'c option>) = |
| 19 | + <@ fun (ndRange: Range1D) (operand: ClCell<'a option>) size valuesLength (indices: ClArray<int>) (values: ClArray<'b>) (resultIndices: ClArray<int>) (resultValues: ClArray<'c>) (resultBitmap: ClArray<int>) -> |
| 20 | + |
| 21 | + let gid = ndRange.GlobalID0 |
| 22 | + |
| 23 | + if gid < size then |
| 24 | + |
| 25 | + let value = |
| 26 | + (%Search.Bin.byKey) valuesLength gid indices values |
| 27 | + |
| 28 | + match (%op) operand.Value value with |
| 29 | + | Some resultValue -> |
| 30 | + resultValues.[gid] <- resultValue |
| 31 | + resultIndices.[gid] <- gid |
| 32 | + resultBitmap.[gid] <- 1 |
| 33 | + | None -> resultBitmap.[gid] <- 0 @> |
| 34 | + |
| 35 | + let kernel = |
| 36 | + clContext.Compile <| preparePositions opAdd |
| 37 | + |
| 38 | + fun (processor: MailboxProcessor<_>) (value: ClCell<'a option>) (vector: Sparse<'b>) -> |
| 39 | + |
| 40 | + let resultBitmap = |
| 41 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, vector.Size) |
| 42 | + |
| 43 | + let resultIndices = |
| 44 | + clContext.CreateClArrayWithSpecificAllocationMode<int>(DeviceOnly, vector.Size) |
| 45 | + |
| 46 | + let resultValues = |
| 47 | + clContext.CreateClArrayWithSpecificAllocationMode<'c>(DeviceOnly, vector.Size) |
| 48 | + |
| 49 | + let ndRange = |
| 50 | + Range1D.CreateValid(vector.Size, workGroupSize) |
| 51 | + |
| 52 | + let kernel = kernel.GetKernel() |
| 53 | + |
| 54 | + processor.Post( |
| 55 | + Msg.MsgSetArguments |
| 56 | + (fun () -> |
| 57 | + kernel.KernelFunc |
| 58 | + ndRange |
| 59 | + value |
| 60 | + vector.Size |
| 61 | + vector.Values.Length |
| 62 | + vector.Indices |
| 63 | + vector.Values |
| 64 | + resultIndices |
| 65 | + resultValues |
| 66 | + resultBitmap) |
| 67 | + ) |
| 68 | + |
| 69 | + processor.Post(Msg.CreateRunMsg<_, _> kernel) |
| 70 | + |
| 71 | + resultIndices, resultValues, resultBitmap |
| 72 | + |
| 73 | + let run<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct> |
| 74 | + (clContext: ClContext) |
| 75 | + workGroupSize |
| 76 | + (op: Expr<'a option -> 'b option -> 'c option>) |
| 77 | + = |
| 78 | + |
| 79 | + let map = |
| 80 | + preparePositions op clContext workGroupSize |
| 81 | + |
| 82 | + let opOnHost = op |> QuotationEvaluator.Evaluate |
| 83 | + |
| 84 | + let setPositions = |
| 85 | + Common.setPositionsOption<'c> clContext workGroupSize |
| 86 | + |
| 87 | + let create = create clContext workGroupSize |
| 88 | + |
| 89 | + let init = |
| 90 | + init <@ fun x -> x @> clContext workGroupSize |
| 91 | + |
| 92 | + fun (queue: MailboxProcessor<_>) allocationMode (value: 'a option) size -> |
| 93 | + function |
| 94 | + | Some vector -> |
| 95 | + let valueClCell = clContext.CreateClCell value |
| 96 | + |
| 97 | + let indices, values, bitmap = map queue valueClCell vector |
| 98 | + |
| 99 | + valueClCell.Free queue |
| 100 | + |
| 101 | + let result = |
| 102 | + setPositions queue allocationMode values indices bitmap |
| 103 | + |
| 104 | + queue.Post(Msg.CreateFreeMsg<_>(indices)) |
| 105 | + queue.Post(Msg.CreateFreeMsg<_>(values)) |
| 106 | + queue.Post(Msg.CreateFreeMsg<_>(bitmap)) |
| 107 | + |
| 108 | + result |
| 109 | + |> Option.bind |
| 110 | + (fun (resultValues, resultIndices) -> |
| 111 | + { Context = clContext |
| 112 | + Size = size |
| 113 | + Indices = resultIndices |
| 114 | + Values = resultValues } |
| 115 | + |> Some) |
| 116 | + | None -> |
| 117 | + opOnHost value None |
| 118 | + |> Option.bind |
| 119 | + (fun resultValue -> |
| 120 | + let resultValues = |
| 121 | + create queue allocationMode size resultValue |
| 122 | + |
| 123 | + let resultIndices = init queue allocationMode size |
| 124 | + |
| 125 | + { Context = clContext |
| 126 | + Size = size |
| 127 | + Indices = resultIndices |
| 128 | + Values = resultValues } |
| 129 | + |> Some) |
0 commit comments