From 4500fcbb18a2f1fd4b3da58018c52fe55bbfe380 Mon Sep 17 00:00:00 2001 From: Anton Ljungdahl Date: Tue, 22 Apr 2025 20:45:06 +0200 Subject: [PATCH] hello cuda world --- build.bat | 24 +++++++++++ src/editortest.cu | 12 ++++++ src/main.cu | 104 ++++++++++++++++++++++++++++++++++++++++++++++ timeBuild.ctm | Bin 0 -> 100 bytes 4 files changed, 140 insertions(+) create mode 100644 build.bat create mode 100644 src/editortest.cu create mode 100644 src/main.cu create mode 100644 timeBuild.ctm diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..9153492 --- /dev/null +++ b/build.bat @@ -0,0 +1,24 @@ +@echo off + +ctime -begin timeBuild.ctm + +set CommonCompilerFlags=/nologo /Zi /FC /Od +@rem /WX /W4 /wd4201 /wd4100 /wd4189 /wd4244 /wd4127 /wd4456 +@rem +@rem + +set cuda_root=D:/lib/cudatoolkit/lib/x64 + +set CudaSources=../src/main.cu + +IF NOT EXIST .\build mkdir .\build +pushd .\build +nvcc %CudaSources% -o program.exe + +set LastError=%ERRORLEVEL% +popd + +ctime -end timeBuild.ctm %LastError% +IF NOT %LastError%==0 GOTO :end + +:end diff --git a/src/editortest.cu b/src/editortest.cu new file mode 100644 index 0000000..64ea95c --- /dev/null +++ b/src/editortest.cu @@ -0,0 +1,12 @@ + +__global__ void some_global_kernel() { + + some_kernel<<<32, 32>>>(0,0); +} + + +__device__ void some_kernel(int a, int b) +{ + + return; +} \ No newline at end of file diff --git a/src/main.cu b/src/main.cu new file mode 100644 index 0000000..9d1e107 --- /dev/null +++ b/src/main.cu @@ -0,0 +1,104 @@ +#include + + +typedef int S32; +typedef unsigned int U32; +typedef float F32; + + +#define CUDA_CHECK(err) do { \ + if (err != cudaSuccess) { \ + fprintf(stderr, "CUDA ERROR: %s at %s:%d\n", \ + cudaGetErrorString(err), __FILE__, __LINE__); \ + exit(EXIT_FAILURE); \ + } \ +} while (0) + + + +#define LOG printf + + + +#define NUM_BLOCKS 1 +#define NUM_THREADS 32 + + + + + +__device__ void modify_array(U32 *arr, U32 N) +{ + U32 i = threadIdx.x; + if(i < N) + { + arr[i] = i; + } + + return; +} + + +__global__ void hello_from_device(U32 *arr, U32 N) +{ + + modify_array(arr, N); + + + return; +} + + + + +int main() +{ + + LOG("Hello from cpu\n"); + + + U32 N = NUM_THREADS; + U32 arr_size = N * sizeof(U32); + + + U32 *arr = (U32 *)malloc(arr_size); + memset(arr, 0, arr_size); + + cudaError_t cuErr; + U32 *d_arr = 0; + + + + cuErr = cudaMalloc(&d_arr, arr_size); + CUDA_CHECK(cuErr); + + cuErr = cudaMemcpy(d_arr, arr, arr_size, cudaMemcpyHostToDevice); + CUDA_CHECK(cuErr); + + LOG("Array before CUDA \n"); + for(U32 i = 0; i < N; i += 1) + { + LOG("%i: %i \n", i, arr[i]); + } + LOG("\n"); + + + hello_from_device<<>>(d_arr, N); + + cuErr = cudaMemcpy(arr, d_arr, arr_size, cudaMemcpyDeviceToHost); + CUDA_CHECK(cuErr); + + cuErr = cudaFree(d_arr); + CUDA_CHECK(cuErr); + + LOG("Array after CUDA \n"); + for(U32 i = 0; i < N; i += 1) + { + LOG("%i \n", arr[i]); + } + LOG("\n"); + + + return 0; +} + diff --git a/timeBuild.ctm b/timeBuild.ctm new file mode 100644 index 0000000000000000000000000000000000000000..764b2f9f172497c8b7e382e1431b03099aa838a8 GIT binary patch literal 100 zcmcC7j63yr<=o6In{P8RGB7a2vobK`eRisZ@!eP$81mWnb-?%~K>o$^91}r&W}td= bP6md031|7OP<|0R1A~(7N)H%+5|9r7bJ-mB literal 0 HcmV?d00001