From e52469cc77bef998d99a5695444c852842594549 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Wed, 7 Aug 2024 07:43:43 +0200 Subject: [PATCH] cuda: add minimal CUDA program + cleanup --- gpu/cuda/.gitignore | 1 + gpu/cuda/Makefile | 4 +++- gpu/cuda/cuda-env.sh | 8 ++++---- gpu/cuda/src/hello-world.cu | 39 ++++++++++++++++++++++++++++++++++++- gpu/cuda/src/minimal.cu | 32 ++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 gpu/cuda/src/minimal.cu diff --git a/gpu/cuda/.gitignore b/gpu/cuda/.gitignore index d311cee0..c4f2f12a 100644 --- a/gpu/cuda/.gitignore +++ b/gpu/cuda/.gitignore @@ -3,3 +3,4 @@ hello-world.ptx threads inc array-add +minimal diff --git a/gpu/cuda/Makefile b/gpu/cuda/Makefile index 9b0b1119..6ec8c85e 100644 --- a/gpu/cuda/Makefile +++ b/gpu/cuda/Makefile @@ -1,3 +1,5 @@ +minimal: src/minimal.cu + nvcc -o $@ $< hello-world: src/hello-world.cu nvcc -lnppc -o $@ $< @@ -17,4 +19,4 @@ array-add: src/array-add.cu .PHONY: clean clean: - ${RM} hello-world threads inc hello-world.ptx + ${RM} hello-world threads inc hello-world.ptx minimal diff --git a/gpu/cuda/cuda-env.sh b/gpu/cuda/cuda-env.sh index 58c700bd..de690f99 100644 --- a/gpu/cuda/cuda-env.sh +++ b/gpu/cuda/cuda-env.sh @@ -1,8 +1,8 @@ -VERSION=12.2 +VERSION=12.4 export PATH=/usr/local/cuda-${VERSION}/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda-${VERSION}/lib64:$LD_LIBRARY_PATH # Source Spack so that we can switch to use gcc@12.1.0 -. ~/work/linux/spack/share/spack/setup-env.sh -spack load gcc@12.1.0 -gcc --version +#. ~/work/linux/spack/share/spack/setup-env.sh +#spack load gcc@12.1.0 +#gcc --version diff --git a/gpu/cuda/src/hello-world.cu b/gpu/cuda/src/hello-world.cu index 64fa46d4..7594b192 100644 --- a/gpu/cuda/src/hello-world.cu +++ b/gpu/cuda/src/hello-world.cu @@ -7,9 +7,46 @@ __global__ void helloWorld() { } int main() { + cudaError_t cudaStatus = cudaGetLastError(); + // Print CUDA version + int runtimeVersion = 0; + cudaStatus = cudaRuntimeGetVersion(&runtimeVersion); + if (cudaStatus != cudaSuccess) { + fprintf(stderr, "cudaRuntimeGetVersion failed: %s\n", cudaGetErrorString(cudaStatus)); + return 1; + } + printf("CUDA Runtime version: %d\n", runtimeVersion); + + // Check for CUDA device + int deviceCount = 0; + cudaStatus = cudaGetDeviceCount(&deviceCount); + if (cudaStatus != cudaSuccess) { + fprintf(stderr, "cudaGetDeviceCount failed: %s\n", cudaGetErrorString(cudaStatus)); + return 1; + } + printf("Number of CUDA devices: %d\n", deviceCount); + + if (deviceCount == 0) { + fprintf(stderr, "No CUDA-capable devices found\n"); + return 1; + } + + + + // Print device information + /* + cudaDeviceProp prop; + cudaStatus = cudaGetDeviceProperties(&prop, 0); + if (cudaStatus != cudaSuccess) { + fprintf(stderr, "cudaGetDeviceProperties failed: %s\n", cudaGetErrorString(cudaStatus)); + return 1; + } + printf("Device name: %s\n", prop.name); + printf("Compute capability: %d.%d\n", prop.major, prop.minor); + */ + helloWorld<<<1, 1>>>(); - cudaError_t cudaStatus = cudaGetLastError(); if (cudaStatus != cudaSuccess) { fprintf(stderr, "helloWorld launch failed: %s\n", cudaGetErrorString(cudaStatus)); return 1; diff --git a/gpu/cuda/src/minimal.cu b/gpu/cuda/src/minimal.cu new file mode 100644 index 00000000..3a3818a7 --- /dev/null +++ b/gpu/cuda/src/minimal.cu @@ -0,0 +1,32 @@ +#include + +__global__ void myKernel() {} + +int main() { + cudaError_t err = cudaSuccess; + int count = 0; + + err = cudaGetDeviceCount(&count); + if (err != cudaSuccess) { + printf("cudaGetDeviceCount failed: %s\n", cudaGetErrorString(err)); + return 1; + } + + printf("CUDA device count: %d\n", count); + + myKernel<<<1,1>>>(); + err = cudaGetLastError(); + if (err != cudaSuccess) { + printf("Kernel launch failed: %s\n", cudaGetErrorString(err)); + return 1; + } + + err = cudaDeviceSynchronize(); + if (err != cudaSuccess) { + printf("cudaDeviceSynchronize failed: %s\n", cudaGetErrorString(err)); + return 1; + } + + printf("CUDA program ran successfully\n"); + return 0; +}