From 7c77cf7380f74ee46dbcc24801d87baa08f30b37 Mon Sep 17 00:00:00 2001 From: ilmaalifia <ilma.mahardika@gmail.com> Date: Wed, 10 Apr 2019 20:01:38 +0700 Subject: [PATCH] hello again --- hello.c | 8 -------- hello.cu | 42 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 14 deletions(-) delete mode 100644 hello.c diff --git a/hello.c b/hello.c deleted file mode 100644 index 9dcb9de..0000000 --- a/hello.c +++ /dev/null @@ -1,8 +0,0 @@ -__global__ void cuda_hello(){ - printf("Hello World from GPU!\n"); -} - -int main() { - cuda_hello<<<1,1>>>(); - return 0; -} \ No newline at end of file diff --git a/hello.cu b/hello.cu index 9dcb9de..9c7f494 100644 --- a/hello.cu +++ b/hello.cu @@ -1,8 +1,38 @@ -__global__ void cuda_hello(){ - printf("Hello World from GPU!\n"); +#include <stdio.h> + +const int N = 16; +const int blocksize = 16; + +__global__ +void hello(char *a, int *b) +{ + a[threadIdx.x] += b[threadIdx.x]; } - -int main() { - cuda_hello<<<1,1>>>(); - return 0; + +int main() +{ + char a[N] = "Hello \0\0\0\0\0\0"; + int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + char *ad; + int *bd; + const int csize = N*sizeof(char); + const int isize = N*sizeof(int); + + printf("%s", a); + + cudaMalloc( (void**)&ad, csize ); + cudaMalloc( (void**)&bd, isize ); + cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice ); + cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice ); + + dim3 dimBlock( blocksize, 1 ); + dim3 dimGrid( 1, 1 ); + hello<<<dimGrid, dimBlock>>>(ad, bd); + cudaMemcpy( a, ad, csize, cudaMemcpyDeviceToHost ); + cudaFree( ad ); + cudaFree( bd ); + + printf("%s\n", a); + return EXIT_SUCCESS; } \ No newline at end of file -- GitLab