File: NativeArrayExtensions.fs

package info (click to toggle)
fsharp 3.1.1.26%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 59,244 kB
  • ctags: 4,190
  • sloc: cs: 13,398; ml: 1,098; sh: 399; makefile: 293; xml: 82
file content (51 lines) | stat: -rwxr-xr-x 1,750 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// (c) Microsoft Corporation 2005-2009.

namespace Microsoft.FSharp.NativeInterop

#nowarn "44"
#nowarn "9" // unverifiable constructs
#nowarn "51" // unverifiable constructs

open System
open System.Runtime.InteropServices
open Microsoft.FSharp.NativeInterop
open Microsoft.FSharp.Math

[<AutoOpen>]
module NativArrayExtensionsForMatrix =

    [<NoDynamicInvocation>]
    let inline pinObjUnscoped (obj: obj) =  GCHandle.Alloc(obj,GCHandleType.Pinned) 
    [<NoDynamicInvocation>]
    let inline pinObj (obj: obj) f = 
        let gch = pinObjUnscoped obj 
        try f gch
        finally
            gch.Free()

    type Microsoft.FSharp.NativeInterop.PinnedArray<'T when 'T : unmanaged> with

        [<NoDynamicInvocation>]
        static member inline of_vector(m:Vector<'T>) = 
            let gch = pinObjUnscoped (box m.InternalValues) 
            let ptr = &&m.InternalValues.[0]
            new PinnedArray<'T>(new NativeArray<_>(ptr,m.Length),gch)

        [<NoDynamicInvocation>]
        static member inline of_rowvec(m:RowVector<'T>) = 
            let gch = pinObjUnscoped (box m.InternalValues) 
            let ptr = &&m.InternalValues.[0]
            new PinnedArray<'T>(new NativeArray<_>(ptr,m.Length),gch)
            

    type Microsoft.FSharp.NativeInterop.PinnedArray2<'T when 'T : unmanaged> with

        [<NoDynamicInvocation>]
        static member inline of_matrix(m:Matrix<'T>) = 
            if m.IsDense then
                let gch = pinObjUnscoped (box m.InternalDenseValues) 
                let ptr = && m.InternalDenseValues.[0,0]
                new PinnedArray2<'T>(new NativeArray2<_>(ptr,m.NumRows,m.NumCols),gch) 
            else
                invalidArg "m" "cannot pin sparse matrices"