在this question之后参考官方指南中的shared memory example,我试图构建热方程矩阵,这就像我制作的这张糟糕绘制的图像一样
这是我迄今为止所做的,最小的例子
#define N 32
#define BLOCK_SIZE 16
#define NUM_BLOCKS ((N + BLOCK_SIZE - 1)/ BLOCK_SIZE)
__global__ void heat_matrix(int* A)
{
const unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
__shared__ int temp_sm_A[N*N];
int* temp_A = &temp_sm_A[0]; memset(temp_A, 0, N*N*sizeof(int));
if (tid < N) //(*)
{
#pragma unroll
for (unsigned int m = 0; m < NUM_BLOCKS; ++m)
{
#pragma unroll
for (unsigned int e = 0; e < BLOCK_SIZE ; ++e)
{
if ( (tid == 0 && e == 0) || (tid == (N-1) && e == (BLOCK_SIZE-1) ) )
{
temp_A[tid + (e + BLOCK_SIZE * m) * N] = -2;
temp_A[tid + (e + BLOCK_SIZE * m) * N + ( tid==0 ? 1 : -1 )] = 1;
}
if ( tid == e )
{
temp_A[tid + (e + BLOCK_SIZE * m) * N - 1] = 1;
//printf("temp_A[%d] = 1;\n", (tid + (e + BLOCK_SIZE * m) * N -1));
temp_A[tid + (e + BLOCK_SIZE * m) * N] = -2;
//printf("temp_A[%d] = -2;\n", (tid + (e + BLOCK_SIZE * m) * N));
temp_A[tid + (e + BLOCK_SIZE * m) * N + 1] = 1;
//printf("temp_A[%d] = 1;\n", (tid + (e + BLOCK_SIZE * m) * N +1));
}
}
}
__syncthreads(); //(**)
memcpy(A, temp_A, N*N*sizeof(int));
}
}
int main(){
int* h_A = (int*)malloc(N*N*sizeof(int)); memset(h_A, 0, N*N*sizeof(int));
int* d_A;
checkCudaErrors(cudaMalloc((void**)&d_A, N*N*sizeof(int)));
checkCudaErrors(cudaMemcpy(d_A, h_A, N*N*sizeof(int), cudaMemcpyHostToDevice));
dim3 dim_grid((N/2 + BLOCK_SIZE -1)/ BLOCK_SIZE);
dim3 dim_block(BLOCK_SIZE);
heat_matrix <<< dim_grid, dim_block >>> (d_A);
checkCudaErrors(cudaMemcpy(h_A, d_A, N*N*sizeof(int), cudaMemcpyDeviceToHost));
...
}
代码被安排为适合大N(大于32)。我利用了区块划分。执行nvcc
时产生
CUDA error at matrix.cu:102 code=77(cudaErrorIllegalAddress) "cudaMemcpy(h_A, d_A, N*N*sizeof(int), cudaMemcpyDeviceToHost)"
cuda-memcheck
只提供一个错误(实际上还有另一个错误,但它来自cudasuccess=checkCudaErrors(cudaDeviceReset()); ...
)
========= CUDA-MEMCHECK
========= Invalid __shared__ write of size 4
========= at 0x00000cd0 in heat_matrix(int*)
========= by thread (0,0,0) in block (0,0,0)
========= Address 0xfffffffc is out of bounds
...
我无法在代码中看到我在哪里做错了。第一个块中的线程0
如何引发非法访问?甚至还有特定的if
案例来处理它,并且没有报告发生错误的代码行。
此外,我的代码是否有更有效的方式来处理所有这些if
?当然有,但我找不到更好的并行表达式来将案例分成第二个for
。
另一方面,(*)
对我来说似乎是不必要的;如果我想跟随其他GPU函数调用,则需要(**)
。我是对的吗?
答案 0 :(得分:2)
查看此行:
temp_A[tid + (e + BLOCK_SIZE * m) * N - 1] = 1;
对于在第一次迭代期间tid
等于零的线程,tid + (e + BLOCK_SIZE * m) * N - 1
求值为-1的索引。这正是cuda-memcheck输出所抱怨的(由于下溢导致地址崩溃)。
稍后将对行
进行类似的越界访问 temp_A[tid + (e + BLOCK_SIZE * m) * N + 1] = 1;
当tid
,e
和m
都假定其最大值时。
您有多个线程写入同一内存位置。每个内部循环迭代,每个线程应该写入一个数组元素。没有必要写出相邻元素,因为它们已经被自己的线程所覆盖。
初始化memset()
和主循环内的商店之间存在竞争条件。在syncthreads()
。
memset()
对memset()
和memcpy()
的调用将导致每个线程执行完整设置/复制,执行操作N
次而不是仅执行一次。
处理这个问题的常用方法是明确地写出操作,在块的线程之间划分工作
但是......
首先在共享内存中创建矩阵然后将其复制到全局内存中没有任何好处。直接写入全局内存中的A
消除了对memset()
,memcpy()
和syncthreads()
的全部需求。
使用仅16个线程的块大小会留下一半未使用的资源,因为线程块是以32个线程(warp)为单位分配的。
您可能需要重新阅读“CUDA C编程指南”中有关Thread Hierarchy的部分。
答案 1 :(得分:1)
在你的内核中,temp_A
是指向共享内存数组开头的本地指针。考虑到:
N = 32;
BLOCK_SIZE = 16;
m(0,1);
e(0,BLOCK_SIZE)
像temp_A[tid + (e + BLOCK_SIZE * m) * N]
这样的访问很容易超出1024个元素的长数组。