Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. $ cat t571.cu
  2. #include <stdio.h>
  3. #define DSIZE 16
  4. #define BLACK 0
  5. #define RED 1
  6.  
  7. __global__ void red_black_ordering(float *d_grid, int grid_row, int value, float *d_diff1)
  8.  
  9. {
  10. int index = blockIdx.x * blockDim.x + threadIdx.x;
  11. float temp = 0.0;
  12. int row = index/grid_row; // what row we're currently on
  13. int col = index - (row * grid_row); // what col we're currently on
  14.  
  15. if (row != 0 && col != 0 && row != grid_row-1 && col != grid_row-1 && (row+col)%2 == value) {
  16. temp = d_grid[index];
  17. d_grid[index] = 99;
  18. //d_grid[index] = .2 * (d_grid[index] + d_grid[index-1] + d_grid[index+1] + d_grid[index-grid_row] + d_grid[index+grid_row]);
  19. d_diff1[index] = abs(d_grid[index] - temp);
  20. }
  21. }
  22.  
  23. int main(){
  24.  
  25. float *d_grid, *h_grid, *d_diff, *h_diff;
  26. int size = DSIZE*DSIZE;
  27. int row = DSIZE;
  28. h_grid = (float *)malloc(size*sizeof(float));
  29. h_diff = (float *)malloc(size*sizeof(float));
  30. cudaMalloc(&d_grid, size*sizeof(float));
  31. cudaMalloc(&d_diff, size*sizeof(float));
  32. cudaMemset(d_grid, 0, size*sizeof(float));
  33. cudaMemset(d_diff, 0, size*sizeof(float));
  34. int threads_per_block = 256;
  35. int blocks_per_grid = (int)( ( size + threads_per_block - 1) / threads_per_block);
  36. red_black_ordering <<< blocks_per_grid, threads_per_block >>> (d_grid, row, BLACK, d_diff);
  37. cudaMemcpy(h_grid, d_grid, size*sizeof(float), cudaMemcpyDeviceToHost);
  38. cudaMemcpy(h_diff, d_diff, size*sizeof(float), cudaMemcpyDeviceToHost);
  39. for (int i = 0; i < row; i++){
  40. for (int j = 0; j < row; j++)
  41. printf("%.0f ", h_diff[(i*row)+j]);
  42. printf("\n");}
  43. return 0;
  44. }
  45. $ nvcc -arch=sm_35 -o t571 t571.cu
  46. $ ./t571
  47. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  48. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  49. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  50. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  51. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  52. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  53. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  54. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  55. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  56. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  57. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  58. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  59. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  60. 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
  61. 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
  62. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  63. $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement