Skip to content

Commit 1c013e9

Browse files
authored
Merge pull request #34 from kirillgarbar/net5
Backend matrix generalization
2 parents 1f780ba + 77df414 commit 1c013e9

10 files changed

Lines changed: 73 additions & 20 deletions

File tree

src/GraphBLAS-sharp.Backend/GraphBLAS-sharp.Backend.fsproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
<ItemGroup>
1010
<Compile Include="AssemblyInfo.fs" />
1111
<Compile Include="Common/Utils.fs" />
12-
<Compile Include="Common/ClArray.fs" />
12+
<Compile Include="Common/ClArray.fs" />
1313
<Compile Include="Common/Sum.fs" />
1414
<Compile Include="Common/BitonicSort.fs" />
1515
<Compile Include="Matrices.fs" />
16-
<Compile Include="COOMatrix/COOMatrix.fs" />
17-
<Compile Include="CSRMatrix/CSRMatrix.fs" />
16+
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
17+
<Compile Include="Matrix/CSRMatrix/CSRMatrix.fs" />
18+
<Compile Include="Matrix/CSRMatrix/SpGEMM.fs" />
19+
<Compile Include="Matrix/CSRMatrix/SpMV.fs" />
20+
<Compile Include="Matrix/Matrix.fs" />
1821
<!--Compile Include="Backend/CSRMatrix/GetTuples.fs" /-->
19-
<Compile Include="CSRMatrix/SpGEMM.fs" />
2022
<!--Compile Include="Backend/CSRMatrix/SpMSpV.fs" /-->
21-
<Compile Include="CSRMatrix/SpMV.fs" />
2223
<!--Compile Include="Backend/CSRMatrix/Transpose.fs" /-->
2324
<!--Compile Include="Backend/COOVector/Utilities/SetPositions.fs" />
2425
<Compile Include="Backend/COOVector/Utilities/AssignSubVector/Intersect.fs" />

src/GraphBLAS-sharp.Backend/Matrices.fs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@ open Brahma.FSharp.OpenCL
55
type IDeviceMemObject =
66
abstract Dispose : unit -> unit
77

8-
type CSRMatrix<'elem when 'elem: struct> =
8+
type MatrixFromat =
9+
| CSR
10+
| COO
11+
12+
type Matrix<'a when 'a: struct> =
13+
| MatrixCSR of CSRMatrix<'a>
14+
| MatrixCOO of COOMatrix<'a>
15+
16+
member this.RowCount =
17+
match this with
18+
| MatrixCSR matrix -> matrix.RowCount
19+
| MatrixCOO matrix -> matrix.RowCount
20+
21+
member this.ColumnCount =
22+
match this with
23+
| MatrixCSR matrix -> matrix.ColumnCount
24+
| MatrixCOO matrix -> matrix.ColumnCount
25+
26+
and CSRMatrix<'elem when 'elem: struct> =
927
{ Context: ClContext
1028
RowCount: int
1129
ColumnCount: int
@@ -21,32 +39,32 @@ type CSRMatrix<'elem when 'elem: struct> =
2139
q.Post(Msg.CreateFreeMsg<_>(this.RowPointers))
2240
q.PostAndReply(Msg.MsgNotifyMe)
2341

24-
type TupleMatrix<'elem when 'elem: struct> =
42+
and COOMatrix<'elem when 'elem: struct> =
2543
{ Context: ClContext
26-
RowIndices: ClArray<int>
27-
ColumnIndices: ClArray<int>
44+
RowCount: int
45+
ColumnCount: int
46+
Rows: ClArray<int>
47+
Columns: ClArray<int>
2848
Values: ClArray<'elem> }
2949

3050
interface IDeviceMemObject with
3151
member this.Dispose() =
3252
let q = this.Context.Provider.CommandQueue
33-
q.Post(Msg.CreateFreeMsg<_>(this.RowIndices))
34-
q.Post(Msg.CreateFreeMsg<_>(this.ColumnIndices))
3553
q.Post(Msg.CreateFreeMsg<_>(this.Values))
54+
q.Post(Msg.CreateFreeMsg<_>(this.Columns))
55+
q.Post(Msg.CreateFreeMsg<_>(this.Rows))
3656
q.PostAndReply(Msg.MsgNotifyMe)
3757

38-
type COOMatrix<'elem when 'elem: struct> =
58+
and TupleMatrix<'elem when 'elem: struct> =
3959
{ Context: ClContext
40-
RowCount: int
41-
ColumnCount: int
42-
Rows: ClArray<int>
43-
Columns: ClArray<int>
60+
RowIndices: ClArray<int>
61+
ColumnIndices: ClArray<int>
4462
Values: ClArray<'elem> }
4563

4664
interface IDeviceMemObject with
4765
member this.Dispose() =
4866
let q = this.Context.Provider.CommandQueue
67+
q.Post(Msg.CreateFreeMsg<_>(this.RowIndices))
68+
q.Post(Msg.CreateFreeMsg<_>(this.ColumnIndices))
4969
q.Post(Msg.CreateFreeMsg<_>(this.Values))
50-
q.Post(Msg.CreateFreeMsg<_>(this.Columns))
51-
q.Post(Msg.CreateFreeMsg<_>(this.Rows))
5270
q.PostAndReply(Msg.MsgNotifyMe)

src/GraphBLAS-sharp.Backend/COOMatrix/COOMatrix.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/COOMatrix/COOMatrix.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ module COOMatrix =
355355
queue.Post(Msg.CreateFreeMsg<_>(allValues))
356356

357357
{ Context = clContext
358-
RowCount = resultRows.Length
359-
ColumnCount = resultColumns.Length
358+
RowCount = matrixLeft.RowCount
359+
ColumnCount = matrixLeft.ColumnCount
360360
Rows = resultRows
361361
Columns = resultColumns
362362
Values = resultValues }

src/GraphBLAS-sharp.Backend/CSRMatrix/CSRMatrix.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/CSRMatrix.fs

File renamed without changes.

src/GraphBLAS-sharp.Backend/CSRMatrix/GetTuples.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/GetTuples.fs

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/GraphBLAS-sharp.Backend/CSRMatrix/Transpose.fs renamed to src/GraphBLAS-sharp.Backend/Matrix/CSRMatrix/Transpose.fs

File renamed without changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace GraphBLAS.FSharp.Backend
2+
3+
open Brahma.FSharp.OpenCL
4+
open Microsoft.FSharp.Quotations
5+
6+
module Matrix =
7+
let toCSR (clContext: ClContext) workGroupSize =
8+
let toCSR = COOMatrix.toCSR clContext workGroupSize
9+
10+
fun (processor: MailboxProcessor<_>) (matrix: Matrix<'a>) ->
11+
match matrix with
12+
| MatrixCOO m -> toCSR processor m |> MatrixCSR
13+
| MatrixCSR _ -> matrix
14+
15+
let toCOO (clContext: ClContext) workGroupSize =
16+
let toCOO = CSRMatrix.toCOO clContext
17+
18+
fun (processor: MailboxProcessor<_>) (matrix: Matrix<'a>) ->
19+
match matrix with
20+
| MatrixCOO _ -> matrix
21+
| MatrixCSR m -> toCOO workGroupSize processor m |> MatrixCOO
22+
23+
let eWiseAdd (clContext: ClContext) (opAdd: Expr<'a -> 'a -> 'a>) workGroupSize =
24+
let COOeWiseAdd =
25+
COOMatrix.eWiseAdd clContext opAdd workGroupSize
26+
27+
let CSReWiseAdd =
28+
CSRMatrix.eWiseAdd clContext opAdd workGroupSize
29+
30+
fun (processor: MailboxProcessor<_>) matrix1 matrix2 ->
31+
match matrix1, matrix2 with
32+
| MatrixCOO m1, MatrixCOO m2 -> COOeWiseAdd processor m1 m2 |> MatrixCOO
33+
| MatrixCSR m1, MatrixCSR m2 -> CSReWiseAdd processor m1 m2 |> MatrixCSR
34+
| _ -> failwith "Matrix formats are not matching"

0 commit comments

Comments
 (0)