Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ cat t571.cu
- #include <stdio.h>
- #define DSIZE 16
- #define BLACK 0
- #define RED 1
- __global__ void red_black_ordering(float *d_grid, int grid_row, int value, float *d_diff1)
- {
- int index = blockIdx.x * blockDim.x + threadIdx.x;
- float temp = 0.0;
- int row = index/grid_row; // what row we're currently on
- int col = index - (row * grid_row); // what col we're currently on
- if (row != 0 && col != 0 && row != grid_row-1 && col != grid_row-1 && (row+col)%2 == value) {
- temp = d_grid[index];
- d_grid[index] = 99;
- //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]);
- d_diff1[index] = abs(d_grid[index] - temp);
- }
- }
- int main(){
- float *d_grid, *h_grid, *d_diff, *h_diff;
- int size = DSIZE*DSIZE;
- int row = DSIZE;
- h_grid = (float *)malloc(size*sizeof(float));
- h_diff = (float *)malloc(size*sizeof(float));
- cudaMalloc(&d_grid, size*sizeof(float));
- cudaMalloc(&d_diff, size*sizeof(float));
- cudaMemset(d_grid, 0, size*sizeof(float));
- cudaMemset(d_diff, 0, size*sizeof(float));
- int threads_per_block = 256;
- int blocks_per_grid = (int)( ( size + threads_per_block - 1) / threads_per_block);
- red_black_ordering <<< blocks_per_grid, threads_per_block >>> (d_grid, row, BLACK, d_diff);
- cudaMemcpy(h_grid, d_grid, size*sizeof(float), cudaMemcpyDeviceToHost);
- cudaMemcpy(h_diff, d_diff, size*sizeof(float), cudaMemcpyDeviceToHost);
- for (int i = 0; i < row; i++){
- for (int j = 0; j < row; j++)
- printf("%.0f ", h_diff[(i*row)+j]);
- printf("\n");}
- return 0;
- }
- $ nvcc -arch=sm_35 -o t571 t571.cu
- $ ./t571
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0 0
- 0 0 99 0 99 0 99 0 99 0 99 0 99 0 99 0
- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement