I am studying cuda library management api on RTX 5060 Ti with CUDA 12.9 on Windows
I write an device.cu, use nvcc -rdc=true -cubin device.cu -o device.cubin
And then I write an main.cu, which use the follow api:
-
cuLibraryLoadFromFileto get theCUlibrary -
cuLibraryGetKernelto get theCUkernel -
cuLibraryGetUnifiedFunctionto get thefunction ptr void **
But I just can’t run cuLibraryGetUnifiedFunction, is there any example or document to show how to use get unified function?
Here is the device.cu:
#include <cuda.h>
extern "C" __device__ void device_func_add(int* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if(i < n){
data[i] += 1;
}
}
extern "C" __device__ void device_func_sub(int* data, int n) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if(i < n){
data[i] -= 1;
}
}
__device__ void (*g_fn_add)(int* data, int n) = device_func_add;
__device__ void (*g_fn_sub)(int* data, int n) = device_func_sub;
extern "C" __global__
void elementWise(void (*func)(int*, int), int* data, int n)
{
func(data, n);
}
Here is the main.cu:
#include <cuda.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
namespace {
constexpr int kNumElements = 256;
// Driver API returns CUresult instead of cudaError_t, and there is no
// cudaGetErrorString(); resolve both the symbolic name and the message.
void reportError(const char* expr, CUresult rc) {
const char* name = nullptr;
const char* str = nullptr;
cuGetErrorName(rc, &name);
cuGetErrorString(rc, &str);
std::cerr << expr << " failed: " << (name ? name : "?") << " ("
<< (str ? str : "?") << ")" << std::endl;
}
#define CU_CHECK(expr) \
do { \
CUresult _rc = (expr); \
if (_rc != CUDA_SUCCESS) { \
reportError(#expr, _rc); \
std::exit(EXIT_FAILURE); \
} \
} while (false)
// Matches the device-side signature of device_func_add / device_func_sub.
using DeviceFuncPtr = void (*)(int*, int);
} // namespace
int main() {
CU_CHECK(cuInit(0));
int deviceCount = 0;
CU_CHECK(cuDeviceGetCount(&deviceCount));
if (deviceCount == 0) {
std::cerr << "No CUDA-capable device found." << std::endl;
return EXIT_FAILURE;
}
CUdevice device;
CU_CHECK(cuDeviceGet(&device, 0));
char deviceName[256] = {};
CU_CHECK(cuDeviceGetName(deviceName, sizeof(deviceName), device));
int unifiedAddressing = 0;
CU_CHECK(cuDeviceGetAttribute(
&unifiedAddressing, CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING, device));
int unifiedFunctionPointers = 0;
CU_CHECK(cuDeviceGetAttribute(
&unifiedFunctionPointers,
CU_DEVICE_ATTRIBUTE_UNIFIED_FUNCTION_POINTERS, device));
std::cout << "Device: " << deviceName << "\n"
<< " unified addressing: " << unifiedAddressing << "\n"
<< " unified function pointers: " << unifiedFunctionPointers
<< std::endl;
if (!unifiedFunctionPointers) {
std::cerr << "Device does not support unified function pointers."
<< std::endl;
return EXIT_FAILURE;
}
// cuInit() does NOT create a context. The driver API needs a current
// context for allocations, copies and kernel launches.
CUcontext context;
CU_CHECK(cuDevicePrimaryCtxRetain(&context, device));
CU_CHECK(cuCtxSetCurrent(context));
// Context-less library load: kernels are addressed by name, not by a
// module + function handle.
CUlibrary library;
CU_CHECK(cuLibraryLoadFromFile(&library, "device.cubin",
nullptr, nullptr, 0,
nullptr, nullptr, 0));
std::cout << "Loaded device.cubin" << std::endl;
// Unified function pointer: a host-callable pointer to a __device__
// function, usable as a kernel argument.
DeviceFuncPtr deviceFunc = nullptr;
CUresult rc = cuLibraryGetUnifiedFunction(
reinterpret_cast<void**>(&deviceFunc), library, "device_func_add");
if (rc != CUDA_SUCCESS) {
std::cout.flush();
reportError("cuLibraryGetUnifiedFunction(\"device_func_add\")", rc);
} else {
std::cout << "Resolved unified function 'device_func_add'" << std::endl;
}
CUkernel kernel;
CU_CHECK(cuLibraryGetKernel(&kernel, library, "elementWise"));
std::cout << "Resolved kernel \"elementWise\"" << std::endl;
int hostIn[kNumElements];
int hostOut[kNumElements];
for (int i = 0; i < kNumElements; ++i) {
hostIn[i] = i;
hostOut[i] = -1;
}
CUdeviceptr deviceData = 0;
CU_CHECK(cuMemAlloc(&deviceData, kNumElements * sizeof(int)));
CU_CHECK(cuMemcpyHtoD(deviceData, hostIn, kNumElements * sizeof(int)));
// Round-trip the buffer so the allocation/copy path is verified even when
// the launch cannot run.
CU_CHECK(cuMemcpyDtoH(hostOut, deviceData, kNumElements * sizeof(int)));
bool copyOk = true;
for (int i = 0; i < kNumElements; ++i) {
if (hostOut[i] != hostIn[i]) {
copyOk = false;
break;
}
}
std::cout << "Host->device->host round trip: "
<< (copyOk ? "OK" : "MISMATCH") << std::endl;
bool launchOk = false;
if (deviceFunc != nullptr) {
// Kernel parameters are passed as an array of pointers to the
// arguments. 'n' must be a real lvalue: the driver reads sizeof(int)
// bytes from the address it is given.
int n = kNumElements;
void* kernelArgs[] = {
static_cast<void*>(&deviceFunc), // void (*)(int*, int)
static_cast<void*>(&deviceData), // int*
static_cast<void*>(&n), // int
};
// dim3 is a runtime-API type and is unavailable here, so the launch
// geometry is passed as plain unsigned ints. A CUKernel is launched
// by casting it to CUfunction.
CU_CHECK(cuLaunchKernel(reinterpret_cast<CUfunction>(kernel),
1, 1, 1, // gridDim x/y/z
kNumElements, 1, 1, // blockDim x/y/z
0, nullptr, // sharedMemBytes, stream
kernelArgs, nullptr));
CU_CHECK(cuCtxSynchronize());
CU_CHECK(cuMemcpyDtoH(hostOut, deviceData,
kNumElements * sizeof(int)));
int mismatches = 0;
for (int i = 0; i < kNumElements; ++i) {
if (hostOut[i] != hostIn[i] + 1) {
++mismatches;
}
}
launchOk = (mismatches == 0);
std::cout << "data[0..3]: ";
for (int i = 0; i < 4; ++i) {
std::cout << hostOut[i] << (i == 3 ? "" : ", ");
}
std::cout << " (expected " << hostIn[0] + 1 << ", " << hostIn[1] + 1
<< ", " << hostIn[2] + 1 << ", " << hostIn[3] + 1 << ")\n"
<< "data[" << kNumElements - 1 << "]: "
<< hostOut[kNumElements - 1] << " (expected "
<< hostIn[kNumElements - 1] + 1 << ")" << std::endl;
if (launchOk) {
std::cout << "All " << kNumElements
<< " elements incremented correctly" << std::endl;
} else {
std::cerr << mismatches << " / " << kNumElements
<< " elements are wrong" << std::endl;
}
}
CU_CHECK(cuMemFree(deviceData));
CU_CHECK(cuLibraryUnload(library));
CU_CHECK(cuDevicePrimaryCtxRelease(device));
if (!copyOk) {
return EXIT_FAILURE;
}
if (deviceFunc == nullptr) {
// Distinct code: the driver-API path is fine, the cubin lacks a UFT.
return 2;
}
return launchOk ? EXIT_SUCCESS : EXIT_FAILURE;
}
Here is the running log:
PS D:\Users\xxx\WorkSpace\libmanagement> cuobjdump.exe -symbols .\device.cubin
symbols:
STT_OBJECT STB_WEAK STV_DEFAULT U __UDT_OFFSET
STT_OBJECT STB_WEAK STV_DEFAULT U __UFT_OFFSET
STT_OBJECT STB_WEAK STV_DEFAULT U __UFT_CANONICAL
STT_OBJECT STB_WEAK STV_DEFAULT U __UDT_CANONICAL
STT_OBJECT STB_WEAK STV_DEFAULT U __UFT
STT_OBJECT STB_WEAK STV_DEFAULT U __UDT
STT_OBJECT STB_WEAK STV_DEFAULT U __UFT_END
STT_OBJECT STB_WEAK STV_DEFAULT U __UDT_END
STT_OBJECT STB_WEAK STV_DEFAULT U __nv.reservedSmem.offset0
STT_FUNC STB_GLOBAL STO_ENTRY elementwise
STT_FUNC STB_GLOBAL STV_DEFAULT device_func_sub
STT_FUNC STB_GLOBAL STV_DEFAULT device_func_add
PS D:\Users\xxx\WorkSpace\libmanagement> .\main_without_runtime.exe
Device: NVIDIA GeForce RTX 5060 Ti
unified addressing: 1
unified function pointers: 1
Loaded device.cubin
cuLibraryGetUnifiedFunction("device_func_add") failed: CUDA_ERROR_INVALID_VALUE (invalid argument)
The cubin has no unified function table (.nv.uft/.nv.udt).
ptxas emits it only for .unified_func_stub entries, which the
CUDA 12.9 toolchain does not produce for a plain
extern "C" __device__ function.
Check with: cuobjdump -symbols device.cubin | findstr UFT
(WEAK UNDEFINED __UFT_* means no table was built)
Skipping the kernel launch; the rest of the driver-API path
still runs below.
Resolved kernel 'elementWise'
Host->device->host round trip: OK