Skip to content

Commit c342ff9

Browse files
committed
Add generic gen for matrices; remove ability to choose matrix backend on build
1 parent ec125c0 commit c342ff9

13 files changed

Lines changed: 244 additions & 207 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ type EWiseAddBenchmarks4Float32() =
130130
Array.blit this.FirstMatrix.Values 0 leftVals 0 this.FirstMatrix.Values.Length
131131

132132
leftCOO <-
133-
Matrix.build
134-
this.FirstMatrix.RowCount
135-
this.FirstMatrix.ColumnCount
136-
leftRows
137-
leftCols
133+
COOMatrix(
134+
this.FirstMatrix.RowCount,
135+
this.FirstMatrix.ColumnCount,
136+
leftRows,
137+
leftCols,
138138
leftVals
139-
COO
139+
) :> Matrix<float32>
140140

141141
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
142142
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
@@ -146,13 +146,13 @@ type EWiseAddBenchmarks4Float32() =
146146
Array.blit this.SecondMatrix.Values 0 rightVals 0 this.SecondMatrix.Values.Length
147147

148148
rightCOO <-
149-
Matrix.build
150-
this.SecondMatrix.RowCount
151-
this.SecondMatrix.ColumnCount
152-
rightRows
153-
rightCols
149+
COOMatrix(
150+
this.SecondMatrix.RowCount,
151+
this.SecondMatrix.ColumnCount,
152+
rightRows,
153+
rightCols,
154154
rightVals
155-
COO
155+
) :> Matrix<float32>
156156

157157
[<Benchmark>]
158158
member this.EWiseAdditionCOOFloat32() =
@@ -201,13 +201,13 @@ type EWiseAddBenchmarks4Bool() =
201201
Array.blit this.FirstMatrix.Columns 0 leftCols 0 this.FirstMatrix.Columns.Length
202202

203203
leftCOO <-
204-
Matrix.build
205-
this.FirstMatrix.RowCount
206-
this.FirstMatrix.ColumnCount
207-
leftRows
208-
leftCols
204+
COOMatrix(
205+
this.FirstMatrix.RowCount,
206+
this.FirstMatrix.ColumnCount,
207+
leftRows,
208+
leftCols,
209209
leftVals
210-
COO
210+
) :> Matrix<bool>
211211

212212
let rightRows = Array.zeroCreate<int> this.SecondMatrix.Rows.Length
213213
let rightCols = Array.zeroCreate<int> this.SecondMatrix.Columns.Length
@@ -216,13 +216,13 @@ type EWiseAddBenchmarks4Bool() =
216216
Array.blit this.SecondMatrix.Columns 0 rightCols 0 this.SecondMatrix.Columns.Length
217217

218218
rightCOO <-
219-
Matrix.build
220-
this.SecondMatrix.RowCount
221-
this.SecondMatrix.ColumnCount
222-
rightRows
223-
rightCols
219+
COOMatrix(
220+
this.SecondMatrix.RowCount,
221+
this.SecondMatrix.ColumnCount,
222+
rightRows,
223+
rightCols,
224224
rightVals
225-
COO
225+
) :> Matrix<bool>
226226

227227
[<Benchmark>]
228228
member this.EWiseAdditionCOOBool() =

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/Matrix.fs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@ namespace GraphBLAS.FSharp
22

33
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
44
module Matrix =
5-
let toSeq (matrix: Matrix<'a>) = failwith "Not Implemented"
6-
7-
let build (rowCount: int) (columnCount: int) (rows: int[]) (columns: int[]) (values: 'a[]) (matrixType: MatrixBackendFormat) : Matrix<'a> =
8-
match matrixType with
9-
| CSR -> upcast CSRMatrix(rows, columns, values)
10-
| COO -> upcast COOMatrix(rowCount, columnCount, rows, columns, values)
11-
| _ -> failwith "Not Implemented"
5+
let build (rowCount: int) (columnCount: int) (rows: int[]) (columns: int[]) (values: 'a[]) : Matrix<'a> =
6+
failwith "Not Implemented yet"
127

13-
let ofArray2D (array: 'a[,]) (zero: 'a) (matrixType: MatrixBackendFormat) : Matrix<'a> =
8+
let ofArray2D (array: 'a[,]) (checkZero: 'a -> bool) : Matrix<'a> =
149
failwith "Not Implemented yet"
1510

16-
let fromFile (pathToMatrix: string) (matrixType: MatrixBackendFormat) : Matrix<'a> =
11+
let fromFile (pathToMatrix: string) : Matrix<'a> =
1712
failwith "Not Implemented yet"
1813

19-
let init (rowCount: int) (columnCount: int) (initializer: int -> int -> 'a) (matrixType: MatrixBackendFormat) : Matrix<'a> =
14+
let init (rowCount: int) (columnCount: int) (initializer: int -> int -> 'a) : Matrix<'a> =
2015
failwith "Not Implemented yet"
2116

22-
let zeroCreate (rowCount: int) (columnCount: int) (matrixType: MatrixBackendFormat) : Matrix<'a> =
17+
let zeroCreate (rowCount: int) (columnCount: int) : Matrix<'a> =
2318
failwith "Not Implemented yet"

src/GraphBLAS-sharp/Toolbox.fs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
namespace GraphBLAS.FSharp
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
10-
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
115

126
module internal Toolbox =
13-
14-
let internal workGroupSize = 128
15-
let internal workSize n =
7+
let workGroupSize = 128
8+
let workSize n =
169
let m = n - 1
1710
m - m % workGroupSize + workGroupSize
1811

19-
let rec internal prefixSum
20-
(inputArray: int[]) =
21-
12+
let rec prefixSum (inputArray: int[]) =
2213
let outputArray = Array.zeroCreate inputArray.Length
2314

2415
if inputArray.Length = 1 then
@@ -103,9 +94,7 @@ module internal Toolbox =
10394
return outputArray
10495
}
10596

106-
let internal prefixSum2
107-
(inputArray: int[]) =
108-
97+
let prefixSum2 (inputArray: int[]) =
10998
let firstIntermediateArray = Array.copy inputArray
11099
let secondIntermediateArray = Array.copy inputArray
111100
let outputArrayLength = firstIntermediateArray.Length
@@ -135,7 +124,6 @@ module internal Toolbox =
135124
let mutable arrays = firstIntermediateArray, secondIntermediateArray
136125

137126
opencl {
138-
139127
let mutable offset = 1
140128
while offset < outputArrayLength do
141129
arrays <- swap arrays

src/GraphBLAS-sharp/Vector.fs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ module VectorExtensions =
2626

2727
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
2828
module Vector =
29-
let toSeq (vector: Vector<'a>) = failwith "Not Implemented"
30-
3129
let ofArray (denseVector: 'a[]) (zero: 'a) (vectorFormat : VectorBackendFormat) : Vector<'a> =
3230
failwith "Not Implemented"

tests/GraphBLAS-sharp.Tests/Common.fs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace GraphBLAS.FSharp.Tests
22

3+
open FsCheck
34
open System
45

56
type VectorType =
@@ -15,6 +16,45 @@ type MaskType =
1516
| Complemented = 1
1617
| None = 2
1718

19+
type OperationCase = {
20+
VectorCase: VectorType
21+
MatrixCase: MatrixType
22+
MaskCase: MaskType
23+
}
24+
25+
module Generators =
26+
let dimension2DGenerator =
27+
Gen.sized <| fun size ->
28+
Gen.choose (0, size |> float |> sqrt |> int)
29+
|> Gen.two
30+
31+
let dimension3DGenerator =
32+
Gen.sized <| fun size ->
33+
Gen.choose (0, size |> float |> sqrt |> int)
34+
|> Gen.three
35+
36+
// non-empty and nonzeroes exists
37+
let pairOfSparseMatricesGenerator (valuesGenerator: Gen<'a>) (zero: 'a) (checkZero: 'a -> bool) =
38+
Gen.sized <| fun size ->
39+
let sparseGenerator =
40+
Gen.oneof [
41+
valuesGenerator
42+
Gen.constant zero
43+
]
44+
45+
gen {
46+
let! (rowsA, colsA, colsB) = dimension3DGenerator
47+
let! matrixA = valuesGenerator |> Gen.array2DOfDim (rowsA, colsA)
48+
let! matrixB = valuesGenerator |> Gen.array2DOfDim (colsA, colsB)
49+
return (matrixA, matrixB)
50+
}
51+
|> Gen.filter (fun (matrixA, matrixB) -> matrixA.Length <> 0 && matrixB.Length <> 0)
52+
|> Gen.filter
53+
(fun (matrixA, matrixB) ->
54+
matrixA |> Seq.cast<'a> |> Seq.exists (not << checkZero) &&
55+
matrixB |> Seq.cast<'a> |> Seq.exists (not << checkZero)
56+
)
57+
1858
module Utils =
1959
let rec cartesian lstlst =
2060
match lstlst with

tests/GraphBLAS-sharp.Tests/GraphBLAS-sharp.Tests.fsproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
<Compile Include="AssemblyInfo.fs" />
1111
<Compile Include="Common.fs" />
1212
<Compile Include="OperationsTests/VxmTests.fs" />
13-
<Compile Include="Main.fs" />
13+
<Compile Include="OperationsTests/EWiseAddTests.fs" />
14+
<Compile Include="Program.fs" />
1415
</ItemGroup>
1516
<Import Project="..\..\.paket\Paket.Restore.targets" />
16-
</Project>
17+
</Project>

tests/GraphBLAS-sharp.Tests/Main.fs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module EWiseAddTests
2+
3+
open Expecto
4+
open FsCheck
5+
open GraphBLAS.FSharp
6+
open MathNet.Numerics
7+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
8+
open GlobalContext
9+
open TypeShape.Core
10+
open GraphBLAS.FSharp.Tests
11+
12+
type PrimitiveType =
13+
| Float32
14+
| Bool
15+
16+
type PairOfSparseMatrices =
17+
static member Float32Type() =
18+
Arb.fromGen <| Generators.pairOfSparseMatricesGenerator
19+
Arb.generate<float32>
20+
0f
21+
((=) 0f)
22+
23+
static member BoolType() =
24+
Arb.fromGen <| Generators.pairOfSparseMatricesGenerator
25+
Arb.generate<bool>
26+
false
27+
((=) false)
28+
29+
// type IPredicate =
30+
// abstract Invoke : 'a -> bool
31+
32+
// let equals (a: 'a) (b: 'a) = true
33+
34+
// let reflexivity = {
35+
// new IPredicate with
36+
// member this.Invoke item = equals item item
37+
// }
38+
39+
// let conf = {
40+
// Config.Verbose with
41+
// Arbitrary = [typeof<Arbi>]
42+
// }
43+
44+
// let meaning randomType =
45+
// match randomType with
46+
// | Float32 -> typeof<float32>
47+
// | Bool -> typeof<bool>
48+
49+
// let check (predicate: IPredicate) (randomType: PrimitiveTypes) =
50+
// let systemType = meaning randomType
51+
// let shape = TypeShape.Create systemType
52+
// shape.Accept {
53+
// new ITypeVisitor<bool> with
54+
// member this.Visit<'a>() =
55+
// Check.One<'a -> bool>(conf, predicate.Invoke)
56+
// true
57+
// }
58+
59+
// // генерится тип
60+
// let checkGeneric (predicate: IPredicate) =
61+
// Check.Quick<PrimitiveTypes -> bool>(check predicate)

0 commit comments

Comments
 (0)