openmp 并行 速度更慢_为什么使用OpenMP会使代码运行速度变慢?

我有以下代码使用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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值