我陷入了一件非常简单的事情,我需要一个意见。我在 CUDA 中有一个非常简单的内核,可以在两个数组之间复制元素(我想这样做是有原因的)和
__global__
void kernelExample( float* A, float* B, float* C, int rows, int cols )
{
int r = blockIdx.y * blockDim.y + threadIdx.y; // vertical dim in block
int c = blockIdx.x * blockDim.x + threadIdx.x; // horizontal dim in block
if ( r < rows && c < cols) {
// row-major order
C[ c + r*cols ] = A[ c + r*cols ];
}
//__syncthreads();
}
我正在接受不满意的结果。请问有什么建议吗?
内核是这样调用的:
int numElements = rows * cols;
int threadsPerBlock = 256;
int blocksPerGrid = ceil( (double) numElements / threadsPerBlock);
kernelExample<<<blocksPerGrid , threadsPerBlock >>>( d_A, d_B, d_C, rows, cols );
更新(在 Eric 的帮助下):
int numElements = rows * cols;
int threadsPerBlock = 32; //talonmies comment
int blocksPerGrid = ceil( (double) numElements / threadsPerBlock);
dim3 dimBlock( threadsPerBlock,threadsPerBlock );
dim3 dimGrid( blocksPerGrid,blocksPerGrid );
kernelExample<<<dimBlock, dimBlock>>>( d_A, d_B, d_C, rows, cols );
例如具有矩阵 A
A =[
0 1
2 1
0 2
0 0
2 0
0 1
2 1
2 2
2 2
0 0
2 1
2 2
3 1
2 2
2 2
]
返回的矩阵 C 是
C = [
0 1
2 1
0 2
0 0
2 0
0 1
2 1
2 2
2 2
0 0
2 1
2 2
3 1
2 2
2 2
]