File: NativeArray.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 (106 lines) | stat: -rwxr-xr-x 3,647 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// (c) Microsoft Corporation 2005-2009.

namespace Microsoft.FSharp.NativeInterop

#nowarn "44"
#nowarn "9" // unverifiable constructs
#nowarn "51" // The address-of operator may result in non-verifiable code. Its use is restricted to passing byrefs to functions that require them

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

module NativeOps = 
    [<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()

[<Sealed>]
type NativeArray<'T when 'T : unmanaged>(ptr : nativeptr<'T>, len: int) =
    member x.Ptr = ptr
    [<NoDynamicInvocation>]
    member inline x.Item 
       with get n = NativePtr.get x.Ptr n
       and  set n v = NativePtr.set x.Ptr n v
    member x.Length = len

[<Sealed>]
type FortranMatrix<'T when 'T : unmanaged>(ptr : nativeptr<'T>, nrows: int, ncols:int) = 
    member x.NumCols = ncols
    member x.NumRows = nrows
    member x.Ptr = ptr
    [<NoDynamicInvocation>]
    member inline x.Item 
       with get (row,col) = NativePtr.get x.Ptr (row + col*x.NumRows)
       and  set (row,col) v = NativePtr.set x.Ptr (row + col*x.NumRows) v
    member x.NativeTranspose = new CMatrix<_>(ptr,ncols,nrows)
  
and 
  [<Sealed>]
  NativeArray2<'T when 'T : unmanaged>(ptr : nativeptr<'T>, nrows:int, ncols: int) = 
    member x.Ptr = ptr
    member x.NumRows = nrows
    member x.NumCols = ncols
    [<NoDynamicInvocation>]
    member inline x.Item 
       with get (row,col) = NativePtr.get x.Ptr (row*x.NumCols + col)
       and  set (row,col) v = NativePtr.set x.Ptr (row*x.NumCols + col) v
    member x.NativeTranspose = new FortranMatrix<_>(x.Ptr,ncols,nrows)
  
and CMatrix<'T when 'T : unmanaged> = NativeArray2<'T> 

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Ref = 
    [<NoDynamicInvocation>]
    let inline pin (ref: 'T ref) (f : nativeptr<'T> -> 'b) = 
        NativeOps.pinObj (box ref) (fun gch -> 
            f (gch.AddrOfPinnedObject() |> NativePtr.ofNativeInt))

open Microsoft.FSharp.Math

[<Sealed>]
type PinnedArray<'T when 'T : unmanaged>(narray: NativeArray<'T>, gch: GCHandle) =
    [<NoDynamicInvocation>]
    static member inline of_array(arr: 'T[]) =
        let gch = NativeOps.pinObjUnscoped (box arr) 
        let ptr = &&arr.[0]
        new PinnedArray<'T>(new NativeArray<_>(ptr,Array.length arr),gch)

    member x.Ptr = narray.Ptr
    member x.Free() = gch.Free()
    member x.Length = narray.Length
    member x.NativeArray = narray
    interface System.IDisposable with 
        member x.Dispose() = gch.Free()
        

[<Sealed>]
type PinnedArray2<'T when 'T : unmanaged>(narray: NativeArray2<'T>, gch: GCHandle) =

    [<NoDynamicInvocation>]
    static member inline of_array2(arr: 'T[,]) = 
        let gch = NativeOps.pinObjUnscoped (box arr) 
        let ptr = &&arr.[0,0]
        new PinnedArray2<'T>(new NativeArray2<_>(ptr,Array2D.length1 arr,Array2D.length2 arr),gch)

    [<NoDynamicInvocation>]
    static member inline of_array2D(arr: 'T[,]) = 
        let gch = NativeOps.pinObjUnscoped (box arr) 
        let ptr = &&arr.[0,0]
        new PinnedArray2<'T>(new NativeArray2<_>(ptr,Array2D.length1 arr,Array2D.length2 arr),gch)


    member x.Ptr = narray.Ptr
    member x.Free() = gch.Free()
    member x.NumRows = narray.NumRows
    member x.NumCols = narray.NumCols
    member x.NativeArray = narray
    interface System.IDisposable with 
        member x.Dispose() = gch.Free()