Skip to content

Commit 9156991

Browse files
committed
ClDenseVector inherits ClArray
1 parent 6cd18ba commit 9156991

2 files changed

Lines changed: 33 additions & 30 deletions

File tree

benchmarks/GraphBLAS-sharp.Benchmarks/Helpers.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ module Utils =
239239

240240
let rowIndices2rowPointers (rowIndices: int []) rowCount =
241241
let nnzPerRow = Array.zeroCreate rowCount
242-
let rowPointers = Array.zeroCreate rowCount
242+
let rowPointers = Array.zeroCreate (rowCount + 1)
243243

244244
Array.iter (fun rowIndex -> nnzPerRow.[rowIndex] <- nnzPerRow.[rowIndex] + 1) rowIndices
245245

246-
for i in 1 .. rowCount - 1 do
246+
for i in 1 .. rowCount do
247247
rowPointers.[i] <- rowPointers.[i - 1] + nnzPerRow.[i - 1]
248248

249249
rowPointers

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

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ type VectorFormat =
88
| Dense
99

1010
type COOVector<'a> =
11-
{ Size: int
12-
Indices: int []
11+
{ Indices: int []
1312
Values: 'a [] }
1413

14+
member this.Size = this.Indices.Length
15+
1516
override this.ToString() =
1617
[ sprintf "Sparse Vector\n"
1718
sprintf "Size: %i \n" this.Size
@@ -24,14 +25,10 @@ type COOVector<'a> =
2425
let values = context.CreateClArray this.Values
2526

2627
{ Context = context
27-
Size = this.Size
2828
Indices = indices
2929
Values = values }
3030

31-
static member FromTuples(size: int, indices: int [], values: 'a []) =
32-
{ Size = size
33-
Indices = indices
34-
Values = values }
31+
static member FromTuples(indices: int [], values: 'a []) = { Indices = indices; Values = values }
3532

3633
static member FromArray(array: 'a [], isZero: 'a -> bool) =
3734
let (indices, vals) =
@@ -42,14 +39,15 @@ type COOVector<'a> =
4239
|> Array.ofSeq
4340
|> Array.unzip
4441

45-
COOVector.FromTuples(array.Length, indices, vals)
42+
COOVector.FromTuples(indices, vals)
4643

4744
and ClCooVector<'a> =
4845
{ Context: ClContext
49-
Size: int
5046
Indices: ClArray<int>
5147
Values: ClArray<'a> }
5248

49+
member this.Size = this.Indices.Length
50+
5351
member this.ToHost(q: MailboxProcessor<_>) =
5452
let indices = Array.zeroCreate this.Indices.Length
5553
let values = Array.zeroCreate this.Values.Length
@@ -60,9 +58,7 @@ and ClCooVector<'a> =
6058
let _ =
6159
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(this.Values, values, ch))
6260

63-
{ Size = this.Size
64-
Indices = indices
65-
Values = values }
61+
{ Indices = indices; Values = values }
6662

6763
interface IDeviceMemObject with
6864
member this.Dispose(q) =
@@ -73,44 +69,47 @@ and ClCooVector<'a> =
7369
member this.Dispose(q) = (this :> IDeviceMemObject).Dispose(q)
7470

7571
type DenseVector<'a> =
76-
{ Size: int
77-
Values: 'a [] }
72+
{ Values: 'a option [] }
73+
74+
member this.Size = this.Values.Length
7875

7976
override this.ToString() =
8077
[ sprintf "Dense Vector\n"
81-
sprintf "Size: %i \n" this.Size
78+
sprintf "Size: %i \n" this.Values.Length
8279
sprintf "Values: %A \n" this.Values ]
8380
|> String.concat ""
8481

8582
member this.ToDevice(context: ClContext) =
86-
let values = context.CreateClArray this.Values
83+
context.CreateClArray this.Values :?> ClDenseVector<'a>
8784

88-
{ Context = context
89-
Size = this.Size
90-
Values = values }
91-
92-
static member FromArray(array: 'a []) = { Size = array.Length; Values = array }
85+
static member FromArray(array: 'a [], isZero: 'a -> bool) =
86+
{ Values =
87+
array
88+
|> Array.map (fun v -> if isZero v then None else Some v) }
9389

9490
and ClDenseVector<'a> =
95-
{ Context: ClContext
96-
Size: int
97-
Values: ClArray<'a> }
91+
inherit ClArray<'a option>
92+
93+
member this.Size = this.Length
9894

9995
member this.ToHost(q: MailboxProcessor<_>) =
100-
let values = Array.zeroCreate this.Values.Length
96+
let vector = Array.zeroCreate this.Length
10197

10298
let _ =
103-
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(this.Values, values, ch))
99+
q.PostAndReply(fun ch -> Msg.CreateToHostMsg(this, vector, ch))
104100

105-
{ Size = this.Size; Values = values }
101+
{ Values = vector }
106102

107103
interface IDeviceMemObject with
108104
member this.Dispose(q) =
109-
q.Post(Msg.CreateFreeMsg<_>(this.Values))
105+
q.Post(Msg.CreateFreeMsg<_>(this))
110106
q.PostAndReply(Msg.MsgNotifyMe)
111107

112108
member this.Dispose(q) = (this :> IDeviceMemObject).Dispose(q)
113109

110+
static member FromArray(context: ClContext, array: 'a option []) =
111+
context.CreateClArray array :?> ClDenseVector<'a>
112+
114113
type TuplesVector<'a> =
115114
{ Indices: int []
116115
Values: 'a [] }
@@ -121,6 +120,8 @@ type TuplesVector<'a> =
121120
sprintf "Values: %A \n" this.Values ]
122121
|> String.concat ""
123122

123+
member this.Size = this.Indices.Length
124+
124125
member this.ToDevice(context: ClContext) =
125126
let indices = context.CreateClArray this.Indices
126127
let values = context.CreateClArray this.Values
@@ -136,6 +137,8 @@ and ClTuplesVector<'a> =
136137
Indices: ClArray<int>
137138
Values: ClArray<'a> }
138139

140+
member this.Size = this.Indices.Length
141+
139142
member this.ToHost(q: MailboxProcessor<_>) =
140143
let indices = Array.zeroCreate this.Indices.Length
141144
let values = Array.zeroCreate this.Values.Length

0 commit comments

Comments
 (0)