Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cuda_runtime.h>
  3. #include <device_launch_parameters.h>
  4. #define w 100
  5. #define h 100
  6. #define mw 10
  7. #define mh 10
  8. using namespace std;
  9. __global__ void kernel(int *a, int* b, int* count)
  10. {
  11.  
  12. int tidx = threadIdx.x + blockIdx.x * blockDim.x;
  13. int tidy = threadIdx.y + blockIdx.y * blockDim.y;
  14. int n =0;
  15. // if(a[tidy+tidx]==b[tidx]) atomicAdd(&count[tidy], 1);
  16. count[tidy*w+tidx] = a[tidy*w+tidx];
  17. }
  18. int main(void)
  19. {
  20. int *a_device, *b_device, *count_device;
  21. int b[mw][mh];
  22.  
  23. int **a = new int*[h];
  24. for(int i = 0; i< h; i++) a[i] = new int[w];
  25.  
  26. for(int i=0; i<h; i++)
  27. for(int j=0; j<w; j++)
  28. a[i][j] = i*w+j;
  29.  
  30. for(int i=0; i<mh; i++)
  31. for(int j=0; j<mw; j++)
  32. b[i][j] = i*mw+j;
  33.  
  34. const size_t a_size = sizeof(int) * size_t(w*h);
  35. const size_t b_size = sizeof(int) * size_t(mw*mh);
  36.  
  37. cudaMalloc((void **)&a_device, a_size);
  38. cudaMalloc((void **)&b_device, b_size);
  39.  
  40. count_device = new int[h*w - mh*mw];
  41. const size_t count_size = sizeof(int) * size_t(h*w - mh*mw);
  42.  
  43. cudaMalloc((void**)&count_device, count_size);
  44.  
  45. cudaMemcpy(a_device, a, a_size, cudaMemcpyHostToDevice);
  46. cudaMemcpy(b_device, b, b_size, cudaMemcpyHostToDevice);
  47.  
  48. const dim3 block(16,16);
  49. const dim3 grid((mw*mh + block.x - 1)/block.x, (w*h-mw*mh + block.y - 1)/block.y);
  50.  
  51. kernel<<<grid, block>>>(a_device, b_device, count_device);
  52. int* result = new int[h*w - mh*mw];
  53.  
  54. cudaMemcpy(result, count_device, count_size, cudaMemcpyDeviceToHost);
  55.  
  56. for(int i=0; i< h*w - mh*mw; i++)
  57. //if(result[i]>0)
  58. cout << "i= " << i << "res =" << result[i] << endl;
  59.  
  60. getchar();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement