Skip to content

Commit

Permalink
[NativeTensors] Add method cache for copy utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jul 8, 2024
1 parent 6d4c148 commit 051e576
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
44 changes: 30 additions & 14 deletions src/Aardvark.Base.Tensors/Native/NativeTensorGenerated.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Aardvark.Base

open Microsoft.FSharp.NativeInterop
open System
open System.Collections.Concurrent
open System.Runtime.InteropServices
open System.Reflection

Expand Down Expand Up @@ -7849,6 +7851,8 @@ module PixImageTensorExtensions =

[<AutoOpen>]
module private CopyDispatch =
let private flags = BindingFlags.Static ||| BindingFlags.NonPublic ||| BindingFlags.Public

type private Dispatcher() =
static member CopyImageToNative<'a when 'a : unmanaged>(src : PixImage<'a>, dst : nativeint, dstInfo : VolumeInfo) =
let dst = dst |> NativeVolume.ofNativeInt dstInfo
Expand All @@ -7858,19 +7862,24 @@ module PixImageTensorExtensions =
let src = src |> NativeVolume.ofNativeInt srcInfo
dst |> copyFromNativeVolume src

module Method =
let private flags = BindingFlags.Static ||| BindingFlags.NonPublic ||| BindingFlags.Public
let copyImageToNative = typeof<Dispatcher>.GetMethod("CopyImageToNative", flags)
let copyNativeToImage = typeof<Dispatcher>.GetMethod("CopyNativeToImage", flags)

module ImageToNative =
let private def = typeof<Dispatcher>.GetMethod(nameof Dispatcher.CopyImageToNative, flags)
let private lookup = ConcurrentDictionary<Type, MethodInfo>()
let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)

module NativeToImage =
let private def = typeof<Dispatcher>.GetMethod(nameof Dispatcher.CopyNativeToImage, flags)
let private lookup = ConcurrentDictionary<Type, MethodInfo>()
let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)

/// Copies the given untyped PixImage to the given native address
let copyToNative (dst : nativeint) (dstInfo : VolumeInfo) (pix : PixImage) =
let mi = Method.copyImageToNative.MakeGenericMethod [| pix.PixFormat.Type |]
let mi = ImageToNative.getGenericMethod pix.PixFormat.Type
mi.Invoke(null, [|pix; dst; dstInfo|]) |> ignore

/// Copies from the given native address to the given untyped PixImage
let copyFromNative (src : nativeint) (srcInfo : VolumeInfo) (pix : PixImage) =
let mi = Method.copyNativeToImage.MakeGenericMethod [| pix.PixFormat.Type |]
let mi = NativeToImage.getGenericMethod pix.PixFormat.Type
mi.Invoke(null, [|src; srcInfo; pix|]) |> ignore


Expand Down Expand Up @@ -51980,6 +51989,8 @@ module PixVolumeTensorExtensions =

[<AutoOpen>]
module private CopyDispatch =
let private flags = BindingFlags.Static ||| BindingFlags.NonPublic ||| BindingFlags.Public

type private Dispatcher() =
static member CopyImageToNative<'a when 'a : unmanaged>(src : PixVolume<'a>, dst : nativeint, dstInfo : Tensor4Info) =
let dst = dst |> NativeTensor4.ofNativeInt dstInfo
Expand All @@ -51989,19 +52000,24 @@ module PixVolumeTensorExtensions =
let src = src |> NativeTensor4.ofNativeInt srcInfo
dst |> copyFromNativeTensor4 src

module Method =
let private flags = BindingFlags.Static ||| BindingFlags.NonPublic ||| BindingFlags.Public
let copyImageToNative = typeof<Dispatcher>.GetMethod("CopyImageToNative", flags)
let copyNativeToImage = typeof<Dispatcher>.GetMethod("CopyNativeToImage", flags)

module ImageToNative =
let private def = typeof<Dispatcher>.GetMethod(nameof Dispatcher.CopyImageToNative, flags)
let private lookup = ConcurrentDictionary<Type, MethodInfo>()
let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)

module NativeToImage =
let private def = typeof<Dispatcher>.GetMethod(nameof Dispatcher.CopyNativeToImage, flags)
let private lookup = ConcurrentDictionary<Type, MethodInfo>()
let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)

/// Copies the given untyped PixVolume to the given native address
let copyToNative (dst : nativeint) (dstInfo : Tensor4Info) (pix : PixVolume) =
let mi = Method.copyImageToNative.MakeGenericMethod [| pix.PixFormat.Type |]
let mi = ImageToNative.getGenericMethod pix.PixFormat.Type
mi.Invoke(null, [|pix; dst; dstInfo|]) |> ignore

/// Copies from the given native address to the given untyped PixVolume
let copyFromNative (src : nativeint) (srcInfo : Tensor4Info) (pix : PixVolume) =
let mi = Method.copyNativeToImage.MakeGenericMethod [| pix.PixFormat.Type |]
let mi = NativeToImage.getGenericMethod pix.PixFormat.Type
mi.Invoke(null, [|src; srcInfo; pix|]) |> ignore


Expand Down
28 changes: 20 additions & 8 deletions src/Aardvark.Base.Tensors/Native/NativeTensorGenerator.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,8 @@ module Generator =

line "[<AutoOpen>]"
start "module private CopyDispatch ="
line "let private flags = BindingFlags.Static ||| BindingFlags.NonPublic ||| BindingFlags.Public"
line ""
start "type private Dispatcher() ="
start "static member CopyImageToNative<'a when 'a : unmanaged>(src : %s<'a>, dst : nativeint, dstInfo : %sInfo) =" pix managedName
line "let dst = dst |> %s.ofNativeInt dstInfo" name
Expand All @@ -1311,25 +1313,33 @@ module Generator =
stop() // Dispatcher end
line ""

start "module Method ="
line "let private flags = BindingFlags.Static ||| BindingFlags.NonPublic ||| BindingFlags.Public"
line "let copyImageToNative = typeof<Dispatcher>.GetMethod(\"CopyImageToNative\", flags)"
line "let copyNativeToImage = typeof<Dispatcher>.GetMethod(\"CopyNativeToImage\", flags)"
stop() // Method end

start "module ImageToNative ="
line "let private def = typeof<Dispatcher>.GetMethod(nameof Dispatcher.CopyImageToNative, flags)"
line "let private lookup = ConcurrentDictionary<Type, MethodInfo>()"
line "let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)"
stop()
line ""

start "module NativeToImage ="
line "let private def = typeof<Dispatcher>.GetMethod(nameof Dispatcher.CopyNativeToImage, flags)"
line "let private lookup = ConcurrentDictionary<Type, MethodInfo>()"
line "let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)"
stop()
line ""

stop() // CopyDispatch end
line""

line "/// Copies the given untyped %s to the given native address" pix
start "let copyToNative (dst : nativeint) (dstInfo : %sInfo) (pix : %s) =" managedName pix
line "let mi = Method.copyImageToNative.MakeGenericMethod [| pix.PixFormat.Type |]"
line "let mi = ImageToNative.getGenericMethod pix.PixFormat.Type"
line "mi.Invoke(null, [|pix; dst; dstInfo|]) |> ignore"
stop()
line ""

line "/// Copies from the given native address to the given untyped %s" pix
start "let copyFromNative (src : nativeint) (srcInfo : %sInfo) (pix : %s) =" managedName pix
line "let mi = Method.copyNativeToImage.MakeGenericMethod [| pix.PixFormat.Type |]"
line "let mi = NativeToImage.getGenericMethod pix.PixFormat.Type"
line "mi.Invoke(null, [|src; srcInfo; pix|]) |> ignore"
stop()
line ""
Expand All @@ -1346,6 +1356,8 @@ module Generator =
line "namespace Aardvark.Base"
line ""
line "open Microsoft.FSharp.NativeInterop"
line "open System"
line "open System.Collections.Concurrent"
line "open System.Runtime.InteropServices"
line "open System.Reflection"
line ""
Expand Down

0 comments on commit 051e576

Please sign in to comment.