Skip to content

Commit b48d80e

Browse files
committed
refactor: ClVector module
1 parent ec970fe commit b48d80e

19 files changed

Lines changed: 141 additions & 100 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/BenchmarksMxv.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open GraphBLAS.FSharp
44
open GraphBLAS.FSharp.Backend
55
open BenchmarkDotNet.Attributes
66
open GraphBLAS.FSharp.Backend.Objects
7+
open GraphBLAS.FSharp.Objects
78

89
[<Config(typeof<CommonConfig>)>]
910
type MxvBenchmarks() =

src/GraphBLAS-sharp.Backend/Objects/Vector.fs

Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,103 +7,30 @@ type VectorFormat =
77
| Sparse
88
| Dense
99

10-
type SparseVector<'a> =
11-
{ Indices: int []
12-
Values: 'a []
13-
Size: int }
10+
module ClVector =
11+
type Sparse<'a> =
12+
{ Context: ClContext
13+
Indices: ClArray<int>
14+
Values: ClArray<'a>
15+
Size: int }
1416

15-
override this.ToString() =
16-
[ sprintf "Sparse Vector\n"
17-
sprintf "Size: %i \n" this.Size
18-
sprintf "Indices: %A \n" this.Indices
19-
sprintf "Values: %A \n" this.Values ]
20-
|> String.concat ""
17+
interface IDeviceMemObject with
18+
member this.Dispose(q) =
19+
q.Post(Msg.CreateFreeMsg<_>(this.Values))
20+
q.Post(Msg.CreateFreeMsg<_>(this.Indices))
21+
q.PostAndReply(Msg.MsgNotifyMe)
2122

22-
member this.ToDevice(context: ClContext) =
23-
let indices = context.CreateClArray this.Indices
24-
let values = context.CreateClArray this.Values
25-
26-
{ Context = context
27-
Indices = indices
28-
Values = values
29-
Size = this.Size }
30-
31-
static member FromTuples(indices: int [], values: 'a [], size: int) =
32-
{ Indices = indices
33-
Values = values
34-
Size = size }
35-
36-
static member FromArray(array: 'a [], isZero: 'a -> bool) =
37-
let (indices, vals) =
38-
array
39-
|> Seq.cast<'a>
40-
|> Seq.mapi (fun idx v -> (idx, v))
41-
|> Seq.filter (fun (_, v) -> not (isZero v))
42-
|> Array.ofSeq
43-
|> Array.unzip
44-
45-
SparseVector.FromTuples(indices, vals, array.Length)
46-
47-
and ClSparseVector<'a> =
48-
{ Context: ClContext
49-
Indices: ClArray<int>
50-
Values: ClArray<'a>
51-
Size: int }
52-
53-
member this.ToHost(q: MailboxProcessor<_>) =
54-
let indices = Array.zeroCreate this.Indices.Length
55-
let values = Array.zeroCreate this.Values.Length
56-
57-
let _ =
58-
q.Post(Msg.CreateToHostMsg(this.Indices, indices))
59-
60-
let _ =
61-
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(this.Values, values, ch))
62-
63-
{ Indices = indices
64-
Values = values
65-
Size = this.Size }
66-
67-
interface IDeviceMemObject with
68-
member this.Dispose(q) =
69-
q.Post(Msg.CreateFreeMsg<_>(this.Values))
70-
q.Post(Msg.CreateFreeMsg<_>(this.Indices))
71-
q.PostAndReply(Msg.MsgNotifyMe)
72-
73-
member this.Dispose(q) = (this :> IDeviceMemObject).Dispose(q)
23+
member this.Dispose(q) = (this :> IDeviceMemObject).Dispose(q)
7424

7525
[<RequireQualifiedAccess>]
76-
type Vector<'a when 'a: struct> =
77-
| Sparse of SparseVector<'a>
78-
| Dense of 'a option []
79-
member this.Size =
80-
match this with
81-
| Sparse vector -> vector.Size
82-
| Dense vector -> vector.Size
83-
84-
override this.ToString() =
85-
match this with
86-
| Sparse vector -> vector.ToString()
87-
| Dense vector -> DenseVectorToString vector
88-
89-
member this.ToDevice(context: ClContext) =
90-
match this with
91-
| Sparse vector -> ClVector.Sparse <| vector.ToDevice(context)
92-
| Dense vector -> ClVector.Dense <| vector.ToDevice(context)
93-
94-
and [<RequireQualifiedAccess>] ClVector<'a when 'a: struct> =
95-
| Sparse of ClSparseVector<'a>
26+
type ClVector<'a when 'a: struct> =
27+
| Sparse of ClVector.Sparse<'a>
9628
| Dense of ClArray<'a option>
9729
member this.Size =
9830
match this with
9931
| Sparse vector -> vector.Size
10032
| Dense vector -> vector.Size
10133

102-
member this.ToHost(q: MailboxProcessor<_>) =
103-
match this with
104-
| Sparse vector -> Vector.Sparse <| vector.ToHost(q)
105-
| Dense vector -> Vector.Dense <| vector.ToHost(q)
106-
10734
member this.Dispose(q) =
10835
match this with
10936
| Sparse vector -> vector.Dispose(q)

src/GraphBLAS-sharp.Backend/Vector/DenseVector/DenseVector.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open GraphBLAS.FSharp.Backend.Quotes
66
open Microsoft.FSharp.Quotations
77
open GraphBLAS.FSharp.Backend.Predefined
88
open GraphBLAS.FSharp.Backend.Objects
9+
open GraphBLAS.FSharp.Backend.Objects.ClVector
910

1011
module DenseVector =
1112
let elementWiseTo<'a, 'b, 'c when 'a: struct and 'b: struct and 'c: struct>

src/GraphBLAS-sharp.Backend/Vector/SparseVector/SparseVector.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open Microsoft.FSharp.Control
77
open Microsoft.FSharp.Quotations
88
open GraphBLAS.FSharp.Backend.Predefined
99
open GraphBLAS.FSharp.Backend.Objects
10+
open GraphBLAS.FSharp.Backend.Objects.ClVector
1011

1112
module SparseVector =
1213
let private merge<'a, 'b when 'a: struct and 'b: struct> (clContext: ClContext) (workGroupSize: int) =
@@ -191,7 +192,7 @@ module SparseVector =
191192

192193
let setPositions = setPositions clContext workGroupSize
193194

194-
fun (processor: MailboxProcessor<_>) (leftVector: ClSparseVector<'a>) (rightVector: ClSparseVector<'b>) ->
195+
fun (processor: MailboxProcessor<_>) (leftVector: ClVector.Sparse<'a>) (rightVector: ClVector.Sparse<'b>) ->
195196

196197
let allIndices, leftValues, rightValues, isLeft =
197198
merge processor leftVector.Indices leftVector.Values rightVector.Indices rightVector.Values
@@ -287,7 +288,7 @@ module SparseVector =
287288

288289
let setPositions = setPositions clContext workGroupSize
289290

290-
fun (processor: MailboxProcessor<_>) (leftVector: ClSparseVector<'a>) (rightVector: ClSparseVector<'b>) (value: ClCell<'a>) ->
291+
fun (processor: MailboxProcessor<_>) (leftVector: ClVector.Sparse<'a>) (rightVector: ClVector.Sparse<'b>) (value: ClCell<'a>) ->
291292

292293
let allIndices, leftValues, rightValues, isLeft =
293294
merge processor leftVector.Indices leftVector.Values rightVector.Indices rightVector.Values
@@ -327,7 +328,7 @@ module SparseVector =
327328
let create =
328329
ClArray.zeroCreate clContext workGroupSize
329330

330-
fun (processor: MailboxProcessor<_>) (vector: ClSparseVector<'a>) ->
331+
fun (processor: MailboxProcessor<_>) (vector: ClVector.Sparse<'a>) ->
331332
let resultVector = create processor vector.Size
332333

333334
let ndRange =
@@ -350,4 +351,4 @@ module SparseVector =
350351
let reduce =
351352
Fold.reduce clContext workGroupSize opAdd
352353

353-
fun (processor: MailboxProcessor<_>) (vector: ClSparseVector<'a>) -> reduce processor vector.Values
354+
fun (processor: MailboxProcessor<_>) (vector: ClVector.Sparse<'a>) -> reduce processor vector.Values

src/GraphBLAS-sharp.Backend/Vector/Vector.fs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ open GraphBLAS.FSharp.Backend.Vector.Dense
99
open GraphBLAS.FSharp.Backend.Vector.Sparse
1010
open GraphBLAS.FSharp.Backend.Objects
1111
open GraphBLAS.FSharp.Backend.Common.ClArray
12+
open GraphBLAS.FSharp.Backend.Objects.ClVector
13+
1214

1315
module Vector =
1416
let zeroCreate (clContext: ClContext) (workGroupSize: int) =
@@ -37,9 +39,14 @@ module Vector =
3739

3840
match format with
3941
| Sparse ->
40-
SparseVector
41-
.FromTuples(indices, values, size)
42-
.ToDevice clContext
42+
let indices = clContext.CreateClArray indices
43+
let values = clContext.CreateClArray values
44+
45+
{ Context = clContext
46+
Indices = indices
47+
Values = values
48+
Size = size }
49+
4350
|> ClVector.Sparse
4451
| Dense ->
4552
let res = Array.zeroCreate size

src/GraphBLAS-sharp/GraphBLAS-sharp.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<Compile Include="Objects/Matrix.fs" />
2222
<Compile Include="Objects/Scalar.fs" />
2323
<Compile Include="Objects/Masks.fs" />
24+
<Compile Include="Objects\Vector.fs" />
25+
<Compile Include="Objects\VectorExtensions.fs" />
2426
<Compile Include="Operations/Scalar.fs" />
2527
<Compile Include="IO/MtxReader.fs" />
2628
<Compile Include="Predefined/Monoids/Any.fs" />

src/GraphBLAS-sharp/Objects/Matrix.fs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend.Objects
55
open GraphBLAS.FSharp.Backend.Objects.ClMatrix
66

7-
type MatrixFormat =
8-
| CSR
9-
| COO
10-
| CSC
11-
127
[<RequireQualifiedAccess>]
138
type Matrix<'a when 'a: struct> =
149
| CSR of CSRMatrix<'a>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace GraphBLAS.FSharp.Objects
2+
3+
open Brahma.FSharp
4+
open GraphBLAS.FSharp.Backend.Objects
5+
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
6+
open GraphBLAS.FSharp.Backend.Objects.ClVector
7+
8+
module Vector =
9+
type Sparse<'a> =
10+
{ Indices: int []
11+
Values: 'a []
12+
Size: int }
13+
14+
override this.ToString() =
15+
[ sprintf "Sparse Vector\n"
16+
sprintf "Size: %i \n" this.Size
17+
sprintf "Indices: %A \n" this.Indices
18+
sprintf "Values: %A \n" this.Values ]
19+
|> String.concat ""
20+
21+
member this.ToDevice(context: ClContext) =
22+
let indices = context.CreateClArray this.Indices
23+
let values = context.CreateClArray this.Values
24+
25+
{ Context = context
26+
Indices = indices
27+
Values = values
28+
Size = this.Size }
29+
30+
static member FromTuples(indices: int [], values: 'a [], size: int) =
31+
{ Indices = indices
32+
Values = values
33+
Size = size }
34+
35+
static member FromArray(array: 'a [], isZero: 'a -> bool) =
36+
let (indices, vals) =
37+
array
38+
|> Seq.cast<'a>
39+
|> Seq.mapi (fun idx v -> (idx, v))
40+
|> Seq.filter (fun (_, v) -> not (isZero v))
41+
|> Array.ofSeq
42+
|> Array.unzip
43+
44+
Sparse.FromTuples(indices, vals, array.Length)
45+
46+
[<RequireQualifiedAccess>]
47+
type Vector<'a when 'a: struct> =
48+
| Sparse of Vector.Sparse<'a>
49+
| Dense of 'a option []
50+
member this.Size =
51+
match this with
52+
| Sparse vector -> vector.Size
53+
| Dense vector -> vector.Size
54+
55+
override this.ToString() =
56+
match this with
57+
| Sparse vector -> vector.ToString()
58+
| Dense vector -> DenseVectorToString vector
59+
60+
member this.ToDevice(context: ClContext) =
61+
match this with
62+
| Sparse vector -> ClVector.Sparse <| vector.ToDevice(context)
63+
| Dense vector -> ClVector.Dense <| vector.ToDevice(context)
64+
65+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace GraphBLAS.FSharp.Objects
2+
3+
open GraphBLAS.FSharp.Backend.Objects.ArraysExtensions
4+
open GraphBLAS.FSharp.Backend.Objects
5+
open Brahma.FSharp
6+
7+
module ClVectorExtensions =
8+
type ClVector<'a when 'a: struct> with
9+
member this.ToHost(q: MailboxProcessor<_>) =
10+
match this with
11+
| ClVector.Sparse vector ->
12+
let indices = Array.zeroCreate vector.Indices.Length
13+
let values = Array.zeroCreate vector.Values.Length
14+
15+
q.Post(Msg.CreateToHostMsg(vector.Indices, indices))
16+
17+
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(vector.Values, values, ch))
18+
|> ignore
19+
20+
Vector.Sparse
21+
<| { Indices = indices
22+
Values = values
23+
Size = this.Size }
24+
| ClVector.Dense vector -> Vector.Dense <| vector.ToHost q
25+

tests/GraphBLAS-sharp.Tests/Algorithms/BFS.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ open GraphBLAS.FSharp.Tests.Context
99
open GraphBLAS.FSharp.Tests.QuickGraph.Algorithms
1010
open GraphBLAS.FSharp.Tests.QuickGraph.CreateGraph
1111
open GraphBLAS.FSharp.Backend.Objects
12+
open GraphBLAS.FSharp.Objects.ClVectorExtensions
13+
open GraphBLAS.FSharp.Objects
1214

1315
let testFixtures (testContext: TestContext) =
1416
[ let config = Utils.undirectedAlgoConfig

0 commit comments

Comments
 (0)