并行计算02-《cuda by examples》代码配置及julia例子

来源:互联网 发布:网络接口芯片 编辑:程序博客网 时间:2024/04/28 18:11

这里写图片描述

摘要:回忆VS的使用,把《cuda by examples》书的代码配置一下,使julian在CPU及GPU都跑起来, 对比GPU与CPU的编程的区别。

VS很久没用,用起有些不熟悉了,今天复习了一下VS的使用,练习一下把一些例子使用起来,记录一下。感觉一个技术如果不用,一段时间就可以忘记得一点印象都没有,这时就得回忆了,一边做一边回忆。

1. h文件

包的存放问题,本想创建一个文件夹来存放头文件的,把所有的头文件都copy过来了。可是后来发现这个只是把这个文件只是把它的路径引用来的,那个显示像文件夹的东西是一个过滤器来的。后在在目录下创建相关目录,把头文件放入去就可以。
这里写图片描述
这样:
这里写图片描述
这个是相对于项目demo_cuda的目录,引用时像例子所给的那样就可以用了。

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

头文件配置好之后,编译就不会有问题了:

1>CUDACOMPILE : nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).1>CUDACOMPILE : nvcc warning : nvcc support for Microsoft Visual Studio 2010 and earlier has been deprecated and is no longer being maintained1>  demo_julia.cu1>  support for Microsoft Visual Studio 2010 has been deprecated!1>1>生成成功。1>1>已用时间 00:00:02.47========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========

2. lib配置

当头文件解决了,到lib了,如果不配置会出现如下问题:
1>LINK : fatal error LNK1104: 无法打开文件“glut32.lib”
配置附加库目录
这里写图片描述
配置附加依赖项
这里写图片描述
注意了,如果glut32.lib少了后缀,再编译一下,再生成,会出现问题2:
1>LINK : fatal error LNK1104: 无法打开文件“glut32.obj”

问题3:1>LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
这里写图片描述

3. julia例子

3.1 CPU

#include "../common/book.h"#include "../common/cpu_bitmap.h"#define DIM 1000struct cuComplex {    float   r;    float   i;    cuComplex( float a, float b ) : r(a), i(b)  {}    float magnitude2( void ) { return r * r + i * i; }    cuComplex operator*(const cuComplex& a) {        return cuComplex(r*a.r - i*a.i, i*a.r + r*a.i);    }    cuComplex operator+(const cuComplex& a) {        return cuComplex(r+a.r, i+a.i);    }};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;}void kernel( unsigned char *ptr ){    for (int y=0; y<DIM; y++) {        for (int x=0; x<DIM; x++) {            int offset = x + y * DIM;            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;        }    } }int main( void ) {    CPUBitmap bitmap( DIM, DIM );    unsigned char *ptr = bitmap.get_ptr();    kernel( ptr );    bitmap.display_and_exit();}

3.2 GPU

#include "cuda_runtime.h"#include "device_launch_parameters.h"#include <stdio.h>#include "../common/book.h"#include "../common/cpu_bitmap.h"#define DIM 1000struct 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 routinestruct 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();}

注意,GPU的代码下载包里的代码有些问题,要在cuComplex构造函数前加上device ,上面已经加上了。详细修改参见《CUDA BY EXAMPLES第4章代码错误解决方法》;
http://blog.csdn.net/chevroletss/article/details/48130953
对julia这个东西是什么,参见《再谈Julia集与Mandelbrot集》
http://www.matrix67.com/blog/archives/4570

4. 运行结果

这里写图片描述

【作者:happyprince, http://blog.csdn.net/ld326/article/details/78573914】

原创粉丝点击