hello.cpp
1 // examples/nv/hello.cpp 2 // 3 // minimal nv target test - compiled with clang, NOT nvcc 4 // 5 // verifies: 6 // - clang can compile .cu files 7 // - nvidia-sdk headers are found 8 // - libcudart links correctly 9 // - device code executes on sm_90+ 10 11 #include <cstdio> 12 #include <cuda_runtime.h> 13 14 // device kernel - runs on nv hardware 15 // cppcheck-suppress unusedFunction ; called via CUDA launch syntax 16 __global__ void straylight_kernel(int *result) { *result = 42; } 17 18 // host code 19 auto main() -> int { 20 // check for nv devices 21 int device_count = 0; 22 cudaError_t error = cudaGetDeviceCount(&device_count); 23 24 if (error != cudaSuccess) { 25 std::printf("straylight nv toolchain: no devices (driver not loaded)\n"); 26 std::printf("compilation succeeded - clang handled .cu correctly\n"); 27 return 0; // success - we're testing the toolchain, not the hardware 28 } 29 30 if (device_count == 0) { 31 std::printf("straylight nv toolchain: no devices found\n"); 32 std::printf("compilation succeeded - clang handled .cu correctly\n"); 33 return 0; 34 } 35 36 // allocate device memory 37 int *device_result = nullptr; 38 cudaMalloc(&device_result, sizeof(int)); 39 40 // launch kernel 41 straylight_kernel<<<1, 1>>>(device_result); 42 cudaDeviceSynchronize(); 43 44 // copy result back 45 int host_result = 0; 46 cudaMemcpy(&host_result, device_result, sizeof(int), cudaMemcpyDeviceToHost); 47 48 // cleanup 49 cudaFree(device_result); 50 51 if (host_result == 42) { 52 std::printf("straylight nv toolchain operational (device returned %d)\n", 53 host_result); 54 return 0; 55 } else { 56 std::printf("straylight nv toolchain: unexpected result %d\n", host_result); 57 return 1; 58 } 59 }