Skip to content

Commit 7b21b4f

Browse files
committed
fix: msbfs levels bug
1 parent 95941af commit 7b21b4f

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

src/GraphBLAS-sharp.Backend/Algorithms/MSBFS.fs

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,62 @@ namespace GraphBLAS.FSharp.Backend.Algorithms
33
open Brahma.FSharp
44
open FSharp.Quotations
55
open GraphBLAS.FSharp
6-
open GraphBLAS.FSharp.Backend.Quotes
76
open GraphBLAS.FSharp.Objects
7+
open GraphBLAS.FSharp.Common
88
open GraphBLAS.FSharp.Objects.ClMatrix
99
open GraphBLAS.FSharp.Objects.ArraysExtensions
1010
open GraphBLAS.FSharp.Objects.ClContextExtensions
1111
open GraphBLAS.FSharp.Objects.ClCellExtensions
12+
open GraphBLAS.FSharp.Backend.Quotes
1213
open GraphBLAS.FSharp.Backend.Matrix.LIL
1314
open GraphBLAS.FSharp.Backend.Matrix.COO
1415

1516
module internal MSBFS =
1617
let private frontExclude (clContext: ClContext) workGroupSize =
1718

18-
let excludeValues =
19-
ClArray.excludeElements clContext workGroupSize
19+
let invert =
20+
ClArray.mapInPlace ArithmeticOperations.intNotQ clContext workGroupSize
21+
22+
let prefixSum =
23+
PrefixSum.standardExcludeInPlace clContext workGroupSize
24+
25+
let scatterIndices =
26+
Scatter.lastOccurrence clContext workGroupSize
2027

21-
let excludeIndices =
22-
ClArray.excludeElements clContext workGroupSize
28+
let scatterValues =
29+
Scatter.lastOccurrence clContext workGroupSize
2330

2431
fun (queue: MailboxProcessor<_>) allocationMode (front: ClMatrix.COO<_>) (intersection: ClArray<int>) ->
2532

26-
let newRows =
27-
excludeIndices queue allocationMode intersection front.Rows
33+
invert queue intersection
34+
35+
let length =
36+
(prefixSum queue intersection)
37+
.ToHostAndFree queue
38+
39+
if length = 0 then
40+
None
41+
else
42+
let rows =
43+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, length)
44+
45+
let columns =
46+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, length)
2847

29-
let newColumns =
30-
excludeIndices queue allocationMode intersection front.Columns
48+
let values =
49+
clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, length)
3150

32-
let newValues =
33-
excludeValues queue allocationMode intersection front.Values
51+
scatterIndices queue intersection front.Rows rows
52+
scatterIndices queue intersection front.Columns columns
53+
scatterValues queue intersection front.Values values
3454

35-
match newRows, newColumns, newValues with
36-
| Some rows, Some columns, Some values ->
3755
{ Context = clContext
3856
Rows = rows
3957
Columns = columns
4058
Values = values
4159
RowCount = front.RowCount
4260
ColumnCount = front.ColumnCount }
4361
|> Some
44-
| _ -> None
4562

4663
module Levels =
4764
let private updateFrontAndLevels (clContext: ClContext) workGroupSize =
@@ -70,13 +87,14 @@ module internal MSBFS =
7087

7188
match newFront with
7289
| Some f ->
73-
// Update levels
7490
let levelClCell = clContext.CreateClCell level
7591

92+
// Set current level value to all remaining front positions
7693
setLevel queue levelClCell 0 f.Values.Length f.Values
7794

7895
levelClCell.Free queue
7996

97+
// Update levels
8098
let newLevels = mergeDisjoint queue levels f
8199

82100
newLevels, newFront
@@ -110,7 +128,7 @@ module internal MSBFS =
110128

111129
let mutable front = copy queue DeviceOnly levels
112130

113-
let mutable level = 0
131+
let mutable level = 1
114132
let mutable stop = false
115133

116134
while not stop do
@@ -121,15 +139,21 @@ module internal MSBFS =
121139
| None ->
122140
front.Dispose queue
123141
stop <- true
142+
124143
| Some newFrontier ->
125144
front.Dispose queue
145+
126146
//Filtering visited vertices
127147
match updateFrontAndLevels queue DeviceOnly level newFrontier levels with
128148
| l, Some f ->
129149
front <- f
150+
130151
levels.Dispose queue
152+
131153
levels <- l
154+
132155
newFrontier.Dispose queue
156+
133157
| _, None ->
134158
stop <- true
135159
newFrontier.Dispose queue
@@ -151,8 +175,6 @@ module internal MSBFS =
151175

152176
module Parents =
153177
let private updateFrontAndParents (clContext: ClContext) workGroupSize =
154-
// update parents same as levels
155-
// every front value should be equal to its column number
156178
let frontExclude = frontExclude clContext workGroupSize
157179

158180
let mergeDisjoint =
@@ -175,10 +197,15 @@ module internal MSBFS =
175197

176198
match newFront with
177199
| Some f ->
178-
// Update levels
179200
let resultFront = { f with Values = f.Columns }
180-
let newLevels = mergeDisjoint queue parents f
181-
newLevels, Some resultFront
201+
202+
// Update parents
203+
let newParents = mergeDisjoint queue parents f
204+
205+
f.Values.Free queue
206+
207+
newParents, Some resultFront
208+
182209
| _ -> parents, None
183210

184211
let run<'a when 'a: struct> (clContext: ClContext) workGroupSize =
@@ -190,7 +217,7 @@ module internal MSBFS =
190217
clContext
191218
workGroupSize
192219

193-
let updateFrontAndLevels =
220+
let updateFrontAndParents =
194221
updateFrontAndParents clContext workGroupSize
195222

196223
fun (queue: MailboxProcessor<Msg>) (inputMatrix: ClMatrix<'a>) (source: int list) ->
@@ -227,15 +254,20 @@ module internal MSBFS =
227254
| None ->
228255
front.Dispose queue
229256
stop <- true
257+
230258
| Some newFrontier ->
231259
front.Dispose queue
260+
232261
//Filtering visited vertices
233-
match updateFrontAndLevels queue DeviceOnly newFrontier parents with
234-
| l, Some f ->
262+
match updateFrontAndParents queue DeviceOnly newFrontier parents with
263+
| p, Some f ->
235264
front <- f
265+
236266
parents.Dispose queue
237-
parents <- l
267+
parents <- p
268+
238269
newFrontier.Dispose queue
270+
239271
| _, None ->
240272
stop <- true
241273
newFrontier.Dispose queue

src/GraphBLAS-sharp.Backend/Matrix/COO/Matrix.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,9 @@ module Matrix =
260260

261261
let ofList (clContext: ClContext) allocationMode rowCount columnCount (elements: (int * int * 'a) list) =
262262
let rows, columns, values =
263-
elements
264-
|> Array.ofList
265-
|> Array.sortBy (fun (x, _, _) -> x)
266-
|> Array.unzip3
263+
let elements = elements |> Array.ofList
264+
elements |> Array.sortInPlaceBy (fun (x, _, _) -> x)
265+
elements |> Array.unzip3
267266

268267
{ Context = clContext
269268
Rows = clContext.CreateClArrayWithSpecificAllocationMode(allocationMode, rows)

src/GraphBLAS-sharp.Backend/Quotes/Arithmetic.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace GraphBLAS.FSharp.Backend.Quotes
22

33
open GraphBLAS.FSharp.Objects
4+
open Microsoft.FSharp.Quotations
45

56
module ArithmeticOperations =
67
let inline private mkUnaryOp zero unaryOp =
@@ -231,6 +232,13 @@ module ArithmeticOperations =
231232

232233
let float32Mul = createPair 0.0f (*) <@ (*) @>
233234

235+
// without zero
236+
let intAddWithoutZero =
237+
<@ fun x y -> Some (x + y) @>
238+
239+
let intMulWithoutZero =
240+
<@ fun x y -> Some (x * y) @>
241+
234242
// other operations
235243
let less<'a when 'a: comparison> =
236244
<@ fun (x: 'a option) (y: 'a option) ->

0 commit comments

Comments
 (0)