Skip to content

Commit

Permalink
cuda: add minimal CUDA program + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danbev committed Aug 7, 2024
1 parent bf0d01f commit e52469c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
1 change: 1 addition & 0 deletions gpu/cuda/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ hello-world.ptx
threads
inc
array-add
minimal
4 changes: 3 additions & 1 deletion gpu/cuda/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
minimal: src/minimal.cu
nvcc -o $@ $<

hello-world: src/hello-world.cu
nvcc -lnppc -o $@ $<
Expand All @@ -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
8 changes: 4 additions & 4 deletions gpu/cuda/cuda-env.sh
Original file line number Diff line number Diff line change
@@ -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 [email protected]
. ~/work/linux/spack/share/spack/setup-env.sh
spack load [email protected]
gcc --version
#. ~/work/linux/spack/share/spack/setup-env.sh
#spack load [email protected]
#gcc --version
39 changes: 38 additions & 1 deletion gpu/cuda/src/hello-world.cu
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions gpu/cuda/src/minimal.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>

__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;
}

0 comments on commit e52469c

Please sign in to comment.