Skip to content

Commit ada8e43

Browse files
committed
Refactor
1 parent 2e0dfe8 commit ada8e43

16 files changed

Lines changed: 241 additions & 93 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksBFS.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ type BFSBenchmark4CSRMatrix() =
2020

2121
[<GlobalSetup>]
2222
member this.BuildMatrix() =
23-
matrix <- CSRMatrix(this.PathToGraph)
24-
source <- random.Next matrix.RowCount
23+
matrix <- MatrixCSR <| CSRMatrix.FromFile this.PathToGraph
24+
source <- random.Next <| Matrix.rowCount matrix
2525

2626
[<Benchmark>]
2727
member this.LevelBFS() =
28-
levelBFS matrix source
28+
BFS.levelSingleSource matrix source
2929

3030
/// Sequence of paths to files where data for benchmarking will be taken from
3131
static member GraphPaths = seq {

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksEWiseAdd.fs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ type EWiseAddBenchmarks4Float32() =
9797
let mutable leftCOO = Unchecked.defaultof<Matrix<float32>>
9898
let mutable rightCOO = Unchecked.defaultof<Matrix<float32>>
9999

100-
member val FirstMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
101-
member val SecondMatrix = Unchecked.defaultof<COOFormat<float32>> with get, set
100+
member val FirstMatrix = Unchecked.defaultof<COOMatrix<float32>> with get, set
101+
member val SecondMatrix = Unchecked.defaultof<COOMatrix<float32>> with get, set
102102

103103
[<GlobalSetup>]
104104
member this.FormInputData() =
@@ -130,13 +130,13 @@ type EWiseAddBenchmarks4Float32() =
130130
Array.blit this.FirstMatrix.Values 0 leftVals 0 this.FirstMatrix.Values.Length
131131

132132
leftCOO <-
133-
COOMatrix(
133+
COOMatrix.FromTuples(
134134
this.FirstMatrix.RowCount,
135135
this.FirstMatrix.ColumnCount,
136136
leftRows,
137137
leftCols,
138138
leftVals
139-
) :> Matrix<float32>
139+
) |> MatrixCOO
140140

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

148148
rightCOO <-
149-
COOMatrix(
149+
COOMatrix.FromTuples(
150150
this.SecondMatrix.RowCount,
151151
this.SecondMatrix.ColumnCount,
152152
rightRows,
153153
rightCols,
154154
rightVals
155-
) :> Matrix<float32>
155+
) |> MatrixCOO
156156

157157
[<Benchmark>]
158158
member this.EWiseAdditionCOOFloat32() =
159159
let (ClContext context) = this.OclContext
160-
leftCOO.EWiseAdd rightCOO None AddMult.float32
161-
|> context.RunSync
160+
(leftCOO, rightCOO) ||> Matrix.eWiseAdd AddMult.float32
161+
|> EvalGB.runWithClContext context
162162

163163
static member InputMatricesProvider =
164164
"EWiseAddBenchmarks4Float32.txt"
@@ -176,8 +176,8 @@ type EWiseAddBenchmarks4Bool() =
176176
let mutable leftCOO = Unchecked.defaultof<Matrix<bool>>
177177
let mutable rightCOO = Unchecked.defaultof<Matrix<bool>>
178178

179-
member val FirstMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
180-
member val SecondMatrix = Unchecked.defaultof<COOFormat<bool>> with get, set
179+
member val FirstMatrix = Unchecked.defaultof<COOMatrix<bool>> with get, set
180+
member val SecondMatrix = Unchecked.defaultof<COOMatrix<bool>> with get, set
181181

182182
[<GlobalSetup>]
183183
member this.FormInputData() =
@@ -201,13 +201,13 @@ type EWiseAddBenchmarks4Bool() =
201201
Array.blit this.FirstMatrix.Columns 0 leftCols 0 this.FirstMatrix.Columns.Length
202202

203203
leftCOO <-
204-
COOMatrix(
204+
COOMatrix.FromTuples(
205205
this.FirstMatrix.RowCount,
206206
this.FirstMatrix.ColumnCount,
207207
leftRows,
208208
leftCols,
209209
leftVals
210-
) :> Matrix<bool>
210+
) |> MatrixCOO
211211

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

218218
rightCOO <-
219-
COOMatrix(
219+
COOMatrix.FromTuples(
220220
this.SecondMatrix.RowCount,
221221
this.SecondMatrix.ColumnCount,
222222
rightRows,
223223
rightCols,
224224
rightVals
225-
) :> Matrix<bool>
225+
) |> MatrixCOO
226226

227227
[<Benchmark>]
228228
member this.EWiseAdditionCOOBool() =
229229
let (ClContext context) = this.OclContext
230-
leftCOO.EWiseAdd rightCOO None AnyAll.bool
231-
|> context.RunSync
230+
(leftCOO, rightCOO) ||> Matrix.eWiseAdd AnyAll.bool
231+
|> EvalGB.runWithClContext context
232232

233233
static member InputMatricesProvider =
234234
"EWiseAddBenchmarks4Bool.txt"

benchmarks/GraphBLAS-sharp.Benchmarks/Utils.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module Utils =
122122
ColumnCount = mtx.Shape.ColumnCount
123123
}
124124

125-
let transposeCOO (matrix: COOFormat<'a>) =
125+
let transposeCOO (matrix: COOMatrix<'a>) =
126126
printfn "Start transpose COO"
127127

128128
(matrix.Columns, matrix.Rows, matrix.Values)

src/GraphBLAS-sharp/Algorithms/BFS.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ open GraphBLAS.FSharp.Predefined
44
open GraphBLAS.FSharp
55

66
module BFS =
7-
let level (matrix: Matrix<bool>) (source: int) = graphblas {
7+
let levelSingleSource (matrix: Matrix<bool>) (source: int) = graphblas {
88
let vertexCount = Matrix.rowCount matrix
99
let levels = Vector.zeroCreate vertexCount 0
1010
let frontier = Vector.ofList vertexCount [source, true]

src/GraphBLAS-sharp/GraphBLAS-sharp.fsproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
<Compile Include="AlgebraicStructures.fs" />
1515
<Compile Include="GraphblasEvaluation.fs" />
1616
<Compile Include="Objects/Matrix.fs" />
17-
<Compile Include="Objects\MatrixTuples.fs" />
1817
<Compile Include="Objects/Vector.fs" />
19-
<Compile Include="Objects\VectorTuples.fs" />
2018
<Compile Include="Objects/Scalar.fs" />
2119
<Compile Include="Objects/Masks.fs" />
2220
<Compile Include="Backend/Common/Utils.fs" />
2321
<Compile Include="Backend/Common/Scan.fs" />
2422
<Compile Include="Backend\COOMatrix\GetTuples.fs" />
2523
<Compile Include="Backend/COOMatrix/EWiseAdd.fs" />
24+
<Compile Include="IO/MtxReader.fs" />
2625
<Compile Include="Methods/Matrix.fs" />
2726
<Compile Include="Methods/Vector.fs" />
2827
<Compile Include="Predefined/Monoids/Any.fs" />
@@ -39,4 +38,4 @@
3938
</Content>
4039
</ItemGroup>
4140
<Import Project="..\..\.paket\Paket.Restore.targets" />
42-
</Project>
41+
</Project>

src/GraphBLAS-sharp/GraphblasEvaluation.fs

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

33
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
44
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
5+
open GraphBLAS.FSharp.Helpers
56

67
type GraphblasContext =
78
{
@@ -11,6 +12,10 @@ type GraphblasContext =
1112
type GraphblasEvaluation<'a> = EvalGB of (GraphblasContext -> 'a)
1213

1314
module EvalGB =
15+
let defaultEnv = {
16+
ClContext = OpenCLEvaluationContext()
17+
}
18+
1419
let private runCl env (OpenCLEvaluation f) = f env
1520

1621
let run env (EvalGB action) = action env
@@ -24,15 +29,25 @@ module EvalGB =
2429
let x = run env reader
2530
run env (f x)
2631

32+
let (>>=) x f = bind f x
33+
2734
let return' x =
2835
EvalGB <| fun _ -> x
2936

37+
let returnFrom x = x
38+
3039
let fromCl clEvaluation =
3140
EvalGB <| fun env ->
3241
runCl env.ClContext clEvaluation
3342

34-
let runWithClContext clContext (EvalGB action) =
35-
action { ClContext = clContext }
43+
let withClContext clContext (EvalGB action) =
44+
ask >>= fun env ->
45+
return' ^ action { env with ClContext = clContext }
46+
47+
let runSync (EvalGB action) =
48+
let result = action defaultEnv
49+
defaultEnv.ClContext.CommandQueue.Finish() |> ignore
50+
result
3651

3752
type GraphblasBuilder() =
3853
member this.Bind(x, f) = EvalGB.bind f x

src/GraphBLAS-sharp/Helpers.fs

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

3+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
4+
open OpenCL.Net
5+
open System.Text.RegularExpressions
6+
7+
[<AutoOpen>]
38
module Helpers =
49
let inline (!>) (x: ^a) : ^b = (^a: (static member op_Implicit : ^a -> ^b) x)
510

611
let inline (^) f x = f x
12+
13+
module Utils =
14+
let avaliableContexts (platformRegex: string) =
15+
let mutable e = ErrorCode.Unknown
16+
Cl.GetPlatformIDs &e
17+
|> Array.collect (fun platform -> Cl.GetDeviceIDs(platform, DeviceType.All, &e))
18+
|> Seq.ofArray
19+
|> Seq.distinctBy (fun device -> Cl.GetDeviceInfo(device, DeviceInfo.Name, &e).ToString())
20+
|> Seq.filter
21+
(fun device ->
22+
let platform = Cl.GetDeviceInfo(device, DeviceInfo.Platform, &e).CastTo<Platform>()
23+
let platformName = Cl.GetPlatformInfo(platform, PlatformInfo.Name, &e).ToString()
24+
(Regex platformRegex).IsMatch platformName
25+
)
26+
|> Seq.map
27+
(fun device ->
28+
let platform = Cl.GetDeviceInfo(device, DeviceInfo.Platform, &e).CastTo<Platform>()
29+
let platformName = Cl.GetPlatformInfo(platform, PlatformInfo.Name, &e).ToString()
30+
let deviceType = Cl.GetDeviceInfo(device, DeviceInfo.Type, &e).CastTo<DeviceType>()
31+
OpenCLEvaluationContext(platformName, deviceType)
32+
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
namespace GraphBLAS.FSharp.IO
2+
3+
open System.IO
4+
open GraphBLAS.FSharp
5+
open System
6+
7+
type MtxShape =
8+
{
9+
Filename: string
10+
Object: string
11+
Format: string
12+
Field: string
13+
Symmetry: string
14+
Size: int[]
15+
}
16+
17+
member this.RowCount = this.Size.[0]
18+
member this.ColumnCount = this.Size.[1]
19+
20+
override this.ToString() =
21+
sprintf "%s" <| Path.GetFileNameWithoutExtension this.Filename
22+
23+
module MtxReader =
24+
let private readShapeWithReader (streamReader: StreamReader) (pathToFile: string) =
25+
let shape = streamReader.ReadLine().Split(' ')
26+
let object = shape.[1]
27+
let format = shape.[2]
28+
let field = shape.[3]
29+
let symmetry = shape.[4]
30+
31+
while streamReader.Peek() = int '%' do
32+
streamReader.ReadLine() |> ignore
33+
34+
let size =
35+
streamReader.ReadLine().Split(' ')
36+
|> Array.map int
37+
38+
{
39+
Filename = pathToFile |> Path.GetFileName
40+
Object = object
41+
Format = format
42+
Field = field
43+
Symmetry = symmetry
44+
Size = size
45+
}
46+
47+
let readShapeFromFile (pathToFile: string) =
48+
use streamReader = new StreamReader(pathToFile)
49+
readShapeWithReader streamReader pathToFile
50+
51+
let private readGenericMatrixFromFile (pathToFile: string) : Matrix<'a> =
52+
use streamReader = new StreamReader(pathToFile)
53+
let shape = readShapeWithReader streamReader pathToFile
54+
55+
let len =
56+
match shape.Format with
57+
| "array" -> shape.Size.[0] * shape.Size.[1]
58+
| "coordinate" -> shape.Size.[2]
59+
| _ -> failwith "Unsupported matrix format"
60+
61+
let data =
62+
[0 .. len - 1]
63+
|> List.map (fun _ -> streamReader.ReadLine().Split(' '))
64+
65+
let makeCOO () =
66+
let pack x y = (uint64 x <<< 32) ||| (uint64 y)
67+
let unpack x = (int ((x &&& 0xFFFFFFFF0000000UL) >>> 32)), (int (x &&& 0xFFFFFFFUL))
68+
69+
data
70+
|> Array.ofList
71+
|> Array.Parallel.map
72+
(fun line ->
73+
let value = Convert.ChangeType(line.[2], typeof<'a>) |> unbox<'a>
74+
struct(pack <| int line.[0] <| int line.[1], value)
75+
)
76+
|> Array.sortBy (fun struct(packedIndex, _) -> packedIndex)
77+
|>
78+
fun data ->
79+
let rows = Array.zeroCreate data.Length
80+
let cols = Array.zeroCreate data.Length
81+
let values = Array.zeroCreate data.Length
82+
83+
Array.Parallel.iteri (fun i struct(packedIndex, value) ->
84+
let (rowIdx, columnIdx) = unpack packedIndex
85+
// in mtx indecies start at 1
86+
rows.[i] <- rowIdx - 1
87+
cols.[i] <- columnIdx - 1
88+
values.[i] <- value
89+
) data
90+
91+
{
92+
Rows = rows
93+
Columns = cols
94+
Values = values
95+
RowCount = shape.RowCount
96+
ColumnCount = shape.ColumnCount
97+
}
98+
99+
100+
match shape.Format with
101+
| "array" -> failwith "Unsupported matrix format"
102+
| "coordinate" -> MatrixCOO <| makeCOO ()
103+
104+
let readRealMatrix (pathToFile: string) : Matrix<float32> =
105+
readGenericMatrixFromFile pathToFile

src/GraphBLAS-sharp/Methods/Matrix.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ module Matrix =
4747
let mask (matrix: Matrix<'a>) : GraphblasEvaluation<Mask2D> = failwith "Not Implemented yet"
4848
let complemented (matrix: Matrix<'a>) : GraphblasEvaluation<Mask2D> = failwith "Not Implemented yet"
4949
let synchronize (matrix: Matrix<'a>) : GraphblasEvaluation<unit> = failwith "Not Implemented yet"
50+
// let convert<'a when 'a :> Matrix<'a>>() = ()
5051

5152
(*
5253
assignment, extraction and filling
@@ -165,3 +166,16 @@ module Matrix =
165166
let reduceRowsWithMask (monoid: IMonoid<'a>) (mask: Mask1D) (matrix: Matrix<'a>) : GraphblasEvaluation<Vector<'a>> = failwith "Not Implemented yet"
166167
let reduceColsWithMask (monoid: IMonoid<'a>) (mask: Mask1D) (matrix: Matrix<'a>) : GraphblasEvaluation<Vector<'a>> = failwith "Not Implemented yet"
167168
let kroneckerWithMask (semiring: ISemiring<'a>) (mask: Mask2D) (leftMatrix: Matrix<'a>) (rightMatrix: Matrix<'a>) : GraphblasEvaluation<Matrix<'a>> = failwith "Not Implemented yet"
169+
170+
// ждём тайпклассов чтобы можно было вызывать synchronize для всех объектов,
171+
// для которых он реализован, не привязывая реализацию к классу (как стратегия)
172+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
173+
module MatrixTuples =
174+
let synchronize (matrixTuples: MatrixTuples<'a>) =
175+
opencl {
176+
let! _ = ToHost matrixTuples.RowIndices
177+
let! _ = ToHost matrixTuples.ColumnIndices
178+
let! _ = ToHost matrixTuples.Values
179+
return ()
180+
}
181+
|> EvalGB.fromCl

src/GraphBLAS-sharp/Methods/Vector.fs

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

3+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
4+
35
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
46
module Vector =
57

@@ -85,3 +87,13 @@ module Vector =
8587
let eWiseAddWithMask (semiring: ISemiring<'a>) (mask: Mask1D) (leftVector: Vector<'a>) (rightVector: Vector<'a>) : GraphblasEvaluation<Vector<'a>> = failwith "Not Implemented yet"
8688
let applyWithMask (mapper: UnaryOp<'a, 'b>) (mask: Mask1D) (vector: Vector<'a>) : GraphblasEvaluation<Vector<'b>> = failwith "Not Implemented yet"
8789
let pruneWithMask (predicate: UnaryOp<'a, bool>) (mask: Mask1D) (vector: Vector<'a>) : GraphblasEvaluation<Vector<'a>> = failwith "Not Implemented yet"
90+
91+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
92+
module VectorTuples =
93+
let synchronize (vectorTuples: VectorTuples<'a>) =
94+
opencl {
95+
let! _ = ToHost vectorTuples.Indices
96+
let! _ = ToHost vectorTuples.Values
97+
return ()
98+
}
99+
|> EvalGB.fromCl

0 commit comments

Comments
 (0)