在这段代码中,我使用 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;
}