You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

  1. #include "cuda/kernels/graph.h"
  2. #include <stdio.h>
  3. const int N = 16;
  4. const int blocksize = 16;
  5. #define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
  6. inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
  7. {
  8. if (code != cudaSuccess)
  9. {
  10. fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
  11. if (abort) exit(code);
  12. }
  13. }
  14. __global__
  15. void hello(char *a, int *b)
  16. {
  17. a[threadIdx.x] += b[threadIdx.x];
  18. }
  19. void helloWorldCuda()
  20. {
  21. printf("CUDA TEST START\n");
  22. printf("Should print \"Hello World\"\n");
  23. char a[N] = "Hello \0\0\0\0\0\0";
  24. int b[N] = {15, 10, 6, 0, -11, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  25. char c[N] = "YELLO \0\0\0\0\0\0";
  26. char *ad;
  27. int *bd;
  28. const int csize = N*sizeof(char);
  29. const int isize = N*sizeof(int);
  30. printf("%s", a);
  31. cudaMalloc( (void**)&ad, csize );
  32. cudaMalloc( (void**)&bd, isize );
  33. cudaMemcpy( ad, a, csize, cudaMemcpyHostToDevice );
  34. cudaMemcpy( bd, b, isize, cudaMemcpyHostToDevice );
  35. dim3 dimBlock( blocksize, 1 );
  36. dim3 dimGrid( 1, 1 );
  37. hello<<<dimGrid, dimBlock>>>(ad, bd);
  38. gpuErrchk( cudaPeekAtLastError() );
  39. gpuErrchk( cudaDeviceSynchronize() );
  40. cudaMemcpy( c, ad, csize, cudaMemcpyDeviceToHost );
  41. cudaFree( ad );
  42. cudaFree( bd );
  43. printf("%s\n", c);
  44. printf("CUDA TEST END\n");
  45. }