Skip to content

Commit beee153

Browse files
committed
Add tests for checking coo eWiseAdd on int, float, bool
1 parent 4ba1e28 commit beee153

22 files changed

Lines changed: 365 additions & 261 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksBFS.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type BFSBenchmark4CSRMatrix() =
2020

2121
[<GlobalSetup>]
2222
member this.BuildMatrix() =
23-
matrix <- Matrix.fromFile this.PathToGraph CSR
23+
matrix <- CSRMatrix(this.PathToGraph)
2424
source <- random.Next matrix.RowCount
2525

2626
[<Benchmark>]

benchmarks/GraphBLAS-sharp.Benchmarks/Utils.fs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,13 @@ module Utils =
106106
let cols = Array.zeroCreate data.Length
107107
let values = Array.zeroCreate data.Length
108108

109-
Array.Parallel.iteri
110-
(fun i struct(packedIndex, value) ->
111-
let (rowIdx, columnIdx) = unpack packedIndex
112-
// in mtx indecies start at 1
113-
rows.[i] <- rowIdx - 1
114-
cols.[i] <- columnIdx - 1
115-
values.[i] <- value
116-
) data
109+
Array.Parallel.iteri (fun i struct(packedIndex, value) ->
110+
let (rowIdx, columnIdx) = unpack packedIndex
111+
// in mtx indecies start at 1
112+
rows.[i] <- rowIdx - 1
113+
cols.[i] <- columnIdx - 1
114+
values.[i] <- value
115+
) data
117116

118117
{
119118
Rows = rows
@@ -135,13 +134,12 @@ module Utils =
135134
let cols = Array.zeroCreate data.Length
136135
let values = Array.zeroCreate data.Length
137136

138-
Array.Parallel.iteri
139-
(fun i struct(packedIndex, value) ->
140-
let (rowIdx, columnIdx) = unpack packedIndex
141-
rows.[i] <- rowIdx
142-
cols.[i] <- columnIdx
143-
values.[i] <- value
144-
) data
137+
Array.Parallel.iteri (fun i struct(packedIndex, value) ->
138+
let (rowIdx, columnIdx) = unpack packedIndex
139+
rows.[i] <- rowIdx
140+
cols.[i] <- columnIdx
141+
values.[i] <- value
142+
) data
145143

146144
{
147145
Rows = rows

src/GraphBLAS-sharp/Abstracts.fs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
namespace GraphBLAS.FSharp
22

33
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
4+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
5+
6+
type MatrixTuples<'a when 'a : struct and 'a : equality> = {
7+
RowIndices: int[]
8+
ColumnIndices: int[]
9+
Values: 'a[]
10+
}
11+
with
12+
member this.ToHost() = opencl {
13+
let! rows = ToHost this.RowIndices
14+
let! cols = ToHost this.ColumnIndices
15+
let! vals = ToHost this.Values
16+
17+
return {
18+
RowIndices = rows
19+
ColumnIndices = cols
20+
Values = vals
21+
}
22+
}
423

524
[<AbstractClass>]
625
type Matrix<'a when 'a : struct and 'a : equality>(nrow: int, ncol: int) =
@@ -13,7 +32,7 @@ type Matrix<'a when 'a : struct and 'a : equality>(nrow: int, ncol: int) =
1332
abstract Copy: unit -> OpenCLEvaluation<Matrix<'a>>
1433
abstract Resize: int -> int -> OpenCLEvaluation<Matrix<'a>>
1534
abstract GetNNZ: unit -> OpenCLEvaluation<int>
16-
abstract GetTuples: unit -> OpenCLEvaluation<{| Rows: int[]; Columns: int[]; Values: 'a[] |}>
35+
abstract GetTuples: unit -> OpenCLEvaluation<MatrixTuples<'a>>
1736
abstract GetMask: ?isComplemented: bool -> OpenCLEvaluation<Mask2D option>
1837
abstract ToHost: unit -> OpenCLEvaluation<Matrix<'a>>
1938

@@ -41,11 +60,6 @@ type Matrix<'a when 'a : struct and 'a : equality>(nrow: int, ncol: int) =
4160
abstract Transpose: unit -> OpenCLEvaluation<Matrix<'a>>
4261
abstract Kronecker: Matrix<'a> -> Mask2D option -> Semiring<'a> -> OpenCLEvaluation<Matrix<'a>>
4362

44-
static member inline (+) (x: Matrix<'a>, y: Matrix<'a>) = x.EWiseAdd y
45-
static member inline (*) (x: Matrix<'a>, y: Matrix<'a>) = x.EWiseMult y
46-
static member inline (@.) (x: Matrix<'a>, y: Matrix<'a>) = x.Mxm y
47-
static member inline (@.) (x: Matrix<'a>, y: Vector<'a>) = x.Mxv y
48-
4963
and [<AbstractClass>] Vector<'a when 'a : struct and 'a : equality>(size: int) =
5064
abstract Size: int
5165
default this.Size = size
@@ -71,10 +85,6 @@ and [<AbstractClass>] Vector<'a when 'a : struct and 'a : equality>(size: int) =
7185
abstract Prune: Mask1D option -> UnaryOp<'a, bool> -> OpenCLEvaluation<Vector<'a>>
7286
abstract Reduce: Monoid<'a> -> OpenCLEvaluation<Scalar<'a>>
7387

74-
static member inline (+) (x: Vector<'a>, y: Vector<'a>) = x.EWiseAdd y
75-
static member inline (*) (x: Vector<'a>, y: Vector<'a>) = x.EWiseMult y
76-
static member inline (@.) (x: Vector<'a>, y: Matrix<'a>) = x.Vxm y
77-
7888
and Mask1D(indices: int[], size: int, isComplemented: bool) =
7989
member this.Indices = indices
8090
member this.Size = size

src/GraphBLAS-sharp/Algorithms/BFS.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module BFS =
1919
let! frontierMask = frontier.GetMask()
2020
do! levels.Assign(frontierMask, Scalar currentLevel)
2121
let! levelsComplemented = levels.GetMask(isComplemented = true)
22-
let! frontier = (frontier @. matrix) levelsComplemented BooleanSemiring.anyAll
22+
let! frontier = frontier.Vxm matrix levelsComplemented BooleanSemiring.anyAll
2323
currentLevel <- currentLevel + 1
2424

2525
return levels

src/GraphBLAS-sharp/Algorithms/SSSP.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module SSSP =
1313

1414
opencl {
1515
for _ in 1 .. vertexCount - 1 do
16-
let! step = (distance @. matrix) None FloatSemiring.minAdd
16+
let! step = distance.Vxm matrix None FloatSemiring.minAdd
1717
do! distance.Assign(None, step)
1818

1919
return distance

src/GraphBLAS-sharp/Algorithms/TriangleCounting.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ module TriangleCounting =
1717
let! convertedMatrix = lowerTriangular.Apply None (UnaryOp <@ bool2int @>)
1818
let! convertedTransposed = convertedMatrix.Transpose()
1919
let! lowerTriangularMask = lowerTriangular.GetMask()
20-
let! result = (convertedMatrix @. convertedTransposed) lowerTriangularMask IntegerSemiring.addMult
20+
let! result = convertedMatrix.Mxm convertedTransposed lowerTriangularMask IntegerSemiring.addMult
2121
return! result.Reduce IntegerMonoid.add
2222
}

src/GraphBLAS-sharp/GlobalContext.fs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/GraphBLAS-sharp/GraphBLAS-sharp.fsproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@
1414
<Compile Include="Operators.fs" />
1515
<Compile Include="Monoid.fs" />
1616
<Compile Include="Semiring.fs" />
17-
<Compile Include="GlobalContext.fs" />
1817
<Compile Include="Toolbox.fs" />
1918
<Compile Include="Scalar.fs" />
2019
<Compile Include="Abstracts.fs" />
2120
<Compile Include="Implementations.fs" />
2221
<Compile Include="Matrix.fs" />
2322
<Compile Include="Vector.fs" />
23+
<Compile Include="Predefined/Monoids/Any.fs" />
24+
<Compile Include="Predefined/Monoids/Add.fs" />
25+
<Compile Include="Predefined/Monoids/Min.fs" />
26+
<Compile Include="Predefined/Semirings/AnyAll.fs" />
27+
<Compile Include="Predefined/Semirings/AddMult.fs" />
28+
<Compile Include="Predefined/Semirings/MinAdd.fs" />
2429
<Compile Include="Predefined/Float.fs" />
2530
<Compile Include="Predefined/Float32.fs" />
2631
<Compile Include="Predefined/Integer.fs" />

src/GraphBLAS-sharp/Implementations.fs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace GraphBLAS.FSharp
33
open Brahma.OpenCL
44
open Brahma.FSharp.OpenCL.Core
55
open Brahma.FSharp.OpenCL.Extensions
6-
open GlobalContext
76
open Helpers
87
open FSharp.Quotations.Evaluator
98
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
@@ -85,6 +84,7 @@ type CSRMatrix<'a when 'a : struct and 'a : equality>(csrTuples: CSRFormat<'a>)
8584

8685
// Not Implemented
8786
new(rows: int[], columns: int[], values: 'a[]) = CSRMatrix(CSRFormat.CreateEmpty())
87+
new(pathToMatrix: string) = CSRMatrix(CSRFormat.CreateEmpty())
8888

8989
member this.Values = csrTuples.Values
9090
member this.Columns = csrTuples.ColumnIndices
@@ -128,6 +128,27 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
128128
inherit Matrix<'a>(rowCount, columnCount)
129129

130130
let mutable rows, columns, values = rows, columns, values
131+
132+
new(array: 'a[,], isZero: 'a -> bool) =
133+
let (rows, cols, vals) =
134+
array
135+
|> Seq.cast<'a>
136+
|> Seq.mapi (fun idx v -> (idx / Array2D.length2 array, idx % Array2D.length2 array, v))
137+
|> Seq.filter (fun (i, j, v) -> not <| isZero v)
138+
|> Array.ofSeq
139+
|> Array.unzip3
140+
141+
COOMatrix(Array2D.length1 array, Array2D.length2 array, rows, cols, vals)
142+
143+
override this.ToString() =
144+
[
145+
sprintf "COO Matrix %ix%i \n" rowCount columnCount
146+
sprintf "RowIndices: %A \n" rows
147+
sprintf "ColumnIndices: %A \n" columns
148+
sprintf "Values: %A \n" values
149+
]
150+
|> String.concat ""
151+
131152
member this.Rows with get() = rows
132153
member this.Columns with get() = columns
133154
member this.Values with get() = values
@@ -138,23 +159,25 @@ and COOMatrix<'a when 'a : struct and 'a : equality>(rowCount: int, columnCount:
138159
override this.Resize a b = failwith "Not Implemented"
139160
override this.GetNNZ () = failwith "Not Implemented"
140161

141-
override this.GetTuples () =
142-
opencl {
143-
return {| Rows = this.Rows; Columns = this.Columns; Values = this.Values |}
162+
override this.GetTuples() = opencl {
163+
return {
164+
RowIndices = this.Rows
165+
ColumnIndices = this.Columns
166+
Values = this.Values
144167
}
168+
}
145169

146170
override this.GetMask(?isComplemented: bool) =
147171
let isComplemented = defaultArg isComplemented false
148172
failwith "Not Implemented"
149173

150-
override this.ToHost () =
151-
opencl {
152-
let! _ = ToHost this.Rows
153-
let! _ = ToHost this.Columns
154-
let! _ = ToHost this.Values
174+
override this.ToHost() = opencl {
175+
let! _ = ToHost this.Rows
176+
let! _ = ToHost this.Columns
177+
let! _ = ToHost this.Values
155178

156-
return upcast this
157-
}
179+
return upcast this
180+
}
158181

159182
override this.Extract (mask: Mask2D option) : OpenCLEvaluation<Matrix<'a>> = failwith "Not Implemented"
160183
override this.Extract (colMask: Mask1D option * int) : OpenCLEvaluation<Vector<'a>> = failwith "Not Implemented"

src/GraphBLAS-sharp/Matrix.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
namespace GraphBLAS.FSharp
22

3+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
4+
35
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
46
module Matrix =
57
let build (rowCount: int) (columnCount: int) (rows: int[]) (columns: int[]) (values: 'a[]) : Matrix<'a> =
68
failwith "Not Implemented yet"
79

8-
let ofArray2D (array: 'a[,]) (checkZero: 'a -> bool) : Matrix<'a> =
10+
let ofArray2D (array: 'a[,]) (isZero: 'a -> bool) : Matrix<'a> =
911
failwith "Not Implemented yet"
1012

1113
let fromFile (pathToMatrix: string) : Matrix<'a> =

0 commit comments

Comments
 (0)