Advertisement
Ucurrent

Types_cuda.h

Jan 17th, 2023
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | Source Code | 0 0
  1. //types.h cuda attempt
  2. #include <cuda_runtime.h>
  3.  
  4. __global__ void image_kernel(Pixel32 *image, int width, int height) {
  5.     int x = threadIdx.x + blockIdx.x * blockDim.x;
  6.     int y = threadIdx.y + blockIdx.y * blockDim.y;
  7.     if (x < width && y < height) {
  8.         int index = y * width + x;
  9.         Pixel32 pixel = image[index];
  10.         // Do some operation on the pixel
  11.         // ...
  12.         image[index] = pixel;
  13.     }
  14. }
  15.  
  16. Pixel32 *image_data;
  17. int width, height;
  18.  
  19. // Allocate memory for image_data on the GPU
  20. cudaMalloc(&image_data, width * height * sizeof(Pixel32));
  21.  
  22. // Copy image_data to the GPU
  23. cudaMemcpy(image_data, host_image_data, width * height * sizeof(Pixel32), cudaMemcpyHostToDevice);
  24.  
  25. // Launch the kernel
  26. dim3 block(16, 16);
  27. dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y);
  28. image_kernel<<<grid, block>>>(image_data, width, height);
  29.  
  30. // Copy image_data back to the host
  31. cudaMemcpy(host_image_data, image_data, width * height * sizeof(Pixel32), cudaMemcpyDeviceToHost);
  32.  
  33. // Free GPU memory
  34. cudaFree(image_data);
  35. /* better to use code below?
  36. SFLOAT *data;
  37. int size;
  38.  
  39. // Allocate memory for data on the GPU
  40. cudaMalloc(&data, size * sizeof(SFLOAT));
  41.  
  42. // Copy data to the GPU
  43. cudaMemcpy(data, host_data, size * sizeof(SFLOAT), cudaMemcpyHostToDevice);
  44.  
  45. // Launch the kernel
  46. int threads = 256;
  47. int blocks = (size + threads - 1) / threads;
  48. visual_kernel<<<blocks, threads>>>(data, size);
  49.  
  50. // Copy data back to the host
  51. cudaMemcpy(host_data, data, size * sizeof(SFLOAT), cudaMemcpyDeviceToHost);
  52.  
  53. // Free GPU memory
  54. cudaFree(data); */
Tags: CUDA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement