Julia on CPU
来源:互联网 发布:大数据服务架构 编辑:程序博客网 时间:2024/04/21 00:04
最近开始学习CUDA,感触良多。看书过程中发现Julia这个图形非常漂亮,照着书自己敲了一遍,发现其中运用了许多早已忘记的数学知识,于是赶紧恶补。
最终效果图:
=========================编译环境=========================
系统: 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 1000struct 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文件夹
- Julia on CPU
- Julia On GPU
- Julia曲线绘制-——CPU版本与GPU版本对比
- Julia set
- julia笔记
- Julia: readdlm
- Julia: rand
- Julia: 奇技淫巧......
- Julia :where
- NarrowCast license based on CPU clock speed
- The articel on the old cpu
- Linux, BUG: spinlock recursion on CPU
- spark on yarn的cpu使用
- Switched to NOHz mode on CPU
- Andorid 中文API:Keep the CPU On
- Generate high cpuload on specified cpu core
- [ATF]ARMv8 cpu warm boot (hotplug on)
- Spark On YARN内存和CPU分配
- 在C语言环境下使用google protobuf
- 第二次接触Lua
- Cantor展开式
- jquery总结
- Django Meta小节(二) 之ModelForm
- Julia on CPU
- 利刃出鞘:站长不得不知的九种工具
- Find the First Common Ancestor
- Core Impact 7.5 破解
- Mysql sending data 问题
- Leetcode: Insertion Sort List
- 倒序输出数字,计算一年之中的某一天是那一天
- LeetCode题解:Rotate List
- 需要使用Git branch switch 的情况&Git stash的使用时机