Skip to content

Commit 77f4be6

Browse files
authored
Merge pull request #15 from dpanfilyonok/ewa-generic-tests
Added EWiseAdd tests for COO matrix
2 parents 1a6a787 + 157f510 commit 77f4be6

30 files changed

Lines changed: 822 additions & 651 deletions

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksBFS.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ open BenchmarkDotNet.Configs
77
open BenchmarkDotNet.Columns
88
open System.IO
99
open System
10-
open MatrixBackend
1110

1211
[<SimpleJob(targetCount=10)>]
1312
type BFSBenchmark4CSRMatrix() =
@@ -21,7 +20,7 @@ type BFSBenchmark4CSRMatrix() =
2120

2221
[<GlobalSetup>]
2322
member this.BuildMatrix() =
24-
matrix <- Matrix.Build<bool>(this.PathToGraph, CSR)
23+
matrix <- CSRMatrix(this.PathToGraph)
2524
source <- random.Next matrix.RowCount
2625

2726
[<Benchmark>]

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ open System.IO
44
open System.Text.RegularExpressions
55
open GraphBLAS.FSharp
66
open GraphBLAS.FSharp.Predefined
7-
open GraphBLAS.FSharp.MatrixBackend
87
open BenchmarkDotNet.Attributes
98
open BenchmarkDotNet.Configs
109
open BenchmarkDotNet.Columns
@@ -131,14 +130,13 @@ type EWiseAddBenchmarks4Float32() =
131130
Array.blit this.FirstMatrix.Values 0 leftVals 0 this.FirstMatrix.Values.Length
132131

133132
leftCOO <-
134-
Matrix.Build<float32>(
133+
COOMatrix(
135134
this.FirstMatrix.RowCount,
136135
this.FirstMatrix.ColumnCount,
137136
leftRows,
138137
leftCols,
139-
leftVals,
140-
COO
141-
)
138+
leftVals
139+
) :> Matrix<float32>
142140

143141
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
144142
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
@@ -148,14 +146,13 @@ type EWiseAddBenchmarks4Float32() =
148146
Array.blit this.SecondMatrix.Values 0 rightVals 0 this.SecondMatrix.Values.Length
149147

150148
rightCOO <-
151-
Matrix.Build<float32>(
149+
COOMatrix(
152150
this.SecondMatrix.RowCount,
153151
this.SecondMatrix.ColumnCount,
154152
rightRows,
155153
rightCols,
156-
rightVals,
157-
COO
158-
)
154+
rightVals
155+
) :> Matrix<float32>
159156

160157
[<Benchmark>]
161158
member this.EWiseAdditionCOOFloat32() =
@@ -204,14 +201,13 @@ type EWiseAddBenchmarks4Bool() =
204201
Array.blit this.FirstMatrix.Columns 0 leftCols 0 this.FirstMatrix.Columns.Length
205202

206203
leftCOO <-
207-
Matrix.Build<bool>(
204+
COOMatrix(
208205
this.FirstMatrix.RowCount,
209206
this.FirstMatrix.ColumnCount,
210207
leftRows,
211208
leftCols,
212-
leftVals,
213-
COO
214-
)
209+
leftVals
210+
) :> Matrix<bool>
215211

216212
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
217213
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
@@ -220,14 +216,13 @@ type EWiseAddBenchmarks4Bool() =
220216
Array.blit this.SecondMatrix.Columns 0 rightCols 0 this.SecondMatrix.Columns.Length
221217

222218
rightCOO <-
223-
Matrix.Build<bool>(
219+
COOMatrix(
224220
this.SecondMatrix.RowCount,
225221
this.SecondMatrix.ColumnCount,
226222
rightRows,
227223
rightCols,
228-
rightVals,
229-
COO
230-
)
224+
rightVals
225+
) :> Matrix<bool>
231226

232227
[<Benchmark>]
233228
member this.EWiseAdditionCOOBool() =

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

paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nuget Brahma.FSharp.OpenCL.WorkflowBuilder 2.0.0-alpha5
2222
nuget BenchmarkDotNet
2323
nuget MathNet.Numerics.FSharp
2424
nuget MathNet.Numerics.MKL.Win-x64 2.5.0
25+
nuget TypeShape 9.0.0
2526

2627
// [ FAKE GROUP ]
2728
group Build

paket.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,8 @@ NUGET
920920
System.Threading (>= 4.3) - restriction: || (&& (< monoandroid) (< monotouch) (< net45) (>= netstandard1.3) (< win8) (< wpa81) (< xamarinios) (< xamarinmac) (< xamarintvos) (< xamarinwatchos)) (>= netcoreapp5.0)
921921
System.Xml.ReaderWriter (>= 4.3) - restriction: || (&& (< monoandroid) (< monotouch) (< net45) (>= netstandard1.3) (< win8) (< wpa81) (< xamarinios) (< xamarinmac) (< xamarintvos) (< xamarinwatchos)) (&& (< monoandroid) (< net45) (>= netstandard1.0) (< netstandard1.3) (< win8) (< wp8) (< wpa81)) (>= netcoreapp5.0)
922922
System.Xml.XmlDocument (>= 4.3) - restriction: || (&& (< monoandroid) (< monotouch) (< net45) (>= netstandard1.3) (< win8) (< wpa81) (< xamarinios) (< xamarinmac) (< xamarintvos) (< xamarinwatchos)) (>= netcoreapp5.0)
923+
TypeShape (9.0)
924+
FSharp.Core (>= 4.3.2) - restriction: >= netstandard2.0
923925
YC.Brahma (2.0.0-alpha5) - restriction: || (>= net461) (>= netstandard2.1)
924926
YC.Brahma.OpenCL (2.0.0-alpha5) - restriction: || (>= net461) (>= netstandard2.1)
925927
YC.Brahma (>= 2.0.0-alpha5) - restriction: || (>= net461) (>= netstandard2.1)

src/GraphBLAS-sharp/Abstracts.fs

Lines changed: 42 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
@@ -86,3 +96,25 @@ and Mask2D(rowIndices: int[], columnIndices: int[], rowCount: int, columnCount:
8696
member this.RowCount = rowCount
8797
member this.ColumnCount = columnCount
8898
member this.IsComplemented = isComplemented
99+
100+
type COOFormat<'a> = {
101+
RowCount: int
102+
ColumnCount: int
103+
Rows: int[]
104+
Columns: int[]
105+
Values: 'a[]
106+
}
107+
108+
type CSRFormat<'a> = {
109+
ColumnCount: int
110+
RowPointers: int[]
111+
ColumnIndices: int[]
112+
Values: 'a[]
113+
}
114+
with
115+
static member CreateEmpty<'a>() = {
116+
RowPointers = Array.zeroCreate<int> 0
117+
ColumnIndices = Array.zeroCreate<int> 0
118+
Values = Array.zeroCreate<'a> 0
119+
ColumnCount = 0
120+
}

src/GraphBLAS-sharp/Algorithms/BFS.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
1010
module BFS =
1111
let levelBFS (matrix: Matrix<bool>) (source: int) : OpenCLEvaluation<Vector<int>> =
1212
let vertexCount = matrix.RowCount
13-
let levels = Vector.Dense(Array.zeroCreate vertexCount, IntegerMonoid.add)
14-
let frontier = Vector.Sparse(vertexCount, [source, true])
13+
let levels = Vector.ofArray <| Array.zeroCreate vertexCount <| (=) 0
14+
let frontier = Vector.ofTuples vertexCount [source, true]
1515

1616
opencl {
1717
let mutable currentLevel = 1
1818
while currentLevel < vertexCount do
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
99
module SSSP =
1010
let SSSP (matrix: Matrix<float>) (source: int) : OpenCLEvaluation<Vector<float>> =
1111
let vertexCount = matrix.RowCount
12-
let distance = Vector.Sparse(vertexCount, [source, 0.])
12+
let distance = Vector.ofTuples vertexCount [source, 0.]
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/Toolbox.fs renamed to src/GraphBLAS-sharp/Backend/Common/Scan.fs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
namespace GraphBLAS.FSharp
1+
namespace GraphBLAS.FSharp.Backend.Common
22

33
open Brahma.OpenCL
4-
open Brahma.FSharp.OpenCL.Core
5-
open Brahma.FSharp.OpenCL.Extensions
6-
open GlobalContext
7-
open Helpers
8-
open FSharp.Quotations.Evaluator
94
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
105
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
6+
open Utils
117

12-
module internal Toolbox =
13-
14-
let internal workGroupSize = 128
15-
let internal workSize n =
16-
let m = n - 1
17-
m - m % workGroupSize + workGroupSize
18-
19-
let rec internal prefixSum
20-
(inputArray: int[]) =
21-
8+
// functions in mudule could be named run\get\if\it\t
9+
// like mentioned here https://www.reddit.com/r/fsharp/comments/5kvsyk/modules_or_namespaces/dbt0zf7?utm_source=share&utm_medium=web2x&context=3
10+
module internal Scan =
11+
let rec v1 (inputArray: int[]) =
2212
let outputArray = Array.zeroCreate inputArray.Length
2313

2414
if inputArray.Length = 1 then
@@ -89,7 +79,7 @@ module internal Toolbox =
8979

9080
opencl {
9181
do! fillIntermediateArray
92-
let! auxiliaryPrefixSumArray = prefixSum intermediateArray
82+
let! auxiliaryPrefixSumArray = v1 intermediateArray
9383

9484
let binder kernelP =
9585
let ndRange = _1D(workSize inputArray.Length, workGroupSize)
@@ -103,9 +93,7 @@ module internal Toolbox =
10393
return outputArray
10494
}
10595

106-
let internal prefixSum2
107-
(inputArray: int[]) =
108-
96+
let v2 (inputArray: int[]) =
10997
let firstIntermediateArray = Array.copy inputArray
11098
let secondIntermediateArray = Array.copy inputArray
11199
let outputArrayLength = firstIntermediateArray.Length
@@ -135,7 +123,6 @@ module internal Toolbox =
135123
let mutable arrays = firstIntermediateArray, secondIntermediateArray
136124

137125
opencl {
138-
139126
let mutable offset = 1
140127
while offset < outputArrayLength do
141128
arrays <- swap arrays

0 commit comments

Comments
 (0)