diff --git a/examples/collector/README.md b/examples/collector/README.md index aaf5eed32a..fbdf18a8ae 100644 --- a/examples/collector/README.md +++ b/examples/collector/README.md @@ -19,7 +19,7 @@ $ mkdir build $ cd build $ cmake .. -DUR_ENABLE_TRACING=ON $ make -$ UR_ADAPTERS_FORCE_LOAD=./lib/libur_adapter_null.so XPTI_TRACE_ENABLE=1 XPTI_FRAMEWORK_DISPATCHER=./lib/libxptifw.so XPTI_SUBSCRIBERS=./lib/libcollector.so ./bin/hello_world +$ UR_ADAPTERS_FORCE_LOAD=./lib/libur_adapter_null.so UR_ENABLE_LAYERS=UR_LAYER_TRACING XPTI_TRACE_ENABLE=1 XPTI_FRAMEWORK_DISPATCHER=./lib/libxptifw.so XPTI_SUBSCRIBERS=./lib/libcollector.so ./bin/hello_world ``` See [XPTI framework documentation](https://github.com/intel/llvm/blob/sycl/xptifw/doc/XPTI_Framework.md) for more information. diff --git a/examples/collector/collector.cpp b/examples/collector/collector.cpp index 6f2f6d57b1..ba60b083d2 100644 --- a/examples/collector/collector.cpp +++ b/examples/collector/collector.cpp @@ -34,15 +34,23 @@ constexpr uint16_t TRACE_FN_END = constexpr std::string_view UR_STREAM_NAME = "ur"; /** - * @brief Formats the function parameters and arguments for urInit + * @brief Formats the function parameters and arguments for urAdapterGet */ std::ostream &operator<<(std::ostream &os, - const struct ur_init_params_t *params) { - os << ".device_flags = "; - if (*params->pdevice_flags & UR_DEVICE_INIT_FLAG_GPU) { - os << "UR_DEVICE_INIT_FLAG_GPU"; - } else { - os << "0"; + const struct ur_adapter_get_params_t *params) { + os << ".NumEntries = "; + os << *params->pNumEntries; + os << ", "; + os << ".phAdapters = "; + os << *params->pphAdapters; + if (*params->pphAdapters) { + os << " (" << **params->pphAdapters << ")"; + } + os << ", "; + os << ".pNumAdapters = "; + os << *params->ppNumAdapters; + if (*params->ppNumAdapters) { + os << " (" << **params->ppNumAdapters << ")"; } os << ""; return os; @@ -50,16 +58,17 @@ std::ostream &operator<<(std::ostream &os, /** * A map of functions that format the parameters and arguments for each UR function. - * This example only implements a handler for one function, `urInit`, but it's + * This example only implements a handler for one function, `urAdapterGet`, but it's * trivial to expand it to support more. */ static std::unordered_map< std::string_view, std::function> - handlers = {{"urInit", [](const xpti::function_with_args_t *fn_args, - std::ostream &os) { - auto params = static_cast( - fn_args->args_data); + handlers = {{"urAdapterGet", [](const xpti::function_with_args_t *fn_args, + std::ostream &os) { + auto params = + static_cast( + fn_args->args_data); os << params; }}}; diff --git a/examples/hello_world/hello_world.cpp b/examples/hello_world/hello_world.cpp index 4d903da65a..11e5374390 100644 --- a/examples/hello_world/hello_world.cpp +++ b/examples/hello_world/hello_world.cpp @@ -19,9 +19,10 @@ int main(int argc, char *argv[]) { ur_result_t status; // Initialize the platform - status = urInit(0, nullptr); + status = urLoaderInit(0, nullptr); if (status != UR_RESULT_SUCCESS) { - std::cout << "urInit failed with return code: " << status << std::endl; + std::cout << "urLoaderInit failed with return code: " << status + << std::endl; return 1; } std::cout << "Platform initialized.\n"; @@ -119,6 +120,6 @@ int main(int argc, char *argv[]) { for (auto adapter : adapters) { urAdapterRelease(adapter); } - urTearDown(nullptr); + urLoaderTearDown(); return status == UR_RESULT_SUCCESS ? 0 : 1; } diff --git a/include/ur.py b/include/ur.py index f606ce4127..794e92e800 100644 --- a/include/ur.py +++ b/include/ur.py @@ -117,8 +117,6 @@ class ur_function_v(IntEnum): QUEUE_CREATE_WITH_NATIVE_HANDLE = 96 ## Enumerator for ::urQueueCreateWithNativeHandle QUEUE_FINISH = 97 ## Enumerator for ::urQueueFinish QUEUE_FLUSH = 98 ## Enumerator for ::urQueueFlush - INIT = 99 ## Enumerator for ::urInit - TEAR_DOWN = 100 ## Enumerator for ::urTearDown SAMPLER_CREATE = 101 ## Enumerator for ::urSamplerCreate SAMPLER_RETAIN = 102 ## Enumerator for ::urSamplerRetain SAMPLER_RELEASE = 103 ## Enumerator for ::urSamplerRelease @@ -196,6 +194,8 @@ class ur_function_v(IntEnum): ADAPTER_RETAIN = 179 ## Enumerator for ::urAdapterRetain ADAPTER_GET_LAST_ERROR = 180 ## Enumerator for ::urAdapterGetLastError ADAPTER_GET_INFO = 181 ## Enumerator for ::urAdapterGetInfo + LOADER_INIT = 182 ## Enumerator for ::urLoaderInit + LOADER_TEAR_DOWN = 183 ## Enumerator for ::urLoaderTearDown class ur_function_t(c_int): def __str__(self): @@ -484,32 +484,6 @@ class ur_rect_region_t(Structure): ("depth", c_ulonglong) ## [in] scalar (scalar) ] -############################################################################### -## @brief Supported device initialization flags -class ur_device_init_flags_v(IntEnum): - GPU = UR_BIT(0) ## initialize GPU device adapters. - CPU = UR_BIT(1) ## initialize CPU device adapters. - FPGA = UR_BIT(2) ## initialize FPGA device adapters. - MCA = UR_BIT(3) ## initialize MCA device adapters. - VPU = UR_BIT(4) ## initialize VPU device adapters. - -class ur_device_init_flags_t(c_int): - def __str__(self): - return hex(self.value) - - -############################################################################### -## @brief Supported loader info -class ur_loader_config_info_v(IntEnum): - AVAILABLE_LAYERS = 0 ## [char[]] Null-terminated, semi-colon separated list of available - ## layers. - REFERENCE_COUNT = 1 ## [uint32_t] Reference count of the loader config object. - -class ur_loader_config_info_t(c_int): - def __str__(self): - return str(ur_loader_config_info_v(self.value)) - - ############################################################################### ## @brief Supported adapter info class ur_adapter_info_v(IntEnum): @@ -540,6 +514,32 @@ def __str__(self): return str(ur_adapter_backend_v(self.value)) +############################################################################### +## @brief Supported device initialization flags +class ur_device_init_flags_v(IntEnum): + GPU = UR_BIT(0) ## initialize GPU device adapters. + CPU = UR_BIT(1) ## initialize CPU device adapters. + FPGA = UR_BIT(2) ## initialize FPGA device adapters. + MCA = UR_BIT(3) ## initialize MCA device adapters. + VPU = UR_BIT(4) ## initialize VPU device adapters. + +class ur_device_init_flags_t(c_int): + def __str__(self): + return hex(self.value) + + +############################################################################### +## @brief Supported loader info +class ur_loader_config_info_v(IntEnum): + AVAILABLE_LAYERS = 0 ## [char[]] Null-terminated, semi-colon separated list of available + ## layers. + REFERENCE_COUNT = 1 ## [uint32_t] Reference count of the loader config object. + +class ur_loader_config_info_t(c_int): + def __str__(self): + return str(ur_loader_config_info_v(self.value)) + + ############################################################################### ## @brief Supported platform info class ur_platform_info_v(IntEnum): @@ -2870,6 +2870,53 @@ class ur_physical_mem_dditable_t(Structure): ("pfnRelease", c_void_p) ## _urPhysicalMemRelease_t ] +############################################################################### +## @brief Function-pointer for urAdapterGet +if __use_win_types: + _urAdapterGet_t = WINFUNCTYPE( ur_result_t, c_ulong, POINTER(ur_adapter_handle_t), POINTER(c_ulong) ) +else: + _urAdapterGet_t = CFUNCTYPE( ur_result_t, c_ulong, POINTER(ur_adapter_handle_t), POINTER(c_ulong) ) + +############################################################################### +## @brief Function-pointer for urAdapterRelease +if __use_win_types: + _urAdapterRelease_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t ) +else: + _urAdapterRelease_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t ) + +############################################################################### +## @brief Function-pointer for urAdapterRetain +if __use_win_types: + _urAdapterRetain_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t ) +else: + _urAdapterRetain_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t ) + +############################################################################### +## @brief Function-pointer for urAdapterGetLastError +if __use_win_types: + _urAdapterGetLastError_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t, POINTER(c_char_p), POINTER(c_long) ) +else: + _urAdapterGetLastError_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t, POINTER(c_char_p), POINTER(c_long) ) + +############################################################################### +## @brief Function-pointer for urAdapterGetInfo +if __use_win_types: + _urAdapterGetInfo_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t, ur_adapter_info_t, c_size_t, c_void_p, POINTER(c_size_t) ) +else: + _urAdapterGetInfo_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t, ur_adapter_info_t, c_size_t, c_void_p, POINTER(c_size_t) ) + + +############################################################################### +## @brief Table of Global functions pointers +class ur_global_dditable_t(Structure): + _fields_ = [ + ("pfnAdapterGet", c_void_p), ## _urAdapterGet_t + ("pfnAdapterRelease", c_void_p), ## _urAdapterRelease_t + ("pfnAdapterRetain", c_void_p), ## _urAdapterRetain_t + ("pfnAdapterGetLastError", c_void_p), ## _urAdapterGetLastError_t + ("pfnAdapterGetInfo", c_void_p) ## _urAdapterGetInfo_t + ] + ############################################################################### ## @brief Function-pointer for urEnqueueKernelLaunch if __use_win_types: @@ -3543,69 +3590,6 @@ class ur_usm_p2p_exp_dditable_t(Structure): ("pfnPeerAccessGetInfoExp", c_void_p) ## _urUsmP2PPeerAccessGetInfoExp_t ] -############################################################################### -## @brief Function-pointer for urInit -if __use_win_types: - _urInit_t = WINFUNCTYPE( ur_result_t, ur_device_init_flags_t, ur_loader_config_handle_t ) -else: - _urInit_t = CFUNCTYPE( ur_result_t, ur_device_init_flags_t, ur_loader_config_handle_t ) - -############################################################################### -## @brief Function-pointer for urTearDown -if __use_win_types: - _urTearDown_t = WINFUNCTYPE( ur_result_t, c_void_p ) -else: - _urTearDown_t = CFUNCTYPE( ur_result_t, c_void_p ) - -############################################################################### -## @brief Function-pointer for urAdapterGet -if __use_win_types: - _urAdapterGet_t = WINFUNCTYPE( ur_result_t, c_ulong, POINTER(ur_adapter_handle_t), POINTER(c_ulong) ) -else: - _urAdapterGet_t = CFUNCTYPE( ur_result_t, c_ulong, POINTER(ur_adapter_handle_t), POINTER(c_ulong) ) - -############################################################################### -## @brief Function-pointer for urAdapterRelease -if __use_win_types: - _urAdapterRelease_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t ) -else: - _urAdapterRelease_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t ) - -############################################################################### -## @brief Function-pointer for urAdapterRetain -if __use_win_types: - _urAdapterRetain_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t ) -else: - _urAdapterRetain_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t ) - -############################################################################### -## @brief Function-pointer for urAdapterGetLastError -if __use_win_types: - _urAdapterGetLastError_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t, POINTER(c_char_p), POINTER(c_long) ) -else: - _urAdapterGetLastError_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t, POINTER(c_char_p), POINTER(c_long) ) - -############################################################################### -## @brief Function-pointer for urAdapterGetInfo -if __use_win_types: - _urAdapterGetInfo_t = WINFUNCTYPE( ur_result_t, ur_adapter_handle_t, ur_adapter_info_t, c_size_t, c_void_p, POINTER(c_size_t) ) -else: - _urAdapterGetInfo_t = CFUNCTYPE( ur_result_t, ur_adapter_handle_t, ur_adapter_info_t, c_size_t, c_void_p, POINTER(c_size_t) ) - - -############################################################################### -## @brief Table of Global functions pointers -class ur_global_dditable_t(Structure): - _fields_ = [ - ("pfnInit", c_void_p), ## _urInit_t - ("pfnTearDown", c_void_p), ## _urTearDown_t - ("pfnAdapterGet", c_void_p), ## _urAdapterGet_t - ("pfnAdapterRelease", c_void_p), ## _urAdapterRelease_t - ("pfnAdapterRetain", c_void_p), ## _urAdapterRetain_t - ("pfnAdapterGetLastError", c_void_p), ## _urAdapterGetLastError_t - ("pfnAdapterGetInfo", c_void_p) ## _urAdapterGetInfo_t - ] - ############################################################################### ## @brief Function-pointer for urVirtualMemGranularityGetInfo if __use_win_types: @@ -3759,6 +3743,7 @@ class ur_dditable_t(Structure): ("Sampler", ur_sampler_dditable_t), ("Mem", ur_mem_dditable_t), ("PhysicalMem", ur_physical_mem_dditable_t), + ("Global", ur_global_dditable_t), ("Enqueue", ur_enqueue_dditable_t), ("Queue", ur_queue_dditable_t), ("BindlessImagesExp", ur_bindless_images_exp_dditable_t), @@ -3766,7 +3751,6 @@ class ur_dditable_t(Structure): ("USMExp", ur_usm_exp_dditable_t), ("CommandBufferExp", ur_command_buffer_exp_dditable_t), ("UsmP2PExp", ur_usm_p2p_exp_dditable_t), - ("Global", ur_global_dditable_t), ("VirtualMem", ur_virtual_mem_dditable_t), ("Device", ur_device_dditable_t) ] @@ -3785,7 +3769,7 @@ def __init__(self, version : ur_api_version_t): self.__dditable = ur_dditable_t() # initialize the UR - self.__dll.urInit(0, 0) + self.__dll.urLoaderInit(0, 0) # call driver to get function pointers Platform = ur_platform_dditable_t() @@ -3927,6 +3911,20 @@ def __init__(self, version : ur_api_version_t): self.urPhysicalMemRetain = _urPhysicalMemRetain_t(self.__dditable.PhysicalMem.pfnRetain) self.urPhysicalMemRelease = _urPhysicalMemRelease_t(self.__dditable.PhysicalMem.pfnRelease) + # call driver to get function pointers + Global = ur_global_dditable_t() + r = ur_result_v(self.__dll.urGetGlobalProcAddrTable(version, byref(Global))) + if r != ur_result_v.SUCCESS: + raise Exception(r) + self.__dditable.Global = Global + + # attach function interface to function address + self.urAdapterGet = _urAdapterGet_t(self.__dditable.Global.pfnAdapterGet) + self.urAdapterRelease = _urAdapterRelease_t(self.__dditable.Global.pfnAdapterRelease) + self.urAdapterRetain = _urAdapterRetain_t(self.__dditable.Global.pfnAdapterRetain) + self.urAdapterGetLastError = _urAdapterGetLastError_t(self.__dditable.Global.pfnAdapterGetLastError) + self.urAdapterGetInfo = _urAdapterGetInfo_t(self.__dditable.Global.pfnAdapterGetInfo) + # call driver to get function pointers Enqueue = ur_enqueue_dditable_t() r = ur_result_v(self.__dll.urGetEnqueueProcAddrTable(version, byref(Enqueue))) @@ -4068,22 +4066,6 @@ def __init__(self, version : ur_api_version_t): self.urUsmP2PDisablePeerAccessExp = _urUsmP2PDisablePeerAccessExp_t(self.__dditable.UsmP2PExp.pfnDisablePeerAccessExp) self.urUsmP2PPeerAccessGetInfoExp = _urUsmP2PPeerAccessGetInfoExp_t(self.__dditable.UsmP2PExp.pfnPeerAccessGetInfoExp) - # call driver to get function pointers - Global = ur_global_dditable_t() - r = ur_result_v(self.__dll.urGetGlobalProcAddrTable(version, byref(Global))) - if r != ur_result_v.SUCCESS: - raise Exception(r) - self.__dditable.Global = Global - - # attach function interface to function address - self.urInit = _urInit_t(self.__dditable.Global.pfnInit) - self.urTearDown = _urTearDown_t(self.__dditable.Global.pfnTearDown) - self.urAdapterGet = _urAdapterGet_t(self.__dditable.Global.pfnAdapterGet) - self.urAdapterRelease = _urAdapterRelease_t(self.__dditable.Global.pfnAdapterRelease) - self.urAdapterRetain = _urAdapterRetain_t(self.__dditable.Global.pfnAdapterRetain) - self.urAdapterGetLastError = _urAdapterGetLastError_t(self.__dditable.Global.pfnAdapterGetLastError) - self.urAdapterGetInfo = _urAdapterGetInfo_t(self.__dditable.Global.pfnAdapterGetInfo) - # call driver to get function pointers VirtualMem = ur_virtual_mem_dditable_t() r = ur_result_v(self.__dll.urGetVirtualMemProcAddrTable(version, byref(VirtualMem))) diff --git a/include/ur_api.h b/include/ur_api.h index c9f29a1535..a3bf3da66e 100644 --- a/include/ur_api.h +++ b/include/ur_api.h @@ -126,8 +126,6 @@ typedef enum ur_function_t { UR_FUNCTION_QUEUE_CREATE_WITH_NATIVE_HANDLE = 96, ///< Enumerator for ::urQueueCreateWithNativeHandle UR_FUNCTION_QUEUE_FINISH = 97, ///< Enumerator for ::urQueueFinish UR_FUNCTION_QUEUE_FLUSH = 98, ///< Enumerator for ::urQueueFlush - UR_FUNCTION_INIT = 99, ///< Enumerator for ::urInit - UR_FUNCTION_TEAR_DOWN = 100, ///< Enumerator for ::urTearDown UR_FUNCTION_SAMPLER_CREATE = 101, ///< Enumerator for ::urSamplerCreate UR_FUNCTION_SAMPLER_RETAIN = 102, ///< Enumerator for ::urSamplerRetain UR_FUNCTION_SAMPLER_RELEASE = 103, ///< Enumerator for ::urSamplerRelease @@ -205,6 +203,8 @@ typedef enum ur_function_t { UR_FUNCTION_ADAPTER_RETAIN = 179, ///< Enumerator for ::urAdapterRetain UR_FUNCTION_ADAPTER_GET_LAST_ERROR = 180, ///< Enumerator for ::urAdapterGetLastError UR_FUNCTION_ADAPTER_GET_INFO = 181, ///< Enumerator for ::urAdapterGetInfo + UR_FUNCTION_LOADER_INIT = 182, ///< Enumerator for ::urLoaderInit + UR_FUNCTION_LOADER_TEAR_DOWN = 183, ///< Enumerator for ::urLoaderTearDown /// @cond UR_FUNCTION_FORCE_UINT32 = 0x7fffffff /// @endcond @@ -515,206 +515,10 @@ typedef struct ur_rect_region_t { #if !defined(__GNUC__) #pragma endregion #endif -// Intel 'oneAPI' Unified Runtime APIs for Runtime +// Intel 'oneAPI' Unified Runtime APIs for Adapter #if !defined(__GNUC__) -#pragma region runtime +#pragma region adapter #endif -/////////////////////////////////////////////////////////////////////////////// -/// @brief Supported device initialization flags -typedef uint32_t ur_device_init_flags_t; -typedef enum ur_device_init_flag_t { - UR_DEVICE_INIT_FLAG_GPU = UR_BIT(0), ///< initialize GPU device adapters. - UR_DEVICE_INIT_FLAG_CPU = UR_BIT(1), ///< initialize CPU device adapters. - UR_DEVICE_INIT_FLAG_FPGA = UR_BIT(2), ///< initialize FPGA device adapters. - UR_DEVICE_INIT_FLAG_MCA = UR_BIT(3), ///< initialize MCA device adapters. - UR_DEVICE_INIT_FLAG_VPU = UR_BIT(4), ///< initialize VPU device adapters. - /// @cond - UR_DEVICE_INIT_FLAG_FORCE_UINT32 = 0x7fffffff - /// @endcond - -} ur_device_init_flag_t; -/// @brief Bit Mask for validating ur_device_init_flags_t -#define UR_DEVICE_INIT_FLAGS_MASK 0xffffffe0 - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Create a loader config object. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == phLoaderConfig` -UR_APIEXPORT ur_result_t UR_APICALL -urLoaderConfigCreate( - ur_loader_config_handle_t *phLoaderConfig ///< [out] Pointer to handle of loader config object created. -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Get a reference to the loader config object. -/// -/// @details -/// - Get a reference to the loader config handle. Increment its reference -/// count -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -UR_APIEXPORT ur_result_t UR_APICALL -urLoaderConfigRetain( - ur_loader_config_handle_t hLoaderConfig ///< [in] loader config handle to retain -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Release config handle. -/// -/// @details -/// - Decrement reference count and destroy the config handle if reference -/// count becomes zero. -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -UR_APIEXPORT ur_result_t UR_APICALL -urLoaderConfigRelease( - ur_loader_config_handle_t hLoaderConfig ///< [in] config handle to release -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Supported loader info -typedef enum ur_loader_config_info_t { - UR_LOADER_CONFIG_INFO_AVAILABLE_LAYERS = 0, ///< [char[]] Null-terminated, semi-colon separated list of available - ///< layers. - UR_LOADER_CONFIG_INFO_REFERENCE_COUNT = 1, ///< [uint32_t] Reference count of the loader config object. - /// @cond - UR_LOADER_CONFIG_INFO_FORCE_UINT32 = 0x7fffffff - /// @endcond - -} ur_loader_config_info_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Retrieves various information about the loader. -/// -/// @details -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION -/// + `::UR_LOADER_CONFIG_INFO_REFERENCE_COUNT < propName` -/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION -/// + If `propName` is not supported by the loader. -/// - ::UR_RESULT_ERROR_INVALID_SIZE -/// + `propSize == 0 && pPropValue != NULL` -/// + If `propSize` is less than the real number of bytes needed to return the info. -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `propSize != 0 && pPropValue == NULL` -/// + `pPropValue == NULL && pPropSizeRet == NULL` -/// - ::UR_RESULT_ERROR_INVALID_DEVICE -/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -UR_APIEXPORT ur_result_t UR_APICALL -urLoaderConfigGetInfo( - ur_loader_config_handle_t hLoaderConfig, ///< [in] handle of the loader config object - ur_loader_config_info_t propName, ///< [in] type of the info to retrieve - size_t propSize, ///< [in] the number of bytes pointed to by pPropValue. - void *pPropValue, ///< [out][optional][typename(propName, propSize)] array of bytes holding - ///< the info. - ///< If propSize is not equal to or greater than the real number of bytes - ///< needed to return the info - ///< then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and - ///< pPropValue is not used. - size_t *pPropSizeRet ///< [out][optional] pointer to the actual size in bytes of the queried propName. -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Enable a layer for the specified loader config. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == pLayerName` -/// - ::UR_RESULT_ERROR_LAYER_NOT_PRESENT -/// + If layer specified with `pLayerName` can't be found by the loader. -UR_APIEXPORT ur_result_t UR_APICALL -urLoaderConfigEnableLayer( - ur_loader_config_handle_t hLoaderConfig, ///< [in] Handle to config object the layer will be enabled for. - const char *pLayerName ///< [in] Null terminated string containing the name of the layer to - ///< enable. -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Initialize the 'oneAPI' adapter(s) -/// -/// @details -/// - The application must call this function before calling any other -/// function. -/// - If this function is not called then all other functions will return -/// ::UR_RESULT_ERROR_UNINITIALIZED. -/// - Only one instance of each adapter will be initialized per process. -/// - The application may call this function multiple times with different -/// flags or environment variables enabled. -/// - The application must call this function after forking new processes. -/// Each forked process must call this function. -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function must be thread-safe for scenarios -/// where multiple libraries may initialize the adapter(s) simultaneously. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION -/// + `::UR_DEVICE_INIT_FLAGS_MASK & device_flags` -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -UR_APIEXPORT ur_result_t UR_APICALL -urInit( - ur_device_init_flags_t device_flags, ///< [in] device initialization flags. - ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. - ur_loader_config_handle_t hLoaderConfig ///< [in][optional] Handle of loader config handle. -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Tear down the 'oneAPI' instance and release all its resources -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == pParams` -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -UR_APIEXPORT ur_result_t UR_APICALL -urTearDown( - void *pParams ///< [in] pointer to tear down parameters -); - /////////////////////////////////////////////////////////////////////////////// /// @brief Retrieves all available adapters /// @@ -753,7 +557,9 @@ urAdapterGet( /// /// @details /// - When the reference count of the adapter reaches zero, the adapter may -/// perform adapter-specififc resource teardown +/// perform adapter-specififc resource teardown. Resources must be left in +/// a state where it safe for the adapter to be subsequently reinitialized +/// with ::urAdapterGet /// /// @returns /// - ::UR_RESULT_SUCCESS @@ -906,6 +712,206 @@ typedef enum ur_adapter_backend_t { } ur_adapter_backend_t; +#if !defined(__GNUC__) +#pragma endregion +#endif +// Intel 'oneAPI' Unified Runtime APIs for Loader +#if !defined(__GNUC__) +#pragma region loader +#endif +/////////////////////////////////////////////////////////////////////////////// +/// @brief Supported device initialization flags +typedef uint32_t ur_device_init_flags_t; +typedef enum ur_device_init_flag_t { + UR_DEVICE_INIT_FLAG_GPU = UR_BIT(0), ///< initialize GPU device adapters. + UR_DEVICE_INIT_FLAG_CPU = UR_BIT(1), ///< initialize CPU device adapters. + UR_DEVICE_INIT_FLAG_FPGA = UR_BIT(2), ///< initialize FPGA device adapters. + UR_DEVICE_INIT_FLAG_MCA = UR_BIT(3), ///< initialize MCA device adapters. + UR_DEVICE_INIT_FLAG_VPU = UR_BIT(4), ///< initialize VPU device adapters. + /// @cond + UR_DEVICE_INIT_FLAG_FORCE_UINT32 = 0x7fffffff + /// @endcond + +} ur_device_init_flag_t; +/// @brief Bit Mask for validating ur_device_init_flags_t +#define UR_DEVICE_INIT_FLAGS_MASK 0xffffffe0 + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Create a loader config object. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NULL == phLoaderConfig` +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderConfigCreate( + ur_loader_config_handle_t *phLoaderConfig ///< [out] Pointer to handle of loader config object created. +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Get a reference to the loader config object. +/// +/// @details +/// - Get a reference to the loader config handle. Increment its reference +/// count +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderConfigRetain( + ur_loader_config_handle_t hLoaderConfig ///< [in] loader config handle to retain +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Release config handle. +/// +/// @details +/// - Decrement reference count and destroy the config handle if reference +/// count becomes zero. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderConfigRelease( + ur_loader_config_handle_t hLoaderConfig ///< [in] config handle to release +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Supported loader info +typedef enum ur_loader_config_info_t { + UR_LOADER_CONFIG_INFO_AVAILABLE_LAYERS = 0, ///< [char[]] Null-terminated, semi-colon separated list of available + ///< layers. + UR_LOADER_CONFIG_INFO_REFERENCE_COUNT = 1, ///< [uint32_t] Reference count of the loader config object. + /// @cond + UR_LOADER_CONFIG_INFO_FORCE_UINT32 = 0x7fffffff + /// @endcond + +} ur_loader_config_info_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Retrieves various information about the loader. +/// +/// @details +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION +/// + `::UR_LOADER_CONFIG_INFO_REFERENCE_COUNT < propName` +/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION +/// + If `propName` is not supported by the loader. +/// - ::UR_RESULT_ERROR_INVALID_SIZE +/// + `propSize == 0 && pPropValue != NULL` +/// + If `propSize` is less than the real number of bytes needed to return the info. +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `propSize != 0 && pPropValue == NULL` +/// + `pPropValue == NULL && pPropSizeRet == NULL` +/// - ::UR_RESULT_ERROR_INVALID_DEVICE +/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderConfigGetInfo( + ur_loader_config_handle_t hLoaderConfig, ///< [in] handle of the loader config object + ur_loader_config_info_t propName, ///< [in] type of the info to retrieve + size_t propSize, ///< [in] the number of bytes pointed to by pPropValue. + void *pPropValue, ///< [out][optional][typename(propName, propSize)] array of bytes holding + ///< the info. + ///< If propSize is not equal to or greater than the real number of bytes + ///< needed to return the info + ///< then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and + ///< pPropValue is not used. + size_t *pPropSizeRet ///< [out][optional] pointer to the actual size in bytes of the queried propName. +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Enable a layer for the specified loader config. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NULL == pLayerName` +/// - ::UR_RESULT_ERROR_LAYER_NOT_PRESENT +/// + If layer specified with `pLayerName` can't be found by the loader. +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderConfigEnableLayer( + ur_loader_config_handle_t hLoaderConfig, ///< [in] Handle to config object the layer will be enabled for. + const char *pLayerName ///< [in] Null terminated string containing the name of the layer to + ///< enable. +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Initialize the 'oneAPI' loader +/// +/// @details +/// - The application must call this function before calling any other +/// function. +/// - If this function is not called then all other functions will return +/// ::UR_RESULT_ERROR_UNINITIALIZED. +/// - Only one instance of the loader will be initialized per process. +/// - The application may call this function multiple times with different +/// flags or environment variables enabled. +/// - The application must call this function after forking new processes. +/// Each forked process must call this function. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe for scenarios +/// where multiple libraries may initialize the loader simultaneously. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION +/// + `::UR_DEVICE_INIT_FLAGS_MASK & device_flags` +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderInit( + ur_device_init_flags_t device_flags, ///< [in] device initialization flags. + ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. + ur_loader_config_handle_t hLoaderConfig ///< [in][optional] Handle of loader config handle. +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Tear down the 'oneAPI' loader and release all its resources +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +UR_APIEXPORT ur_result_t UR_APICALL +urLoaderTearDown( + void); + #if !defined(__GNUC__) #pragma endregion #endif @@ -8995,6 +9001,54 @@ typedef struct ur_physical_mem_release_params_t { ur_physical_mem_handle_t *phPhysicalMem; } ur_physical_mem_release_params_t; +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function parameters for urAdapterGet +/// @details Each entry is a pointer to the parameter passed to the function; +/// allowing the callback the ability to modify the parameter's value +typedef struct ur_adapter_get_params_t { + uint32_t *pNumEntries; + ur_adapter_handle_t **pphAdapters; + uint32_t **ppNumAdapters; +} ur_adapter_get_params_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function parameters for urAdapterRelease +/// @details Each entry is a pointer to the parameter passed to the function; +/// allowing the callback the ability to modify the parameter's value +typedef struct ur_adapter_release_params_t { + ur_adapter_handle_t *phAdapter; +} ur_adapter_release_params_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function parameters for urAdapterRetain +/// @details Each entry is a pointer to the parameter passed to the function; +/// allowing the callback the ability to modify the parameter's value +typedef struct ur_adapter_retain_params_t { + ur_adapter_handle_t *phAdapter; +} ur_adapter_retain_params_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function parameters for urAdapterGetLastError +/// @details Each entry is a pointer to the parameter passed to the function; +/// allowing the callback the ability to modify the parameter's value +typedef struct ur_adapter_get_last_error_params_t { + ur_adapter_handle_t *phAdapter; + const char ***pppMessage; + int32_t **ppError; +} ur_adapter_get_last_error_params_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function parameters for urAdapterGetInfo +/// @details Each entry is a pointer to the parameter passed to the function; +/// allowing the callback the ability to modify the parameter's value +typedef struct ur_adapter_get_info_params_t { + ur_adapter_handle_t *phAdapter; + ur_adapter_info_t *ppropName; + size_t *ppropSize; + void **ppPropValue; + size_t **ppPropSizeRet; +} ur_adapter_get_info_params_t; + /////////////////////////////////////////////////////////////////////////////// /// @brief Function parameters for urEnqueueKernelLaunch /// @details Each entry is a pointer to the parameter passed to the function; @@ -10027,69 +10081,13 @@ typedef struct ur_usm_p2p_peer_access_get_info_exp_params_t { } ur_usm_p2p_peer_access_get_info_exp_params_t; /////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urInit +/// @brief Function parameters for urLoaderInit /// @details Each entry is a pointer to the parameter passed to the function; /// allowing the callback the ability to modify the parameter's value -typedef struct ur_init_params_t { +typedef struct ur_loader_init_params_t { ur_device_init_flags_t *pdevice_flags; ur_loader_config_handle_t *phLoaderConfig; -} ur_init_params_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urTearDown -/// @details Each entry is a pointer to the parameter passed to the function; -/// allowing the callback the ability to modify the parameter's value -typedef struct ur_tear_down_params_t { - void **ppParams; -} ur_tear_down_params_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urAdapterGet -/// @details Each entry is a pointer to the parameter passed to the function; -/// allowing the callback the ability to modify the parameter's value -typedef struct ur_adapter_get_params_t { - uint32_t *pNumEntries; - ur_adapter_handle_t **pphAdapters; - uint32_t **ppNumAdapters; -} ur_adapter_get_params_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urAdapterRelease -/// @details Each entry is a pointer to the parameter passed to the function; -/// allowing the callback the ability to modify the parameter's value -typedef struct ur_adapter_release_params_t { - ur_adapter_handle_t *phAdapter; -} ur_adapter_release_params_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urAdapterRetain -/// @details Each entry is a pointer to the parameter passed to the function; -/// allowing the callback the ability to modify the parameter's value -typedef struct ur_adapter_retain_params_t { - ur_adapter_handle_t *phAdapter; -} ur_adapter_retain_params_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urAdapterGetLastError -/// @details Each entry is a pointer to the parameter passed to the function; -/// allowing the callback the ability to modify the parameter's value -typedef struct ur_adapter_get_last_error_params_t { - ur_adapter_handle_t *phAdapter; - const char ***pppMessage; - int32_t **ppError; -} ur_adapter_get_last_error_params_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function parameters for urAdapterGetInfo -/// @details Each entry is a pointer to the parameter passed to the function; -/// allowing the callback the ability to modify the parameter's value -typedef struct ur_adapter_get_info_params_t { - ur_adapter_handle_t *phAdapter; - ur_adapter_info_t *ppropName; - size_t *ppropSize; - void **ppPropValue; - size_t **ppPropSizeRet; -} ur_adapter_get_info_params_t; +} ur_loader_init_params_t; /////////////////////////////////////////////////////////////////////////////// /// @brief Function parameters for urVirtualMemGranularityGetInfo diff --git a/include/ur_ddi.h b/include/ur_ddi.h index a61cbee653..cd6e9f77d9 100644 --- a/include/ur_ddi.h +++ b/include/ur_ddi.h @@ -803,6 +803,70 @@ typedef ur_result_t(UR_APICALL *ur_pfnGetPhysicalMemProcAddrTable_t)( ur_api_version_t, ur_physical_mem_dditable_t *); +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function-pointer for urAdapterGet +typedef ur_result_t(UR_APICALL *ur_pfnAdapterGet_t)( + uint32_t, + ur_adapter_handle_t *, + uint32_t *); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function-pointer for urAdapterRelease +typedef ur_result_t(UR_APICALL *ur_pfnAdapterRelease_t)( + ur_adapter_handle_t); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function-pointer for urAdapterRetain +typedef ur_result_t(UR_APICALL *ur_pfnAdapterRetain_t)( + ur_adapter_handle_t); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function-pointer for urAdapterGetLastError +typedef ur_result_t(UR_APICALL *ur_pfnAdapterGetLastError_t)( + ur_adapter_handle_t, + const char **, + int32_t *); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function-pointer for urAdapterGetInfo +typedef ur_result_t(UR_APICALL *ur_pfnAdapterGetInfo_t)( + ur_adapter_handle_t, + ur_adapter_info_t, + size_t, + void *, + size_t *); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Table of Global functions pointers +typedef struct ur_global_dditable_t { + ur_pfnAdapterGet_t pfnAdapterGet; + ur_pfnAdapterRelease_t pfnAdapterRelease; + ur_pfnAdapterRetain_t pfnAdapterRetain; + ur_pfnAdapterGetLastError_t pfnAdapterGetLastError; + ur_pfnAdapterGetInfo_t pfnAdapterGetInfo; +} ur_global_dditable_t; + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Exported function for filling application's Global table +/// with current process' addresses +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// - ::UR_RESULT_ERROR_UNSUPPORTED_VERSION +UR_DLLEXPORT ur_result_t UR_APICALL +urGetGlobalProcAddrTable( + ur_api_version_t version, ///< [in] API version requested + ur_global_dditable_t *pDdiTable ///< [in,out] pointer to table of DDI function pointers +); + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Function-pointer for urGetGlobalProcAddrTable +typedef ur_result_t(UR_APICALL *ur_pfnGetGlobalProcAddrTable_t)( + ur_api_version_t, + ur_global_dditable_t *); + /////////////////////////////////////////////////////////////////////////////// /// @brief Function-pointer for urEnqueueKernelLaunch typedef ur_result_t(UR_APICALL *ur_pfnEnqueueKernelLaunch_t)( @@ -1860,83 +1924,6 @@ typedef ur_result_t(UR_APICALL *ur_pfnGetUsmP2PExpProcAddrTable_t)( ur_api_version_t, ur_usm_p2p_exp_dditable_t *); -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urInit -typedef ur_result_t(UR_APICALL *ur_pfnInit_t)( - ur_device_init_flags_t, - ur_loader_config_handle_t); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urTearDown -typedef ur_result_t(UR_APICALL *ur_pfnTearDown_t)( - void *); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urAdapterGet -typedef ur_result_t(UR_APICALL *ur_pfnAdapterGet_t)( - uint32_t, - ur_adapter_handle_t *, - uint32_t *); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urAdapterRelease -typedef ur_result_t(UR_APICALL *ur_pfnAdapterRelease_t)( - ur_adapter_handle_t); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urAdapterRetain -typedef ur_result_t(UR_APICALL *ur_pfnAdapterRetain_t)( - ur_adapter_handle_t); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urAdapterGetLastError -typedef ur_result_t(UR_APICALL *ur_pfnAdapterGetLastError_t)( - ur_adapter_handle_t, - const char **, - int32_t *); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urAdapterGetInfo -typedef ur_result_t(UR_APICALL *ur_pfnAdapterGetInfo_t)( - ur_adapter_handle_t, - ur_adapter_info_t, - size_t, - void *, - size_t *); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Table of Global functions pointers -typedef struct ur_global_dditable_t { - ur_pfnInit_t pfnInit; - ur_pfnTearDown_t pfnTearDown; - ur_pfnAdapterGet_t pfnAdapterGet; - ur_pfnAdapterRelease_t pfnAdapterRelease; - ur_pfnAdapterRetain_t pfnAdapterRetain; - ur_pfnAdapterGetLastError_t pfnAdapterGetLastError; - ur_pfnAdapterGetInfo_t pfnAdapterGetInfo; -} ur_global_dditable_t; - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Exported function for filling application's Global table -/// with current process' addresses -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// - ::UR_RESULT_ERROR_UNSUPPORTED_VERSION -UR_DLLEXPORT ur_result_t UR_APICALL -urGetGlobalProcAddrTable( - ur_api_version_t version, ///< [in] API version requested - ur_global_dditable_t *pDdiTable ///< [in,out] pointer to table of DDI function pointers -); - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Function-pointer for urGetGlobalProcAddrTable -typedef ur_result_t(UR_APICALL *ur_pfnGetGlobalProcAddrTable_t)( - ur_api_version_t, - ur_global_dditable_t *); - /////////////////////////////////////////////////////////////////////////////// /// @brief Function-pointer for urVirtualMemGranularityGetInfo typedef ur_result_t(UR_APICALL *ur_pfnVirtualMemGranularityGetInfo_t)( @@ -2143,6 +2130,7 @@ typedef struct ur_dditable_t { ur_sampler_dditable_t Sampler; ur_mem_dditable_t Mem; ur_physical_mem_dditable_t PhysicalMem; + ur_global_dditable_t Global; ur_enqueue_dditable_t Enqueue; ur_queue_dditable_t Queue; ur_bindless_images_exp_dditable_t BindlessImagesExp; @@ -2150,7 +2138,6 @@ typedef struct ur_dditable_t { ur_usm_exp_dditable_t USMExp; ur_command_buffer_exp_dditable_t CommandBufferExp; ur_usm_p2p_exp_dditable_t UsmP2PExp; - ur_global_dditable_t Global; ur_virtual_mem_dditable_t VirtualMem; ur_device_dditable_t Device; } ur_dditable_t; diff --git a/scripts/core/CONTRIB.rst b/scripts/core/CONTRIB.rst index f40307e34e..137f190b31 100644 --- a/scripts/core/CONTRIB.rst +++ b/scripts/core/CONTRIB.rst @@ -137,8 +137,8 @@ defined within them, with the following exceptions: enumerations, and structure type enumerations. * `scripts/core/enqueue.yml`_ defines commands which can be enqueued on a queue object. -* `scripts/core/runtime.yml`_ defines global symbols pertaining to - initialization and tear down of the entire runtime. +* `scripts/core/loader.yml`_ defines global symbols pertaining to + initialization and tear down of the loader. * `scripts/core/registry.yml`_ contains an enumeration of all entry-points, past and present, for use in the XPTI tracing framework. It is automatically updated so shouldn't require manual editing. @@ -148,8 +148,8 @@ defined within them, with the following exceptions: https://github.com/oneapi-src/unified-runtime/blob/main/scripts/core/common.yml .. _scripts/core/enqueue.yml: https://github.com/oneapi-src/unified-runtime/blob/main/scripts/core/enqueue.yml -.. _scripts/core/runtime.yml: - https://github.com/oneapi-src/unified-runtime/blob/main/scripts/core/runtime.yml +.. _scripts/core/loader.yml: + https://github.com/oneapi-src/unified-runtime/blob/main/scripts/core/loader.yml .. _scripts/core/registry.yml: https://github.com/oneapi-src/unified-runtime/blob/main/scripts/core/registry.yml diff --git a/scripts/core/INTRO.rst b/scripts/core/INTRO.rst index 8b9938cfb2..d3a862ad87 100644 --- a/scripts/core/INTRO.rst +++ b/scripts/core/INTRO.rst @@ -307,7 +307,7 @@ Specific environment variables can be set to control the behavior of unified run .. envvar:: UR_ENABLE_LAYERS - Holds a comma-separated list of layers to enable in addition to any specified via ``urInit``. + Holds a comma-separated list of layers to enable in addition to any specified via ``urLoaderInit``. .. note:: diff --git a/scripts/core/runtime.yml b/scripts/core/adapter.yml similarity index 52% rename from scripts/core/runtime.yml rename to scripts/core/adapter.yml index c14f939cc2..4026376eb1 100644 --- a/scripts/core/runtime.yml +++ b/scripts/core/adapter.yml @@ -1,5 +1,5 @@ # -# Copyright (C) 2022 Intel Corporation +# Copyright (C) 2022-2023 Intel Corporation # # Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. # See LICENSE.TXT @@ -9,177 +9,9 @@ # --- #-------------------------------------------------------------------------- type: header -desc: "Intel $OneApi Unified Runtime APIs for Runtime" +desc: "Intel $OneApi Unified Runtime APIs for Adapter" ordinal: "0" --- #-------------------------------------------------------------------------- -type: enum -desc: "Supported device initialization flags" -class: $x -name: $x_device_init_flags_t -etors: - - name: GPU - desc: "initialize GPU device adapters." - - name: CPU - desc: "initialize CPU device adapters." - - name: FPGA - desc: "initialize FPGA device adapters." - - name: MCA - desc: "initialize MCA device adapters." - - name: VPU - desc: "initialize VPU device adapters." ---- #-------------------------------------------------------------------------- -type: function -desc: "Create a loader config object." -class: $xLoaderConfig -loader_only: True -name: Create -decl: static -params: - - type: $x_loader_config_handle_t* - name: phLoaderConfig - desc: "[out] Pointer to handle of loader config object created." ---- #-------------------------------------------------------------------------- -type: function -desc: "Get a reference to the loader config object." -class: $xLoaderConfig -loader_only: True -name: Retain -decl: static -details: - - "Get a reference to the loader config handle. Increment its reference count" - - "The application may call this function from simultaneous threads." - - "The implementation of this function should be lock-free." -params: - - type: $x_loader_config_handle_t - name: hLoaderConfig - desc: "[in] loader config handle to retain" ---- #-------------------------------------------------------------------------- -type: function -desc: "Release config handle." -class: $xLoaderConfig -loader_only: True -name: Release -decl: static -details: - - "Decrement reference count and destroy the config handle if reference count becomes zero." - - "The application may call this function from simultaneous threads." - - "The implementation of this function should be lock-free." -params: - - type: $x_loader_config_handle_t - name: hLoaderConfig - desc: "[in] config handle to release" ---- #-------------------------------------------------------------------------- -type: enum -desc: "Supported loader info" -class: $xLoaderConfig -name: $x_loader_config_info_t -typed_etors: True -etors: - - name: AVAILABLE_LAYERS - desc: "[char[]] Null-terminated, semi-colon separated list of available layers." - - name: REFERENCE_COUNT - desc: "[uint32_t] Reference count of the loader config object." ---- #-------------------------------------------------------------------------- -type: function -desc: "Retrieves various information about the loader." -class: $xLoaderConfig -loader_only: True -name: GetInfo -decl: static -details: - - "The application may call this function from simultaneous threads." - - "The implementation of this function should be lock-free." -params: - - type: $x_loader_config_handle_t - name: hLoaderConfig - desc: "[in] handle of the loader config object" - - type: $x_loader_config_info_t - name: propName - desc: "[in] type of the info to retrieve" - - type: "size_t" - name: propSize - desc: | - [in] the number of bytes pointed to by pPropValue. - - type: "void*" - name: pPropValue - desc: | - [out][optional][typename(propName, propSize)] array of bytes holding the info. - If propSize is not equal to or greater than the real number of bytes needed to return the info - then the $X_RESULT_ERROR_INVALID_SIZE error is returned and pPropValue is not used. - - type: "size_t*" - name: pPropSizeRet - desc: | - [out][optional] pointer to the actual size in bytes of the queried propName. -returns: - - $X_RESULT_ERROR_UNSUPPORTED_ENUMERATION: - - "If `propName` is not supported by the loader." - - $X_RESULT_ERROR_INVALID_SIZE: - - "`propSize == 0 && pPropValue != NULL`" - - "If `propSize` is less than the real number of bytes needed to return the info." - - $X_RESULT_ERROR_INVALID_NULL_POINTER: - - "`propSize != 0 && pPropValue == NULL`" - - "`pPropValue == NULL && pPropSizeRet == NULL`" - - $X_RESULT_ERROR_INVALID_DEVICE - - $X_RESULT_ERROR_OUT_OF_RESOURCES - - $X_RESULT_ERROR_OUT_OF_HOST_MEMORY ---- #-------------------------------------------------------------------------- -type: function -desc: "Enable a layer for the specified loader config." -class: $xLoaderConfig -loader_only: True -name: EnableLayer -decl: static -params: - - type: $x_loader_config_handle_t - name: hLoaderConfig - desc: "[in] Handle to config object the layer will be enabled for." - - type: const char* - name: pLayerName - desc: "[in] Null terminated string containing the name of the layer to enable." -returns: - - $X_RESULT_ERROR_LAYER_NOT_PRESENT: - - "If layer specified with `pLayerName` can't be found by the loader." ---- #-------------------------------------------------------------------------- -type: function -desc: "Initialize the $OneApi adapter(s)" -class: $x -name: Init -decl: static -ordinal: "0" -details: - - "The application must call this function before calling any other function." - - "If this function is not called then all other functions will return $X_RESULT_ERROR_UNINITIALIZED." - - "Only one instance of each adapter will be initialized per process." - - "The application may call this function multiple times with different flags or environment variables enabled." - - "The application must call this function after forking new processes. Each forked process must call this function." - - "The application may call this function from simultaneous threads." - - "The implementation of this function must be thread-safe for scenarios where multiple libraries may initialize the adapter(s) simultaneously." -params: - - type: $x_device_init_flags_t - name: device_flags - desc: | - [in] device initialization flags. - must be 0 (default) or a combination of $x_device_init_flag_t. - init: "0" - - type: $x_loader_config_handle_t - name: hLoaderConfig - desc: "[in][optional] Handle of loader config handle." -returns: - - $X_RESULT_ERROR_OUT_OF_HOST_MEMORY ---- #-------------------------------------------------------------------------- -type: function -desc: "Tear down the $OneApi instance and release all its resources" -class: $x -name: TearDown -decl: static -ordinal: "1" -params: - - type: void* - name: pParams - desc: "[in] pointer to tear down parameters" -returns: - - $X_RESULT_ERROR_OUT_OF_HOST_MEMORY ---- #-------------------------------------------------------------------------- type: function desc: "Retrieves all available adapters" class: $x @@ -218,7 +50,8 @@ name: AdapterRelease decl: static ordinal: "3" details: - - "When the reference count of the adapter reaches zero, the adapter may perform adapter-specififc resource teardown" + - "When the reference count of the adapter reaches zero, the adapter may perform adapter-specififc resource teardown. Resources + must be left in a state where it safe for the adapter to be subsequently reinitialized with $xAdapterGet" params: - type: "$x_adapter_handle_t" name: hAdapter diff --git a/scripts/core/loader.yml b/scripts/core/loader.yml new file mode 100644 index 0000000000..c0311bd31a --- /dev/null +++ b/scripts/core/loader.yml @@ -0,0 +1,180 @@ +# +# Copyright (C) 2022-2023 Intel Corporation +# +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +# See LICENSE.TXT +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# See YaML.md for syntax definition +# +--- #-------------------------------------------------------------------------- +type: header +desc: "Intel $OneApi Unified Runtime APIs for Loader" +ordinal: "0" +--- #-------------------------------------------------------------------------- +type: enum +desc: "Supported device initialization flags" +class: $x +name: $x_device_init_flags_t +etors: + - name: GPU + desc: "initialize GPU device adapters." + - name: CPU + desc: "initialize CPU device adapters." + - name: FPGA + desc: "initialize FPGA device adapters." + - name: MCA + desc: "initialize MCA device adapters." + - name: VPU + desc: "initialize VPU device adapters." +--- #-------------------------------------------------------------------------- +type: function +desc: "Create a loader config object." +class: $xLoaderConfig +loader_only: True +name: Create +decl: static +params: + - type: $x_loader_config_handle_t* + name: phLoaderConfig + desc: "[out] Pointer to handle of loader config object created." +--- #-------------------------------------------------------------------------- +type: function +desc: "Get a reference to the loader config object." +class: $xLoaderConfig +loader_only: True +name: Retain +decl: static +details: + - "Get a reference to the loader config handle. Increment its reference count" + - "The application may call this function from simultaneous threads." + - "The implementation of this function should be lock-free." +params: + - type: $x_loader_config_handle_t + name: hLoaderConfig + desc: "[in] loader config handle to retain" +--- #-------------------------------------------------------------------------- +type: function +desc: "Release config handle." +class: $xLoaderConfig +loader_only: True +name: Release +decl: static +details: + - "Decrement reference count and destroy the config handle if reference count becomes zero." + - "The application may call this function from simultaneous threads." + - "The implementation of this function should be lock-free." +params: + - type: $x_loader_config_handle_t + name: hLoaderConfig + desc: "[in] config handle to release" +--- #-------------------------------------------------------------------------- +type: enum +desc: "Supported loader info" +class: $xLoaderConfig +name: $x_loader_config_info_t +typed_etors: True +etors: + - name: AVAILABLE_LAYERS + desc: "[char[]] Null-terminated, semi-colon separated list of available layers." + - name: REFERENCE_COUNT + desc: "[uint32_t] Reference count of the loader config object." +--- #-------------------------------------------------------------------------- +type: function +desc: "Retrieves various information about the loader." +class: $xLoaderConfig +loader_only: True +name: GetInfo +decl: static +details: + - "The application may call this function from simultaneous threads." + - "The implementation of this function should be lock-free." +params: + - type: $x_loader_config_handle_t + name: hLoaderConfig + desc: "[in] handle of the loader config object" + - type: $x_loader_config_info_t + name: propName + desc: "[in] type of the info to retrieve" + - type: "size_t" + name: propSize + desc: | + [in] the number of bytes pointed to by pPropValue. + - type: "void*" + name: pPropValue + desc: | + [out][optional][typename(propName, propSize)] array of bytes holding the info. + If propSize is not equal to or greater than the real number of bytes needed to return the info + then the $X_RESULT_ERROR_INVALID_SIZE error is returned and pPropValue is not used. + - type: "size_t*" + name: pPropSizeRet + desc: | + [out][optional] pointer to the actual size in bytes of the queried propName. +returns: + - $X_RESULT_ERROR_UNSUPPORTED_ENUMERATION: + - "If `propName` is not supported by the loader." + - $X_RESULT_ERROR_INVALID_SIZE: + - "`propSize == 0 && pPropValue != NULL`" + - "If `propSize` is less than the real number of bytes needed to return the info." + - $X_RESULT_ERROR_INVALID_NULL_POINTER: + - "`propSize != 0 && pPropValue == NULL`" + - "`pPropValue == NULL && pPropSizeRet == NULL`" + - $X_RESULT_ERROR_INVALID_DEVICE + - $X_RESULT_ERROR_OUT_OF_RESOURCES + - $X_RESULT_ERROR_OUT_OF_HOST_MEMORY +--- #-------------------------------------------------------------------------- +type: function +desc: "Enable a layer for the specified loader config." +class: $xLoaderConfig +loader_only: True +name: EnableLayer +decl: static +params: + - type: $x_loader_config_handle_t + name: hLoaderConfig + desc: "[in] Handle to config object the layer will be enabled for." + - type: const char* + name: pLayerName + desc: "[in] Null terminated string containing the name of the layer to enable." +returns: + - $X_RESULT_ERROR_LAYER_NOT_PRESENT: + - "If layer specified with `pLayerName` can't be found by the loader." +--- #-------------------------------------------------------------------------- +type: function +desc: "Initialize the $OneApi loader" +class: $xLoader +loader_only: True +name: Init +decl: static +ordinal: "0" +details: + - "The application must call this function before calling any other function." + - "If this function is not called then all other functions will return $X_RESULT_ERROR_UNINITIALIZED." + - "Only one instance of the loader will be initialized per process." + - "The application may call this function multiple times with different flags or environment variables enabled." + - "The application must call this function after forking new processes. Each forked process must call this function." + - "The application may call this function from simultaneous threads." + - "The implementation of this function must be thread-safe for scenarios where multiple libraries may initialize the loader simultaneously." +params: + - type: $x_device_init_flags_t + name: device_flags + desc: | + [in] device initialization flags. + must be 0 (default) or a combination of $x_device_init_flag_t. + init: "0" + - type: $x_loader_config_handle_t + name: hLoaderConfig + desc: "[in][optional] Handle of loader config handle." +returns: + - $X_RESULT_ERROR_OUT_OF_HOST_MEMORY +--- #-------------------------------------------------------------------------- +type: function +desc: "Tear down the $OneApi loader and release all its resources" +class: $xLoader +loader_only: True +name: TearDown +decl: static +ordinal: "1" +params: [] +returns: + - $X_RESULT_ERROR_OUT_OF_HOST_MEMORY diff --git a/scripts/core/registry.yml b/scripts/core/registry.yml index 30596ec14e..435e7d8eda 100644 --- a/scripts/core/registry.yml +++ b/scripts/core/registry.yml @@ -292,12 +292,6 @@ etors: - name: QUEUE_FLUSH desc: Enumerator for $xQueueFlush value: '98' -- name: INIT - desc: Enumerator for $xInit - value: '99' -- name: TEAR_DOWN - desc: Enumerator for $xTearDown - value: '100' - name: SAMPLER_CREATE desc: Enumerator for $xSamplerCreate value: '101' @@ -529,6 +523,12 @@ etors: - name: ADAPTER_GET_INFO desc: Enumerator for $xAdapterGetInfo value: '181' +- name: LOADER_INIT + desc: Enumerator for $xLoaderInit + value: '182' +- name: LOADER_TEAR_DOWN + desc: Enumerator for $xLoaderTearDown + value: '183' --- type: enum desc: Defines structure types diff --git a/scripts/parse_specs.py b/scripts/parse_specs.py index 414b25d8e0..195d53fc5c 100644 --- a/scripts/parse_specs.py +++ b/scripts/parse_specs.py @@ -872,6 +872,7 @@ def parse(section, version, tags, meta, ref): specs = [] files = util.findFiles(path, "*.yml") + files.sort(key = lambda f: 0 if f.endswith('common.yml') else 1) registry = [f for f in files if f.endswith('registry.yml')][0] enum_extensions = [] diff --git a/scripts/templates/api.h.mako b/scripts/templates/api.h.mako index 74ba84beeb..41d6d8f456 100644 --- a/scripts/templates/api.h.mako +++ b/scripts/templates/api.h.mako @@ -151,6 +151,7 @@ typedef struct ${th.subt(n, tags, obj['name'])}_ *${th.subt(n, tags, obj['name'] #endif %for tbl in th.get_pfncbtables(specs, meta, n, tags): %for obj in tbl['functions']: +%if obj['params']: /////////////////////////////////////////////////////////////////////////////// /// @brief Function parameters for ${th.make_func_name(n, tags, obj)} /// @details Each entry is a pointer to the parameter passed to the function; @@ -167,6 +168,7 @@ typedef struct ${th.make_pfncb_param_type(n, tags, obj)} %if 'condition' in obj: #endif // ${th.subt(n, tags, obj['condition'])} %endif +%endif %endfor %endfor diff --git a/scripts/templates/api.py.mako b/scripts/templates/api.py.mako index 35a2fd6d27..7815f2cf53 100644 --- a/scripts/templates/api.py.mako +++ b/scripts/templates/api.py.mako @@ -175,7 +175,7 @@ class ${N}_DDI: self.__dditable = ${n}_dditable_t() # initialize the UR - self.__dll.${x}Init(0, 0) + self.__dll.${x}LoaderInit(0, 0) %for tbl in tables: # call driver to get function pointers diff --git a/scripts/templates/ldrddi.cpp.mako b/scripts/templates/ldrddi.cpp.mako index 0498ba00dc..0c9a3ed8b0 100644 --- a/scripts/templates/ldrddi.cpp.mako +++ b/scripts/templates/ldrddi.cpp.mako @@ -51,22 +51,7 @@ namespace ur_loader add_local = False %> - %if re.match(r"Init", obj['name']): - for( auto& platform : context->platforms ) - { - if(platform.initStatus != ${X}_RESULT_SUCCESS) - continue; - platform.initStatus = platform.dditable.${n}.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} ); - } - - %elif re.match(r"\w+TearDown$", th.make_func_name(n, tags, obj)): - - for( auto& platform : context->platforms ) - { - platform.dditable.${n}.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} ); - } - - %elif re.match(r"\w+AdapterGet$", th.make_func_name(n, tags, obj)): + %if re.match(r"\w+AdapterGet$", th.make_func_name(n, tags, obj)): size_t adapterIndex = 0; if( nullptr != ${obj['params'][1]['name']} && ${obj['params'][0]['name']} !=0) diff --git a/scripts/templates/libapi.cpp.mako b/scripts/templates/libapi.cpp.mako index d269d62241..6fe1f3992b 100644 --- a/scripts/templates/libapi.cpp.mako +++ b/scripts/templates/libapi.cpp.mako @@ -56,19 +56,27 @@ ${th.make_func_name(n, tags, obj)}( %endfor ) try { -%if th.obj_traits.is_loader_only(obj): - return ur_lib::${th.make_func_name(n, tags, obj)}(${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} ); -%else: %if re.match("Init", obj['name']): + <% + param_checks=th.make_param_checks(n, tags, obj, meta=meta).items() + %> + %for key, values in param_checks: + %for val in values: + if( ${val} ) + return ${key}; + + %endfor + %endfor + static ${x}_result_t result = ${X}_RESULT_SUCCESS; std::call_once(${x}_lib::context->initOnce, [device_flags, hLoaderConfig]() { result = ${x}_lib::context->Init(device_flags, hLoaderConfig); }); - if( ${X}_RESULT_SUCCESS != result ) - return result; - -%endif + return result; +%elif th.obj_traits.is_loader_only(obj): + return ur_lib::${th.make_func_name(n, tags, obj)}(${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} ); +%else: auto ${th.make_pfn_name(n, tags, obj)} = ${x}_lib::context->${n}DdiTable.${th.get_table_name(n, tags, obj)}.${th.make_pfn_name(n, tags, obj)}; if( nullptr == ${th.make_pfn_name(n, tags, obj)} ) return ${X}_RESULT_ERROR_UNINITIALIZED; diff --git a/scripts/templates/libddi.cpp.mako b/scripts/templates/libddi.cpp.mako index de73cc2fc7..eae178324c 100644 --- a/scripts/templates/libddi.cpp.mako +++ b/scripts/templates/libddi.cpp.mako @@ -28,7 +28,7 @@ namespace ${x}_lib /////////////////////////////////////////////////////////////////////////////// - __${x}dlllocal ${x}_result_t context_t::${n}Init() + __${x}dlllocal ${x}_result_t context_t::${n}LoaderInit() { ${x}_result_t result = ${X}_RESULT_SUCCESS; diff --git a/scripts/templates/valddi.cpp.mako b/scripts/templates/valddi.cpp.mako index 862c8b81a5..e778d01833 100644 --- a/scripts/templates/valddi.cpp.mako +++ b/scripts/templates/valddi.cpp.mako @@ -64,7 +64,22 @@ namespace ur_validation_layer ${x}_result_t result = ${th.make_pfn_name(n, tags, obj)}( ${", ".join(th.make_param_lines(n, tags, obj, format=["name"]))} ); - %if func_name in create_retain_release_funcs["create"]: + %if func_name == n + "AdapterRelease": + if( context.enableLeakChecking && result == UR_RESULT_SUCCESS ) + { + refCountContext.decrementRefCount(${object_param}, true); + } + %elif func_name == n + "AdapterRetain": + if( context.enableLeakChecking && result == UR_RESULT_SUCCESS ) + { + refCountContext.decrementRefCount(${object_param}, true); + } + %elif func_name == n + "AdapterGet": + if( context.enableLeakChecking && phAdapters && result == UR_RESULT_SUCCESS ) + { + refCountContext.createOrIncrementRefCount(*phAdapters, true); + } + %elif func_name in create_retain_release_funcs["create"]: if( context.enableLeakChecking && result == UR_RESULT_SUCCESS ) { refCountContext.createRefCount(*${object_param}); @@ -79,12 +94,6 @@ namespace ur_validation_layer { refCountContext.decrementRefCount(${object_param}); } - %elif func_name == n + "TearDown": - if ( context.enableLeakChecking ) - { - refCountContext.logInvalidReferences(); - refCountContext.clear(); - } %endif return result; @@ -170,4 +179,14 @@ namespace ur_validation_layer return result; } + ${x}_result_t context_t::tearDown() { + ${x}_result_t result = ${X}_RESULT_SUCCESS; + + if (enableLeakChecking) { + refCountContext.logInvalidReferences(); + refCountContext.clear(); + } + return result; + } + } // namespace ur_validation_layer diff --git a/source/adapters/null/ur_nullddi.cpp b/source/adapters/null/ur_nullddi.cpp index f9b8fb4d11..e5efe2b49c 100644 --- a/source/adapters/null/ur_nullddi.cpp +++ b/source/adapters/null/ur_nullddi.cpp @@ -12,49 +12,6 @@ #include "ur_null.hpp" namespace driver { -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urInit -__urdlllocal ur_result_t UR_APICALL urInit( - ur_device_init_flags_t device_flags, ///< [in] device initialization flags. - ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. - ur_loader_config_handle_t - hLoaderConfig ///< [in][optional] Handle of loader config handle. - ) try { - ur_result_t result = UR_RESULT_SUCCESS; - - // if the driver has created a custom function, then call it instead of using the generic path - auto pfnInit = d_context.urDdiTable.Global.pfnInit; - if (nullptr != pfnInit) { - result = pfnInit(device_flags, hLoaderConfig); - } else { - // generic implementation - } - - return result; -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urTearDown -__urdlllocal ur_result_t UR_APICALL urTearDown( - void *pParams ///< [in] pointer to tear down parameters - ) try { - ur_result_t result = UR_RESULT_SUCCESS; - - // if the driver has created a custom function, then call it instead of using the generic path - auto pfnTearDown = d_context.urDdiTable.Global.pfnTearDown; - if (nullptr != pfnTearDown) { - result = pfnTearDown(pParams); - } else { - // generic implementation - } - - return result; -} catch (...) { - return exceptionToResult(std::current_exception()); -} - /////////////////////////////////////////////////////////////////////////////// /// @brief Intercept function for urAdapterGet __urdlllocal ur_result_t UR_APICALL urAdapterGet( @@ -5052,10 +5009,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetGlobalProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - pDdiTable->pfnInit = driver::urInit; - - pDdiTable->pfnTearDown = driver::urTearDown; - pDdiTable->pfnAdapterGet = driver::urAdapterGet; pDdiTable->pfnAdapterRelease = driver::urAdapterRelease; diff --git a/source/common/ur_params.hpp b/source/common/ur_params.hpp index 76641dcb65..0979fab3ca 100644 --- a/source/common/ur_params.hpp +++ b/source/common/ur_params.hpp @@ -49,6 +49,10 @@ inline void serializeTagged(std::ostream &os, const void *ptr, T value, inline void serializeStruct(std::ostream &os, const void *ptr); +template <> +inline void serializeTagged(std::ostream &os, const void *ptr, + ur_adapter_info_t value, size_t size); + template <> inline void serializeFlag(std::ostream &os, uint32_t flag); @@ -57,10 +61,6 @@ template <> inline void serializeTagged(std::ostream &os, const void *ptr, ur_loader_config_info_t value, size_t size); -template <> -inline void serializeTagged(std::ostream &os, const void *ptr, - ur_adapter_info_t value, size_t size); - template <> inline void serializeTagged(std::ostream &os, const void *ptr, ur_platform_info_t value, size_t size); @@ -232,13 +232,13 @@ inline std::ostream &operator<<(std::ostream &os, const struct ur_rect_offset_t params); inline std::ostream &operator<<(std::ostream &os, const struct ur_rect_region_t params); +inline std::ostream &operator<<(std::ostream &os, enum ur_adapter_info_t value); +inline std::ostream &operator<<(std::ostream &os, + enum ur_adapter_backend_t value); inline std::ostream &operator<<(std::ostream &os, enum ur_device_init_flag_t value); inline std::ostream &operator<<(std::ostream &os, enum ur_loader_config_info_t value); -inline std::ostream &operator<<(std::ostream &os, enum ur_adapter_info_t value); -inline std::ostream &operator<<(std::ostream &os, - enum ur_adapter_backend_t value); inline std::ostream &operator<<(std::ostream &os, enum ur_platform_info_t value); inline std::ostream &operator<<(std::ostream &os, enum ur_api_version_t value); @@ -825,14 +825,6 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_function_t value) { os << "UR_FUNCTION_QUEUE_FLUSH"; break; - case UR_FUNCTION_INIT: - os << "UR_FUNCTION_INIT"; - break; - - case UR_FUNCTION_TEAR_DOWN: - os << "UR_FUNCTION_TEAR_DOWN"; - break; - case UR_FUNCTION_SAMPLER_CREATE: os << "UR_FUNCTION_SAMPLER_CREATE"; break; @@ -1141,6 +1133,14 @@ inline std::ostream &operator<<(std::ostream &os, enum ur_function_t value) { case UR_FUNCTION_ADAPTER_GET_INFO: os << "UR_FUNCTION_ADAPTER_GET_INFO"; break; + + case UR_FUNCTION_LOADER_INIT: + os << "UR_FUNCTION_LOADER_INIT"; + break; + + case UR_FUNCTION_LOADER_TEAR_DOWN: + os << "UR_FUNCTION_LOADER_TEAR_DOWN"; + break; default: os << "unknown enumerator"; break; @@ -1938,6 +1938,100 @@ inline std::ostream &operator<<(std::ostream &os, os << "}"; return os; } +inline std::ostream &operator<<(std::ostream &os, + enum ur_adapter_info_t value) { + switch (value) { + + case UR_ADAPTER_INFO_BACKEND: + os << "UR_ADAPTER_INFO_BACKEND"; + break; + + case UR_ADAPTER_INFO_REFERENCE_COUNT: + os << "UR_ADAPTER_INFO_REFERENCE_COUNT"; + break; + default: + os << "unknown enumerator"; + break; + } + return os; +} +namespace ur_params { +template <> +inline void serializeTagged(std::ostream &os, const void *ptr, + ur_adapter_info_t value, size_t size) { + if (ptr == NULL) { + serializePtr(os, ptr); + return; + } + + switch (value) { + + case UR_ADAPTER_INFO_BACKEND: { + const ur_adapter_backend_t *tptr = (const ur_adapter_backend_t *)ptr; + if (sizeof(ur_adapter_backend_t) > size) { + os << "invalid size (is: " << size + << ", expected: >=" << sizeof(ur_adapter_backend_t) << ")"; + return; + } + os << (const void *)(tptr) << " ("; + + os << *tptr; + + os << ")"; + } break; + + case UR_ADAPTER_INFO_REFERENCE_COUNT: { + const uint32_t *tptr = (const uint32_t *)ptr; + if (sizeof(uint32_t) > size) { + os << "invalid size (is: " << size + << ", expected: >=" << sizeof(uint32_t) << ")"; + return; + } + os << (const void *)(tptr) << " ("; + + os << *tptr; + + os << ")"; + } break; + default: + os << "unknown enumerator"; + break; + } +} +} // namespace ur_params +inline std::ostream &operator<<(std::ostream &os, + enum ur_adapter_backend_t value) { + switch (value) { + + case UR_ADAPTER_BACKEND_UNKNOWN: + os << "UR_ADAPTER_BACKEND_UNKNOWN"; + break; + + case UR_ADAPTER_BACKEND_LEVEL_ZERO: + os << "UR_ADAPTER_BACKEND_LEVEL_ZERO"; + break; + + case UR_ADAPTER_BACKEND_OPENCL: + os << "UR_ADAPTER_BACKEND_OPENCL"; + break; + + case UR_ADAPTER_BACKEND_CUDA: + os << "UR_ADAPTER_BACKEND_CUDA"; + break; + + case UR_ADAPTER_BACKEND_HIP: + os << "UR_ADAPTER_BACKEND_HIP"; + break; + + case UR_ADAPTER_BACKEND_NATIVE_CPU: + os << "UR_ADAPTER_BACKEND_NATIVE_CPU"; + break; + default: + os << "unknown enumerator"; + break; + } + return os; +} inline std::ostream &operator<<(std::ostream &os, enum ur_device_init_flag_t value) { switch (value) { @@ -2089,100 +2183,6 @@ inline void serializeTagged(std::ostream &os, const void *ptr, } } } // namespace ur_params -inline std::ostream &operator<<(std::ostream &os, - enum ur_adapter_info_t value) { - switch (value) { - - case UR_ADAPTER_INFO_BACKEND: - os << "UR_ADAPTER_INFO_BACKEND"; - break; - - case UR_ADAPTER_INFO_REFERENCE_COUNT: - os << "UR_ADAPTER_INFO_REFERENCE_COUNT"; - break; - default: - os << "unknown enumerator"; - break; - } - return os; -} -namespace ur_params { -template <> -inline void serializeTagged(std::ostream &os, const void *ptr, - ur_adapter_info_t value, size_t size) { - if (ptr == NULL) { - serializePtr(os, ptr); - return; - } - - switch (value) { - - case UR_ADAPTER_INFO_BACKEND: { - const ur_adapter_backend_t *tptr = (const ur_adapter_backend_t *)ptr; - if (sizeof(ur_adapter_backend_t) > size) { - os << "invalid size (is: " << size - << ", expected: >=" << sizeof(ur_adapter_backend_t) << ")"; - return; - } - os << (const void *)(tptr) << " ("; - - os << *tptr; - - os << ")"; - } break; - - case UR_ADAPTER_INFO_REFERENCE_COUNT: { - const uint32_t *tptr = (const uint32_t *)ptr; - if (sizeof(uint32_t) > size) { - os << "invalid size (is: " << size - << ", expected: >=" << sizeof(uint32_t) << ")"; - return; - } - os << (const void *)(tptr) << " ("; - - os << *tptr; - - os << ")"; - } break; - default: - os << "unknown enumerator"; - break; - } -} -} // namespace ur_params -inline std::ostream &operator<<(std::ostream &os, - enum ur_adapter_backend_t value) { - switch (value) { - - case UR_ADAPTER_BACKEND_UNKNOWN: - os << "UR_ADAPTER_BACKEND_UNKNOWN"; - break; - - case UR_ADAPTER_BACKEND_LEVEL_ZERO: - os << "UR_ADAPTER_BACKEND_LEVEL_ZERO"; - break; - - case UR_ADAPTER_BACKEND_OPENCL: - os << "UR_ADAPTER_BACKEND_OPENCL"; - break; - - case UR_ADAPTER_BACKEND_CUDA: - os << "UR_ADAPTER_BACKEND_CUDA"; - break; - - case UR_ADAPTER_BACKEND_HIP: - os << "UR_ADAPTER_BACKEND_HIP"; - break; - - case UR_ADAPTER_BACKEND_NATIVE_CPU: - os << "UR_ADAPTER_BACKEND_NATIVE_CPU"; - break; - default: - os << "unknown enumerator"; - break; - } - return os; -} inline std::ostream &operator<<(std::ostream &os, enum ur_platform_info_t value) { switch (value) { @@ -9961,32 +9961,6 @@ inline void serializeTagged(std::ostream &os, const void *ptr, } } // namespace ur_params -inline std::ostream &operator<<(std::ostream &os, - const struct ur_init_params_t *params) { - - os << ".device_flags = "; - - ur_params::serializeFlag(os, - *(params->pdevice_flags)); - - os << ", "; - os << ".hLoaderConfig = "; - - ur_params::serializePtr(os, *(params->phLoaderConfig)); - - return os; -} - -inline std::ostream &operator<<(std::ostream &os, - const struct ur_tear_down_params_t *params) { - - os << ".pParams = "; - - ur_params::serializePtr(os, *(params->ppParams)); - - return os; -} - inline std::ostream &operator<<(std::ostream &os, const struct ur_adapter_get_params_t *params) { @@ -13382,6 +13356,29 @@ inline std::ostream &operator<<( return os; } +inline std::ostream &operator<<(std::ostream &os, + const struct ur_loader_init_params_t *params) { + + os << ".device_flags = "; + + ur_params::serializeFlag(os, + *(params->pdevice_flags)); + + os << ", "; + os << ".hLoaderConfig = "; + + ur_params::serializePtr(os, *(params->phLoaderConfig)); + + return os; +} + +inline std::ostream & +operator<<(std::ostream &os, + const struct ur_loader_tear_down_params_t *params) { + + return os; +} + inline std::ostream & operator<<(std::ostream &os, const struct ur_loader_config_create_params_t *params) { @@ -15334,12 +15331,6 @@ template inline void serializePtr(std::ostream &os, const T *ptr) { inline int serializeFunctionParams(std::ostream &os, uint32_t function, const void *params) { switch ((enum ur_function_t)function) { - case UR_FUNCTION_INIT: { - os << (const struct ur_init_params_t *)params; - } break; - case UR_FUNCTION_TEAR_DOWN: { - os << (const struct ur_tear_down_params_t *)params; - } break; case UR_FUNCTION_ADAPTER_GET: { os << (const struct ur_adapter_get_params_t *)params; } break; @@ -15648,6 +15639,12 @@ inline int serializeFunctionParams(std::ostream &os, uint32_t function, os << (const struct ur_kernel_set_specialization_constants_params_t *) params; } break; + case UR_FUNCTION_LOADER_INIT: { + os << (const struct ur_loader_init_params_t *)params; + } break; + case UR_FUNCTION_LOADER_TEAR_DOWN: { + os << (const struct ur_loader_tear_down_params_t *)params; + } break; case UR_FUNCTION_LOADER_CONFIG_CREATE: { os << (const struct ur_loader_config_create_params_t *)params; } break; diff --git a/source/loader/layers/tracing/ur_tracing_layer.hpp b/source/loader/layers/tracing/ur_tracing_layer.hpp index b00d12d301..04a2d7c857 100644 --- a/source/loader/layers/tracing/ur_tracing_layer.hpp +++ b/source/loader/layers/tracing/ur_tracing_layer.hpp @@ -33,6 +33,7 @@ class __urdlllocal context_t : public proxy_layer_context_t { std::vector getNames() const override { return {name}; } ur_result_t init(ur_dditable_t *dditable, const std::set &enabledLayerNames) override; + ur_result_t tearDown() override { return UR_RESULT_SUCCESS; } uint64_t notify_begin(uint32_t id, const char *name, void *args); void notify_end(uint32_t id, const char *name, void *args, ur_result_t *resultp, uint64_t instance); diff --git a/source/loader/layers/tracing/ur_trcddi.cpp b/source/loader/layers/tracing/ur_trcddi.cpp index f30fac3807..3f79c3aa13 100644 --- a/source/loader/layers/tracing/ur_trcddi.cpp +++ b/source/loader/layers/tracing/ur_trcddi.cpp @@ -15,54 +15,6 @@ #include namespace ur_tracing_layer { -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urInit -__urdlllocal ur_result_t UR_APICALL urInit( - ur_device_init_flags_t device_flags, ///< [in] device initialization flags. - ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. - ur_loader_config_handle_t - hLoaderConfig ///< [in][optional] Handle of loader config handle. -) { - auto pfnInit = context.urDdiTable.Global.pfnInit; - - if (nullptr == pfnInit) { - return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; - } - - ur_init_params_t params = {&device_flags, &hLoaderConfig}; - uint64_t instance = - context.notify_begin(UR_FUNCTION_INIT, "urInit", ¶ms); - - ur_result_t result = pfnInit(device_flags, hLoaderConfig); - - context.notify_end(UR_FUNCTION_INIT, "urInit", ¶ms, &result, instance); - - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urTearDown -__urdlllocal ur_result_t UR_APICALL urTearDown( - void *pParams ///< [in] pointer to tear down parameters -) { - auto pfnTearDown = context.urDdiTable.Global.pfnTearDown; - - if (nullptr == pfnTearDown) { - return UR_RESULT_ERROR_UNSUPPORTED_FEATURE; - } - - ur_tear_down_params_t params = {&pParams}; - uint64_t instance = - context.notify_begin(UR_FUNCTION_TEAR_DOWN, "urTearDown", ¶ms); - - ur_result_t result = pfnTearDown(pParams); - - context.notify_end(UR_FUNCTION_TEAR_DOWN, "urTearDown", ¶ms, &result, - instance); - - return result; -} - /////////////////////////////////////////////////////////////////////////////// /// @brief Intercept function for urAdapterGet __urdlllocal ur_result_t UR_APICALL urAdapterGet( @@ -5828,12 +5780,6 @@ __urdlllocal ur_result_t UR_APICALL urGetGlobalProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - dditable.pfnInit = pDdiTable->pfnInit; - pDdiTable->pfnInit = ur_tracing_layer::urInit; - - dditable.pfnTearDown = pDdiTable->pfnTearDown; - pDdiTable->pfnTearDown = ur_tracing_layer::urTearDown; - dditable.pfnAdapterGet = pDdiTable->pfnAdapterGet; pDdiTable->pfnAdapterGet = ur_tracing_layer::urAdapterGet; diff --git a/source/loader/layers/ur_proxy_layer.hpp b/source/loader/layers/ur_proxy_layer.hpp index 782a7e241b..20bb47394e 100644 --- a/source/loader/layers/ur_proxy_layer.hpp +++ b/source/loader/layers/ur_proxy_layer.hpp @@ -27,6 +27,7 @@ class __urdlllocal proxy_layer_context_t { virtual ur_result_t init(ur_dditable_t *dditable, const std::set &enabledLayerNames) = 0; + virtual ur_result_t tearDown() = 0; }; #endif /* UR_PROXY_LAYER_H */ diff --git a/source/loader/layers/validation/ur_leak_check.hpp b/source/loader/layers/validation/ur_leak_check.hpp index 475742fc75..3f3fb80b0b 100644 --- a/source/loader/layers/validation/ur_leak_check.hpp +++ b/source/loader/layers/validation/ur_leak_check.hpp @@ -24,6 +24,7 @@ struct RefCountContext { }; enum RefCountUpdateType { + REFCOUNT_CREATE_OR_INCREASE, REFCOUNT_CREATE, REFCOUNT_INCREASE, REFCOUNT_DECREASE, @@ -31,13 +32,25 @@ struct RefCountContext { std::mutex mutex; std::unordered_map counts; + int64_t adapterCount = 0; - void updateRefCount(void *ptr, enum RefCountUpdateType type) { + void updateRefCount(void *ptr, enum RefCountUpdateType type, + bool isAdapterHandle = false) { std::unique_lock ulock(mutex); auto it = counts.find(ptr); switch (type) { + case REFCOUNT_CREATE_OR_INCREASE: + if (it == counts.end()) { + counts[ptr] = {1, getCurrentBacktrace()}; + if (isAdapterHandle) { + adapterCount++; + } + } else { + counts[ptr].refCount++; + } + break; case REFCOUNT_CREATE: if (it == counts.end()) { counts[ptr] = {1, getCurrentBacktrace()}; @@ -65,6 +78,8 @@ struct RefCountContext { if (counts[ptr].refCount < 0) { context.logger.error( "Attempting to release nonexistent handle {}", ptr); + } else if (counts[ptr].refCount == 0 && isAdapterHandle) { + adapterCount--; } break; } @@ -75,17 +90,27 @@ struct RefCountContext { if (counts[ptr].refCount == 0) { counts.erase(ptr); } + + // No more active adapters, so any references still held are leaked + if (adapterCount == 0) { + logInvalidReferences(); + clear(); + } } public: void createRefCount(void *ptr) { updateRefCount(ptr, REFCOUNT_CREATE); } - void incrementRefCount(void *ptr) { - updateRefCount(ptr, REFCOUNT_INCREASE); + void incrementRefCount(void *ptr, bool isAdapterHandle = false) { + updateRefCount(ptr, REFCOUNT_INCREASE, isAdapterHandle); + } + + void decrementRefCount(void *ptr, bool isAdapterHandle = false) { + updateRefCount(ptr, REFCOUNT_DECREASE, isAdapterHandle); } - void decrementRefCount(void *ptr) { - updateRefCount(ptr, REFCOUNT_DECREASE); + void createOrIncrementRefCount(void *ptr, bool isAdapterHandle = false) { + updateRefCount(ptr, REFCOUNT_CREATE_OR_INCREASE, isAdapterHandle); } void clear() { counts.clear(); } diff --git a/source/loader/layers/validation/ur_valddi.cpp b/source/loader/layers/validation/ur_valddi.cpp index 46b0eef491..08cf04c672 100644 --- a/source/loader/layers/validation/ur_valddi.cpp +++ b/source/loader/layers/validation/ur_valddi.cpp @@ -14,58 +14,6 @@ namespace ur_validation_layer { -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urInit -__urdlllocal ur_result_t UR_APICALL urInit( - ur_device_init_flags_t device_flags, ///< [in] device initialization flags. - ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. - ur_loader_config_handle_t - hLoaderConfig ///< [in][optional] Handle of loader config handle. -) { - auto pfnInit = context.urDdiTable.Global.pfnInit; - - if (nullptr == pfnInit) { - return UR_RESULT_ERROR_UNINITIALIZED; - } - - if (context.enableParameterValidation) { - if (UR_DEVICE_INIT_FLAGS_MASK & device_flags) { - return UR_RESULT_ERROR_INVALID_ENUMERATION; - } - } - - ur_result_t result = pfnInit(device_flags, hLoaderConfig); - - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urTearDown -__urdlllocal ur_result_t UR_APICALL urTearDown( - void *pParams ///< [in] pointer to tear down parameters -) { - auto pfnTearDown = context.urDdiTable.Global.pfnTearDown; - - if (nullptr == pfnTearDown) { - return UR_RESULT_ERROR_UNINITIALIZED; - } - - if (context.enableParameterValidation) { - if (NULL == pParams) { - return UR_RESULT_ERROR_INVALID_NULL_POINTER; - } - } - - ur_result_t result = pfnTearDown(pParams); - - if (context.enableLeakChecking) { - refCountContext.logInvalidReferences(); - refCountContext.clear(); - } - - return result; -} - /////////////////////////////////////////////////////////////////////////////// /// @brief Intercept function for urAdapterGet __urdlllocal ur_result_t UR_APICALL urAdapterGet( @@ -92,6 +40,11 @@ __urdlllocal ur_result_t UR_APICALL urAdapterGet( ur_result_t result = pfnAdapterGet(NumEntries, phAdapters, pNumAdapters); + if (context.enableLeakChecking && phAdapters && + result == UR_RESULT_SUCCESS) { + refCountContext.createOrIncrementRefCount(*phAdapters, true); + } + return result; } @@ -115,7 +68,7 @@ __urdlllocal ur_result_t UR_APICALL urAdapterRelease( ur_result_t result = pfnAdapterRelease(hAdapter); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { - refCountContext.decrementRefCount(hAdapter); + refCountContext.decrementRefCount(hAdapter, true); } return result; @@ -141,7 +94,7 @@ __urdlllocal ur_result_t UR_APICALL urAdapterRetain( ur_result_t result = pfnAdapterRetain(hAdapter); if (context.enableLeakChecking && result == UR_RESULT_SUCCESS) { - refCountContext.incrementRefCount(hAdapter); + refCountContext.decrementRefCount(hAdapter, true); } return result; @@ -7183,12 +7136,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetGlobalProcAddrTable( ur_result_t result = UR_RESULT_SUCCESS; - dditable.pfnInit = pDdiTable->pfnInit; - pDdiTable->pfnInit = ur_validation_layer::urInit; - - dditable.pfnTearDown = pDdiTable->pfnTearDown; - pDdiTable->pfnTearDown = ur_validation_layer::urTearDown; - dditable.pfnAdapterGet = pDdiTable->pfnAdapterGet; pDdiTable->pfnAdapterGet = ur_validation_layer::urAdapterGet; @@ -8409,4 +8356,14 @@ ur_result_t context_t::init(ur_dditable_t *dditable, return result; } +ur_result_t context_t::tearDown() { + ur_result_t result = UR_RESULT_SUCCESS; + + if (enableLeakChecking) { + refCountContext.logInvalidReferences(); + refCountContext.clear(); + } + return result; +} + } // namespace ur_validation_layer diff --git a/source/loader/layers/validation/ur_validation_layer.hpp b/source/loader/layers/validation/ur_validation_layer.hpp index 3201a5345e..bab9e56e88 100644 --- a/source/loader/layers/validation/ur_validation_layer.hpp +++ b/source/loader/layers/validation/ur_validation_layer.hpp @@ -35,6 +35,7 @@ class __urdlllocal context_t : public proxy_layer_context_t { } ur_result_t init(ur_dditable_t *dditable, const std::set &enabledLayerNames) override; + ur_result_t tearDown() override; private: const std::string nameFullValidation = "UR_LAYER_FULL_VALIDATION"; diff --git a/source/loader/ur_ldrddi.cpp b/source/loader/ur_ldrddi.cpp index e192088bbc..ce615c9945 100644 --- a/source/loader/ur_ldrddi.cpp +++ b/source/loader/ur_ldrddi.cpp @@ -33,41 +33,6 @@ ur_exp_interop_mem_factory_t ur_exp_interop_mem_factory; ur_exp_interop_semaphore_factory_t ur_exp_interop_semaphore_factory; ur_exp_command_buffer_factory_t ur_exp_command_buffer_factory; -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urInit -__urdlllocal ur_result_t UR_APICALL urInit( - ur_device_init_flags_t device_flags, ///< [in] device initialization flags. - ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. - ur_loader_config_handle_t - hLoaderConfig ///< [in][optional] Handle of loader config handle. -) { - ur_result_t result = UR_RESULT_SUCCESS; - - for (auto &platform : context->platforms) { - if (platform.initStatus != UR_RESULT_SUCCESS) { - continue; - } - platform.initStatus = - platform.dditable.ur.Global.pfnInit(device_flags, hLoaderConfig); - } - - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Intercept function for urTearDown -__urdlllocal ur_result_t UR_APICALL urTearDown( - void *pParams ///< [in] pointer to tear down parameters -) { - ur_result_t result = UR_RESULT_SUCCESS; - - for (auto &platform : context->platforms) { - platform.dditable.ur.Global.pfnTearDown(pParams); - } - - return result; -} - /////////////////////////////////////////////////////////////////////////////// /// @brief Intercept function for urAdapterGet __urdlllocal ur_result_t UR_APICALL urAdapterGet( @@ -6994,8 +6959,6 @@ UR_DLLEXPORT ur_result_t UR_APICALL urGetGlobalProcAddrTable( if (ur_loader::context->platforms.size() != 1 || ur_loader::context->forceIntercept) { // return pointers to loader's DDIs - pDdiTable->pfnInit = ur_loader::urInit; - pDdiTable->pfnTearDown = ur_loader::urTearDown; pDdiTable->pfnAdapterGet = ur_loader::urAdapterGet; pDdiTable->pfnAdapterRelease = ur_loader::urAdapterRelease; pDdiTable->pfnAdapterRetain = ur_loader::urAdapterRetain; diff --git a/source/loader/ur_lib.cpp b/source/loader/ur_lib.cpp index 964da234f1..e1c0ed5e2f 100644 --- a/source/loader/ur_lib.cpp +++ b/source/loader/ur_lib.cpp @@ -60,6 +60,14 @@ void context_t::initLayers() const { } } +void context_t::tearDownLayers() const { + for (auto &l : layers) { + if (l->isAvailable()) { + l->tearDown(); + } + } +} + ////////////////////////////////////////////////////////////////////////// __urdlllocal ur_result_t context_t::Init(ur_device_init_flags_t device_flags, @@ -72,7 +80,7 @@ context_t::Init(ur_device_init_flags_t device_flags, result = ur_loader::context->init(); if (UR_RESULT_SUCCESS == result) { - result = urInit(); + result = urLoaderInit(); } if (hLoaderConfig) { @@ -174,4 +182,10 @@ ur_result_t urLoaderConfigEnableLayer(ur_loader_config_handle_t hLoaderConfig, hLoaderConfig->enabledLayers.insert(pLayerName); return UR_RESULT_SUCCESS; } + +ur_result_t urLoaderTearDown() { + context->tearDownLayers(); + + return UR_RESULT_SUCCESS; +} } // namespace ur_lib diff --git a/source/loader/ur_lib.hpp b/source/loader/ur_lib.hpp index 1f0f23658b..58ee62b936 100644 --- a/source/loader/ur_lib.hpp +++ b/source/loader/ur_lib.hpp @@ -60,7 +60,7 @@ class __urdlllocal context_t { ur_result_t Init(ur_device_init_flags_t dflags, ur_loader_config_handle_t hLoaderConfig); - ur_result_t urInit(); + ur_result_t urLoaderInit(); ur_dditable_t urDdiTable = {}; const std::vector layers = { @@ -75,6 +75,7 @@ class __urdlllocal context_t { bool layerExists(const std::string &layerName) const; void parseEnvEnabledLayers(); void initLayers() const; + void tearDownLayers() const; }; extern context_t *context; @@ -87,5 +88,6 @@ ur_result_t urLoaderConfigGetInfo(ur_loader_config_handle_t hLoaderConfig, size_t *pPropSizeRet); ur_result_t urLoaderConfigEnableLayer(ur_loader_config_handle_t hLoaderConfig, const char *pLayerName); +ur_result_t urLoaderTearDown(); } // namespace ur_lib #endif /* UR_LOADER_LIB_H */ diff --git a/source/loader/ur_libapi.cpp b/source/loader/ur_libapi.cpp index 7a64efd088..9ae33fe2d2 100644 --- a/source/loader/ur_libapi.cpp +++ b/source/loader/ur_libapi.cpp @@ -15,222 +15,6 @@ extern "C" { -/////////////////////////////////////////////////////////////////////////////// -/// @brief Create a loader config object. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == phLoaderConfig` -ur_result_t UR_APICALL urLoaderConfigCreate( - ur_loader_config_handle_t * - phLoaderConfig ///< [out] Pointer to handle of loader config object created. - ) try { - return ur_lib::urLoaderConfigCreate(phLoaderConfig); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Get a reference to the loader config object. -/// -/// @details -/// - Get a reference to the loader config handle. Increment its reference -/// count -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -ur_result_t UR_APICALL urLoaderConfigRetain( - ur_loader_config_handle_t - hLoaderConfig ///< [in] loader config handle to retain - ) try { - return ur_lib::urLoaderConfigRetain(hLoaderConfig); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Release config handle. -/// -/// @details -/// - Decrement reference count and destroy the config handle if reference -/// count becomes zero. -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -ur_result_t UR_APICALL urLoaderConfigRelease( - ur_loader_config_handle_t hLoaderConfig ///< [in] config handle to release - ) try { - return ur_lib::urLoaderConfigRelease(hLoaderConfig); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Retrieves various information about the loader. -/// -/// @details -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION -/// + `::UR_LOADER_CONFIG_INFO_REFERENCE_COUNT < propName` -/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION -/// + If `propName` is not supported by the loader. -/// - ::UR_RESULT_ERROR_INVALID_SIZE -/// + `propSize == 0 && pPropValue != NULL` -/// + If `propSize` is less than the real number of bytes needed to return the info. -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `propSize != 0 && pPropValue == NULL` -/// + `pPropValue == NULL && pPropSizeRet == NULL` -/// - ::UR_RESULT_ERROR_INVALID_DEVICE -/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -ur_result_t UR_APICALL urLoaderConfigGetInfo( - ur_loader_config_handle_t - hLoaderConfig, ///< [in] handle of the loader config object - ur_loader_config_info_t propName, ///< [in] type of the info to retrieve - size_t propSize, ///< [in] the number of bytes pointed to by pPropValue. - void * - pPropValue, ///< [out][optional][typename(propName, propSize)] array of bytes holding - ///< the info. - ///< If propSize is not equal to or greater than the real number of bytes - ///< needed to return the info - ///< then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and - ///< pPropValue is not used. - size_t * - pPropSizeRet ///< [out][optional] pointer to the actual size in bytes of the queried propName. - ) try { - return ur_lib::urLoaderConfigGetInfo(hLoaderConfig, propName, propSize, - pPropValue, pPropSizeRet); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Enable a layer for the specified loader config. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hLoaderConfig` -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == pLayerName` -/// - ::UR_RESULT_ERROR_LAYER_NOT_PRESENT -/// + If layer specified with `pLayerName` can't be found by the loader. -ur_result_t UR_APICALL urLoaderConfigEnableLayer( - ur_loader_config_handle_t - hLoaderConfig, ///< [in] Handle to config object the layer will be enabled for. - const char * - pLayerName ///< [in] Null terminated string containing the name of the layer to - ///< enable. - ) try { - return ur_lib::urLoaderConfigEnableLayer(hLoaderConfig, pLayerName); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Initialize the 'oneAPI' adapter(s) -/// -/// @details -/// - The application must call this function before calling any other -/// function. -/// - If this function is not called then all other functions will return -/// ::UR_RESULT_ERROR_UNINITIALIZED. -/// - Only one instance of each adapter will be initialized per process. -/// - The application may call this function multiple times with different -/// flags or environment variables enabled. -/// - The application must call this function after forking new processes. -/// Each forked process must call this function. -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function must be thread-safe for scenarios -/// where multiple libraries may initialize the adapter(s) simultaneously. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION -/// + `::UR_DEVICE_INIT_FLAGS_MASK & device_flags` -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -ur_result_t UR_APICALL urInit( - ur_device_init_flags_t device_flags, ///< [in] device initialization flags. - ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. - ur_loader_config_handle_t - hLoaderConfig ///< [in][optional] Handle of loader config handle. - ) try { - static ur_result_t result = UR_RESULT_SUCCESS; - std::call_once(ur_lib::context->initOnce, [device_flags, hLoaderConfig]() { - result = ur_lib::context->Init(device_flags, hLoaderConfig); - }); - - if (UR_RESULT_SUCCESS != result) { - return result; - } - - auto pfnInit = ur_lib::context->urDdiTable.Global.pfnInit; - if (nullptr == pfnInit) { - return UR_RESULT_ERROR_UNINITIALIZED; - } - - return pfnInit(device_flags, hLoaderConfig); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Tear down the 'oneAPI' instance and release all its resources -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == pParams` -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -ur_result_t UR_APICALL urTearDown( - void *pParams ///< [in] pointer to tear down parameters - ) try { - auto pfnTearDown = ur_lib::context->urDdiTable.Global.pfnTearDown; - if (nullptr == pfnTearDown) { - return UR_RESULT_ERROR_UNINITIALIZED; - } - - return pfnTearDown(pParams); -} catch (...) { - return exceptionToResult(std::current_exception()); -} - /////////////////////////////////////////////////////////////////////////////// /// @brief Retrieves all available adapters /// @@ -280,7 +64,9 @@ ur_result_t UR_APICALL urAdapterGet( /// /// @details /// - When the reference count of the adapter reaches zero, the adapter may -/// perform adapter-specififc resource teardown +/// perform adapter-specififc resource teardown. Resources must be left in +/// a state where it safe for the adapter to be subsequently reinitialized +/// with ::urAdapterGet /// /// @returns /// - ::UR_RESULT_SUCCESS @@ -443,6 +229,209 @@ ur_result_t UR_APICALL urAdapterGetInfo( return exceptionToResult(std::current_exception()); } +/////////////////////////////////////////////////////////////////////////////// +/// @brief Create a loader config object. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NULL == phLoaderConfig` +ur_result_t UR_APICALL urLoaderConfigCreate( + ur_loader_config_handle_t * + phLoaderConfig ///< [out] Pointer to handle of loader config object created. + ) try { + return ur_lib::urLoaderConfigCreate(phLoaderConfig); +} catch (...) { + return exceptionToResult(std::current_exception()); +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Get a reference to the loader config object. +/// +/// @details +/// - Get a reference to the loader config handle. Increment its reference +/// count +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +ur_result_t UR_APICALL urLoaderConfigRetain( + ur_loader_config_handle_t + hLoaderConfig ///< [in] loader config handle to retain + ) try { + return ur_lib::urLoaderConfigRetain(hLoaderConfig); +} catch (...) { + return exceptionToResult(std::current_exception()); +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Release config handle. +/// +/// @details +/// - Decrement reference count and destroy the config handle if reference +/// count becomes zero. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +ur_result_t UR_APICALL urLoaderConfigRelease( + ur_loader_config_handle_t hLoaderConfig ///< [in] config handle to release + ) try { + return ur_lib::urLoaderConfigRelease(hLoaderConfig); +} catch (...) { + return exceptionToResult(std::current_exception()); +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Retrieves various information about the loader. +/// +/// @details +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION +/// + `::UR_LOADER_CONFIG_INFO_REFERENCE_COUNT < propName` +/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION +/// + If `propName` is not supported by the loader. +/// - ::UR_RESULT_ERROR_INVALID_SIZE +/// + `propSize == 0 && pPropValue != NULL` +/// + If `propSize` is less than the real number of bytes needed to return the info. +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `propSize != 0 && pPropValue == NULL` +/// + `pPropValue == NULL && pPropSizeRet == NULL` +/// - ::UR_RESULT_ERROR_INVALID_DEVICE +/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +ur_result_t UR_APICALL urLoaderConfigGetInfo( + ur_loader_config_handle_t + hLoaderConfig, ///< [in] handle of the loader config object + ur_loader_config_info_t propName, ///< [in] type of the info to retrieve + size_t propSize, ///< [in] the number of bytes pointed to by pPropValue. + void * + pPropValue, ///< [out][optional][typename(propName, propSize)] array of bytes holding + ///< the info. + ///< If propSize is not equal to or greater than the real number of bytes + ///< needed to return the info + ///< then the ::UR_RESULT_ERROR_INVALID_SIZE error is returned and + ///< pPropValue is not used. + size_t * + pPropSizeRet ///< [out][optional] pointer to the actual size in bytes of the queried propName. + ) try { + return ur_lib::urLoaderConfigGetInfo(hLoaderConfig, propName, propSize, + pPropValue, pPropSizeRet); +} catch (...) { + return exceptionToResult(std::current_exception()); +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Enable a layer for the specified loader config. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hLoaderConfig` +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NULL == pLayerName` +/// - ::UR_RESULT_ERROR_LAYER_NOT_PRESENT +/// + If layer specified with `pLayerName` can't be found by the loader. +ur_result_t UR_APICALL urLoaderConfigEnableLayer( + ur_loader_config_handle_t + hLoaderConfig, ///< [in] Handle to config object the layer will be enabled for. + const char * + pLayerName ///< [in] Null terminated string containing the name of the layer to + ///< enable. + ) try { + return ur_lib::urLoaderConfigEnableLayer(hLoaderConfig, pLayerName); +} catch (...) { + return exceptionToResult(std::current_exception()); +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Initialize the 'oneAPI' loader +/// +/// @details +/// - The application must call this function before calling any other +/// function. +/// - If this function is not called then all other functions will return +/// ::UR_RESULT_ERROR_UNINITIALIZED. +/// - Only one instance of the loader will be initialized per process. +/// - The application may call this function multiple times with different +/// flags or environment variables enabled. +/// - The application must call this function after forking new processes. +/// Each forked process must call this function. +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function must be thread-safe for scenarios +/// where multiple libraries may initialize the loader simultaneously. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION +/// + `::UR_DEVICE_INIT_FLAGS_MASK & device_flags` +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +ur_result_t UR_APICALL urLoaderInit( + ur_device_init_flags_t device_flags, ///< [in] device initialization flags. + ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. + ur_loader_config_handle_t + hLoaderConfig ///< [in][optional] Handle of loader config handle. + ) try { + + if (UR_DEVICE_INIT_FLAGS_MASK & device_flags) { + return UR_RESULT_ERROR_INVALID_ENUMERATION; + } + + static ur_result_t result = UR_RESULT_SUCCESS; + std::call_once(ur_lib::context->initOnce, [device_flags, hLoaderConfig]() { + result = ur_lib::context->Init(device_flags, hLoaderConfig); + }); + + return result; +} catch (...) { + return exceptionToResult(std::current_exception()); +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Tear down the 'oneAPI' loader and release all its resources +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +ur_result_t UR_APICALL urLoaderTearDown(void) try { + return ur_lib::urLoaderTearDown(); +} catch (...) { + return exceptionToResult(std::current_exception()); +} + /////////////////////////////////////////////////////////////////////////////// /// @brief Retrieves all available platforms for the given adapters /// diff --git a/source/loader/ur_libddi.cpp b/source/loader/ur_libddi.cpp index 1328a2b071..912449b54e 100644 --- a/source/loader/ur_libddi.cpp +++ b/source/loader/ur_libddi.cpp @@ -17,7 +17,7 @@ namespace ur_lib { /////////////////////////////////////////////////////////////////////////////// -__urdlllocal ur_result_t context_t::urInit() { +__urdlllocal ur_result_t context_t::urLoaderInit() { ur_result_t result = UR_RESULT_SUCCESS; if (UR_RESULT_SUCCESS == result) { diff --git a/source/ur_api.cpp b/source/ur_api.cpp index 456c584d68..0d6c2c07f2 100644 --- a/source/ur_api.cpp +++ b/source/ur_api.cpp @@ -12,6 +12,186 @@ */ #include "ur_api.h" +/////////////////////////////////////////////////////////////////////////////// +/// @brief Retrieves all available adapters +/// +/// @details +/// - Adapter implementations must return exactly one adapter handle from +/// this entry point. +/// - The loader may return more than one adapter handle when there are +/// multiple available. +/// - Each returned adapter has its reference count incremented and should +/// be released with a subsequent call to ::urAdapterRelease. +/// - Adapters may perform adapter-specific state initialization when the +/// first reference to them is taken. +/// - An application may call this entry point multiple times to acquire +/// multiple references to the adapter handle(s). +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_SIZE +ur_result_t UR_APICALL urAdapterGet( + uint32_t + NumEntries, ///< [in] the number of adapters to be added to phAdapters. + ///< If phAdapters is not NULL, then NumEntries should be greater than + ///< zero, otherwise ::UR_RESULT_ERROR_INVALID_SIZE, + ///< will be returned. + ur_adapter_handle_t * + phAdapters, ///< [out][optional][range(0, NumEntries)] array of handle of adapters. + ///< If NumEntries is less than the number of adapters available, then + ///< ::urAdapterGet shall only retrieve that number of platforms. + uint32_t * + pNumAdapters ///< [out][optional] returns the total number of adapters available. +) { + ur_result_t result = UR_RESULT_SUCCESS; + return result; +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Releases the adapter handle reference indicating end of its usage +/// +/// @details +/// - When the reference count of the adapter reaches zero, the adapter may +/// perform adapter-specififc resource teardown. Resources must be left in +/// a state where it safe for the adapter to be subsequently reinitialized +/// with ::urAdapterGet +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` +ur_result_t UR_APICALL urAdapterRelease( + ur_adapter_handle_t hAdapter ///< [in] Adapter handle to release +) { + ur_result_t result = UR_RESULT_SUCCESS; + return result; +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Get a reference to the adapter handle. +/// +/// @details +/// - Get a reference to the adapter handle. Increment its reference count +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` +ur_result_t UR_APICALL urAdapterRetain( + ur_adapter_handle_t hAdapter ///< [in] Adapter handle to retain +) { + ur_result_t result = UR_RESULT_SUCCESS; + return result; +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Get the last adapter specific error. +/// +/// @details +/// To be used after another entry-point has returned +/// ::UR_RESULT_ERROR_ADAPTER_SPECIFIC in order to retrieve a message describing +/// the circumstances of the underlying driver error and the error code +/// returned by the failed driver entry-point. +/// +/// * Implementations *must* store the message and error code in thread-local +/// storage prior to returning ::UR_RESULT_ERROR_ADAPTER_SPECIFIC. +/// +/// * The message and error code storage is will only be valid if a previously +/// called entry-point returned ::UR_RESULT_ERROR_ADAPTER_SPECIFIC. +/// +/// * The memory pointed to by the C string returned in `ppMessage` is owned by +/// the adapter and *must* be null terminated. +/// +/// * The application *may* call this function from simultaneous threads. +/// +/// * The implementation of this function *should* be lock-free. +/// +/// Example usage: +/// +/// ```cpp +/// if (::urQueueCreate(hContext, hDevice, nullptr, &hQueue) == +/// ::UR_RESULT_ERROR_ADAPTER_SPECIFIC) { +/// const char* pMessage; +/// int32_t error; +/// ::urAdapterGetLastError(hAdapter, &pMessage, &error); +/// } +/// ``` +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `NULL == ppMessage` +/// + `NULL == pError` +ur_result_t UR_APICALL urAdapterGetLastError( + ur_adapter_handle_t hAdapter, ///< [in] handle of the adapter instance + const char ** + ppMessage, ///< [out] pointer to a C string where the adapter specific error message + ///< will be stored. + int32_t * + pError ///< [out] pointer to an integer where the adapter specific error code will + ///< be stored. +) { + ur_result_t result = UR_RESULT_SUCCESS; + return result; +} + +/////////////////////////////////////////////////////////////////////////////// +/// @brief Retrieves information about the adapter +/// +/// @details +/// - The application may call this function from simultaneous threads. +/// - The implementation of this function should be lock-free. +/// +/// @returns +/// - ::UR_RESULT_SUCCESS +/// - ::UR_RESULT_ERROR_UNINITIALIZED +/// - ::UR_RESULT_ERROR_DEVICE_LOST +/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC +/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE +/// + `NULL == hAdapter` +/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION +/// + `::UR_ADAPTER_INFO_REFERENCE_COUNT < propName` +/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION +/// + If `propName` is not supported by the adapter. +/// - ::UR_RESULT_ERROR_INVALID_SIZE +/// + `propSize == 0 && pPropValue != NULL` +/// + If `propSize` is less than the real number of bytes needed to return the info. +/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER +/// + `propSize != 0 && pPropValue == NULL` +/// + `pPropValue == NULL && pPropSizeRet == NULL` +/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES +/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY +ur_result_t UR_APICALL urAdapterGetInfo( + ur_adapter_handle_t hAdapter, ///< [in] handle of the adapter + ur_adapter_info_t propName, ///< [in] type of the info to retrieve + size_t propSize, ///< [in] the number of bytes pointed to by pPropValue. + void * + pPropValue, ///< [out][optional][typename(propName, propSize)] array of bytes holding + ///< the info. + ///< If Size is not equal to or greater to the real number of bytes needed + ///< to return the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is + ///< returned and pPropValue is not used. + size_t * + pPropSizeRet ///< [out][optional] pointer to the actual number of bytes being queried by pPropValue. +) { + ur_result_t result = UR_RESULT_SUCCESS; + return result; +} + /////////////////////////////////////////////////////////////////////////////// /// @brief Create a loader config object. /// @@ -149,21 +329,21 @@ ur_result_t UR_APICALL urLoaderConfigEnableLayer( } /////////////////////////////////////////////////////////////////////////////// -/// @brief Initialize the 'oneAPI' adapter(s) +/// @brief Initialize the 'oneAPI' loader /// /// @details /// - The application must call this function before calling any other /// function. /// - If this function is not called then all other functions will return /// ::UR_RESULT_ERROR_UNINITIALIZED. -/// - Only one instance of each adapter will be initialized per process. +/// - Only one instance of the loader will be initialized per process. /// - The application may call this function multiple times with different /// flags or environment variables enabled. /// - The application must call this function after forking new processes. /// Each forked process must call this function. /// - The application may call this function from simultaneous threads. /// - The implementation of this function must be thread-safe for scenarios -/// where multiple libraries may initialize the adapter(s) simultaneously. +/// where multiple libraries may initialize the loader simultaneously. /// /// @returns /// - ::UR_RESULT_SUCCESS @@ -173,7 +353,7 @@ ur_result_t UR_APICALL urLoaderConfigEnableLayer( /// - ::UR_RESULT_ERROR_INVALID_ENUMERATION /// + `::UR_DEVICE_INIT_FLAGS_MASK & device_flags` /// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -ur_result_t UR_APICALL urInit( +ur_result_t UR_APICALL urLoaderInit( ur_device_init_flags_t device_flags, ///< [in] device initialization flags. ///< must be 0 (default) or a combination of ::ur_device_init_flag_t. ur_loader_config_handle_t @@ -184,197 +364,15 @@ ur_result_t UR_APICALL urInit( } /////////////////////////////////////////////////////////////////////////////// -/// @brief Tear down the 'oneAPI' instance and release all its resources -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == pParams` -/// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -ur_result_t UR_APICALL urTearDown( - void *pParams ///< [in] pointer to tear down parameters -) { - ur_result_t result = UR_RESULT_SUCCESS; - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Retrieves all available adapters -/// -/// @details -/// - Adapter implementations must return exactly one adapter handle from -/// this entry point. -/// - The loader may return more than one adapter handle when there are -/// multiple available. -/// - Each returned adapter has its reference count incremented and should -/// be released with a subsequent call to ::urAdapterRelease. -/// - Adapters may perform adapter-specific state initialization when the -/// first reference to them is taken. -/// - An application may call this entry point multiple times to acquire -/// multiple references to the adapter handle(s). -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_SIZE -ur_result_t UR_APICALL urAdapterGet( - uint32_t - NumEntries, ///< [in] the number of adapters to be added to phAdapters. - ///< If phAdapters is not NULL, then NumEntries should be greater than - ///< zero, otherwise ::UR_RESULT_ERROR_INVALID_SIZE, - ///< will be returned. - ur_adapter_handle_t * - phAdapters, ///< [out][optional][range(0, NumEntries)] array of handle of adapters. - ///< If NumEntries is less than the number of adapters available, then - ///< ::urAdapterGet shall only retrieve that number of platforms. - uint32_t * - pNumAdapters ///< [out][optional] returns the total number of adapters available. -) { - ur_result_t result = UR_RESULT_SUCCESS; - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Releases the adapter handle reference indicating end of its usage -/// -/// @details -/// - When the reference count of the adapter reaches zero, the adapter may -/// perform adapter-specififc resource teardown -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hAdapter` -ur_result_t UR_APICALL urAdapterRelease( - ur_adapter_handle_t hAdapter ///< [in] Adapter handle to release -) { - ur_result_t result = UR_RESULT_SUCCESS; - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Get a reference to the adapter handle. -/// -/// @details -/// - Get a reference to the adapter handle. Increment its reference count -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hAdapter` -ur_result_t UR_APICALL urAdapterRetain( - ur_adapter_handle_t hAdapter ///< [in] Adapter handle to retain -) { - ur_result_t result = UR_RESULT_SUCCESS; - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Get the last adapter specific error. -/// -/// @details -/// To be used after another entry-point has returned -/// ::UR_RESULT_ERROR_ADAPTER_SPECIFIC in order to retrieve a message describing -/// the circumstances of the underlying driver error and the error code -/// returned by the failed driver entry-point. -/// -/// * Implementations *must* store the message and error code in thread-local -/// storage prior to returning ::UR_RESULT_ERROR_ADAPTER_SPECIFIC. -/// -/// * The message and error code storage is will only be valid if a previously -/// called entry-point returned ::UR_RESULT_ERROR_ADAPTER_SPECIFIC. -/// -/// * The memory pointed to by the C string returned in `ppMessage` is owned by -/// the adapter and *must* be null terminated. -/// -/// * The application *may* call this function from simultaneous threads. -/// -/// * The implementation of this function *should* be lock-free. -/// -/// Example usage: -/// -/// ```cpp -/// if (::urQueueCreate(hContext, hDevice, nullptr, &hQueue) == -/// ::UR_RESULT_ERROR_ADAPTER_SPECIFIC) { -/// const char* pMessage; -/// int32_t error; -/// ::urAdapterGetLastError(hAdapter, &pMessage, &error); -/// } -/// ``` +/// @brief Tear down the 'oneAPI' loader and release all its resources /// /// @returns /// - ::UR_RESULT_SUCCESS /// - ::UR_RESULT_ERROR_UNINITIALIZED /// - ::UR_RESULT_ERROR_DEVICE_LOST /// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hAdapter` -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `NULL == ppMessage` -/// + `NULL == pError` -ur_result_t UR_APICALL urAdapterGetLastError( - ur_adapter_handle_t hAdapter, ///< [in] handle of the adapter instance - const char ** - ppMessage, ///< [out] pointer to a C string where the adapter specific error message - ///< will be stored. - int32_t * - pError ///< [out] pointer to an integer where the adapter specific error code will - ///< be stored. -) { - ur_result_t result = UR_RESULT_SUCCESS; - return result; -} - -/////////////////////////////////////////////////////////////////////////////// -/// @brief Retrieves information about the adapter -/// -/// @details -/// - The application may call this function from simultaneous threads. -/// - The implementation of this function should be lock-free. -/// -/// @returns -/// - ::UR_RESULT_SUCCESS -/// - ::UR_RESULT_ERROR_UNINITIALIZED -/// - ::UR_RESULT_ERROR_DEVICE_LOST -/// - ::UR_RESULT_ERROR_ADAPTER_SPECIFIC -/// - ::UR_RESULT_ERROR_INVALID_NULL_HANDLE -/// + `NULL == hAdapter` -/// - ::UR_RESULT_ERROR_INVALID_ENUMERATION -/// + `::UR_ADAPTER_INFO_REFERENCE_COUNT < propName` -/// - ::UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION -/// + If `propName` is not supported by the adapter. -/// - ::UR_RESULT_ERROR_INVALID_SIZE -/// + `propSize == 0 && pPropValue != NULL` -/// + If `propSize` is less than the real number of bytes needed to return the info. -/// - ::UR_RESULT_ERROR_INVALID_NULL_POINTER -/// + `propSize != 0 && pPropValue == NULL` -/// + `pPropValue == NULL && pPropSizeRet == NULL` -/// - ::UR_RESULT_ERROR_OUT_OF_RESOURCES /// - ::UR_RESULT_ERROR_OUT_OF_HOST_MEMORY -ur_result_t UR_APICALL urAdapterGetInfo( - ur_adapter_handle_t hAdapter, ///< [in] handle of the adapter - ur_adapter_info_t propName, ///< [in] type of the info to retrieve - size_t propSize, ///< [in] the number of bytes pointed to by pPropValue. - void * - pPropValue, ///< [out][optional][typename(propName, propSize)] array of bytes holding - ///< the info. - ///< If Size is not equal to or greater to the real number of bytes needed - ///< to return the info then the ::UR_RESULT_ERROR_INVALID_SIZE error is - ///< returned and pPropValue is not used. - size_t * - pPropSizeRet ///< [out][optional] pointer to the actual number of bytes being queried by pPropValue. -) { +ur_result_t UR_APICALL urLoaderTearDown(void) { ur_result_t result = UR_RESULT_SUCCESS; return result; } diff --git a/test/conformance/CMakeLists.txt b/test/conformance/CMakeLists.txt index 2cb78a9106..b64f9798d4 100644 --- a/test/conformance/CMakeLists.txt +++ b/test/conformance/CMakeLists.txt @@ -46,7 +46,7 @@ endfunction() add_subdirectory(testing) -add_subdirectory(runtime) +add_subdirectory(adapter) add_subdirectory(platform) add_subdirectory(device) add_subdirectory(context) diff --git a/test/conformance/runtime/CMakeLists.txt b/test/conformance/adapter/CMakeLists.txt similarity index 77% rename from test/conformance/runtime/CMakeLists.txt rename to test/conformance/adapter/CMakeLists.txt index 8c46abd82b..8d71a3cf6a 100644 --- a/test/conformance/runtime/CMakeLists.txt +++ b/test/conformance/adapter/CMakeLists.txt @@ -3,11 +3,9 @@ # See LICENSE.TXT # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -add_conformance_test(runtime +add_conformance_test(adapter urAdapterGet.cpp urAdapterGetInfo.cpp urAdapterGetLastError.cpp urAdapterRetain.cpp - urAdapterRelease.cpp - urInit.cpp - urTearDown.cpp) + urAdapterRelease.cpp) diff --git a/test/conformance/runtime/fixtures.h b/test/conformance/adapter/fixtures.h similarity index 89% rename from test/conformance/runtime/fixtures.h rename to test/conformance/adapter/fixtures.h index 04f72617dd..31b2a2265d 100644 --- a/test/conformance/runtime/fixtures.h +++ b/test/conformance/adapter/fixtures.h @@ -14,15 +14,14 @@ struct urTest : ::testing::Test { ASSERT_SUCCESS(urLoaderConfigCreate(&loader_config)); ASSERT_SUCCESS(urLoaderConfigEnableLayer(loader_config, "UR_LAYER_FULL_VALIDATION")); - ASSERT_SUCCESS(urInit(device_flags, loader_config)); + ASSERT_SUCCESS(urLoaderInit(device_flags, loader_config)); } void TearDown() override { if (loader_config) { ASSERT_SUCCESS(urLoaderConfigRelease(loader_config)); } - ur_tear_down_params_t tear_down_params{}; - ASSERT_SUCCESS(urTearDown(&tear_down_params)); + ASSERT_SUCCESS(urLoaderTearDown()); } ur_loader_config_handle_t loader_config = nullptr; @@ -35,6 +34,7 @@ struct urAdapterTest : urTest { uint32_t adapter_count; ASSERT_SUCCESS(urAdapterGet(0, nullptr, &adapter_count)); + ASSERT_GT(adapter_count, 0); adapters.resize(adapter_count); ASSERT_SUCCESS(urAdapterGet(adapter_count, adapters.data(), nullptr)); } diff --git a/test/conformance/runtime/urAdapterGet.cpp b/test/conformance/adapter/urAdapterGet.cpp similarity index 100% rename from test/conformance/runtime/urAdapterGet.cpp rename to test/conformance/adapter/urAdapterGet.cpp diff --git a/test/conformance/runtime/urAdapterGetInfo.cpp b/test/conformance/adapter/urAdapterGetInfo.cpp similarity index 100% rename from test/conformance/runtime/urAdapterGetInfo.cpp rename to test/conformance/adapter/urAdapterGetInfo.cpp diff --git a/test/conformance/runtime/urAdapterGetLastError.cpp b/test/conformance/adapter/urAdapterGetLastError.cpp similarity index 100% rename from test/conformance/runtime/urAdapterGetLastError.cpp rename to test/conformance/adapter/urAdapterGetLastError.cpp diff --git a/test/conformance/runtime/urAdapterRelease.cpp b/test/conformance/adapter/urAdapterRelease.cpp similarity index 100% rename from test/conformance/runtime/urAdapterRelease.cpp rename to test/conformance/adapter/urAdapterRelease.cpp diff --git a/test/conformance/runtime/urAdapterRetain.cpp b/test/conformance/adapter/urAdapterRetain.cpp similarity index 100% rename from test/conformance/runtime/urAdapterRetain.cpp rename to test/conformance/adapter/urAdapterRetain.cpp diff --git a/test/conformance/platform/fixtures.h b/test/conformance/platform/fixtures.h index 5b532fb433..b294e7031a 100644 --- a/test/conformance/platform/fixtures.h +++ b/test/conformance/platform/fixtures.h @@ -17,7 +17,7 @@ struct urTest : ::testing::Test { ASSERT_SUCCESS(urLoaderConfigCreate(&loader_config)); ASSERT_SUCCESS(urLoaderConfigEnableLayer(loader_config, "UR_LAYER_FULL_VALIDATION")); - ASSERT_SUCCESS(urInit(device_flags, loader_config)); + ASSERT_SUCCESS(urLoaderInit(device_flags, loader_config)); uint32_t adapter_count; ASSERT_SUCCESS(urAdapterGet(0, nullptr, &adapter_count)); @@ -32,8 +32,7 @@ struct urTest : ::testing::Test { if (loader_config) { ASSERT_SUCCESS(urLoaderConfigRelease(loader_config)); } - ur_tear_down_params_t tear_down_params{}; - ASSERT_SUCCESS(urTearDown(&tear_down_params)); + ASSERT_SUCCESS(urLoaderTearDown()); } ur_loader_config_handle_t loader_config = nullptr; diff --git a/test/conformance/runtime/urTearDown.cpp b/test/conformance/runtime/urTearDown.cpp deleted file mode 100644 index 3639515f82..0000000000 --- a/test/conformance/runtime/urTearDown.cpp +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (C) 2022-2023 Intel Corporation -// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. -// See LICENSE.TXT -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include - -struct urTearDownTest : testing::Test { - void SetUp() override { - ur_device_init_flags_t device_flags = 0; - ASSERT_SUCCESS(urInit(device_flags, nullptr)); - } -}; - -TEST_F(urTearDownTest, Success) { - ur_tear_down_params_t tear_down_params{}; - ASSERT_SUCCESS(urTearDown(&tear_down_params)); -} - -TEST_F(urTearDownTest, InvalidNullPointerParams) { - ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_NULL_POINTER, urTearDown(nullptr)); -} diff --git a/test/conformance/source/environment.cpp b/test/conformance/source/environment.cpp index 287310f679..92b84f4b4d 100644 --- a/test/conformance/source/environment.cpp +++ b/test/conformance/source/environment.cpp @@ -57,7 +57,7 @@ uur::PlatformEnvironment::PlatformEnvironment(int argc, char **argv) } ur_device_init_flags_t device_flags = 0; - auto initResult = urInit(device_flags, config); + auto initResult = urLoaderInit(device_flags, config); auto configReleaseResult = urLoaderConfigRelease(config); switch (initResult) { case UR_RESULT_SUCCESS: @@ -66,7 +66,7 @@ uur::PlatformEnvironment::PlatformEnvironment(int argc, char **argv) error = ERROR_NO_ADAPTER; return; default: - error = "urInit() failed"; + error = "urLoaderInit() failed"; return; } @@ -159,9 +159,8 @@ void uur::PlatformEnvironment::TearDown() { for (auto adapter : adapters) { urAdapterRelease(adapter); } - ur_tear_down_params_t tear_down_params{}; - if (urTearDown(&tear_down_params)) { - FAIL() << "urTearDown() failed"; + if (urLoaderTearDown()) { + FAIL() << "urLoaderTearDown() failed"; } } diff --git a/test/layers/tracing/hello_world.out.match b/test/layers/tracing/hello_world.out.match index 7658650d04..cef17b8fdf 100644 --- a/test/layers/tracing/hello_world.out.match +++ b/test/layers/tracing/hello_world.out.match @@ -1,27 +1,23 @@ -function_with_args_begin(1) - urInit(.device_flags = 0); -function_with_args_end(1) - urInit(...) -> ur_result_t(0); Platform initialized. -function_with_args_begin(2) - urAdapterGet(unimplemented); +function_with_args_begin(1) - urAdapterGet(.NumEntries = 0, .phAdapters = {{.*}}, .pNumAdapters = {{.*}}); +function_with_args_end(1) - urAdapterGet(...) -> ur_result_t(0); +function_with_args_begin(2) - urAdapterGet(.NumEntries = 1, .phAdapters = {{.*}}, .pNumAdapters = {{.*}}); function_with_args_end(2) - urAdapterGet(...) -> ur_result_t(0); -function_with_args_begin(3) - urAdapterGet(unimplemented); -function_with_args_end(3) - urAdapterGet(...) -> ur_result_t(0); +function_with_args_begin(3) - urPlatformGet(unimplemented); +function_with_args_end(3) - urPlatformGet(...) -> ur_result_t(0); function_with_args_begin(4) - urPlatformGet(unimplemented); function_with_args_end(4) - urPlatformGet(...) -> ur_result_t(0); -function_with_args_begin(5) - urPlatformGet(unimplemented); -function_with_args_end(5) - urPlatformGet(...) -> ur_result_t(0); -function_with_args_begin(6) - urPlatformGetApiVersion(unimplemented); -function_with_args_end(6) - urPlatformGetApiVersion(...) -> ur_result_t(0); +function_with_args_begin(5) - urPlatformGetApiVersion(unimplemented); +function_with_args_end(5) - urPlatformGetApiVersion(...) -> ur_result_t(0); API version: {{0\.[0-9]+}} +function_with_args_begin(6) - urDeviceGet(unimplemented); +function_with_args_end(6) - urDeviceGet(...) -> ur_result_t(0); function_with_args_begin(7) - urDeviceGet(unimplemented); function_with_args_end(7) - urDeviceGet(...) -> ur_result_t(0); -function_with_args_begin(8) - urDeviceGet(unimplemented); -function_with_args_end(8) - urDeviceGet(...) -> ur_result_t(0); +function_with_args_begin(8) - urDeviceGetInfo(unimplemented); +function_with_args_end(8) - urDeviceGetInfo(...) -> ur_result_t(0); function_with_args_begin(9) - urDeviceGetInfo(unimplemented); function_with_args_end(9) - urDeviceGetInfo(...) -> ur_result_t(0); -function_with_args_begin(10) - urDeviceGetInfo(unimplemented); -function_with_args_end(10) - urDeviceGetInfo(...) -> ur_result_t(0); Found a Null Device gpu. -function_with_args_begin(11) - urAdapterRelease(unimplemented); -function_with_args_end(11) - urAdapterRelease(...) -> ur_result_t(0); -function_with_args_begin(12) - urTearDown(unimplemented); -function_with_args_end(12) - urTearDown(...) -> ur_result_t(0); +function_with_args_begin(10) - urAdapterRelease(unimplemented); +function_with_args_end(10) - urAdapterRelease(...) -> ur_result_t(0); diff --git a/test/layers/validation/fixtures.hpp b/test/layers/validation/fixtures.hpp index a41e48b3a4..ab92ba1e01 100644 --- a/test/layers/validation/fixtures.hpp +++ b/test/layers/validation/fixtures.hpp @@ -17,15 +17,14 @@ struct urTest : ::testing::Test { "UR_LAYER_FULL_VALIDATION"), UR_RESULT_SUCCESS); ur_device_init_flags_t device_flags = 0; - ASSERT_EQ(urInit(device_flags, loader_config), UR_RESULT_SUCCESS); + ASSERT_EQ(urLoaderInit(device_flags, loader_config), UR_RESULT_SUCCESS); } void TearDown() override { if (loader_config) { ASSERT_EQ(urLoaderConfigRelease(loader_config), UR_RESULT_SUCCESS); } - ur_tear_down_params_t tear_down_params{}; - ASSERT_EQ(urTearDown(&tear_down_params), UR_RESULT_SUCCESS); + ASSERT_EQ(urLoaderTearDown(), UR_RESULT_SUCCESS); } ur_loader_config_handle_t loader_config = nullptr; @@ -53,7 +52,12 @@ struct valPlatformsTest : urTest { UR_RESULT_SUCCESS); } - void TearDown() override { urTest::TearDown(); } + void TearDown() override { + for (auto &adapter : adapters) { + ASSERT_EQ(urAdapterRelease(adapter), UR_RESULT_SUCCESS); + } + urTest::TearDown(); + } std::vector adapters; std::vector platforms; diff --git a/test/layers/validation/leaks.cpp b/test/layers/validation/leaks.cpp index b0df81207e..e32aeafc89 100644 --- a/test/layers/validation/leaks.cpp +++ b/test/layers/validation/leaks.cpp @@ -5,6 +5,12 @@ #include "fixtures.hpp" +TEST_F(urTest, testUrAdapterGetLeak) { + ur_adapter_handle_t adapter = nullptr; + ASSERT_EQ(urAdapterGet(1, &adapter, nullptr), UR_RESULT_SUCCESS); + ASSERT_NE(nullptr, adapter); +} + TEST_F(valDeviceTest, testUrContextCreateLeak) { ur_context_handle_t context = nullptr; ASSERT_EQ(urContextCreate(1, &device, nullptr, &context), diff --git a/test/layers/validation/leaks.out.match b/test/layers/validation/leaks.out.match index aadba2252c..9fac722527 100644 --- a/test/layers/validation/leaks.out.match +++ b/test/layers/validation/leaks.out.match @@ -3,28 +3,45 @@ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 +\[ERROR\]: Retained 1 reference\(s\) to handle [0-9xa-fA-F]+ +\[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: +(.*) +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained 2 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[ERROR\]: Attempting to retain nonexistent handle [0-9xa-fA-F]+ (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Attempting to release nonexistent handle [0-9xa-fA-F]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained -1 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[ERROR\]: Attempting to release nonexistent handle [0-9xa-fA-F]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained -1 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) diff --git a/test/layers/validation/leaks_mt.out.match b/test/layers/validation/leaks_mt.out.match index 7d5a0bedd8..86de1e1d76 100644 --- a/test/layers/validation/leaks_mt.out.match +++ b/test/layers/validation/leaks_mt.out.match @@ -1,10 +1,13 @@ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 3 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained 3 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 2 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 3 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 4 @@ -13,17 +16,21 @@ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 7 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 8 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 9 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained 9 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Attempting to release nonexistent handle [0-9xa-fA-F]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained -1 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Attempting to release nonexistent handle [0-9xa-fA-F]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -1 @@ -39,18 +46,22 @@ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -6 \[ERROR\]: Attempting to release nonexistent handle [0-9xa-fA-F]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to -7 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained -7 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained 1 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ @@ -67,6 +78,7 @@ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to [1-9]+ \[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 1 +\[DEBUG\]: Reference count for handle [0-9xa-fA-F]+ changed to 0 \[ERROR\]: Retained 1 reference\(s\) to handle [0-9xa-fA-F]+ \[ERROR\]: Handle [0-9xa-fA-F]+ was recorded for first time here: (.*) diff --git a/test/layers/validation/parameters.cpp b/test/layers/validation/parameters.cpp index ee679363dc..c02afd63d6 100644 --- a/test/layers/validation/parameters.cpp +++ b/test/layers/validation/parameters.cpp @@ -5,18 +5,6 @@ #include "fixtures.hpp" -TEST(valTest, urInit) { - ur_loader_config_handle_t config; - urLoaderConfigCreate(&config); - urLoaderConfigEnableLayer(config, "UR_PARAMETER_VALIDATION_LAYER"); - - const ur_device_init_flags_t device_flags = - UR_DEVICE_INIT_FLAG_FORCE_UINT32; - ASSERT_EQ(urInit(device_flags, config), - UR_RESULT_ERROR_INVALID_ENUMERATION); - ASSERT_EQ(urLoaderConfigRelease(config), UR_RESULT_SUCCESS); -} - TEST_F(valPlatformsTest, testUrPlatformGetApiVersion) { ur_api_version_t api_version = {}; diff --git a/test/loader/CMakeLists.txt b/test/loader/CMakeLists.txt index 0dbf999c45..d36f922098 100644 --- a/test/loader/CMakeLists.txt +++ b/test/loader/CMakeLists.txt @@ -10,4 +10,5 @@ set_tests_properties(example-hello-world PROPERTIES LABELS "loader" add_subdirectory(adapter_registry) add_subdirectory(loader_config) +add_subdirectory(loader_lifetime) add_subdirectory(platforms) diff --git a/test/loader/loader_lifetime/CMakeLists.txt b/test/loader/loader_lifetime/CMakeLists.txt new file mode 100644 index 0000000000..c76ff87d0b --- /dev/null +++ b/test/loader/loader_lifetime/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright (C) 2023 Intel Corporation +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +# See LICENSE.TXT +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +add_executable(test-loader-lifetime + urLoaderInit.cpp + urLoaderTearDown.cpp +) + +target_link_libraries(test-loader-lifetime + PRIVATE + ${PROJECT_NAME}::common + ${PROJECT_NAME}::headers + ${PROJECT_NAME}::loader + gmock + GTest::gtest_main +) + +add_test(NAME loader-lifetime + COMMAND test-loader-lifetime + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} +) diff --git a/test/loader/loader_lifetime/fixtures.hpp b/test/loader/loader_lifetime/fixtures.hpp new file mode 100644 index 0000000000..b1eb3766c5 --- /dev/null +++ b/test/loader/loader_lifetime/fixtures.hpp @@ -0,0 +1,27 @@ +// Copyright (C) 2023 Intel Corporation +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See LICENSE.TXT +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef UR_LOADER_CONFIG_TEST_FIXTURES_H +#define UR_LOADER_CONFIG_TEST_FIXTURES_H + +#include "ur_api.h" +#include +#include + +#ifndef ASSERT_SUCCESS +#define ASSERT_SUCCESS(ACTUAL) ASSERT_EQ(UR_RESULT_SUCCESS, ACTUAL) +#endif + +/// @brief Make a string a valid identifier for gtest. +/// @param str The string to sanitize. +inline std::string GTestSanitizeString(const std::string &str) { + auto str_cpy = str; + std::replace_if( + str_cpy.begin(), str_cpy.end(), [](char c) { return !std::isalnum(c); }, + '_'); + return str_cpy; +} + +#endif diff --git a/test/conformance/runtime/urInit.cpp b/test/loader/loader_lifetime/urLoaderInit.cpp similarity index 67% rename from test/conformance/runtime/urInit.cpp rename to test/loader/loader_lifetime/urLoaderInit.cpp index 1de30ff471..48060240b8 100644 --- a/test/conformance/runtime/urInit.cpp +++ b/test/loader/loader_lifetime/urLoaderInit.cpp @@ -2,11 +2,13 @@ // Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. // See LICENSE.TXT // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -#include +#include "fixtures.hpp" +#include -using urInitTestWithParam = ::testing::TestWithParam; +using urLoaderInitTestWithParam = + ::testing::TestWithParam; INSTANTIATE_TEST_SUITE_P( - , urInitTestWithParam, + , urLoaderInitTestWithParam, ::testing::Values(UR_DEVICE_INIT_FLAG_GPU, UR_DEVICE_INIT_FLAG_CPU, UR_DEVICE_INIT_FLAG_FPGA, UR_DEVICE_INIT_FLAG_MCA, UR_DEVICE_INIT_FLAG_VPU, @@ -16,24 +18,23 @@ INSTANTIATE_TEST_SUITE_P( [](const ::testing::TestParamInfo &info) { std::stringstream ss; ur_params::serializeFlag(ss, info.param); - return uur::GTestSanitizeString(ss.str()); + return GTestSanitizeString(ss.str()); }); -TEST_P(urInitTestWithParam, Success) { +TEST_P(urLoaderInitTestWithParam, Success) { ur_loader_config_handle_t config = nullptr; urLoaderConfigCreate(&config); urLoaderConfigEnableLayer(config, "UR_LAYER_FULL_VALIDATION"); ur_device_init_flags_t device_flags = GetParam(); - ASSERT_SUCCESS(urInit(device_flags, config)); + ASSERT_SUCCESS(urLoaderInit(device_flags, config)); - ur_tear_down_params_t tear_down_params{nullptr}; - ASSERT_SUCCESS(urTearDown(&tear_down_params)); + ASSERT_SUCCESS(urLoaderTearDown()); } -TEST(urInitTest, ErrorInvalidEnumerationDeviceFlags) { +TEST(urLoaderInitTest, ErrorInvalidEnumerationDeviceFlags) { const ur_device_init_flags_t device_flags = UR_DEVICE_INIT_FLAG_FORCE_UINT32; - ASSERT_EQ_RESULT(UR_RESULT_ERROR_INVALID_ENUMERATION, - urInit(device_flags, nullptr)); + ASSERT_EQ(UR_RESULT_ERROR_INVALID_ENUMERATION, + urLoaderInit(device_flags, nullptr)); } diff --git a/test/loader/loader_lifetime/urLoaderTearDown.cpp b/test/loader/loader_lifetime/urLoaderTearDown.cpp new file mode 100644 index 0000000000..a4c3dc83fb --- /dev/null +++ b/test/loader/loader_lifetime/urLoaderTearDown.cpp @@ -0,0 +1,14 @@ +// Copyright (C) 2022-2023 Intel Corporation +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +// See LICENSE.TXT +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include "fixtures.hpp" + +struct urLoaderTearDownTest : testing::Test { + void SetUp() override { + ur_device_init_flags_t device_flags = 0; + ASSERT_SUCCESS(urLoaderInit(device_flags, nullptr)); + } +}; + +TEST_F(urLoaderTearDownTest, Success) { ASSERT_SUCCESS(urLoaderTearDown()); } diff --git a/test/loader/platforms/no_platforms.match b/test/loader/platforms/no_platforms.match index da17800c0c..b695672e4d 100644 --- a/test/loader/platforms/no_platforms.match +++ b/test/loader/platforms/no_platforms.match @@ -1,2 +1,2 @@ -[INFO]: urInit succeeded. +[INFO]: urLoaderInit succeeded. [INFO]: urPlatformGet found 0 platforms diff --git a/test/loader/platforms/null_platform.match b/test/loader/platforms/null_platform.match index 6c7d8a97f4..f60d4a8d79 100644 --- a/test/loader/platforms/null_platform.match +++ b/test/loader/platforms/null_platform.match @@ -1,3 +1,3 @@ -[INFO]: urInit succeeded. +[INFO]: urLoaderInit succeeded. [INFO]: urPlatformGet found 1 platforms [INFO]: Found UR_PLATFORM_NULL \ No newline at end of file diff --git a/test/loader/platforms/platforms.cpp b/test/loader/platforms/platforms.cpp index 2a665fe8d6..bf85ae9cf4 100644 --- a/test/loader/platforms/platforms.cpp +++ b/test/loader/platforms/platforms.cpp @@ -24,12 +24,12 @@ int main(int argc, char *argv[]) { ur_result_t status; // Initialize the platform - status = urInit(0, nullptr); + status = urLoaderInit(0, nullptr); if (status != UR_RESULT_SUCCESS) { - error("urInit failed with return code: {}", status); + error("urLoaderInit failed with return code: {}", status); return 1; } - info("urInit succeeded."); + info("urLoaderInit succeeded."); uint32_t adapterCount = 0; std::vector adapters; @@ -89,6 +89,6 @@ int main(int argc, char *argv[]) { free(name); } out: - urTearDown(nullptr); + urLoaderTearDown(); return status == UR_RESULT_SUCCESS ? 0 : 1; } diff --git a/test/tools/urtrace/null_hello.match b/test/tools/urtrace/null_hello.match index b58a4d8d96..54c6efb9cb 100644 --- a/test/tools/urtrace/null_hello.match +++ b/test/tools/urtrace/null_hello.match @@ -1,4 +1,3 @@ -urInit(.device_flags = 0, .hLoaderConfig = nullptr) -> UR_RESULT_SUCCESS; Platform initialized. urAdapterGet(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (1)) -> UR_RESULT_SUCCESS; urAdapterGet(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr) -> UR_RESULT_SUCCESS; @@ -12,4 +11,3 @@ urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = {{.*}}, .pPropValue = {{.*}}, .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS; Found a Null Device gpu. urAdapterRelease(.hAdapter = {{.*}}) -> UR_RESULT_SUCCESS; -urTearDown(.pParams = nullptr) -> UR_RESULT_SUCCESS; diff --git a/test/tools/urtrace/null_hello_begin.match b/test/tools/urtrace/null_hello_begin.match index 81c15da60f..bf2d85145a 100644 --- a/test/tools/urtrace/null_hello_begin.match +++ b/test/tools/urtrace/null_hello_begin.match @@ -1,27 +1,23 @@ -begin(1) - urInit(.device_flags = 0, .hLoaderConfig = nullptr); -end(1) - urInit(.device_flags = 0, .hLoaderConfig = nullptr) -> UR_RESULT_SUCCESS; Platform initialized. -begin(2) - urAdapterGet(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (0)); -end(2) - urAdapterGet(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (1)) -> UR_RESULT_SUCCESS; -begin(3) - urAdapterGet(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr); -end(3) - urAdapterGet(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr) -> UR_RESULT_SUCCESS; -begin(4) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {}, .pNumPlatforms = {{.*}} (0)); -end(4) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {}, .pNumPlatforms = {{.*}} (1)) -> UR_RESULT_SUCCESS; -begin(5) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {nullptr}, .pNumPlatforms = nullptr); -end(5) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {{{.*}}}, .pNumPlatforms = nullptr) -> UR_RESULT_SUCCESS; -begin(6) - urPlatformGetApiVersion(.hPlatform = {{.*}}, .pVersion = {{.*}} (0.0)); -end(6) - urPlatformGetApiVersion(.hPlatform = {{.*}}, .pVersion = {{.*}} (@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@)) -> UR_RESULT_SUCCESS; +begin(1) - urAdapterGet(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (0)); +end(1) - urAdapterGet(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (1)) -> UR_RESULT_SUCCESS; +begin(2) - urAdapterGet(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr); +end(2) - urAdapterGet(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr) -> UR_RESULT_SUCCESS; +begin(3) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {}, .pNumPlatforms = {{.*}} (0)); +end(3) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {}, .pNumPlatforms = {{.*}} (1)) -> UR_RESULT_SUCCESS; +begin(4) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {nullptr}, .pNumPlatforms = nullptr); +end(4) - urPlatformGet(.phAdapters = {{{.*}}}, .NumAdapters = 1, .NumEntries = 1, .phPlatforms = {{{.*}}}, .pNumPlatforms = nullptr) -> UR_RESULT_SUCCESS; +begin(5) - urPlatformGetApiVersion(.hPlatform = {{.*}}, .pVersion = {{.*}} (0.0)); +end(5) - urPlatformGetApiVersion(.hPlatform = {{.*}}, .pVersion = {{.*}} (@PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@)) -> UR_RESULT_SUCCESS; API version: {{.*}} -begin(7) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 0, .phDevices = {}, .pNumDevices = {{.*}} (0)); -end(7) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 0, .phDevices = {}, .pNumDevices = {{.*}} (1)) -> UR_RESULT_SUCCESS; -begin(8) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 1, .phDevices = {nullptr}, .pNumDevices = nullptr); -end(8) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 1, .phDevices = {{{.*}}}, .pNumDevices = nullptr) -> UR_RESULT_SUCCESS; -begin(9) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = 4, .pPropValue = {{.*}}, .pPropSizeRet = nullptr); -end(9) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = 4, .pPropValue = {{.*}}, .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS; -begin(10) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = 1023, .pPropValue = {{.*}}, .pPropSizeRet = nullptr); -end(10) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = 1023, .pPropValue = {{.*}}, .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS; +begin(6) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 0, .phDevices = {}, .pNumDevices = {{.*}} (0)); +end(6) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 0, .phDevices = {}, .pNumDevices = {{.*}} (1)) -> UR_RESULT_SUCCESS; +begin(7) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 1, .phDevices = {nullptr}, .pNumDevices = nullptr); +end(7) - urDeviceGet(.hPlatform = {{.*}}, .DeviceType = UR_DEVICE_TYPE_GPU, .NumEntries = 1, .phDevices = {{{.*}}}, .pNumDevices = nullptr) -> UR_RESULT_SUCCESS; +begin(8) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = 4, .pPropValue = {{.*}}, .pPropSizeRet = nullptr); +end(8) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = 4, .pPropValue = {{.*}}, .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS; +begin(9) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = 1023, .pPropValue = {{.*}}, .pPropSizeRet = nullptr); +end(9) - urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = 1023, .pPropValue = {{.*}}, .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS; Found a Null Device gpu. -begin(11) - urAdapterRelease(.hAdapter = {{.*}}); -end(11) - urAdapterRelease(.hAdapter = {{.*}}) -> UR_RESULT_SUCCESS; -begin(12) - urTearDown(.pParams = nullptr); -end(12) - urTearDown(.pParams = nullptr) -> UR_RESULT_SUCCESS; +begin(10) - urAdapterRelease(.hAdapter = {{.*}}); +end(10) - urAdapterRelease(.hAdapter = {{.*}}) -> UR_RESULT_SUCCESS; diff --git a/test/tools/urtrace/null_hello_json.match b/test/tools/urtrace/null_hello_json.match index 18c5fbac78..5b9377e8d6 100644 --- a/test/tools/urtrace/null_hello_json.match +++ b/test/tools/urtrace/null_hello_json.match @@ -1,6 +1,5 @@ { "traceEvents": [ -{ "cat": "UR", "ph": "X", "pid": {{.*}}, "tid": {{.*}}, "ts": {{.*}}, "dur": {{.*}}, "name": "urInit", "args": "(.device_flags = 0, .hLoaderConfig = nullptr)" }, Platform initialized. { "cat": "UR", "ph": "X", "pid": {{.*}}, "tid": {{.*}}, "ts": {{.*}}, "dur": {{.*}}, "name": "urAdapterGet", "args": "(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (1))" }, { "cat": "UR", "ph": "X", "pid": {{.*}}, "tid": {{.*}}, "ts": {{.*}}, "dur": {{.*}}, "name": "urAdapterGet", "args": "(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr)" }, @@ -14,7 +13,6 @@ API version: @PROJECT_VERSION_MAJOR@.@PROJECT_VERSION_MINOR@ { "cat": "UR", "ph": "X", "pid": {{.*}}, "tid": {{.*}}, "ts": {{.*}}, "dur": {{.*}}, "name": "urDeviceGetInfo", "args": "(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = 1023, .pPropValue = {{.*}} (Null Device), .pPropSizeRet = nullptr)" }, Found a Null Device gpu. { "cat": "UR", "ph": "X", "pid": {{.*}}, "tid": {{.*}}, "ts": {{.*}}, "dur": {{.*}}, "name": "urAdapterRelease", "args": "(.hAdapter = {{.*}})" }, -{ "cat": "UR", "ph": "X", "pid": {{.*}}, "tid": {{.*}}, "ts": {{.*}}, "dur": {{.*}}, "name": "urTearDown", "args": "(.pParams = nullptr)" }, {"name": "", "cat": "", "ph": "", "pid": "", "tid": "", "ts": ""} ] } diff --git a/test/tools/urtrace/null_hello_no_args.match b/test/tools/urtrace/null_hello_no_args.match index e0afcd2868..6462f41d02 100644 --- a/test/tools/urtrace/null_hello_no_args.match +++ b/test/tools/urtrace/null_hello_no_args.match @@ -1,4 +1,3 @@ -urInit(...) -> UR_RESULT_SUCCESS; Platform initialized. urAdapterGet(...) -> UR_RESULT_SUCCESS; urAdapterGet(...) -> UR_RESULT_SUCCESS; @@ -12,4 +11,3 @@ urDeviceGetInfo(...) -> UR_RESULT_SUCCESS; urDeviceGetInfo(...) -> UR_RESULT_SUCCESS; Found a Null Device gpu. urAdapterRelease(...) -> UR_RESULT_SUCCESS; -urTearDown(...) -> UR_RESULT_SUCCESS; diff --git a/test/tools/urtrace/null_hello_profiling.match b/test/tools/urtrace/null_hello_profiling.match index 635c3c8784..7bd3bd53c1 100644 --- a/test/tools/urtrace/null_hello_profiling.match +++ b/test/tools/urtrace/null_hello_profiling.match @@ -1,4 +1,3 @@ -urInit(.device_flags = 0, .hLoaderConfig = nullptr) -> UR_RESULT_SUCCESS; ({{[0-9]+}}ns) Platform initialized. urAdapterGet(.NumEntries = 0, .phAdapters = {}, .pNumAdapters = {{.*}} (1)) -> UR_RESULT_SUCCESS; ({{[0-9]+}}ns) urAdapterGet(.NumEntries = 1, .phAdapters = {{{.*}}}, .pNumAdapters = nullptr) -> UR_RESULT_SUCCESS; ({{[0-9]+}}ns) @@ -12,4 +11,3 @@ urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_TYPE, .propSize = urDeviceGetInfo(.hDevice = {{.*}}, .propName = UR_DEVICE_INFO_NAME, .propSize = {{.*}}, .pPropValue = {{.*}}, .pPropSizeRet = nullptr) -> UR_RESULT_SUCCESS; ({{[0-9]+}}ns) Found a Null Device gpu. urAdapterRelease(.hAdapter = {{.*}}) -> UR_RESULT_SUCCESS; ({{[0-9]+}}ns) -urTearDown(.pParams = nullptr) -> UR_RESULT_SUCCESS; ({{[0-9]+}}ns) diff --git a/test/unit/utils/params.cpp b/test/unit/utils/params.cpp index 964d117e49..d54263591b 100644 --- a/test/unit/utils/params.cpp +++ b/test/unit/utils/params.cpp @@ -17,30 +17,30 @@ template class ParamsTest : public testing::Test { T params; }; -struct UrInitParams { - ur_init_params_t params; +struct UrLoaderInitParams { + ur_loader_init_params_t params; ur_device_init_flags_t flags; ur_loader_config_handle_t config; - UrInitParams(ur_device_init_flags_t _flags) + UrLoaderInitParams(ur_device_init_flags_t _flags) : flags(_flags), config(nullptr) { params.pdevice_flags = &flags; params.phLoaderConfig = &config; } - ur_init_params_t *get_struct() { return ¶ms; } + ur_loader_init_params_t *get_struct() { return ¶ms; } }; -struct UrInitParamsNoFlags : UrInitParams { - UrInitParamsNoFlags() : UrInitParams(0) {} +struct UrLoaderInitParamsNoFlags : UrLoaderInitParams { + UrLoaderInitParamsNoFlags() : UrLoaderInitParams(0) {} const char *get_expected() { return ".device_flags = 0, .hLoaderConfig = nullptr"; }; }; -struct UrInitParamsInvalidFlags : UrInitParams { - UrInitParamsInvalidFlags() - : UrInitParams(UR_DEVICE_INIT_FLAG_GPU | UR_DEVICE_INIT_FLAG_MCA | - UR_BIT(25) | UR_BIT(30) | UR_BIT(31)) {} +struct UrLoaderInitParamsInvalidFlags : UrLoaderInitParams { + UrLoaderInitParamsInvalidFlags() + : UrLoaderInitParams(UR_DEVICE_INIT_FLAG_GPU | UR_DEVICE_INIT_FLAG_MCA | + UR_BIT(25) | UR_BIT(30) | UR_BIT(31)) {} const char *get_expected() { return ".device_flags = UR_DEVICE_INIT_FLAG_GPU \\| " "UR_DEVICE_INIT_FLAG_MCA \\| unknown bit flags " @@ -368,14 +368,15 @@ struct UrDevicePartitionPropertyTest { }; using testing::Types; -typedef Types< - UrInitParamsNoFlags, UrInitParamsInvalidFlags, UrUsmHostAllocParamsEmpty, - UrPlatformGetEmptyArray, UrPlatformGetTwoPlatforms, - UrUsmHostAllocParamsUsmDesc, UrUsmHostAllocParamsHostDesc, - UrDeviceGetInfoParamsEmpty, UrDeviceGetInfoParamsName, - UrDeviceGetInfoParamsQueueFlag, UrDeviceGetInfoParamsPartitionArray, - UrContextGetInfoParamsDevicesArray, UrDeviceGetInfoParamsInvalidSize, - UrProgramMetadataTest, UrDevicePartitionPropertyTest> +typedef Types Implementations; using ::testing::MatchesRegex;