2017-07-15 2 views
0

次のコードは機能しません。カーネル関数add()が呼び出された後、私の期待はすべてy [i]の3になります。しかし、N> =(1 < < 24) - 255の場合、すべてのy [i]は2です(カーネル関数add()が実行されなかったかのように)。CUDAストライド機能が動作しません。

#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です。私はここで何か間違っていますか?助けてください。

+1

[適切なCUDAエラーチェック](https://stackoverflow.com/questions/14038589/what-is-the-canonical-way-to-check-for-errors-using-the-cuda -runtime-api)は、問題に注意を集中するのに役立ちます –

+0

ありがとう!私はgpuErrchk(cudaPeekAtLastError())を追加しました。カーネル関数addの直後に呼び出します。その後、-arch = sm_60を指定せずにコンパイルすると、 "GPUassert:invalid argument test.cu 42"が返されました。 – eii0000

答えて

関連する問題