使用 cudaMemcpy() 时收到很多 0

A lot of 0's received when using cudaMemcpy()

我刚开始学习 CUDA,我想用随机数填充一个数组(表示为一维数组的二维数组)。我关注了另一篇文章以生成随机数,但我不知道数字的生成或从设备或其他任何东西恢复的内存是否存在问题。问题是,虽然我试图用正在处理它的线程的 ID 填充数组的任何单元格,以便在复制到主机内存后查看结果,但我收到一个填充有 [=11 的数组=] 在使用 cudaMemcpy().

恢复数据后的任何位置

我在 Visual Studio 2013 年编程,使用 cuda 7.5,i5 2500k 作为我的处理器和 960 GTX 显卡。

这是我尝试填充它的主要方法。我也会更新 cuRand 初始化。如果您还需要看其他内容,请告诉我。

__global__ void setup_cuRand(curandState * state, unsigned long seed)
{
    int id = threadIdx.x;
    curand_init(seed, id, 0, &state[id]);
}

__global__ void poblar(int * adn, curandState * state){

    curandState localState = state[threadIdx.x];
    int random = curand(&localState);
    adn[threadIdx.x] = random;
    // It doesn't mind if i use the following instruction, the result is a lot of 0's
    //adn[threadIdx.x] = threadIdx.x;

}

int main()
{

    const int adnLength = NUMCROMOSOMAS * SIZECROMOSOMAS; // 256 * 128 (32.768)
    const size_t adnSize = adnLength * sizeof(int);
    int adnCPU[adnLength];
    int * adnDevice;

    cudaError_t error = cudaSetDevice(0);
    if (error != cudaSuccess) 
        exit(-EXIT_FAILURE);

    curandState * randState;
    error = cudaMalloc(&randState, adnLength * sizeof(curandState));
    if (error != cudaSuccess){
        cudaFree(randState);
        exit(-EXIT_FAILURE);
    }
    //Here is initialized cuRand
    setup_cuRand <<<1, adnLength >> > (randState, unsigned(time(NULL)));

    error = cudaMalloc((void **)&adnDevice, adnSize);

    if (error == cudaErrorMemoryAllocation){// cudaSuccess){
        cudaFree(adnDevice);
        cudaFree(randState);
        printf("\n error");
        exit(-EXIT_FAILURE);
    }


    poblar <<<1, adnLength >>> (adnDevice, randState);
    error = cudaMemcpy(adnCPU, adnDevice, adnSize, cudaMemcpyDeviceToHost);
    //After here, for any i, adnCPU[i] is 0 and i cannot figure what is wrong
    if (error == cudaSuccess){
        for (int i = 0; i < NUMCROMOSOMAS; i++){
            for (int j = 0; j < SIZECROMOSOMAS; j++){
                printf("%i,", adnCPU[(i*SIZECROMOSOMAS) + j]);
            }
            printf("\n");
        }
    }

    return 0;
} 

答案解决后编辑: 给出的答案有一个特殊性,您需要较少数量的线程(该数量的一半对我有用)以便使用 cuRand 正确播种随机数。出于某种原因,我可以完美地创建线程,但我无法为伪随机算法生成器播种。

在您的硬件上,每个块的最大线程数是 1024,因此,如果它大于 1024,您可能无法安排使用 adnLength 的调用。

您遇到的错误很可能是调用配置错误,它是由 cudaPeekAtLastError 编辑的 return,因为它发生在任何 GPU 工作之前,就在三重角括号调用之后.事实上,cudaMemcpy 可能不会 return 它,即使它 return 从以前的异步调用中出错。

可能出现的错误是cudaErrorLaunchOutOfResources.