并行Pi的计算
来源:互联网 发布:理想国 英剧 知乎 编辑:程序博客网 时间:2024/05/17 08:47
主要考虑两种PI的计算方法 , 一种为积分法的用共享内存试的GPU实现, 另外一种为在面积估算MPI伪代码实现。
1.积分法求pi
// 计算PI // 算法:在[0,1]范围内积分 1/(1+x*x)// 做积分,把每一个block中的和放入sum对应的下标中__global__ void kernel1(float* sum, int num){int gid = blockIdx.x*blockDim.x+threadIdx.x;//线程索引float temp;extern float __shared__ pi[]; // 每一个block中的共享内存pi[threadIdx.x] = 0.0; // 初始化__syncthreads();// 计算积分 , 放入对应线程里while(gid < num){temp = (gid + 0.5f) / num;pi[threadIdx.x] += 4.0/(1 + temp*temp);gid += blockDim.x * gridDim.x}// 对每一个block归约求和for(int i=(blockDim.x>>1);i>0;i>>=1){ if(threadIdx.x<i){ pi[threadIdx.x] += pi[threadIdx.x+i]; } __syncthreads(); } if(threadIdx.x==0) sum[blockIdx.x]=s_pi[0]; }}// 对sum在执行一次归约求和__global__ void kernel2(float *sum,int num,float *pi){ int id=threadIdx.x; extern float __shared__ s_sum[]; s_sum[id] = sum[id]; __syncthreads(); for(int i=(blockDim.x>>1);i>0;i>>=1){ if(id<i) s_sum[id]+=s_sum[id+i]; __syncthreads();}if(id==0){ *d_pi=s_sum[0]/num;} }
/* 算法1.Inscribe a circle in a square2.Randomly generate points in the square3.Determine the number of points in the square that are also in the circleLet r be the number of points in the circle divided by the number of points in the square4.PI ~ 4*r */npoints = 10000circle_count = 0p = number of tasksnum = npoints/pfind out if I am MASTER or WORKER do j = 1,num generate 2 random numbers between 0 and 1 xcoordinate = random1 ycoordinate = random2 if (xcoordinate, ycoordinate) inside circle then circle_count = circle_count + 1end doif I am MASTER receive from WORKERS their circle_counts compute PI (use MASTER and WORKER calculations)else if I am WORKER send to MASTER circle_countendif
两种计算模式, 但其实这两种计算方法都比较适合在GPU上实现
详细的过程可以参考 : 风晨的CUDA入门 和 Introduction to Parallel Computing
0 0
- 并行Pi的计算
- OpenMP并行计算PI的值
- 并行计算mpi [ PI ]
- Pi的计算程序
- PI的计算公式
- 计算pi的方法
- 计算PI的方法
- 计算PI值到一亿位的算法
- 计算PI的外星人程序
- 外星人计算Pi的程序
- 计算PI值到一亿位的算法
- 与PI有关的计算
- 外星人计算Pi的程序
- 计算任意位数的Pi
- 计算任意位数的Pi
- 计算任意位数的Pi
- C++计算PI的值
- 文章标题 并行计算 -- OpenMP简介和计算实例 (计算pi)
- android学习笔记1-布局
- Linux编程---套接字
- IOS UITableView下拉刷新(转)
- 14、排序算法c语言描述---选择排序
- Qt Quick 组件与对象动态创建详解
- 并行Pi的计算
- you're mac makeup training set
- android学习笔记2-intent
- idea 升级13.1.2后 svn: E200007: Retrieval of mergeinfo unsupported解决
- 15、排序算法c语言描述---直接插入排序
- Python语言中的__name__和__doc__
- 详细解析Java中抽象类和接口的区别
- 16、排序算法c语言描述---希尔排序
- Linux内核里的“智能指针”