蒙特卡洛方法实现计算圆周率的方法比较简单,其思想是假设我们向一个正方形的标靶上随机投掷飞镖,靶心在正中央,标靶的长和宽都是2 英尺。同时假设有一个圆与标靶内切。圆的半径是1英尺,面积是π平方英尺。如果击中点在标靶上是均匀分布的(我们总会击中正方形),那么飞镖击中圆的数量近似满足等式

飞镖落在圆内的次数/飞镖落在标靶内的总次数=π/4

因为环包含的面积与正方形面积的比值是π/4。

因为环所包含的面积与正方形面积的比值是π/4。

我们可以用这个公式和随机数产生器来估计π的值。

伪代码如下:

number_in_circle=;
for(toss=;toss<number_of_tosses;toss++){
x=random double between - and ;
y=random double between - and ;
distance_squared=x*x+y*y;
if(distance_squared<=) number_in_cycle++;
}
pi_estimate=*number_in_cycle/((double)number_in_tosses);

这种采用了随机(随机投掷)的方法称为蒙特卡洛(Monte Carlo)方法。

编写了一个采用蒙特卡洛方法的MPI,Pthread,OpenMP程序估计π的值。

使用MPI编写时,进程0读取总的投掷次数,并把它们广播给各个进程。使用MPI_Reduce求出局部变量number_in_cycle的全局总和,并让进程0打印它。

使用Pthread编写时,有主线程读入总的投掷数,然后输出估算值。

使用OpenMP编写时,在开启任何线程前读取总的投掷次数。使用reduction子句计算飞镖集中环内的次数。在合并所有的线程后,打印结果。

这三个程序中,投掷次数可能都非常大,可能总的投掷次数和击中环内的次数都得用long int型来表示。

下面是MPI程序代码

 #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<mpi.h>
void read_num(long long int *num_point,int my_rank,MPI_Comm comm);
void compute_pi(long long int num_point,long long int* num_in_cycle,long long int* local_num_point,int comm_sz,long long int *total_num_in_cycle,MPI_Comm comm,int my_rank);
int main(int argc,char** argv){
long long int num_in_cycle,num_point,total_num_in_cycle,local_num_point;
int my_rank,comm_sz;
MPI_Comm comm;
MPI_Init(NULL,NULL);//初始化
comm=MPI_COMM_WORLD;
MPI_Comm_size(comm,&comm_sz);//得到进程总数
MPI_Comm_rank(comm,&my_rank);//得到进程编号
read_num(&num_point,my_rank,comm);//读取输入数据
compute_pi(num_point,&num_in_cycle,&local_num_point,comm_sz,&total_num_in_cycle,comm,my_rank);
MPI_Finalize();
return ;
}
void read_num(long long int* num_point,int my_rank,MPI_Comm comm){
if(my_rank==){
printf("please input num in sqaure \n");
scanf("%lld",num_point);
}
/*
广播函数
int MPI_Bcast(
void* data_p //in/out
int count //in
MPI_Datatype datatype //in
int source_proc //in
MPI_Comm comm //in
)
*/
MPI_Bcast(num_point,,MPI_LONG_LONG,,comm); }
void compute_pi(long long int num_point,long long int* num_in_cycle,long long int* local_num_point,int comm_sz,long long int *total_num_in_cycle,MPI_Comm comm,int my_rank){
*num_in_cycle=;
*local_num_point=num_point/comm_sz;
double x,y,distance_squared;
srand(time(NULL));
for(long long int i=;i< *local_num_point;i++){
x=(double)rand()/(double)RAND_MAX;
x=x*-;
y=(double)rand()/(double)RAND_MAX;
y=y*-;
distance_squared=x*x+y*y;
if(distance_squared<=)
*num_in_cycle=*num_in_cycle+;
}
/*
全局函数
MPI_Reduce(
void* input_data_p //in
void* output_data_p //out
int count //in
MPI_Datatype datatype //in
MPI_Op oprtator //in
int dest_process //in
MPI_Comm comm //in
)
*/
MPI_Reduce(num_in_cycle,total_num_in_cycle,,MPI_LONG_LONG,MPI_SUM,,comm);
if(my_rank==){
double pi=(double)*total_num_in_cycle/(double)num_point*;
printf("the estimate value of pi is %lf\n",pi);
}
}

进行编译 mpicc -std=c99 -o mpi_mete mpi_mete.c

执行 mpiexec -n mpi_mete

输入数据

下面是Pthread程序代码

 #include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<pthread.h>
int thread_count;
long long int num_in_circle,n;
pthread_mutex_t mutex;
void* compute_pi(void* rank);
int main(int argc,char* argv[]){
long thread;
pthread_t* thread_handles;
thread_count=strtol(argv[],NULL,);
printf("please input the number of point\n");
scanf("%lld",&n);
thread_handles=(pthread_t*)malloc(thread_count*sizeof(pthread_t));
pthread_mutex_init(&mutex,NULL);
for(thread=;thread<thread_count;thread++){
//创建线程
/*
int pthread_create(
pthread_t* thread_p //out
const pthread_attr_t* attr_p
void* (*start_routine)(void*) //in
void* arg_p //in
)
第一个参数是一个指针,指向对应的pthread_t对象。注意,pthread_t对象不是pthread_create函数分配的,必须在调用pthread_create函数前就为pthread_t
对象分配内存空间。第二个参数不用,所以只是函数调用时把NULL传递参数。第三个参数表示该线程将要运行的函数;最后一个参数也是一个指针,指向传给函数start_routine的参数
*/
pthread_create(&thread_handles[thread],NULL,compute_pi,(void*)thread);
}
//停止线程
/*
int pthread_join(
pthread_t thread /in
void** ret_val_p /out

第二个参数可以接收任意由pthread_t对象所关联的那个线程产生的返回值。
*/
for(thread=;thread<thread_count;thread++){
pthread_join(thread_handles[thread],NULL);
}
pthread_mutex_destroy(&mutex);
double pi=*(double)num_in_circle/(double)n;
printf("the esitimate value of pi is %lf\n",pi);
}
void* compute_pi(void* rank){
long long int local_n;
local_n=n/thread_count;
double x,y,distance_squared;
for(long long int i=;i<local_n;i++){
x=(double)rand()/(double)RAND_MAX;
y=(double)rand()/(double)RAND_MAX;
distance_squared=x*x+y*y;
if(distance_squared<=){
pthread_mutex_lock(&mutex);
num_in_circle++;
pthread_mutex_unlock(&mutex);
}
}
return NULL;
}

进行编译 gcc -std=c99 -o pth_mete pth_mete.c

运行 ./pth_mete

输入数据结果如下

下面是OpenMP代码

 #include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<omp.h>
int main(int argc,char** argv){
long long int num_in_cycle=;
long long int num_point;
int thread_count;
thread_count=strtol(argv[],NULL,);
printf("please input the number of point\n");
scanf("%lld",&num_point);
srand(time(NULL));
double x,y,distance_point;
long long int i;
#pragma omp parallel for num_threads(thread_count) default(none) \
reduction(+:num_in_cycle) shared(num_point) private(i,x,y,distance_point)
for( i=;i<num_point;i++){
x=(double)rand()/(double)RAND_MAX;
y=(double)rand()/(double)RAND_MAX;
distance_point=x*x+y*y;
if(distance_point<=){
num_in_cycle++;
}
}
double estimate_pi=(double)num_in_cycle/num_point*;
printf("the estimate value of pi is %lf\n",estimate_pi);
return ;
}

编译代码 gcc -std=c99 -fopenmp -o omp_mete omp_mete.c

执行 ./omp_mete

结果如下

蒙特卡洛方法计算圆周率的三种实现-MPI openmp pthread的更多相关文章

  1. root的方法大体上有以下三种

    root的方法大体上有以下三种一.手机软件安卓版直接root.这种方法不需要电脑的支持,也很安全.安卓版软件有:kingroot,360一键root,一键root大师,Towelroot,支持云roo ...

  2. Struts2中Action接收参数的方法主要有以下三种:

    Struts2中Action接收参数的方法主要有以下三种: 1.使用Action的属性接收参数(最原始的方式):     a.定义:在Action类中定义属性,创建get和set方法:     b.接 ...

  3. 用蒙特卡洛方法计算派-python和R语言

    用蒙特卡洛方法算pi-基于python和R语言 最近follow了MOOC上一门python课,开始学Python.同时,买来了概率论与数理统计,准备自学一下统计.(因为被鄙视过不是统计专业却想搞数据 ...

  4. C++中实现回调机制的几种方式(一共三种方法,另加三种)

    (1)Callback方式Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该方法, 比如Windows的窗口消息处理函数就是这种类型. 比如下面的示例代码,我们在Down ...

  5. HTML5结合CSS的三种方法+结合JS的三种方法

    HTML5+CSS: HTML中应用CSS的三种方法 一.内联 内联样式通过style属性直接套进HTML中去. 示例代码 <pstylepstyle="color:red" ...

  6. mybatis之接口方法多参数的三种实现方式

    关键代码举例: DaoMapper.xml <!-- 传入多个参数时,自动转换为map形式 --> <insert id="insertByColumns" us ...

  7. 算法之美--1.蒙特卡洛方法计算pi

    基本思想: 利用圆与其外接正方形面积之比为pi/4的关系,通过产生大量均匀分布的二维点,计算落在单位圆和单位正方形的数量之比再乘以4便得到pi的近似值.样本点越多,计算出的数据将会越接近真识的pi(前 ...

  8. C#中Math类的计算整数的三种方法

    1.Math.Round:四舍六入五取偶 引用内容 Math.Round( Math.Round( Math.Round( Math.Round( Math.Round( Math.Round( Ma ...

  9. jar包生制作几种方法,jar包导出三种方法:eclipse导出、jar命令、FatJar插件

    Eclipse将引用了第三方jar包的Java项目打包成jar文件的两种方法 方案一:用Eclipse自带的Export功能 步骤1:准备主清单文件 “MANIFEST.MF”, 由于是打包引用了第三 ...

随机推荐

  1. bzoj 4338: BJOI2015 糖果

    4338: BJOI2015 糖果 Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: 200  Solved: 93[Submit][Status][Dis ...

  2. 【bzoj4403】【序列统计】不降转升+组合数添项合并

    (上不了p站我要死了,侵权度娘背锅) Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input ...

  3. DotnetBrowser高级教程-(5)使用内置的MVC UI框架-EasyMvc

    如果DotnetBrowser只是实现了内置chrome浏览器和web/web socket server,似乎还不是很完美.因此,最新的DotnetBrowser已经内置对easy mvc控件的支持 ...

  4. malloc,free和new,delete之间的区别

    1.malloc free 是c语言里面的,不过在c++中也能使用,这个只是申请的一块内存,一般不能申请对象的内存空间:2.new delete,是c++的,申请的也是一块内存,只是这个可以申请对象. ...

  5. 获得Oracle当前日期的年或月的第一天和最后一天

    .当前日期的年份第一天和最后一天 第一天 select trunc(sysdate,'y') FROM DUAL; select trunc(sysdate,'yy') FROM DUAL; sele ...

  6. wp8开发时模拟器无法联网解决方法

    关于模拟器无法联网的正常解决方案在网上有很多 这里讲的是我在做测试的时候模拟器无法上网的特殊情况 由于使用的是无线网络 可能有一些差别 过程如图: 启动模拟器 如果之前没有设置过模拟器的交换器则会出现 ...

  7. DevExpress控件之GridControl、GridView

    GridControl对应标准WinForm里的GridView,相当于是一个控件,里面包含多个GridView也可以放其它的控件 禁止修改gridView1.OptionsBehavior.Edit ...

  8. vscode - 添加背景图片

    首先,Ctrl+Shift+P安装backround , 而后重启vscode会有默认的背景图片 修改背景图,可自定义三张 具体请看gif图 最开始时,发现png根本不是全透明,用ps处理了一下(下列 ...

  9. python的偏函数(partial)

    偏函数就是固定原函数的某个参数,比如newadd就是固定了add方法的第一个参数,让a=3,这样对newadd方法只要传入参数B就可以实现add方法了,刚看偏函数的写法可能会不适应,因为partial ...

  10. JavaScript字符编解码

    示例代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...