【发布时间】:2018-01-10 05:16:11
【问题描述】:
当我的数组大小大于 591 x 591 时,我在启动一个简单内核时遇到了问题。在大小为 591x591 时,数组返回时没有任何错误,但只要我启动网格尺寸为38x38 块,每个块有 16x16 个线程,内核无法启动并返回“未知错误”。
以下代码是我正在调用的内核以及我代码中对内核的调用:
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_device_runtime_api.h>
using namespace std;
#define BLOCKSIZE 16
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__,__LINE__);}
inline void gpuAssert(cudaError_t code, char *file, int line, bool abort = true)
{
if (code != cudaSuccess)
{
fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if(abort) exit(code);
}
}
__global__ void IdentityMatrixKernel(float* identity, int size)
{
int index_x = blockIdx.x * blockDim.x + threadIdx.x;
int index_y = blockIdx.y * blockDim.y + threadIdx.y;
// map the two 2D indices to a single linear, 1D index
int grid_width = gridDim.x * blockDim.x;
int index = index_y * grid_width + index_x;
// map the two 2D block indices to a single linear, 1D block index
//int result = blockIdx.y * gridDim.x + blockIdx.x;
if (index % (size+1))
{
identity[index] = 0;
}
else
{
identity[index] = 1;
}
void foo(float *aArray, int size)
{
float* d_I;
int size2 = size*size*sizeof(float);
gpuErrchk(cudaMalloc(&d_I,size2));
dim3 block_size;
block_size.x = BLOCKSIZE;
block_size.y = BLOCKSIZE;
dim3 grid_size;
grid_size.x = size1/ block_size.x + 1;
grid_size.y = size1/ block_size.y + 1;
IdentityMatrixKernel<<<grid_size,block_size>>>(d_I,size);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaMemcpy(aArray,d_I,size2,cudaMemcpyDeviceToHost));
cudaFree(d_I);
}
int main()
{
int size = 591;
float *aArray = (float*)malloc(size*size*sizeof(float));
foo(aArray,size);
return 0;
}
对于 size = 591 没有错误显示,输出大小为 591x591 的单位矩阵,但对于任何更大的大小,它会向控制台吐出一个“未知错误”。
【问题讨论】:
-
我猜这不是您正在运行的代码。有各种编译问题。请检查以确保您发布的代码能够真正编译,并解决任何问题,然后确保它实际演示了问题。然后用
cuda-memcheck运行你的代码,我想你会看到你的内核产生了很多错误(例如,越界访问 - 大小为 4 的无效全局写入等)。