Skip to content

Commit beef3f5

Browse files
committed
Fix bfs running
1 parent 1f2b95b commit beef3f5

9 files changed

Lines changed: 2712 additions & 59 deletions

File tree

arc130.mtx

Lines changed: 1296 additions & 0 deletions
Large diffs are not rendered by default.

benchmarks/GraphBLAS-sharp.Benchmarks/Program.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let main argv =
77
// typeof<EWiseAddBenchmarks4Float32>
88
// typeof<EWiseAddBenchmarks4Bool>
99
typeof<BFSBenchmarks>
10-
// typeof<QGBenchmarks>
10+
typeof<QGBenchmarks>
1111
|]
1212

1313
benchmarks.Run argv |> ignore

src/GraphBLAS-sharp/Backend/Common/Sum.fs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@ open Microsoft.FSharp.Quotations
77
module internal rec Sum =
88
let run (inputArray: 'a[]) (plus: Expr<'a -> 'a -> 'a>) (zero: 'a) =
99
if inputArray.Length = 0 then
10-
opencl { return [| zero |] }
10+
opencl {
11+
let result = [| zero |]
12+
let bruh =
13+
<@ fun (range: _1D) (array: 'a[]) ->
14+
let mutable a = 0
15+
a <- 0
16+
@>
17+
18+
do! RunCommand bruh <| fun kernelPrepare ->
19+
kernelPrepare
20+
<| _1D(64, 64)
21+
<| result
22+
23+
return result
24+
}
1125
else
1226
runNotEmpty inputArray plus zero
1327

src/GraphBLAS-sharp/Backend/Mask/GetComplemented.fs

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,57 @@ open Brahma.OpenCL
88

99
module internal GetComplemented =
1010
let mask1D (mask: Mask1D) = opencl {
11-
let size = mask.Size
12-
let nnz = mask.Indices.Length
13-
14-
let bitmap = Array.create size 1
15-
let getComplementedBitmap =
16-
<@
17-
fun (range: _1D)
18-
(maskIndices: int[])
19-
(bitmap: int[]) ->
20-
21-
let gid = range.GlobalID0
22-
23-
if gid < nnz then
24-
let maskIdx = maskIndices.[gid]
25-
bitmap.[maskIdx] <- 0
26-
@>
27-
28-
do! RunCommand getComplementedBitmap <| fun kernelPrepare ->
29-
kernelPrepare
30-
<| _1D(Utils.getDefaultGlobalSize nnz, Utils.defaultWorkGroupSize)
31-
<| mask.Indices
32-
<| bitmap
33-
34-
let! (positions, _) = PrefixSum.runExclude bitmap
35-
36-
let complementedIndices = Array.zeroCreate<int> (size - nnz)
37-
let setPosotions =
38-
<@
39-
fun (range: _1D)
40-
(positions: int[])
41-
(bitmap: int[])
42-
(complementedIndices: int[]) ->
43-
44-
let gid = range.GlobalID0
45-
46-
if gid < size && bitmap.[gid] = 1 then
47-
complementedIndices.[positions.[gid]] <- gid
48-
@>
49-
50-
do! RunCommand setPosotions <| fun kernelPrepare ->
51-
kernelPrepare
52-
<| _1D(Utils.getDefaultGlobalSize size, Utils.defaultWorkGroupSize)
53-
<| positions
54-
<| bitmap
55-
<| complementedIndices
56-
57-
return Mask1D(complementedIndices, size, not mask.IsComplemented)
11+
if mask.Indices.Length = 0 then
12+
let indices = Array.init mask.Size id
13+
return Mask1D(indices, mask.Size, not mask.IsComplemented)
14+
elif mask.Indices.Length = mask.Size then
15+
return Mask1D([||], mask.Size, not mask.IsComplemented)
16+
else
17+
let size = mask.Size
18+
let nnz = mask.Indices.Length
19+
20+
let bitmap = Array.create size 1
21+
let getComplementedBitmap =
22+
<@
23+
fun (range: _1D)
24+
(maskIndices: int[])
25+
(bitmap: int[]) ->
26+
27+
let gid = range.GlobalID0
28+
29+
if gid < nnz then
30+
let maskIdx = maskIndices.[gid]
31+
bitmap.[maskIdx] <- 0
32+
@>
33+
34+
do! RunCommand getComplementedBitmap <| fun kernelPrepare ->
35+
kernelPrepare
36+
<| _1D(Utils.getDefaultGlobalSize nnz, Utils.defaultWorkGroupSize)
37+
<| mask.Indices
38+
<| bitmap
39+
40+
let! (positions, _) = PrefixSum.runExclude bitmap
41+
42+
let complementedIndices = Array.zeroCreate<int> (size - nnz)
43+
let setPosotions =
44+
<@
45+
fun (range: _1D)
46+
(positions: int[])
47+
(bitmap: int[])
48+
(complementedIndices: int[]) ->
49+
50+
let gid = range.GlobalID0
51+
52+
if gid < size && bitmap.[gid] = 1 then
53+
complementedIndices.[positions.[gid]] <- gid
54+
@>
55+
56+
do! RunCommand setPosotions <| fun kernelPrepare ->
57+
kernelPrepare
58+
<| _1D(Utils.getDefaultGlobalSize size, Utils.defaultWorkGroupSize)
59+
<| positions
60+
<| bitmap
61+
<| complementedIndices
62+
63+
return Mask1D(complementedIndices, size, not mask.IsComplemented)
5864
}

src/GraphBLAS-sharp/IO/MtxReader.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ open GraphBLAS.FSharp
55
open System
66

77
type MtxReader(pathToFile: string) =
8-
let mutable object = Matrix
8+
let mutable object = MtxMatrix
99
let mutable format = Coordinate
1010
let mutable field = Real
1111
let mutable symmetry = General
@@ -46,7 +46,7 @@ type MtxReader(pathToFile: string) =
4646
|}
4747

4848
member this.ReadMatrixReal() : Matrix<float32> =
49-
if object <> Matrix then failwith "Object is not matrix"
49+
if object <> MtxMatrix then failwith "Object is not matrix"
5050
if field <> Real then failwith "Field is not real"
5151

5252
use streamReader = new StreamReader(pathToFile)
@@ -103,7 +103,7 @@ type MtxReader(pathToFile: string) =
103103
| Array -> failwith "Unsupported matrix format"
104104

105105
member this.ReadMatrixBoolean(converter: string -> bool) : Matrix<bool> =
106-
if object <> Matrix then failwith "Object is not matrix"
106+
if object <> MtxMatrix then failwith "Object is not matrix"
107107
// if field <> f then failwith "Field is not mmm"
108108

109109
use streamReader = new StreamReader(pathToFile)
@@ -160,12 +160,12 @@ type MtxReader(pathToFile: string) =
160160
| Array -> failwith "Unsupported matrix format"
161161

162162
and MtxObject =
163-
| Matrix
164-
| Vector
163+
| MtxMatrix
164+
| MtxVector
165165
static member FromString str =
166166
match str with
167-
| "matrix" -> Matrix
168-
| "vector" -> Vector
167+
| "matrix" -> MtxMatrix
168+
| "vector" -> MtxVector
169169
| _ -> failwithf "Unsupported mtx object %s" str
170170

171171
and MtxFormat =

src/GraphBLAS-sharp/Operations/Vector.fs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ module Vector =
101101
}
102102
|> EvalGB.fromCl
103103

104+
let synchronizeAndReturn (vector: Vector<'a>) : GraphblasEvaluation<Vector<'a>> =
105+
match vector with
106+
| VectorCOO vector ->
107+
opencl {
108+
let! _ = if vector.Indices.Length = 0 then opencl { return [||] } else ToHost vector.Indices
109+
let! _ = if vector.Values.Length = 0 then opencl { return [||] } else ToHost vector.Values
110+
return VectorCOO vector
111+
}
112+
|> EvalGB.fromCl
113+
104114
(*
105115
assignment, extraction and filling
106116
*)
@@ -196,7 +206,6 @@ module VectorTuples =
196206
opencl {
197207
let! _ = if vectorTuples.Indices.Length = 0 then opencl { return [||] } else ToHost vectorTuples.Indices
198208
let! _ = if vectorTuples.Values.Length = 0 then opencl { return [||] } else ToHost vectorTuples.Values
199-
200209
return ()
201210
}
202211
|> EvalGB.fromCl
@@ -205,7 +214,6 @@ module VectorTuples =
205214
opencl {
206215
let! _ = if vectorTuples.Indices.Length = 0 then opencl { return [||] } else ToHost vectorTuples.Indices
207216
let! _ = if vectorTuples.Values.Length = 0 then opencl { return [||] } else ToHost vectorTuples.Values
208-
209217
return vectorTuples
210218
}
211219
|> EvalGB.fromCl
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
module Algo.Bfs
22

3-
()
3+
open Expecto
4+
open Expecto.Logging
5+
open Expecto.Logging.Message
6+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
7+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
8+
open GraphBLAS.FSharp.Backend.Common
9+
open GraphBLAS.FSharp
10+
open GraphBLAS.FSharp.IO
11+
open GraphBLAS.FSharp.Algorithms
12+
13+
let logger = Log.create "Bfs.Tests"
14+
15+
let testCases = [
16+
ftestCase "" <| fun () ->
17+
let expected =
18+
graphblas {
19+
let! matrix =
20+
MtxReader("arc130.mtx").ReadMatrixBoolean(fun _ -> true)
21+
|> Matrix.switch CSR
22+
23+
return!
24+
BFS.levelSingleSource matrix 0
25+
>>= Vector.synchronizeAndReturn
26+
}
27+
|> EvalGB.withClContext (OpenCLEvaluationContext())
28+
|> EvalGB.runSync
29+
30+
Expect.isTrue true ""
31+
]
32+
33+
let tests =
34+
testCases
35+
|> testList "Bfs tests"

tests/GraphBLAS-sharp.Tests/Program.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let allTests =
1616
Matrix.GetTuples.tests
1717
Matrix.Mxv.tests
1818
Matrix.Transpose.tests
19+
Algo.Bfs.tests
1920
]
2021
|> testSequenced
2122

0 commit comments

Comments
 (0)