非方矩阵CUDA的通用快速转置

时间:2012-07-17 10:48:23

标签: matrix cuda transpose

SDK提供了一个处理方阵转置的示例和策略,但是有一种在非方阵上执行转置的好方法吗?我现在有一个非常天真的实现如下,这可能很糟糕:

template<class S>
__global__ void transpose(S *Source, S *Destination, int SizeX, int SizeY) {
    int tid = threadIdx.x + blockIdx.x * blockDim.x;

    if (tid<SizeX*SizeY) {
        int X = tid % SizeX;
        int Y = tid / SizeX;

        //(x,y) => (y,x)

        int newId = (SizeY*X) + Y;
        Destination[newId] = Source[tid];
    }
}

1 个答案:

答案 0 :(得分:1)

这里我的想法是只用必要的线程/块转换矩阵的正方形部分(每个线程交换方形子矩阵的两个条目),然后遍历并转置其余的条目。

__global__ void kernelTranspuesta(float *a, float *c, int m, int n) {
   int i = threadIdx.x + blockIdx.x*blockDim.x; 
   int j = threadIdx.y + blockIdx.y*blockDim.y; 
   int smallest = M < N ? M : N;

   while( j < smallest ){
      i = threadIdx.x + blockIdx.x*blockDim.x;
      while( i < j ){
         c[i*m+j] = a[j*n+i];
         c[j*m+i] = a[i*n+j];
         i+= blockDim.x*gridDim.x; 
      }
      if(i == j)
         c[j*m+i] = a[i*n+j];

     j+= blockDim.y*gridDim.y; 
   }  

   if( M > N ) {
      i = threadIdx.x + blockIdx.x*blockDim.x + N;
      j = threadIdx.y + blockIdx.y*blockDim.y; 

      while( i < M ){
         j = threadIdx.y + blockIdx.y*blockDim.y; 
         while( j < N){
           c[j*m+i] = a[i*n+j];
           j+= blockDim.y*gridDim.y; 
         }
         i+= blockDim.x*gridDim.x; 
      } 
   }else{
      i = threadIdx.x + blockIdx.x*blockDim.x;
      j = threadIdx.y + blockIdx.y*blockDim.y + M; 

     while( i < M ){
         j = threadIdx.y + blockIdx.y*blockDim.y + M; 
         while( j < N){
           c[j*m+i] = a[i*n+j];
           j+= blockDim.y*gridDim.y; 
         }
         i+= blockDim.x*gridDim.x; 
      } 
   }

}

内核调用是

   dim3 hilos(16,16);   // hilos(blockDim.x, blockDim.y) 
   dim3 bloques(8,8); // bloques(gridDim.x, gridDim.y)

   kernelTranspuesta<<<bloques, hilos>>>(aD, cD, m, n); 

我在512x256和256x512矩阵上测试过,让我知道你的想法。