Julia set code CUDA by example 常见问题

来源:互联网 发布:蚌埠seo 编辑:程序博客网 时间:2024/04/28 14:24

  在学习CUDA时,遇到的第一个绘图的程序就是Julia set , 参考书本的代码难免会遇到问题,在一番查找和排错之后,可以得到完美的图片。

  问题1:  无法打开#include "./common/book.h"
                                  #include "./common/cpu_bitmap.h"

         解决方案: 由于自己配置CUDA的时候,没有安装对应的库文件,造成此类问题,所以需要下载相应的库文件并放到CUDA的安装目录下,下载路径  http://pan.baidu.com/s/1gfC3zV1,  打开common 压缩包,将里面的所有数据copy到 你的CUDA安装的目录下C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include, 这样就能打开库函数了。

 

问题2: 

calling a host function("cuComplex::cuComplex") from a __device__/__global__ function("julia") is not allowed
calling a host function("cuComplex::cuComplex") from a __device__/__global__ function("julia") is not allowed
calling a host function("cuComplex::cuComplex") from a __device__/__global__ function("cuComplex::operator *") is not allowed
calling a host function("cuComplex::cuComplex") from a __device__/__global__ 

报错,这是由于代码自身问题导致,修改如下:

   #define DIM 500


struct cuComplex {
float   r;
float   i;
__device__ cuComplex(float a, float b) : r(a), i(b)  {} ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

注意感叹号的部分,前面多加了一个 __device__ 空格  即可


问题3:  编译通过,但一运行就提示,计算机丢失 glut64.dll文件 ,这也是由于CUDA配置是缺少相应的文件导致的, 下载文件 http://pan.baidu.com/s/1skZ9rFB 

              将里面的dll 文件复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin 这个里面,64位对应64位的glut64.dll ,即可完成




整体代码如下:

#include "./common/book.h"
#include "./common/cpu_bitmap.h"


#define DIM 500


struct cuComplex {
float   r;
float   i;
__device__ cuComplex(float a, float b) : r(a), i(b)  {}
__device__ float magnitude2(void) {
return r * r + i * i;
}
__device__ cuComplex operator*(const cuComplex& a) {
return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);
}
__device__ cuComplex operator+(const cuComplex& a) {
return cuComplex(r + a.r, i + a.i);
}
};


__device__ int julia(int x, int y) {
const float scale = 1.5;
float jx = scale * (float)(DIM / 2 - x) / (DIM / 2);
float jy = scale * (float)(DIM / 2 - y) / (DIM / 2);


cuComplex c(-0.8, 0.156);                 //画面修正
cuComplex a(jx, jy);


int i = 0;
for (i = 0; i<200; i++) {                       //改变亮度以及对比度的参量 越小越亮
a = a * a + c;
if (a.magnitude2() > 1000)
return 0;
}


return 1;
}


__global__ void kernel(unsigned char *ptr) {
// map from blockIdx to pixel position
int x = blockIdx.x;
int y = blockIdx.y;
int offset = x + y * gridDim.x;


// now calculate the value at that position
int juliaValue = julia(x, y);
ptr[offset * 4 + 0] = 255 * juliaValue;
ptr[offset * 4 + 1] = 0;
ptr[offset * 4 + 2] = 0;
ptr[offset * 4 + 3] = 255;
}


// globals needed by the update routine
struct DataBlock {
unsigned char   *dev_bitmap;
};


int main(void) {
DataBlock   data;
CPUBitmap bitmap(DIM, DIM, &data);
unsigned char    *dev_bitmap;


HANDLE_ERROR(cudaMalloc((void**)&dev_bitmap, bitmap.image_size()));
data.dev_bitmap = dev_bitmap;


dim3    grid(DIM, DIM);
kernel << <grid, 1 >> >(dev_bitmap);


HANDLE_ERROR(cudaMemcpy(bitmap.get_ptr(), dev_bitmap,
bitmap.image_size(),
cudaMemcpyDeviceToHost));


HANDLE_ERROR(cudaFree(dev_bitmap));


bitmap.display_and_exit();

}

修改参数,图像也会变得有趣。




0 0
原创粉丝点击