并行计算02-《cuda by examples》代码配置及julia例子
2017-11-19 13:19
531 查看
摘要:回忆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 maintained 1> demo_julia.cu 1> 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 1000
struct 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 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/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();
}
注意,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】
相关文章推荐
- CUDA By Examples 2 - 并行计算向量相加
- CUDA_by_examples示例julia成功运行环境配置过程(vs2015+CUDA8.0)
- CUDA BY EXAMPLES第4章代码错误解决方法
- cuda的julia集运行错误解决办法- 《cuda by examples》第四章例子
- CUDA By Examples 3 - 绘制Julia Set
- 【并行计算-CUDA开发】Windows下opencl环境配置
- 【CUDA并行编程之五】计算向量的欧式距离
- 【并行计算与CUDA开发】英伟达硬件加速解码器在 FFMPEG 中的使用
- 【计算机视觉】【并行计算与CUDA开发】GPU硬解码---DXVA
- NVIDIA GPU计算配置:在Archlinux上安装cuda OpenCL
- cuda并行计算的几种模式
- 【并行计算-CUDA开发】OpenACC与OpenHMPP
- windows 系统在vs2010 中配置libxml2,及读取xml代码例子
- CUDA并行计算框架(三)应用前景。相比价微软的并行计算框架
- struts简单例子的分析(含代码注释和配置文件说明)
- CUDA By Examples 6 - 使用常量内存 Constant Memory
- 利用VS的代码优化和openmp并行计算提高程序运行速度
- 【并行计算-CUDA开发】CUDA并行存储模型
- 高性能计算汇总-Mpi,openMpi,tbb,CUDA,OpenCL等并行计算或cpu,gpu混合计算技术
- Akka学习笔记02--PI值计算,采用多核处理器并行计算