Skip to content

Commit f12e605

Browse files
committed
ClArray and Array extensions instead of wrapper-types for vectors
1 parent 22e6c3e commit f12e605

3 files changed

Lines changed: 35 additions & 45 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project Sdk="Microsoft.NET.Sdk">
33

44
<PropertyGroup>
@@ -19,6 +19,7 @@
1919
<Compile Include="Common/StandardOperations.fs" />
2020
<Compile Include="Predefined/PrefixSum.fs" />
2121
<Compile Include="Objects/Common.fs" />
22+
<Compile Include="Objects/ArraysExtentions.fs" />
2223
<Compile Include="Objects/Vector.fs" />
2324
<Compile Include="Objects/Matrix.fs" />
2425
<Compile Include="Matrix/COOMatrix/COOMatrix.fs" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace GraphBLAS.FSharp.Backend
2+
3+
open Brahma.FSharp
4+
5+
module ArraysExtensions =
6+
7+
type ClArray<'a> with
8+
member this.Dispose(q: MailboxProcessor<Msg>) =
9+
q.PostAndReply(fun _ -> Msg.CreateFreeMsg this)
10+
11+
member this.ToHost(q: MailboxProcessor<Msg>) =
12+
let dst = Array.zeroCreate this.Length
13+
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(this, dst, ch))
14+
15+
member this.Size = this.Length
16+
17+
type 'a ``[]`` with
18+
member this.Size = this.Length
19+
20+
member this.ToString() =
21+
[ sprintf "Dense Vector\n"
22+
sprintf "Size: %i \n" this.Length
23+
sprintf "Values: %A \n" this ]
24+
|> String.concat ""
25+
26+
member this.ToDevice(context: ClContext) = context.CreateClArray this
27+
28+
static member FromArray(array: 'a [], isZero: 'a -> bool) =
29+
array
30+
|> Array.map (fun v -> if isZero v then None else Some v)

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

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
open Brahma.FSharp
44
open GraphBLAS.FSharp.Backend
5+
open GraphBLAS.FSharp.Backend.ArraysExtensions
56

67
type VectorFormat =
78
| COO
@@ -72,48 +73,6 @@ and ClCooVector<'a> =
7273

7374
member this.Dispose(q) = (this :> IDeviceMemObject).Dispose(q)
7475

75-
type DenseVector<'a> =
76-
{ Values: 'a option [] }
77-
78-
member this.Size = this.Values.Length
79-
80-
override this.ToString() =
81-
[ sprintf "Dense Vector\n"
82-
sprintf "Size: %i \n" this.Values.Length
83-
sprintf "Values: %A \n" this.Values ]
84-
|> String.concat ""
85-
86-
member this.ToDevice(context: ClContext) =
87-
{ ClDenseVector.Values = context.CreateClArray this.Values }
88-
89-
static member FromArray(array: 'a [], isZero: 'a -> bool) =
90-
{ Values =
91-
array
92-
|> Array.map (fun v -> if isZero v then None else Some v) }
93-
94-
and ClDenseVector<'a> =
95-
{ Values: ClArray<'a option> }
96-
97-
member this.Size = this.Values.Length
98-
99-
member this.ToHost(q: MailboxProcessor<_>) =
100-
let vector = Array.zeroCreate this.Values.Length
101-
102-
let _ =
103-
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(this.Values, vector, ch))
104-
105-
{ DenseVector.Values = vector }
106-
107-
interface IDeviceMemObject with
108-
member this.Dispose(q) =
109-
q.Post(Msg.CreateFreeMsg<_>(this.Values))
110-
q.PostAndReply(Msg.MsgNotifyMe)
111-
112-
member this.Dispose(q) = (this :> IDeviceMemObject).Dispose(q)
113-
114-
static member FromArray(context: ClContext, array: 'a option []) =
115-
{ Values = context.CreateClArray array }
116-
11776
type TuplesVector<'a> =
11877
{ Indices: int []
11978
Values: 'a []
@@ -169,7 +128,7 @@ and ClTuplesVector<'a> =
169128

170129
type Vector<'a when 'a: struct> =
171130
| VectorCOO of COOVector<'a>
172-
| VectorDense of DenseVector<'a>
131+
| VectorDense of 'a option []
173132
member this.Size =
174133
match this with
175134
| VectorCOO vector -> vector.Size
@@ -182,7 +141,7 @@ type Vector<'a when 'a: struct> =
182141

183142
and ClVector<'a when 'a: struct> =
184143
| ClVectorCOO of ClCooVector<'a>
185-
| ClVectorDense of ClDenseVector<'a>
144+
| ClVectorDense of ClArray<'a option>
186145
member this.Size =
187146
match this with
188147
| ClVectorCOO vector -> vector.Size

0 commit comments

Comments
 (0)