|
1 | 1 | namespace GraphBLAS.FSharp.Backend.Common |
2 | 2 |
|
3 | 3 | open Brahma.FSharp |
4 | | -open GraphBLAS.FSharp.Backend |
5 | 4 | open GraphBLAS.FSharp.Backend.Quotes |
| 5 | +open Microsoft.FSharp.Control |
6 | 6 | open Microsoft.FSharp.Quotations |
7 | 7 |
|
8 | | -module internal Sum = |
9 | | - let private scan (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) zero = |
| 8 | +module Fold = |
| 9 | + let private runGeneral (clContext: ClContext) (workGroupSize: int) scan scanToCell = |
| 10 | + |
| 11 | + fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) -> |
| 12 | + |
| 13 | + let scan = scan processor |
| 14 | + |
| 15 | + let firstLength = |
| 16 | + (inputArray.Length - 1) / workGroupSize + 1 |
| 17 | + |
| 18 | + let firstVerticesArray = |
| 19 | + clContext.CreateClArray( |
| 20 | + firstLength, |
| 21 | + hostAccessMode = HostAccessMode.NotAccessible, |
| 22 | + deviceAccessMode = DeviceAccessMode.ReadWrite, |
| 23 | + allocationMode = AllocationMode.Default |
| 24 | + ) |
| 25 | + |
| 26 | + let secondLength = (firstLength - 1) / workGroupSize + 1 |
| 27 | + |
| 28 | + let secondVerticesArray = |
| 29 | + clContext.CreateClArray( |
| 30 | + secondLength, |
| 31 | + hostAccessMode = HostAccessMode.NotAccessible, |
| 32 | + deviceAccessMode = DeviceAccessMode.ReadWrite, |
| 33 | + allocationMode = AllocationMode.Default |
| 34 | + ) |
| 35 | + |
| 36 | + let mutable verticesArrays = firstVerticesArray, secondVerticesArray |
| 37 | + let swap (a, b) = (b, a) |
| 38 | + |
| 39 | + scan inputArray inputArray.Length (fst verticesArrays) |
| 40 | + |
| 41 | + let mutable verticesLength = firstLength |
| 42 | + |
| 43 | + while verticesLength > workGroupSize do |
| 44 | + let fstVertices = fst verticesArrays |
| 45 | + let sndVertices = snd verticesArrays |
| 46 | + |
| 47 | + scan fstVertices verticesLength sndVertices |
| 48 | + |
| 49 | + verticesArrays <- swap verticesArrays |
| 50 | + verticesLength <- (verticesLength - 1) / workGroupSize + 1 |
| 51 | + |
| 52 | + let fstVertices = fst verticesArrays |
| 53 | + |
| 54 | + let result = |
| 55 | + scanToCell processor fstVertices verticesLength |
| 56 | + |
| 57 | + processor.Post(Msg.CreateFreeMsg(firstVerticesArray)) |
| 58 | + processor.Post(Msg.CreateFreeMsg(secondVerticesArray)) |
| 59 | + |
| 60 | + result |
| 61 | + |
| 62 | + let scanSum (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) zero = |
10 | 63 |
|
11 | 64 | let subSum = SubSum.sequentialSum opAdd |
12 | 65 |
|
@@ -43,7 +96,7 @@ module internal Sum = |
43 | 96 |
|
44 | 97 | processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
45 | 98 |
|
46 | | - let private scanToCell (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) zero = |
| 99 | + let scanToCellSum (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) zero = |
47 | 100 |
|
48 | 101 | let subSum = SubSum.sequentialSum opAdd |
49 | 102 |
|
@@ -83,60 +136,111 @@ module internal Sum = |
83 | 136 |
|
84 | 137 | resultCell |
85 | 138 |
|
86 | | - let run (clContext: ClContext) (workGroupSize: int) (opAdd: Expr<'a -> 'a -> 'a>) (zero: 'a) = |
| 139 | + let sum (clContext: ClContext) workGroupSize op zero = |
87 | 140 |
|
88 | | - let scan = scan clContext workGroupSize opAdd zero |
| 141 | + let scan = scanSum clContext workGroupSize op zero |
89 | 142 |
|
90 | 143 | let scanToCell = |
91 | | - scanToCell clContext workGroupSize opAdd zero |
| 144 | + scanToCellSum clContext workGroupSize op zero |
92 | 145 |
|
93 | | - fun (processor: MailboxProcessor<_>) (inputArray: ClArray<'a>) -> |
| 146 | + let run = |
| 147 | + runGeneral clContext workGroupSize scan scanToCell |
94 | 148 |
|
95 | | - let scan = scan processor |
| 149 | + fun (processor: MailboxProcessor<_>) (array: ClArray<'a>) -> run processor array |
96 | 150 |
|
97 | | - let firstLength = |
98 | | - (inputArray.Length - 1) / workGroupSize + 1 |
| 151 | + let private scanReduce<'a when 'a: struct> |
| 152 | + (clContext: ClContext) |
| 153 | + (workGroupSize: int) |
| 154 | + (opAdd: Expr<'a -> 'a -> 'a>) |
| 155 | + = |
99 | 156 |
|
100 | | - let firstVerticesArray = |
101 | | - clContext.CreateClArray( |
102 | | - firstLength, |
103 | | - hostAccessMode = HostAccessMode.NotAccessible, |
104 | | - deviceAccessMode = DeviceAccessMode.ReadWrite, |
105 | | - allocationMode = AllocationMode.Default |
106 | | - ) |
| 157 | + let scan = |
| 158 | + <@ fun (ndRange: Range1D) length (inputArray: ClArray<'a>) (resultArray: ClArray<'a>) -> |
107 | 159 |
|
108 | | - let secondLength = (firstLength - 1) / workGroupSize + 1 |
| 160 | + let gid = ndRange.GlobalID0 |
| 161 | + let lid = ndRange.LocalID0 |
109 | 162 |
|
110 | | - let secondVerticesArray = |
111 | | - clContext.CreateClArray( |
112 | | - secondLength, |
113 | | - hostAccessMode = HostAccessMode.NotAccessible, |
114 | | - deviceAccessMode = DeviceAccessMode.ReadWrite, |
115 | | - allocationMode = AllocationMode.Default |
116 | | - ) |
| 163 | + let localValues = localArray<'a> workGroupSize |
117 | 164 |
|
118 | | - let mutable verticesArrays = firstVerticesArray, secondVerticesArray |
119 | | - let swap (a, b) = (b, a) |
| 165 | + if gid < length then |
| 166 | + localValues.[lid] <- inputArray.[gid] |
120 | 167 |
|
121 | | - scan inputArray inputArray.Length (fst verticesArrays) |
| 168 | + barrierLocal () |
122 | 169 |
|
123 | | - let mutable verticesLength = firstLength |
| 170 | + if gid < length then |
124 | 171 |
|
125 | | - while verticesLength > workGroupSize do |
126 | | - let fstVertices = fst verticesArrays |
127 | | - let sndVertices = snd verticesArrays |
| 172 | + (%SubReduce.run opAdd) length workGroupSize gid lid localValues |
128 | 173 |
|
129 | | - scan fstVertices verticesLength sndVertices |
| 174 | + if lid = 0 then |
| 175 | + resultArray.[gid / workGroupSize] <- localValues.[0] @> |
130 | 176 |
|
131 | | - verticesArrays <- swap verticesArrays |
132 | | - verticesLength <- (verticesLength - 1) / workGroupSize + 1 |
| 177 | + let kernel = clContext.Compile(scan) |
133 | 178 |
|
134 | | - let fstVertices = fst verticesArrays |
| 179 | + fun (processor: MailboxProcessor<_>) (valuesArray: ClArray<'a>) valuesLength (resultArray: ClArray<'a>) -> |
135 | 180 |
|
136 | | - let result = |
137 | | - scanToCell processor fstVertices verticesLength |
| 181 | + let ndRange = |
| 182 | + Range1D.CreateValid(valuesArray.Length, workGroupSize) |
138 | 183 |
|
139 | | - processor.Post(Msg.CreateFreeMsg(firstVerticesArray)) |
140 | | - processor.Post(Msg.CreateFreeMsg(secondVerticesArray)) |
| 184 | + let kernel = kernel.GetKernel() |
141 | 185 |
|
142 | | - result |
| 186 | + processor.Post( |
| 187 | + Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange valuesLength valuesArray resultArray) |
| 188 | + ) |
| 189 | + |
| 190 | + processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
| 191 | + |
| 192 | + let rec private scanToCellReduce<'a when 'a: struct> |
| 193 | + (clContext: ClContext) |
| 194 | + (workGroupSize: int) |
| 195 | + (opAdd: Expr<'a -> 'a -> 'a>) |
| 196 | + = |
| 197 | + |
| 198 | + let scan = |
| 199 | + <@ fun (ndRange: Range1D) length (inputArray: ClArray<'a>) (resultValue: ClCell<'a>) -> |
| 200 | + |
| 201 | + let gid = ndRange.GlobalID0 |
| 202 | + let lid = ndRange.LocalID0 |
| 203 | + |
| 204 | + let localValues = localArray<'a> workGroupSize |
| 205 | + |
| 206 | + if gid < length then |
| 207 | + localValues.[lid] <- inputArray.[gid] |
| 208 | + |
| 209 | + barrierLocal () |
| 210 | + |
| 211 | + if gid < length then |
| 212 | + |
| 213 | + (%SubReduce.run opAdd) length workGroupSize gid lid localValues |
| 214 | + |
| 215 | + if lid = 0 then |
| 216 | + resultValue.Value <- localValues.[0] @> |
| 217 | + |
| 218 | + let kernel = clContext.Compile(scan) |
| 219 | + |
| 220 | + fun (processor: MailboxProcessor<_>) (valuesArray: ClArray<'a>) valuesLength -> |
| 221 | + |
| 222 | + let ndRange = |
| 223 | + Range1D.CreateValid(valuesArray.Length, workGroupSize) |
| 224 | + |
| 225 | + let resultCell = |
| 226 | + clContext.CreateClCell Unchecked.defaultof<'a> |
| 227 | + |
| 228 | + let kernel = kernel.GetKernel() |
| 229 | + |
| 230 | + processor.Post(Msg.MsgSetArguments(fun () -> kernel.KernelFunc ndRange valuesLength valuesArray resultCell)) |
| 231 | + |
| 232 | + processor.Post(Msg.CreateRunMsg<_, _>(kernel)) |
| 233 | + |
| 234 | + resultCell |
| 235 | + |
| 236 | + let reduce (clContext: ClContext) workGroupSize op = |
| 237 | + |
| 238 | + let scan = scanReduce clContext workGroupSize op |
| 239 | + |
| 240 | + let scanToCell = |
| 241 | + scanToCellReduce clContext workGroupSize op |
| 242 | + |
| 243 | + let run = |
| 244 | + runGeneral clContext workGroupSize scan scanToCell |
| 245 | + |
| 246 | + fun (processor: MailboxProcessor<_>) (array: ClArray<'a>) -> run processor array |
0 commit comments