Tag: montecarlo

使用OpenMP计算PI的值

我正在尝试通过并行化monte carlo代码来学习如何使用OpenMP,该代码使用给定的迭代次数来计算PI的值。 代码的内容是这样的: int chunk = CHUNKSIZE; count=0; #pragma omp parallel shared(chunk,count) private(i) { #pragma omp for schedule(dynamic,chunk) for ( i=0; i<niter; i++) { x = (double)rand()/RAND_MAX; y = (double)rand()/RAND_MAX; z = x*x+y*y; if (z<=1) count++; } } pi=(double)count/niter*4; printf("# of trials= %d , estimate of pi is %g \n",niter,pi); 虽然在10,000次迭代时,这并没有产生适当的pi值。 如果取出所有OpenMP东西,它就可以正常工作。 我应该提一下,我使用了monte carlo代码: http […]

Monte Carlo pi逼近的并行化

我正在编写ac脚本来与OpenMp并行化pi近似。 我认为我的代码在令人信服的输出下运行良好。 我现在用4个线程运行它。 我不确定的是,如果此代码容易受到竞争条件的影响? 如果是,如何协调此代码中的线程操作? 代码如下: #include #include #include #include #include double sample_interval(double a, double b) { double x = ((double) rand())/((double) RAND_MAX); return (ba)*x + a; } int main (int argc, char **argv) { int N = atoi( argv[1] ); // convert command-line input to N = number of points int i; int NumThreads […]