julia的cpu和gpu实现版本
- Julia集是一种在复平面上非发散点形成的分形点的集合。具有非常美观的几何特征。
例如,对于复变函数的一个通项公式
下面的代码包括cpu和gpu版本的Julia集算法
其中用到了一维索引和二维索引的转换关系
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include <stdio.h> #include "common/book.h" #include "common/cpu_bitmap.h" const int DIM_WIDTH = 800; const int DIM_HEIGHT = 800; struct cuComplex{ float r, 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); } }; __device__ struct cuComplexGPU { float r, i; __device__ cuComplexGPU(float a, float b) :r(a), i(b) {} __device__ float magnitude2(void) { return r * r + i * i; } __device__ cuComplexGPU operator*(const cuComplexGPU& a) { return cuComplexGPU(r * a.r - i * a.i, i * a.r + r * a.i); } __device__ cuComplexGPU operator+(const cuComplexGPU& a) { return cuComplexGPU(r + a.r, i + a.i); } }; int juliaCPU(int x, int y) { const float scale = 1.5; float jx = scale*(float)(DIM_WIDTH / 2 - x) / (DIM_WIDTH / 2); float jy = scale*(float)(DIM_HEIGHT / 2 - y) / (DIM_HEIGHT / 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; } __device__ int juliaGPU(int x, int y) { const float scale = 1.5; float jx = scale * (float)(DIM_WIDTH / 2 - x) / (DIM_WIDTH / 2); float jy = scale * (float)(DIM_HEIGHT / 2 - y) / (DIM_HEIGHT / 2); cuComplexGPU c(-0.8, 0.156); cuComplexGPU 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 julia(unsigned char *ptr) { for (int y = 0; y < DIM_HEIGHT; y++) { for (int x = 0; x < DIM_WIDTH; x++) { int offset = x + y * DIM_HEIGHT; int juliaValue = juliaCPU(x, y); ptr[offset * 4 + 0] = 255 * juliaValue; ptr[offset * 4 + 1] = 0; ptr[offset * 4 + 2] = 0; ptr[offset * 4 + 3] = 255; //printf("%d ", juliaValue); } } } __global__ void juliaKernel(unsigned char* ptr) { //map threadIdx/BlockIdx to cell pos int x = blockIdx.x; int y = blockIdx.y; int offset = x + y * gridDim.x; //calcualte the value on the cell pos int juliaValue = juliaGPU(x, y); ptr[offset * 4 + 0] = 255 * juliaValue; ptr[offset * 4 + 1] = 0; ptr[offset * 4 + 2] = 0; ptr[offset * 4 + 3] = 255; //printf("%d ", juliaValue); } void testJuliaSet() { CPUBitmap bitmap(DIM_WIDTH, DIM_HEIGHT); unsigned char* ptr = bitmap.get_ptr(); julia(ptr); bitmap.display_and_exit(); } void testGPUJuliaSet() { CPUBitmap bitmap(DIM_WIDTH, DIM_HEIGHT); unsigned char* dev_bitmap; HANDLE_ERROR(cudaMalloc((void**)&dev_bitmap, bitmap.image_size())); dim3 grid(DIM_WIDTH, DIM_HEIGHT); //configure the block grid juliaKernel<<<grid, 1>>>(dev_bitmap); HANDLE_ERROR(cudaMemcpy(bitmap.get_ptr(), dev_bitmap, bitmap.image_size(), cudaMemcpyDeviceToHost)); bitmap.display_and_exit(); HANDLE_ERROR(cudaFree(dev_bitmap)); } int main() { //testJuliaSet(); testGPUJuliaSet(); return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」