diff --git a/cmake/InfiniRTConfig.cmake.in b/cmake/InfiniRTConfig.cmake.in index 051e315..5b73547 100644 --- a/cmake/InfiniRTConfig.cmake.in +++ b/cmake/InfiniRTConfig.cmake.in @@ -188,5 +188,32 @@ elseif(_InfiniRT_VENDOR_BACKEND STREQUAL "ASCEND") "${_InfiniRT_BACKEND_RUNTIME_LIBRARY};${_InfiniRT_BACKEND_DRIVER_LIBRARY};${CMAKE_DL_LIBS}") endif() +if(@WITH_HYGON@) + # The Hygon driver entry points (cuModuleLoadData, cuLaunchKernel, ...) + # exposed by the public driver header live in the CUDA driver library, + # which the build found next to the toolkit; re-find it here so every + # consumer of the installed target links it without extra setup. + find_library(_InfiniRT_HYGON_DRIVER_LIBRARY + NAMES cuda + HINTS "${CUDAToolkit_LIBRARY_DIR}/stubs" + "${CUDAToolkit_LIBRARY_DIR}" + "${CUDAToolkit_ROOT}/lib64/stubs" + "${CUDAToolkit_ROOT}/lib64") + if(NOT _InfiniRT_HYGON_DRIVER_LIBRARY) + set(InfiniRT_FOUND FALSE) + set(InfiniRT_NOT_FOUND_MESSAGE + "The CUDA driver library (libcuda) was not found. It provides the " + "Hygon driver entry points of the public driver header; point " + "CUDAToolkit_ROOT at the DTK CUDA toolkit.") + return() + endif() + if(TARGET InfiniRT::BackendRuntime) + set_property(TARGET InfiniRT::BackendRuntime APPEND PROPERTY + INTERFACE_LINK_LIBRARIES "${_InfiniRT_HYGON_DRIVER_LIBRARY}") + else() + _infinirt_define_backend_runtime("" "${_InfiniRT_HYGON_DRIVER_LIBRARY}") + endif() +endif() + include("${CMAKE_CURRENT_LIST_DIR}/InfiniRTTargets.cmake") check_required_components(InfiniRT) diff --git a/scripts/generate_public_headers.py b/scripts/generate_public_headers.py index 22c49d8..682f180 100644 --- a/scripts/generate_public_headers.py +++ b/scripts/generate_public_headers.py @@ -26,6 +26,7 @@ ("hygon", "data_type_.h", "native/cuda/hygon/data_type_.h"), ("hygon", "device_.h", "native/cuda/hygon/device_.h"), ("hygon", "runtime_.h", "native/cuda/hygon/runtime_.h"), + ("hygon", "driver_.h", "native/cuda/hygon/driver_.h"), ), "thead": ( ("thead", "data_type_.h", "native/cuda/thead/data_type_.h"), diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1dd146f..09c6918 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,8 +3,11 @@ add_library(InfiniRT::infinirt ALIAS infinirt) function(_infinirt_use_backend_runtime include_dirs link_libraries) add_library(InfiniRT::BackendRuntime INTERFACE IMPORTED GLOBAL) + if(include_dirs) + set_target_properties(InfiniRT::BackendRuntime PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${include_dirs}") + endif() set_target_properties(InfiniRT::BackendRuntime PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES "${include_dirs}" INTERFACE_LINK_LIBRARIES "${link_libraries}") target_link_libraries(infinirt PUBLIC InfiniRT::BackendRuntime) endfunction() @@ -57,6 +60,24 @@ if(WITH_HYGON) find_package(CUDAToolkit REQUIRED) target_link_libraries(infinirt PUBLIC CUDA::cudart) + # The public Hygon driver header exposes cuModuleLoadData, cuLaunchKernel + # and the other driver entry points, which live in the CUDA driver + # library, not in cudart. FindCUDAToolkit does not provide CUDA::cuda for + # every DTK layout, so locate the library here and carry it through + # InfiniRT::BackendRuntime: the package config re-finds it on the + # consumer's machine, and the install-consumer test links it by + # referencing the driver entry points. + find_library(HYGON_CUDA_DRIVER_LIBRARY + NAMES cuda + HINTS "${HYGON_CUDA_ROOT}/lib64/stubs" "${HYGON_CUDA_ROOT}/lib64") + if(NOT HYGON_CUDA_DRIVER_LIBRARY) + message(FATAL_ERROR + "The Hygon CUDA driver library (libcuda) was not found under " + "'${HYGON_CUDA_ROOT}'. It provides the driver entry points of " + "the public Hygon driver header.") + endif() + _infinirt_use_backend_runtime("" "${HYGON_CUDA_DRIVER_LIBRARY}") + set_target_properties(infinirt PROPERTIES CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON diff --git a/src/native/cuda/hygon/driver_.h b/src/native/cuda/hygon/driver_.h new file mode 100644 index 0000000..7bc8f5f --- /dev/null +++ b/src/native/cuda/hygon/driver_.h @@ -0,0 +1,54 @@ +#ifndef INFINI_RT_HYGON_DRIVER__H_ +#define INFINI_RT_HYGON_DRIVER__H_ + +// clang-format off +#include +// clang-format on + +#include "native/cuda/driver_.h" +#include "native/cuda/hygon/device_.h" + +namespace infini::rt::driver { + +template <> +struct Driver : CudaDriver> { + using Result = CUresult; + + using Module = CUmodule; + + using Function = CUfunction; + + using Stream = CUstream; + + static constexpr Device::Type kDeviceType = Device::Type::kHygon; + + static constexpr Result kSuccess = CUDA_SUCCESS; + + static constexpr auto ModuleLoadData = cuModuleLoadData; + + static constexpr auto ModuleGetFunction = cuModuleGetFunction; + + static constexpr auto ModuleUnload = cuModuleUnload; + + static constexpr auto FuncGetAttribute = cuFuncGetAttribute; + + static constexpr auto kFuncAttributeSharedSizeBytes = + CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES; + + static constexpr auto FuncSetCacheConfig = cuFuncSetCacheConfig; + + static constexpr auto kFuncCachePreferShared = CU_FUNC_CACHE_PREFER_SHARED; + + static constexpr auto FuncSetAttribute = cuFuncSetAttribute; + + static constexpr auto kFuncAttributeMaxDynamicSharedSizeBytes = + CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES; + + static constexpr auto LaunchKernel = cuLaunchKernel; +}; + +static_assert(Driver::Validate()); + +} // namespace infini::rt::driver + +#endif diff --git a/src/native/cuda/hygon/runtime_.h b/src/native/cuda/hygon/runtime_.h index df88c44..b82f561 100644 --- a/src/native/cuda/hygon/runtime_.h +++ b/src/native/cuda/hygon/runtime_.h @@ -40,6 +40,19 @@ struct Runtime static constexpr auto GetDeviceCount = cudaGetDeviceCount; + static constexpr auto DeviceGetAttribute = cudaDeviceGetAttribute; + + static constexpr auto kDevAttrWarpSize = cudaDevAttrWarpSize; + + static constexpr auto kDevAttrComputeCapabilityMajor = + cudaDevAttrComputeCapabilityMajor; + + static constexpr auto kDevAttrComputeCapabilityMinor = + cudaDevAttrComputeCapabilityMinor; + + static constexpr auto kDevAttrMaxSharedMemoryPerBlockOptin = + cudaDevAttrMaxSharedMemoryPerBlockOptin; + static constexpr auto DeviceSynchronize = cudaDeviceSynchronize; static constexpr auto Malloc = [](auto&&... args) { diff --git a/tests/install_consumer_smoke.cc b/tests/install_consumer_smoke.cc index 968c144..9864d74 100644 --- a/tests/install_consumer_smoke.cc +++ b/tests/install_consumer_smoke.cc @@ -1,4 +1,7 @@ #include +#if defined(INFINI_RT_CONSUMER_BACKEND_HYGON) +#include +#endif #include #include @@ -140,5 +143,36 @@ int main() { } #endif +#if defined(INFINI_RT_CONSUMER_BACKEND_HYGON) + // The Hygon driver interface is header-only and its entry points live in + // the CUDA driver library, not in cudart. Referencing every one of them + // makes this executable's link fail when the installed InfiniRT target + // does not carry that dependency - on any machine, with or without a + // device, which is exactly the stage the missing symbol belongs to. + namespace hygon_driver = infini::rt::driver; + using HygonDriver = hygon_driver::Driver; + using DriverEntryPoint = void (*)(); + // The volatile store keeps each address from being folded away at + // optimization time. + volatile DriverEntryPoint driver_entry_point = nullptr; + driver_entry_point = + reinterpret_cast(HygonDriver::ModuleLoadData); + driver_entry_point = + reinterpret_cast(HygonDriver::ModuleGetFunction); + driver_entry_point = + reinterpret_cast(HygonDriver::ModuleUnload); + driver_entry_point = + reinterpret_cast(HygonDriver::FuncGetAttribute); + driver_entry_point = + reinterpret_cast(HygonDriver::FuncSetCacheConfig); + driver_entry_point = + reinterpret_cast(HygonDriver::FuncSetAttribute); + driver_entry_point = + reinterpret_cast(HygonDriver::LaunchKernel); + if (driver_entry_point == nullptr) { + return 1; + } +#endif + return 0; }