Skip to content

Commit b400b84

Browse files
committed
COO fixes + bench fixes
1 parent 6ac0624 commit b400b84

5 files changed

Lines changed: 145 additions & 107 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ open OpenCL.Net
1717
open Brahma.OpenCL
1818
open System.Text.RegularExpressions
1919

20-
type Config<'a>() =
20+
type Config() =
2121
inherit ManualConfig()
2222

2323
do
2424
base.AddColumn(
25-
MatrixShapeColumn<'a>("RowCount", fun matrix -> matrix.MatrixStructure.RowCount) :> IColumn,
26-
MatrixShapeColumn<'a>("ColumnCount", fun matrix -> matrix.MatrixStructure.ColumnCount) :> IColumn,
27-
MatrixShapeColumn<'a>("NNZ", fun matrix -> matrix.MatrixStructure.Values.Length) :> IColumn,
28-
TEPSColumn<'a>() :> IColumn,
25+
MatrixShapeColumn("RowCount", fun matrix -> matrix.RowCount) :> IColumn,
26+
MatrixShapeColumn("ColumnCount", fun matrix -> matrix.ColumnCount) :> IColumn,
27+
MatrixShapeColumn("NNZ", fun matrix ->
28+
match matrix.Format with
29+
| "coordinate" -> matrix.Size.[2]
30+
| "array" -> 0
31+
| _ -> failwith "Unsupported") :> IColumn,
32+
TEPSColumn() :> IColumn,
2933
StatisticColumn.Min,
3034
StatisticColumn.Max
3135
) |> ignore
@@ -36,19 +40,26 @@ type Config<'a>() =
3640
)
3741
) |> ignore
3842

39-
type Float32Config = Config<float32>
40-
type BoolConfig = Config<bool>
41-
42-
[<SimpleJob(RunStrategy.Monitoring, targetCount=2)>]
43+
[<IterationCount(1)>]
44+
[<WarmupCount(1)>]
45+
[<Config(typeof<Config>)>]
4346
type EWiseAddBenchmarks() =
4447
[<ParamsSource("AvaliableContexts")>]
4548
member val OclContext = Unchecked.defaultof<ClContext> with get, set
4649

50+
[<ParamsSource("InputMatricesProvider")>]
51+
member val InputMatrix = Unchecked.defaultof<MtxShape> with get, set
52+
4753
[<IterationCleanup>]
4854
member this.ClearBuffers() =
4955
let (ClContext context) = this.OclContext
5056
context.Provider.CloseAllBuffers()
5157

58+
[<GlobalCleanup>]
59+
member this.ClearContext() =
60+
let (ClContext context) = this.OclContext
61+
context.Provider.Dispose()
62+
5263
static member AvaliableContexts =
5364
let pathToConfig =
5465
Path.Combine [|
@@ -84,7 +95,6 @@ type EWiseAddBenchmarks() =
8495
OpenCLEvaluationContext(platformName, deviceType) |> ClContext
8596
)
8697

87-
[<Config(typeof<Float32Config>)>]
8898
type EWiseAddBenchmarks4Float32() =
8999
inherit EWiseAddBenchmarks()
90100

@@ -94,13 +104,25 @@ type EWiseAddBenchmarks4Float32() =
94104
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
95105
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
96106

97-
[<ParamsSource("InputMatricesProvider")>]
98-
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat<float32>> with get, set
99-
100107
[<GlobalSetup>]
101108
member this.FormInputData() =
102-
this.FirstMatrix <- this.InputMatrix.MatrixStructure
103-
this.SecondMatrix <- this.InputMatrix.MatrixStructure |> Utils.transposeCOO
109+
let mtxFormat = GraphReader.readMtx <| Utils.getFullPathToMatrix this.InputMatrix.Filename
110+
let cooMatrix =
111+
match mtxFormat.Shape.Format, mtxFormat.Shape.Field with
112+
| "coordinate", "real" -> Utils.makeCOO mtxFormat <| FromString float32
113+
| "coordinate", "integer" -> Utils.makeCOO mtxFormat <| FromString float32
114+
| "coordinate", "pattern" ->
115+
let rand = System.Random()
116+
let nextSingle (random: System.Random) =
117+
let buffer = Array.zeroCreate<byte> 4
118+
random.NextBytes buffer
119+
System.BitConverter.ToSingle(buffer, 0)
120+
121+
Utils.makeCOO mtxFormat <| FromUnit (fun () -> nextSingle rand)
122+
| _ -> failwith "Unsupported matrix format"
123+
124+
this.FirstMatrix <- cooMatrix
125+
this.SecondMatrix <- cooMatrix |> Utils.transposeCOO
104126

105127
[<IterationSetup>]
106128
member this.BuildCOO() =
@@ -157,32 +179,14 @@ type EWiseAddBenchmarks4Float32() =
157179
|> Seq.ofArray
158180
|> Seq.filter (fun line -> not <| line.StartsWith "!")
159181

160-
let matrixHandler (matrixFilename: string) =
161-
match Path.GetExtension matrixFilename with
162-
| ".mtx" ->
163-
let mtx = GraphReader.readMtx <| Utils.getFullPathToMatrix matrixFilename
164-
match mtx.Format, mtx.Field with
165-
| "coordinate", "real" -> Utils.makeCOO mtx <| FromString float32
166-
| "coordinate", "integer" -> Utils.makeCOO mtx <| FromString float32
167-
| "coordinate", "pattern" ->
168-
let rand = System.Random()
169-
let nextSingle (random: System.Random) =
170-
let buffer = Array.zeroCreate<byte> 4
171-
random.NextBytes buffer
172-
System.BitConverter.ToSingle(buffer, 0)
173-
174-
Utils.makeCOO mtx <| FromUnit (fun () -> nextSingle rand)
175-
| _ -> failwith "Unsupported matrix format"
176-
| _ -> failwith "Unsupported matrix format"
177-
178182
matricesFilenames
179-
|> Seq.map (fun filename ->
180-
{
181-
MatrixName = Path.GetFileNameWithoutExtension filename
182-
MatrixStructure = matrixHandler filename
183-
})
183+
|> Seq.map
184+
(fun matrixFilename ->
185+
match Path.GetExtension matrixFilename with
186+
| ".mtx" -> GraphReader.readShapeMtx <| Utils.getFullPathToMatrix matrixFilename
187+
| _ -> failwith "Unsupported matrix format"
188+
)
184189

185-
[<Config(typeof<BoolConfig>)>]
186190
type EWiseAddBenchmarks4Bool() =
187191
inherit EWiseAddBenchmarks()
188192

@@ -192,13 +196,18 @@ type EWiseAddBenchmarks4Bool() =
192196
member val FirstMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
193197
member val SecondMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
194198

195-
[<ParamsSource("InputMatricesProvider")>]
196-
member val InputMatrix = Unchecked.defaultof<InputMatrixFormat<bool>> with get, set
197-
198199
[<GlobalSetup>]
199200
member this.FormInputData() =
200-
this.FirstMatrix <- this.InputMatrix.MatrixStructure
201-
this.SecondMatrix <- this.InputMatrix.MatrixStructure |> Utils.transposeCOO
201+
let mtxFormat = GraphReader.readMtx <| Utils.getFullPathToMatrix this.InputMatrix.Filename
202+
let cooMatrix =
203+
match mtxFormat.Shape.Format, mtxFormat.Shape.Field with
204+
| "coordinate", "real" -> Utils.makeCOO mtxFormat <| FromString (fun _ -> true)
205+
| "coordinate", "integer" -> Utils.makeCOO mtxFormat <| FromString (fun _ -> true)
206+
| "coordinate", "pattern" -> Utils.makeCOO mtxFormat <| FromUnit (fun _ -> true)
207+
| _ -> failwith "Unsupported matrix format"
208+
209+
this.FirstMatrix <- cooMatrix
210+
this.SecondMatrix <- cooMatrix |> Utils.transposeCOO
202211

203212
[<IterationSetup>]
204213
member this.BuildCOO() =
@@ -253,20 +262,10 @@ type EWiseAddBenchmarks4Bool() =
253262
|> Seq.ofArray
254263
|> Seq.filter (fun line -> not <| line.StartsWith "!")
255264

256-
let matrixHandler (matrixFilename: string) =
257-
match Path.GetExtension matrixFilename with
258-
| ".mtx" ->
259-
let mtx = GraphReader.readMtx <| Utils.getFullPathToMatrix matrixFilename
260-
match mtx.Format, mtx.Field with
261-
| "coordinate", "real" -> Utils.makeCOO mtx <| FromString (fun _ -> true)
262-
| "coordinate", "integer" -> Utils.makeCOO mtx <| FromString (fun _ -> true)
263-
| "coordinate", "pattern" -> Utils.makeCOO mtx <| FromUnit (fun _ -> true)
264-
| _ -> failwith "Unsupported matrix format"
265-
| _ -> failwith "Unsupported matrix format"
266-
267265
matricesFilenames
268-
|> Seq.map (fun filename ->
269-
{
270-
MatrixName = Path.GetFileNameWithoutExtension filename
271-
MatrixStructure = matrixHandler filename
272-
})
266+
|> Seq.map
267+
(fun matrixFilename ->
268+
match Path.GetExtension matrixFilename with
269+
| ".mtx" -> GraphReader.readShapeMtx <| Utils.getFullPathToMatrix matrixFilename
270+
| _ -> failwith "Unsupported matrix format"
271+
)

benchmarks/GraphBLAS-sharp.Benchmarks/Common.fs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,13 @@ with
2828

2929
sprintf "%s, %s" platformName deviceType
3030

31-
type InputMatrixFormat<'a> = {
32-
MatrixName: string
33-
MatrixStructure: COOFormat<'a>
34-
}
35-
with
36-
override this.ToString() =
37-
sprintf "%s" this.MatrixName
38-
39-
type MatrixShapeColumn<'a>(columnName: string, getShape: InputMatrixFormat<'a> -> int) =
31+
type MatrixShapeColumn(columnName: string, getShape: MtxShape -> int) =
4032
interface IColumn with
4133
member this.AlwaysShow: bool = true
4234
member this.Category: ColumnCategory = ColumnCategory.Params
4335
member this.ColumnName: string = columnName
4436
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase): string =
45-
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat<'a>
37+
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> MtxShape
4638
sprintf "%i" <| getShape inputMatrix
4739
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase, style: SummaryStyle): string =
4840
(this :> IColumn).GetValue(summary, benchmarkCase)
@@ -54,18 +46,18 @@ type MatrixShapeColumn<'a>(columnName: string, getShape: InputMatrixFormat<'a> -
5446
member this.PriorityInCategory: int = 1
5547
member this.UnitType: UnitType = UnitType.Size
5648

57-
type TEPSColumn<'a>() =
49+
type TEPSColumn() =
5850
interface IColumn with
5951
member this.AlwaysShow: bool = true
6052
member this.Category: ColumnCategory = ColumnCategory.Statistics
6153
member this.ColumnName: string = "TEPS"
6254
member this.GetValue(summary: Summary, benchmarkCase: BenchmarkCase): string =
63-
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> InputMatrixFormat<'a>
64-
let (nrows, ncols, nnz) =
65-
inputMatrix.MatrixStructure.RowCount,
66-
inputMatrix.MatrixStructure.ColumnCount,
67-
inputMatrix.MatrixStructure.Values.Length
68-
let (vertices, edges) = if nrows = ncols then (nrows, nnz) else (ncols, nrows)
55+
let inputMatrix = benchmarkCase.Parameters.["InputMatrix"] :?> MtxShape
56+
let (nrows, ncols) =
57+
inputMatrix.RowCount,
58+
inputMatrix.ColumnCount
59+
// !!!!!!!!!!!!
60+
let (vertices, edges) = (ncols, nrows)
6961
if isNull summary.[benchmarkCase].ResultStatistics then
7062
"NA"
7163
else
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Intel*
2-
All
2+
Cpu

benchmarks/GraphBLAS-sharp.Benchmarks/Utils.fs

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,53 @@ namespace GraphBLAS.FSharp.Benchmarks
33
open System.IO
44
open GraphBLAS.FSharp
55

6-
type MtxFormat = {
6+
type MtxShape = {
7+
Filename: string
78
Object: string
89
Format: string
910
Field: string
1011
Symmetry: string
1112
Size: int[]
12-
Data: string[] list
1313
}
1414
with
1515
member this.RowCount = this.Size.[0]
1616
member this.ColumnCount = this.Size.[1]
17+
override this.ToString() =
18+
sprintf "%s" <| Path.GetFileNameWithoutExtension this.Filename
19+
20+
type MtxFormat = {
21+
Shape: MtxShape
22+
Data: string[] list
23+
}
1724

1825
module GraphReader =
26+
let readShapeMtx (pathToGraph: string) =
27+
use streamReader = new StreamReader(pathToGraph)
28+
29+
let header = streamReader.ReadLine().Split(' ')
30+
let object = header.[1]
31+
let format = header.[2]
32+
let field = header.[3]
33+
let symmetry = header.[4]
34+
35+
while streamReader.Peek() = int '%' do
36+
streamReader.ReadLine() |> ignore
37+
38+
let size =
39+
streamReader.ReadLine().Split(' ')
40+
|> Array.map int
41+
42+
{
43+
Filename = pathToGraph |> Path.GetFileName
44+
Object = object
45+
Format = format
46+
Field = field
47+
Symmetry = symmetry
48+
Size = size
49+
}
50+
1951
let readMtx (pathToGraph: string) =
52+
let meta = readShapeMtx pathToGraph
2053
use streamReader = new StreamReader(pathToGraph)
2154

2255
let header = streamReader.ReadLine().Split(' ')
@@ -33,21 +66,17 @@ module GraphReader =
3366
|> Array.map int
3467

3568
let len =
36-
match format with
37-
| "array" -> size.[0] * size.[1]
38-
| "coordinate" -> size.[2]
69+
match meta.Format with
70+
| "array" -> meta.Size.[0] * meta.Size.[1]
71+
| "coordinate" -> meta.Size.[2]
3972
| _ -> failwith "Unsupported matrix format"
4073

4174
let data =
4275
[0 .. len - 1]
4376
|> List.map (fun _ -> streamReader.ReadLine().Split(' '))
4477

4578
{
46-
Object = object
47-
Format = format
48-
Field = field
49-
Symmetry = symmetry
50-
Size = size
79+
Shape = meta
5180
Data = data
5281
}
5382

@@ -83,12 +112,13 @@ module Utils =
83112
let c f x y = f y x
84113
let rows = rows |> Array.map (c (-) 1)
85114
let cols = cols |> Array.map (c (-) 1)
115+
printfn "kek"
86116
{
87117
Rows = rows
88118
Columns = cols
89119
Values = values
90-
RowCount = mtx.RowCount
91-
ColumnCount = mtx.ColumnCount
120+
RowCount = mtx.Shape.RowCount
121+
ColumnCount = mtx.Shape.ColumnCount
92122
}
93123

94124
let transposeCOO (matrix: COOFormat<'a>) =
@@ -98,6 +128,7 @@ module Utils =
98128
|> Array.unzip3
99129
|>
100130
fun (rows, cols, values) ->
131+
printfn "kek"
101132
{
102133
Rows = cols
103134
Columns = rows

0 commit comments

Comments
 (0)