我有以下代码使用OMP来并行化monte carlo方法 . 我的问题是为什么代码的串行版本(monte_carlo_serial)比并行版本(monte_carlo_parallel)运行得快得多 . 我在具有32个内核的计算机上运行代码,并将以下结果打印到控制台:
-bash-4.1 $ gcc -fopenmp hello.c;
-bash-4.1 $ ./a.out
Pi(Serial):3.140856
花费的时间为0秒50毫秒
Pi(平行):3.132103
耗时127秒990毫秒
#include
#include
#include
#include
#include
int niter = 1000000; //number of iterations per FOR loop
int monte_carlo_parallel() {
double x,y; //x,y value for the random coordinate
int i; //loop counter
int count=0; //Count holds all the number of how many good coordinates
double z; //Used to check if x^2+y^2<=1
double pi; //holds approx value of pi
int numthreads = 32;
#pragma omp parallel firstprivate(x, y, z, i) reduction(+:count) num_threads(numthreads)