From 051e576e1907c6e08751a0305bce4eaf74f30dfa Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 8 Jul 2024 16:31:31 +0200 Subject: [PATCH] [NativeTensors] Add method cache for copy utilities --- .../Native/NativeTensorGenerated.fs | 44 +++++++++++++------ .../Native/NativeTensorGenerator.fsx | 28 ++++++++---- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/Aardvark.Base.Tensors/Native/NativeTensorGenerated.fs b/src/Aardvark.Base.Tensors/Native/NativeTensorGenerated.fs index bda7c74c..401e6e38 100644 --- a/src/Aardvark.Base.Tensors/Native/NativeTensorGenerated.fs +++ b/src/Aardvark.Base.Tensors/Native/NativeTensorGenerated.fs @@ -1,6 +1,8 @@ namespace Aardvark.Base open Microsoft.FSharp.NativeInterop +open System +open System.Collections.Concurrent open System.Runtime.InteropServices open System.Reflection @@ -7849,6 +7851,8 @@ module PixImageTensorExtensions = [] 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 @@ -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.GetMethod("CopyImageToNative", flags) - let copyNativeToImage = typeof.GetMethod("CopyNativeToImage", flags) - + module ImageToNative = + let private def = typeof.GetMethod(nameof Dispatcher.CopyImageToNative, flags) + let private lookup = ConcurrentDictionary() + let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t) + + module NativeToImage = + let private def = typeof.GetMethod(nameof Dispatcher.CopyNativeToImage, flags) + let private lookup = ConcurrentDictionary() + 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 @@ -51980,6 +51989,8 @@ module PixVolumeTensorExtensions = [] 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 @@ -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.GetMethod("CopyImageToNative", flags) - let copyNativeToImage = typeof.GetMethod("CopyNativeToImage", flags) - + module ImageToNative = + let private def = typeof.GetMethod(nameof Dispatcher.CopyImageToNative, flags) + let private lookup = ConcurrentDictionary() + let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t) + + module NativeToImage = + let private def = typeof.GetMethod(nameof Dispatcher.CopyNativeToImage, flags) + let private lookup = ConcurrentDictionary() + 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 diff --git a/src/Aardvark.Base.Tensors/Native/NativeTensorGenerator.fsx b/src/Aardvark.Base.Tensors/Native/NativeTensorGenerator.fsx index 1a9f2796..8990eae4 100644 --- a/src/Aardvark.Base.Tensors/Native/NativeTensorGenerator.fsx +++ b/src/Aardvark.Base.Tensors/Native/NativeTensorGenerator.fsx @@ -1296,6 +1296,8 @@ module Generator = line "[]" 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 @@ -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.GetMethod(\"CopyImageToNative\", flags)" - line "let copyNativeToImage = typeof.GetMethod(\"CopyNativeToImage\", flags)" - stop() // Method end + + start "module ImageToNative =" + line "let private def = typeof.GetMethod(nameof Dispatcher.CopyImageToNative, flags)" + line "let private lookup = ConcurrentDictionary()" + line "let getGenericMethod (t: Type) = lookup.GetOrAdd(t, fun t -> def.MakeGenericMethod t)" + stop() + line "" + + start "module NativeToImage =" + line "let private def = typeof.GetMethod(nameof Dispatcher.CopyNativeToImage, flags)" + line "let private lookup = ConcurrentDictionary()" + 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 "" @@ -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 ""