|
| 1 | +namespace GraphBLAS.FSharp.Backend.Common |
| 2 | + |
| 3 | +open Brahma.FSharp |
| 4 | +open GraphBLAS.FSharp.Backend |
| 5 | +open Microsoft.FSharp.Control |
| 6 | +open Microsoft.FSharp.Quotations |
| 7 | + |
| 8 | +module Reduce = |
| 9 | + let private scan<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) = |
| 10 | + |
| 11 | + let scan = |
| 12 | + <@ fun (ndRange: Range1D) length (inputArray: ClArray<'a>) (resultArray: ClArray<'a>) -> |
| 13 | + |
| 14 | + let gid = ndRange.GlobalID0 |
| 15 | + let lid = ndRange.LocalID0 |
| 16 | + |
| 17 | + let localValues = localArray<'a> workGroupSize |
| 18 | + |
| 19 | + if gid < length then |
| 20 | + localValues.[lid] <- inputArray.[gid] |
| 21 | + |
| 22 | + barrierLocal () |
| 23 | + |
| 24 | + if gid < length then |
| 25 | + |
| 26 | + (%SubReduce.run opAdd) length workGroupSize gid lid localValues |
| 27 | + |
| 28 | + if lid = 0 then |
| 29 | + resultArray.[gid / workGroupSize] <- localValues.[0] @> |
| 30 | + |
| 31 | + let kernel = clContext.Compile(scan) |
| 32 | + |
| 33 | + fun (processor: MailboxProcessor<_>) (valuesArray: ClArray<'a>) valuesLength (resultArray: ClArray<'a>) -> |
| 34 | + |
| 35 | + let ndRange = |
| 36 | + Range1D.CreateValid(valuesArray.Length, workGroupSize) |
| 37 | + |
| 38 | + let kernel = kernel.GetKernel() |
| 39 | + |
| 40 | + processor.Post( |
| 41 | + Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange valuesLength valuesArray resultArray) |
| 42 | + ) |
| 43 | + |
| 44 | + processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
| 45 | + |
| 46 | + let private scanToCell<'a when 'a: struct> |
| 47 | + (clContext: ClContext) |
| 48 | + (workGroupSize: int) |
| 49 | + (opAdd: Expr<'a -> 'a -> 'a>) |
| 50 | + = |
| 51 | + |
| 52 | + let scan = |
| 53 | + <@ fun (ndRange: Range1D) length (inputArray: ClArray<'a>) (resultValue: ClCell<'a>) -> |
| 54 | + |
| 55 | + let gid = ndRange.GlobalID0 |
| 56 | + let lid = ndRange.LocalID0 |
| 57 | + |
| 58 | + let localValues = localArray<'a> workGroupSize |
| 59 | + |
| 60 | + if gid < length then |
| 61 | + localValues.[lid] <- inputArray.[gid] |
| 62 | + |
| 63 | + barrierLocal () |
| 64 | + |
| 65 | + if gid < length then |
| 66 | + |
| 67 | + (%SubReduce.run opAdd) length workGroupSize gid lid localValues |
| 68 | + |
| 69 | + if lid = 0 then |
| 70 | + resultValue.Value <- localValues.[0] @> |
| 71 | + |
| 72 | + let kernel = clContext.Compile(scan) |
| 73 | + |
| 74 | + fun (processor: MailboxProcessor<_>) (valuesArray: ClArray<'a>) valuesLength (resultValue: ClCell<'a>) -> |
| 75 | + |
| 76 | + let ndRange = |
| 77 | + Range1D.CreateValid(valuesArray.Length, workGroupSize) |
| 78 | + |
| 79 | + let kernel = kernel.GetKernel() |
| 80 | + |
| 81 | + processor.Post( |
| 82 | + Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange valuesLength valuesArray resultValue) |
| 83 | + ) |
| 84 | + |
| 85 | + processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
| 86 | + |
| 87 | + let run<'a when 'a: struct> (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) = |
| 88 | + |
| 89 | + let scan = scan clContext workGroupSize opAdd |
| 90 | + |
| 91 | + let scanToCell = scanToCell clContext workGroupSize opAdd |
| 92 | + |
| 93 | + fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) -> |
| 94 | + |
| 95 | + let scan = scan processor |
| 96 | + |
| 97 | + let firstLength = |
| 98 | + (inputArray.Length - 1) / workGroupSize + 1 |
| 99 | + |
| 100 | + let firstVerticesArray = |
| 101 | + clContext.CreateClArray( |
| 102 | + firstLength, |
| 103 | + hostAccessMode = HostAccessMode.NotAccessible, |
| 104 | + deviceAccessMode = DeviceAccessMode.ReadWrite, |
| 105 | + allocationMode = AllocationMode.Default |
| 106 | + ) |
| 107 | + |
| 108 | + let secondLength = (firstLength - 1) / workGroupSize + 1 |
| 109 | + |
| 110 | + let secondVerticesArray = |
| 111 | + clContext.CreateClArray( |
| 112 | + secondLength, |
| 113 | + hostAccessMode = HostAccessMode.NotAccessible, |
| 114 | + deviceAccessMode = DeviceAccessMode.ReadWrite, |
| 115 | + allocationMode = AllocationMode.Default |
| 116 | + ) |
| 117 | + |
| 118 | + let mutable verticesArrays = firstVerticesArray, secondVerticesArray |
| 119 | + let swap (a, b) = (b, a) |
| 120 | + |
| 121 | + scan inputArray inputArray.Length (fst verticesArrays) |
| 122 | + |
| 123 | + let mutable verticesLength = firstLength |
| 124 | + |
| 125 | + while verticesLength > workGroupSize do |
| 126 | + let fstVertices = fst verticesArrays |
| 127 | + let sndVertices = snd verticesArrays |
| 128 | + |
| 129 | + scan fstVertices verticesLength sndVertices |
| 130 | + |
| 131 | + verticesArrays <- swap verticesArrays |
| 132 | + verticesLength <- (verticesLength - 1) / workGroupSize + 1 |
| 133 | + |
| 134 | + let fstVertices = fst verticesArrays |
| 135 | + |
| 136 | + let result = |
| 137 | + clContext.CreateClCell Unchecked.defaultof<'a> |
| 138 | + |
| 139 | + scanToCell processor fstVertices verticesLength result |
| 140 | + |
| 141 | + processor.Post(Msg.CreateFreeMsg(firstVerticesArray)) |
| 142 | + processor.Post(Msg.CreateFreeMsg(secondVerticesArray)) |
| 143 | + |
| 144 | + result |
0 commit comments