《GPU高性能编程CUDA实战中文》中第四章的julia实验

时间:2023-03-09 08:15:35
《GPU高性能编程CUDA实战中文》中第四章的julia实验

在整个过程中出现了各种问题,我先将我调试好的真个项目打包,提供下载

 /*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation.
* Any use, reproduction, disclosure, or distribution of this software
* and related documentation without an express license agreement from
* NVIDIA Corporation is strictly prohibited.
*
* Please refer to the applicable NVIDIA end user license agreement (EULA)
* associated with this source code for terms and conditions that govern
* your use of this NVIDIA software.
*
*/ #include <GL\glut.h>
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include "../common/book.h"
#include "../common/cpu_bitmap.h" #define DIM 1000 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 / - x) / (DIM / );
float jy = scale * (float)(DIM / - y) / (DIM / ); cuComplex c(-0.8, 0.156);
cuComplex a(jx, jy); int i = ;
for (i = ; i<; i++) {
a = a * a + c;
if (a.magnitude2() > )
return ;
} return ;
} __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 * + ] = * juliaValue;
ptr[offset * + ] = ;
ptr[offset * + ] = ;
ptr[offset * + ] = ;
} // 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, >> >(dev_bitmap); HANDLE_ERROR(cudaMemcpy(bitmap.get_ptr(), dev_bitmap,
bitmap.image_size(),
cudaMemcpyDeviceToHost)); HANDLE_ERROR(cudaFree(dev_bitmap)); bitmap.display_and_exit();
}

期间出现的问题:

问题一

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__ function("cuComplex::operator +") is not allowed

这个原因是在原著中提供的代码有问题,原著中结构体中的代码为

cuComplex(float a, float b) : r(a), i(b)  {}

将其修改如下即可:

 __device__ cuComplex(float a, float b) : r(a), i(b)  {}

问题二

error LNK2019: 无法解析的外部符号 ___glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用 1>GEARS.obj : error LNK2019: 无法解析的外部符号 ___gl

这个原因是我的OpenGL文件没有引对

#include <GL\glut.h>

其中glut.h文件要在下面的路径下

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL

如果GL文件夹不在,要手动创建,结构如下图所示:

《GPU高性能编程CUDA实战中文》中第四章的julia实验

注意:

为了运行示例代码,需要抽取可运行的部分,同时为了减少手动修改的麻烦,也要注意各各个文件目录的层次关系,我的截图如下:

《GPU高性能编程CUDA实战中文》中第四章的julia实验

千辛万苦走下来就为了下面这张图:

《GPU高性能编程CUDA实战中文》中第四章的julia实验

确实挺好看的。赞一个!