hello cuda world

This commit is contained in:
Anton Ljungdahl 2025-04-22 20:45:06 +02:00
parent c6ece025dd
commit 4500fcbb18
4 changed files with 140 additions and 0 deletions

24
build.bat Normal file
View File

@ -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

12
src/editortest.cu Normal file
View File

@ -0,0 +1,12 @@
__global__ void some_global_kernel() {
some_kernel<<<32, 32>>>(0,0);
}
__device__ void some_kernel(int a, int b)
{
return;
}

104
src/main.cu Normal file
View File

@ -0,0 +1,104 @@
#include <stdio.h>
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<<<NUM_BLOCKS, NUM_THREADS>>>(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;
}

BIN
timeBuild.ctm Normal file

Binary file not shown.