@@ -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)
0 commit comments