我正在探索 cuda,但似乎输出的打印量超出了预期
__global__ void gpu_Floyd(int *result, int N, int k)
{
int j;
int Row = blockIdx.y * blockDim.y + threadIdx.y;
int Col;
if(Row < N)
{
#if __CUDA_ARCH__>=200
printf("row value: %d, blkId : %d, blkDim : %d, thread : %d \n", Row, blockIdx.y, blockDim.y, threadIdx.y);
#endif
}
}
//CUDA Portion
int Grid_Dim_x = 1, Grid_Dim_y = 1;
int Block_Dim_x = 2, Block_Dim_y = 2;
//int noThreads_x, noThreads_y;
int *result = (int*)malloc(sizeof(int)*N*N);
int *d_result;
memcpy(result, mat, sizeof(int)*N*N);
dim3 Grid(Grid_Dim_x, Grid_Dim_y);
dim3 Block(Block_Dim_x, Block_Dim_y, 1);
cudaMalloc((void **)&d_result, N*N);
int k;
cudaMemcpy(d_result, result, N * N, cudaMemcpyHostToDevice);
for(k = 0; k < 2; k++)
{
gpu_Floyd<<<Grid, Block>>>(d_result, N, k);
}
cudaMemcpy(result, d_result, N*N, cudaMemcpyDeviceToHost);
输出:
row value: 0, blkId : 0, blkDim : 2, thread : 0
row value: 0, blkId : 0, blkDim : 2, thread : 0
row value: 1, blkId : 0, blkDim : 2, thread : 1
row value: 1, blkId : 0, blkDim : 2, thread : 1
row value: 0, blkId : 0, blkDim : 2, thread : 0
row value: 0, blkId : 0, blkDim : 2, thread : 0
row value: 1, blkId : 0, blkDim : 2, thread : 1
row value: 1, blkId : 0, blkDim : 2, thread : 1
输出不应该只有 2 行而不是 8 行,因为 for 循环只迭代两次?块尺寸和网格尺寸如何影响我的输出。
我希望最终输出仅为
row value: 0, blkId : 0, blkDim : 2, thread : 0
row value: 1, blkId : 0, blkDim : 2, thread : 1