以下代码不起作用。我的期望是在调用内核函数add()之后所有y [i]都有3。但是如果N> =(1 <24) - 255,则所有y [i]都是2(好像内核函数add()没有运行)。
#include <iostream>
__global__ void add(int n, int *x, int *y) {
int index = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = index; i < n; i += stride) y[i] = x[i] + y[i];
}
int main() {
int *x, *y, N = (1 << 24) - 255; // 255 wrong / 256 ok
cudaMallocManaged(&x, N * sizeof(int));
cudaMallocManaged(&y, N * sizeof(int));
for (int i = 0; i < N; ++i) {x[i] = 1; y[i] = 2;}
int sz = 256;
dim3 blockDim(sz,1,1);
dim3 gridDim((N+sz-1)/sz,1,1);
add<<<gridDim, blockDim>>>(N, x, y);
cudaDeviceSynchronize();
for (int i = 0; i < N; ++i) if (y[i]!=3) std::cout << "error" << std::endl;
cudaFree(x);
cudaFree(y);
return 0;
}
GPU是GTX1080Ti,具有以下限制:
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
机器是X86_64 Linux Ubuntu 16.04。我在这里做错了吗?请帮忙。
答案 0 :(得分:1)
编译时我没有指定-arch =。所以我最终使用-arch = sm_20,这是默认值。我使用了-arch = sm_60,现在它的工作原理是网格大小的x维度为2147483647,用于计算能力3或更高。
http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#compute-capabilities