1,源码
hello.c
#include <cuda.h>
static void alloc_driver_api()
{
CUdeviceptr dptr = 0;
CUcontext context;
CUdevice device;
CUresult res;
res = cuInit(0);
if (res != CUDA_SUCCESS) {
printf("cuInit() failed: %d\n", res);
return;
}
res = cuDeviceGet(&device, 0);
if (res != CUDA_SUCCESS) {
printf("cuDeviceGet(0) failed: %d\n", res);
return;
}
res = cuCtxCreate(&context, 0, device);
if (res != CUDA_SUCCESS) {
printf("cuCtxCreate() failed: %d\n", res);
return;
}
res = cuMemAlloc(&dptr, 4096);
printf("cuMemAlloc() returned 0x%lx result %d\n", (uintptr_t)dptr, res);
cuMemFree(dptr);
cuCtxDetach(context);
}
int main()
{
alloc_driver_api();
return 0;
}
Makefile
hello_cuda_drv_api: hello.c
gcc $< -o $@ $(INC) $(LD_FLAGS)
INC := -I/usr/local/cuda/include
LD_FLAGS := -L/usr/local/cuda/lib64 -lcuda
.PHONY: clean
clean:
-rm -rf hello_cuda_drv_api
2,编译运行
make
./hello_cuda_drv_api
效果: