Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cmake/InfiniRTConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions scripts/generate_public_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
23 changes: 22 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions src/native/cuda/hygon/driver_.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef INFINI_RT_HYGON_DRIVER__H_
#define INFINI_RT_HYGON_DRIVER__H_

// clang-format off
#include <cuda.h>
// clang-format on

#include "native/cuda/driver_.h"
#include "native/cuda/hygon/device_.h"

namespace infini::rt::driver {

template <>
struct Driver<Device::Type::kHygon> : CudaDriver<Driver<Device::Type::kHygon>> {
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<Device::Type::kHygon>::Validate());

} // namespace infini::rt::driver

#endif
13 changes: 13 additions & 0 deletions src/native/cuda/hygon/runtime_.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ struct Runtime<Device::Type::kHygon>

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) {
Expand Down
34 changes: 34 additions & 0 deletions tests/install_consumer_smoke.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <infini/rt.h>
#if defined(INFINI_RT_CONSUMER_BACKEND_HYGON)
#include <infini/rt/hygon/driver_.h>
#endif

#include <array>
#include <cstddef>
Expand Down Expand Up @@ -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<infini::rt::Device::Type::kHygon>;
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<DriverEntryPoint>(HygonDriver::ModuleLoadData);
driver_entry_point =
reinterpret_cast<DriverEntryPoint>(HygonDriver::ModuleGetFunction);
driver_entry_point =
reinterpret_cast<DriverEntryPoint>(HygonDriver::ModuleUnload);
driver_entry_point =
reinterpret_cast<DriverEntryPoint>(HygonDriver::FuncGetAttribute);
driver_entry_point =
reinterpret_cast<DriverEntryPoint>(HygonDriver::FuncSetCacheConfig);
driver_entry_point =
reinterpret_cast<DriverEntryPoint>(HygonDriver::FuncSetAttribute);
driver_entry_point =
reinterpret_cast<DriverEntryPoint>(HygonDriver::LaunchKernel);
if (driver_entry_point == nullptr) {
return 1;
}
#endif

return 0;
}