Julia on CPU
2013-11-14 03:51
218 查看
最近开始学习CUDA,感触良多。看书过程中发现Julia这个图形非常漂亮,照着书自己敲了一遍,发现其中运用了许多早已忘记的数学知识,于是赶紧恶补。
最终效果图:
/*
=========================编译环境=========================
系统: Win7 sp1 32位
CPU: AMD 黑盒5000+ oc到2.7GHz
内存: DDR2 800
显卡: ASUS GTX550Ti
环境: CUDA 5.5 + VisualStudio 2012 Ultimate update3
=========================================================
*/
以下为CPU执行的代码:
最终效果图:
/*
=========================编译环境=========================
系统: Win7 sp1 32位
CPU: AMD 黑盒5000+ oc到2.7GHz
内存: DDR2 800
显卡: ASUS GTX550Ti
环境: CUDA 5.5 + VisualStudio 2012 Ultimate update3
=========================================================
*/
以下为CPU执行的代码:
#include "common\cpu_bitmap.h" #include "common\cpu_anim.h" #define DIM 1000 struct cuComplex { float r; float i; cuComplex(float a, float b) : r(a), i(b) { } // 复数乘法 // z1 = (r + i) z2 = (a.r + a.i) // z1 * z2 = (r * a.r - i * a.i) + (r * a.i + i * a.r); cuComplex operator* (const cuComplex &a) { return cuComplex(r * a.r - i * a.i, i * a.r + r * a.i); } // 复数加法 // z1 = (r + i) z2 = (a.r + a.i) // z1 + z2 = (r + a.r) + (i + a.i) cuComplex operator+ (const cuComplex &a) { return cuComplex(r + a.r, i + a.i); } // 复数的模 // 模∣z∣=√(a^2+b^2) (1)∣z∣≧0 (2)复数模的平方等于这个复数与它的共轭复数的积。 float magnitude2() { return r * r + i * 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 (int 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); // 每个颜色用4个字节表示,并给每个字节赋值 ptr[offset * 4 + 0] = 255 * juliaValue; // 确定图像的颜色的Red分量 ptr[offset * 4 + 1] = 0; // 确定图像的颜色的Green分量 ptr[offset * 4 + 2] = 0; // 确定图像的颜色的Blue分量 ptr[offset * 4 + 3] = 255; } } } int main() { CPUBitmap bitmap(DIM, DIM); unsigned char *ptr = bitmap.get_ptr(); kernel(ptr); bitmap.display_and_exit(); }注:需要在项目属性中指定lib文件夹
相关文章推荐
- Find the First Common Ancestor
- python环境搭建和开发工具的配置【转】
- 在C语言环境下使用google protobuf
- sqlmap注入常见用法一条龙 【转】
- Java Memory Basic
- Django Meta小节(二) 之ModelForm
- jquery总结
- java Hotspot 内存管理白皮书(中文翻译)
- Cantor展开式
- Ubuntu 下配置 Eclipse + Tomcat7 开发环境
- 在C语言环境下使用google protobuf
- 工作三个月总结2
- hook resize
- 《高可用MySQL——构建健壮的数据中心》读书笔记之二
- VS2110。VC++编译错误"error LNK2005: 已经在 XXX.obj 中定义的问题"
- Find Median With Two heaps
- keepalived
- 关于 C# 中接口的一些小结
- LeetCode 之 Minimum Depth of Binary Tree
- 工作三个月总结1