并行Pi的计算
2014-06-20 21:45
387 查看
主要考虑两种PI的计算方法 , 一种为积分法的用共享内存试的GPU实现, 另外一种为在面积估算MPI伪代码实现。
1.积分法求pi
2.面积法求pi
两种计算模式, 但其实这两种计算方法都比较适合在GPU上实现
详细的过程可以参考 : 风晨的CUDA入门 和 Introduction to Parallel Computing
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; } }
2.面积法求pi
/* 算法 1.Inscribe a circle in a square 2.Randomly generate points in the square 3.Determine the number of points in the square that are also in the circle Let r be the number of points in the circle divided by the number of points in the square 4.PI ~ 4*r */ npoints = 10000 circle_count = 0 p = number of tasks num = npoints/p find 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 + 1 end do if 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_count endif
两种计算模式, 但其实这两种计算方法都比较适合在GPU上实现
详细的过程可以参考 : 风晨的CUDA入门 和 Introduction to Parallel Computing
相关文章推荐
- OpenMP并行计算PI的值
- 并行计算mpi [ PI ]
- 文章标题 并行计算 -- OpenMP简介和计算实例 (计算pi)
- Python并行计算pp模块实践笔记
- 并行计算—OpenMP—临界区critical
- 用GPU通用并行计算绘制曼德勃罗特集图形 下篇
- 并行计算--C#--求π并行实现
- 并行计算大作业之多边形相交(OpenMP、MPI、Java、Windows)
- vs中使用M_PI的问题及解决 角度转弧度&根据弧度计算圆周上点的坐标的方法
- Erlang:并行计算和云计算
- 【Python】【并行计算】Python 多核并行计算
- 计算pi,itertools模块小练习Python3
- 一个很牛的计算pi的c程序!
- delphi’线程新技术 并行计算
- 使用并行计算大幅提升递归算法效率
- Java 7 Fork/Join 并行计算框架概览
- 4.0中的并行计算和多线程详解(一)
- 浅谈.NET下的多线程和并行计算(十四)并行计算前言
- 三十分钟理解:双调排序Bitonic Sort,适合并行计算的排序算法
- (一)初步了解并行计算、OpenMP