0

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];
    }
}
4

1 回答 1

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 矩阵上对其进行了测试,让我知道你的想法。

于 2013-04-09T03:50:14.843 回答