@@ -8,10 +8,11 @@ type VectorFormat =
88 | Dense
99
1010type 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
4744and 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
7571type 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
9490and 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+
114113type 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