Skip to content

Instantly share code, notes, and snippets.

@kmaehashi
Created November 28, 2023 15:27
Show Gist options
  • Save kmaehashi/dbc5fc4c22a70861a90cba2179e588ba to your computer and use it in GitHub Desktop.
Save kmaehashi/dbc5fc4c22a70861a90cba2179e588ba to your computer and use it in GitHub Desktop.
// OK:
// gcc test_dlopen.c -ldl -lcuda -lcudart -L/usr/local/cuda-12.2.1/lib64 -I/usr/local/cuda-12.2.1/include -o test
// FAIL with "Error getting global symbol: 201" when compiled with "-DDLOPEN":
// gcc test_dlopen.c -ldl -lcuda -lcudart -L/usr/local/cuda-12.2.1/lib64 -I/usr/local/cuda-12.2.1/include -o test -DDLOPEN
#include <stdio.h>
#include <dlfcn.h>
#include <cuda.h>
#include <cuda_runtime_api.h>
int main() {
#ifdef DLOPEN
void* libHandle = dlopen("libcuda.so", RTLD_NOW);
if (!libHandle) {
fprintf(stderr, "Error loading libcuda.so: %s\n", dlerror());
return -1;
}
typedef CUresult (*cuInit_t)(unsigned int);
typedef CUresult (*cuDeviceGet_t)(CUdevice*, int);
typedef CUresult (*cuCtxCreate_t)(CUcontext*, unsigned int, CUdevice);
typedef CUresult (*cuModuleLoad_t)(CUmodule*, const char*);
typedef CUresult (*cuModuleGetGlobal_t)(CUdeviceptr*, size_t*, CUmodule, const char*);
typedef CUresult (*cuModuleUnload_t)(CUmodule);
typedef CUresult (*cuCtxDestroy_t)(CUcontext);
cuInit_t cuInit = (cuInit_t)dlsym(libHandle, "cuInit");
cuDeviceGet_t cuDeviceGet = (cuDeviceGet_t)dlsym(libHandle, "cuDeviceGet");
cuCtxCreate_t cuCtxCreate = (cuCtxCreate_t)dlsym(libHandle, "cuCtxCreate");
cuModuleLoad_t cuModuleLoad = (cuModuleLoad_t)dlsym(libHandle, "cuModuleLoad");
cuModuleGetGlobal_t cuModuleGetGlobal = (cuModuleGetGlobal_t)dlsym(libHandle, "cuModuleGetGlobal");
cuModuleUnload_t cuModuleUnload = (cuModuleUnload_t)dlsym(libHandle, "cuModuleUnload");
cuCtxDestroy_t cuCtxDestroy = (cuCtxDestroy_t)dlsym(libHandle, "cuCtxDestroy");
#endif
CUresult res;
CUdevice cuDevice;
CUcontext cuContext;
CUmodule cuModule;
CUdeviceptr dptr;
size_t bytes;
// Create an implicit context.
cudaError_t err = cudaSetDevice(0);
if (err != CUDA_SUCCESS) {
fprintf(stderr, "Error setting device: %d\n", err);
return -1;
}
// Load the module from a PTX file
res = cuModuleLoad(&cuModule, "test.ptx");
if (res != CUDA_SUCCESS) {
printf("Error loading module: %d\n", res);
return -1;
}
// Retrieve a pointer to the global symbol
res = cuModuleGetGlobal(&dptr, &bytes, cuModule, "some_array");
if (res != CUDA_SUCCESS) {
printf("Error getting global symbol: %d\n", res);
cuModuleUnload(cuModule);
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment