Questions about index of 2D array
Hi all,

I need a 2D array in my program. But I found that the index of it is wrong when I print them out. Here is an example.

[code]#define BLOCK_SIZE 8[/code]

host:
[code]   Â  Â  Â // distance is a 2D int array with size of col*row, where col=row=17.
  Â  Â  Â size_t pitchDistance;
  Â  Â  Â CUDA_SAFE_CALL(cudaMallocPitch((void**)&gDistance,&pitchDistance,sizeof(int)*col,row));
  Â  Â  Â CUDA_SAFE_CALL(cudaMemcpy2D(gDistance,pitchDistance,distance,sizeof(int)*col,sizeof(int)*col,row,cudaMemcpyHostToDevice));
.......
  Â  Â  Â /*
  Â  Â  Â  I also define a fake 1D array(dD) with size of col*row here. This array is for testing, values of block index or thread index will be saved, depends on demands. The result of the array will be printed out after the kernel is executed.  
  Â  Â  */
  Â  Â  Â dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE,1);
  Â  Â  Â dim3 dimGrid((col/dimBlock.x)+(!(col%dimBlock.x)?0:1),(row/dimBlock.y)+(!(row%dimBlock.y)?0:1),1);
  Â  Â  Â myKernel<<<dimGrid,dimBlock>>>(...,row,col,gDistance,pitchDistance/sizeof(int),...);
[/code]

device:
[code]__global__ void myKernel(...,int col,int row,int *distance,int pitchDistance,......){
  Â  Â  Â int bx = blockIdx.x;
  Â  Â  Â int by = blockIdx.y;
  Â  Â  Â int tx = threadIdx.x;
  Â  Â  Â int ty = threadIdx.y;
  Â  Â  Â int diagonalId;
  Â  Â  Â int x,y;

  Â  Â  Â x = bx*BLOCK_SIZE+tx;
  Â  Â  Â y = by*BLOCK_SIZE+ty;
  Â  Â  Â diagonalId = x+y;
  Â  Â  Â //dD is the fake array for testing, on the host side, the value of bx will be printed out.
  Â  Â  Â dD[y*targetSize+x]= bx;
       for(int i=0;i<(col+row-1);i++){
           .....
       }
  Â  Â .......
}[/code]

I expect that the print out will be:
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,
0,0,0,0,0,0,0,0,1,1,1,1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1,1,1,1,1,2,
2,2,2,2,2,2,2,0,1,1,1,1,1,1,1,1,2,
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,

Does anyone have ideas why this happens?

Appreciate!!
Hi all,



I need a 2D array in my program. But I found that the index of it is wrong when I print them out. Here is an example.



#define BLOCK_SIZE 8




host:

  Â  Â  Â // distance is a 2D int array with size of col*row, where col=row=17. 

  Â  Â  Â size_t pitchDistance;

  Â  Â  Â CUDA_SAFE_CALL(cudaMallocPitch((void**)&gDistance,&pitchDistance,sizeof(int)*col,row));

  Â  Â  Â CUDA_SAFE_CALL(cudaMemcpy2D(gDistance,pitchDistance,distance,sizeof(int)*col,sizeof(int)*col,row,cudaMemcpyHostToDevice));

.......

  Â  Â  Â /*

  Â  Â  Â  I also define a fake 1D array(dD) with size of col*row here. This array is for testing, values of block index or thread index will be saved, depends on demands. The result of the array will be printed out after the kernel is executed.  

  Â  Â  */

  Â  Â  Â dim3 dimBlock(BLOCK_SIZE,BLOCK_SIZE,1);

  Â  Â  Â dim3 dimGrid((col/dimBlock.x)+(!(col%dimBlock.x)?0:1),(row/dimBlock.y)+(!(row%dimBlock.y)?0:1),1);

  Â  Â  Â myKernel<<<dimGrid,dimBlock>>>(...,row,col,gDistance,pitchDistance/sizeof(int),...);




device:

__global__ void myKernel(...,int col,int row,int *distance,int pitchDistance,......){

  Â  Â  Â int bx = blockIdx.x;

  Â  Â  Â int by = blockIdx.y;

  Â  Â  Â int tx = threadIdx.x;

  Â  Â  Â int ty = threadIdx.y;

  Â  Â  Â int diagonalId;

  Â  Â  Â int x,y;



  Â  Â  Â x = bx*BLOCK_SIZE+tx;

  Â  Â  Â y = by*BLOCK_SIZE+ty;

  Â  Â  Â diagonalId = x+y;

  Â  Â  Â //dD is the fake array for testing, on the host side, the value of bx will be printed out.

  Â  Â  Â dD[y*targetSize+x]= bx;

       for(int i=0;i<(col+row-1);i++){

           .....

       }

  Â  Â .......

}




I expect that the print out will be:

0,0,0,0,0,0,0,0,1,1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1,1,1,1,1,1,1,2,

2,2,2,2,2,2,2,0,1,1,1,1,1,1,1,1,2,

2,2,2,2,2,2,2,0,1,1,1,1,1,1,1,1,2,

2,2,2,2,2,2,2,0,1,1,1,1,1,1,1,1,2,

0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,



Does anyone have ideas why this happens?



Appreciate!!

#1
Posted 04/16/2008 06:52 PM   
Scroll To Top