2

在这段代码中,我使用 CUDA 在 gpu 上生成一维浮点数组。数字介于 0 和 1 之间。出于我的目的,我需要它们介于 -1 和 1 之间,所以我制作了简单的内核,将每个元素乘以 2,然后从中减去 1。但是,这里出了点问题。当我将原始数组打印到 .bmp 中时,我得到了这个http://i.imgur.com/IS5dvSq.png(典型的噪声模式)。但是当我尝试用我的内核修改那个数组时,我得到了空白的黑色图片http://imgur.com/cwTVPTG。该程序是可执行的,但在调试中我得到了这个:

Midpoint_CUDA_Alpha.exe 中 0x75f0c41f 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x003cfacc..

Midpoint_CUDA_Alpha.exe 中 0x75f0c41f 处的第一次机会异常:Microsoft C++ 异常:内存位置 0x003cfb08 处的 cudaError_enum。

Midpoint_CUDA_Alpha.exe 中 0x75f0c41f 处的第一次机会异常:Microsoft C++ 异常:[rethrow] 在内存位置 0x00000000..

我会很感激在这件事上的任何帮助甚至是一点提示。谢谢 !(已编辑)

#include <device_functions.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "stdafx.h"
#include "EasyBMP.h"
#include <curand.h> //curand.lib must be added in project propetties > linker > input
#include "device_launch_parameters.h"

float *heightMap_cpu;
float *randomArray_gpu;
int randCount = 0;
int rozmer = 513;

void createRandoms(int size){
    curandGenerator_t generator;
    cudaMalloc((void**)&randomArray_gpu, size*size*sizeof(float));
    curandCreateGenerator(&generator,CURAND_RNG_PSEUDO_XORWOW);
    curandSetPseudoRandomGeneratorSeed(generator,(int)time(NULL));
    curandGenerateUniform(generator,randomArray_gpu,size*size);
}

__global__ void polarizeRandoms(int size, float *randomArray_gpu){
    int index = threadIdx.x + blockDim.x * blockIdx.x;
    if(index<size*size){
        randomArray_gpu[index] = randomArray_gpu[index]*2.0f - 1.0f;
    }
}

//helper fucnction for getting address in 1D using 2D coords
int ad(int x,int y){
    return x*rozmer+y;
}

void printBmp(){
    BMP AnImage;
    AnImage.SetSize(rozmer,rozmer);
    AnImage.SetBitDepth(24);
    int i,j;
    for(i=0;i<=rozmer-1;i++){
        for(j=0;j<=rozmer-1;j++){
            AnImage(i,j)->Red = (int)((heightMap_cpu[ad(i,j)]*127)+128);
            AnImage(i,j)->Green = (int)((heightMap_cpu[ad(i,j)]*127)+128);
            AnImage(i,j)->Blue = (int)((heightMap_cpu[ad(i,j)]*127)+128);
            AnImage(i,j)->Alpha = 0;
        }
    }
    AnImage.WriteToFile("HeightMap.bmp");
}

int main(){
    createRandoms(rozmer);
    polarizeRandoms<<<((rozmer*rozmer)/1024)+1,1024>>>(rozmer,randomArray_gpu);
    heightMap_cpu = (float*)malloc((rozmer*rozmer)*sizeof(float));
    cudaMemcpy(heightMap_cpu,randomArray_gpu,rozmer*rozmer*sizeof(float),cudaMemcpyDeviceToHost);
    printBmp();

    //cleanup
    cudaFree(randomArray_gpu);
    free(heightMap_cpu);
    return 0;
}
4

1 回答 1

3

这是错误的:

cudaMalloc((void**)&randomArray_gpu, size*size*sizeof(float));

我们不使用cudaMalloc变量__device__。如果您进行正确的 cuda错误检查,我很确定该行会引发错误。

如果您真的想以__device__这种方式使用指针,则需要创建一个单独的普通指针cudaMalloc,然后使用以下命令将指针值复制到设备指针cudaMemcpyToSymbol

float *my_dev_pointer;
cudaMalloc((void**)&my_dev_pointer, size*size*sizeof(float));
cudaMemcpyToSymbol(randomArray_gpu, &my_dev_pointer, sizeof(float *));

每当您的 CUDA 程序出现问题时,您都应该进行适当的 cuda 错误检查。它可能会将您的注意力集中在问题上。

而且,是的,内核可以访问__device__变量,而无需将变量作为参数显式传递给内核。

编程指南涵盖了变量的正确使用以及__device__应该用于从主机访问它们的 api 函数。

于 2013-09-11T05:03:34.560 回答