From 73b518b7183ce4ae816162adf924970aa57784e3 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Tue, 22 Feb 2022 19:00:07 +0800 Subject: [PATCH] feat(lite): add get physic addr interface in lite GitOrigin-RevId: e5a9eb199999a0fe7af5a0401b927f1ded42be05 --- lite/include/lite/global.h | 5 +++++ lite/lite-c/include/lite-c/global_c.h | 6 ++++++ lite/lite-c/src/global.cpp | 8 ++++++++ lite/pylite/megenginelite/global_setting.py | 10 ++++++++++ lite/pylite/test/test_global.py | 14 ++++++++++++++ lite/src/global.cpp | 13 +++++++++++++ 6 files changed, 56 insertions(+) diff --git a/lite/include/lite/global.h b/lite/include/lite/global.h index f9c70777..7fec631e 100644 --- a/lite/include/lite/global.h +++ b/lite/include/lite/global.h @@ -169,6 +169,11 @@ LITE_API bool clear_memory_pair( void* vir_ptr, void* phy_ptr, LiteDeviceType device, LiteBackend backend = LiteBackend::LITE_DEFAULT); +/** + * get the physic address by the virtual address in mge. + */ +void* lookup_physic_ptr(void* vir_ptr, LiteDeviceType device, LiteBackend backend); + } // namespace lite // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/lite-c/include/lite-c/global_c.h b/lite/lite-c/include/lite-c/global_c.h index b270e590..76bbad61 100644 --- a/lite/lite-c/include/lite-c/global_c.h +++ b/lite/lite-c/include/lite-c/global_c.h @@ -175,6 +175,12 @@ LITE_API int LITE_register_memory_pair( LITE_API int LITE_clear_memory_pair( void* vir_ptr, void* phy_ptr, LiteDeviceType device, LiteBackend backend); +/** + * get the physical by the virtual address pair in mge. + */ +LITE_API int LITE_lookup_physic_ptr( + void* vir_ptr, void** phy_ptr, LiteDeviceType device, LiteBackend backend); + #ifdef __cplusplus } #endif diff --git a/lite/lite-c/src/global.cpp b/lite/lite-c/src/global.cpp index ff14113c..74638220 100644 --- a/lite/lite-c/src/global.cpp +++ b/lite/lite-c/src/global.cpp @@ -204,4 +204,12 @@ int LITE_clear_memory_pair( LITE_CAPI_END(); } +int LITE_lookup_physic_ptr( + void* vir_ptr, void** phy_ptr, LiteDeviceType device, LiteBackend backend) { + LITE_CAPI_BEGIN(); + LITE_ASSERT(vir_ptr && phy_ptr, "The ptr pass to vir and phy is nullptr"); + *phy_ptr = lite::lookup_physic_ptr(vir_ptr, device, backend); + LITE_CAPI_END(); +} + // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}} diff --git a/lite/pylite/megenginelite/global_setting.py b/lite/pylite/megenginelite/global_setting.py index 89615e6b..3d734b6b 100644 --- a/lite/pylite/megenginelite/global_setting.py +++ b/lite/pylite/megenginelite/global_setting.py @@ -44,6 +44,7 @@ class _GlobalAPI(_LiteCObjBase): ("LITE_dump_tensor_rt_cache", [c_char_p]), ("LITE_register_memory_pair", [c_void_p, c_void_p, c_size_t, c_int, c_int]), ("LITE_clear_memory_pair", [c_void_p, c_void_p, c_int, c_int]), + ("LITE_lookup_physic_ptr", [c_void_p, POINTER(c_void_p), c_int, c_int]), ] @@ -141,3 +142,12 @@ class LiteGlobal(object): phy_ptr, c_void_p ), "clear memory pair only accept c_void_p type." LiteGlobal._api.LITE_clear_memory_pair(vir_ptr, phy_ptr, device, backend) + + @staticmethod + def lookup_physic_ptr(vir_ptr, device, backend=LiteBackend.LITE_DEFAULT): + assert isinstance( + vir_ptr, c_void_p + ), "lookup physic ptr only accept c_void_p type." + mem = c_void_p() + LiteGlobal._api.LITE_lookup_physic_ptr(vir_ptr, byref(mem), device, backend) + return mem diff --git a/lite/pylite/test/test_global.py b/lite/pylite/test/test_global.py index b30ae9bd..ba8b537c 100644 --- a/lite/pylite/test/test_global.py +++ b/lite/pylite/test/test_global.py @@ -9,6 +9,7 @@ import os import unittest +from ctypes import * import numpy as np @@ -71,3 +72,16 @@ class TestGlobal(TestShuffleNet): network.load(model_path) self.do_forward(network) + + def test_set_get_memory_pair(self): + if LiteGlobal.get_device_count(LiteDeviceType.LITE_AX) > 0: + arr1 = np.ones([2, 3]) + arr2 = np.ones([2, 3]) + vir_ptr = arr1.ctypes.data_as(c_void_p) + phy_ptr = arr2.ctypes.data_as(c_void_p) + LiteGlobal.register_memory_pair( + vir_ptr, phy_ptr, 10, LiteDeviceType.LITE_AX + ) + phy_ptr2 = LiteGlobal.lookup_physic_ptr(vir_ptr, LiteDeviceType.LITE_AX) + assert phy_ptr.value == phy_ptr2.value + LiteGlobal.clear_memory_pair(vir_ptr, phy_ptr, LiteDeviceType.LITE_AX) diff --git a/lite/src/global.cpp b/lite/src/global.cpp index 9f3e9fab..f436bd70 100644 --- a/lite/src/global.cpp +++ b/lite/src/global.cpp @@ -232,6 +232,14 @@ bool lite::clear_memory_pair( LITE_THROW("clear_memory_pair is not implement yet!"); } +void* lite::lookup_physic_ptr( + void* vir_ptr, LiteDeviceType device, LiteBackend backend) { + LITE_MARK_USED_VAR(vir_ptr); + LITE_MARK_USED_VAR(device); + LITE_MARK_USED_VAR(backend); + LITE_THROW("lookup_physic_ptr is not implement yet!"); +} + #else // LITE_BUILD_WITH_MGE void lite::try_coalesce_all_free_memory() {} @@ -266,6 +274,11 @@ bool lite::clear_memory_pair( void* vir_ptr, void* phy_ptr, LiteDeviceType device, LiteBackend beckend) { LITE_THROW("clear_memory_pair is not implement yet!"); } + +void* lite::lookup_physic_ptr( + void* vir_ptr, LiteDeviceType device, LiteBackend beckend) { + LITE_THROW("lookup_physic_ptr is not implement yet!"); +} #endif namespace lite { REGIST_DECRYPTION_METHOD( -- GitLab