gpt4 book ai didi

c - 为什么使用 OpenMP 会使代码运行速度变慢?

转载 作者:行者123 更新时间:2023-11-30 18:15:45 33 4
gpt4 key购买 nike

我有以下代码,它使用 OMP 并行化蒙特卡罗方法。我的问题是为什么代码的串行版本(monte_carlo_serial)比并行版本(monte_carlo_parallel)运行得快很多。我在 32 核的机器上运行代码,并在控制台上打印出以下结果:

-bash-4.1$ gcc -fopenmp hello.c ;
-bash-4.1$ ./a.out
Pi(序列号):3.140856
所用时间 0 秒 50 毫秒
Pi(并行):3.132103
所用时间 127 秒 990 毫秒

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <time.h>

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)
{
srand48((int)time(NULL) ^ omp_get_thread_num()); //Give random() a seed value
for (i=0; i<niter; ++i) //main loop
{
x = (double)drand48(); //gets a random x coordinate
y = (double)drand48(); //gets a random y coordinate
z = ((x*x)+(y*y)); //Checks to see if number is inside unit circle
if (z<=1)
{
++count; //if it is, consider it a valid random point
}
}
}

pi = ((double)count/(double)(niter*numthreads))*4.0;
printf("Pi (Parallel): %f\n", pi);
return 0;
}

int monte_carlo_serial(){
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

srand48((int)time(NULL) ^ omp_get_thread_num()); //Give random() a seed value

for (i=0; i<niter; ++i) //main loop
{
x = (double)drand48(); //gets a random x coordinate
y = (double)drand48(); //gets a random y coordinate
z = ((x*x)+(y*y)); //Checks to see if number is inside unit circle
if (z<=1)
{
++count; //if it is, consider it a valid random point
}
}

pi = ((double)count/(double)(niter))*4.0;
printf("Pi (Serial): %f\n", pi);

return 0;
}


void main(){
clock_t start = clock(), diff;

monte_carlo_serial();

diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds \n", msec/1000, msec%1000);



start = clock(), diff;

monte_carlo_parallel();

diff = clock() - start;
msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds \n", msec/1000, msec%1000);

}

最佳答案

变量

count 

在所有生成的线程之间共享。他们每个人都必须锁定计数以增加它。此外,如果线程在单独的 cpu 上运行(如果不是,则不可能获胜),您需要将计数值从一个核心发送到另一个核心并再次返回的成本。

这是虚假共享的教科书示例。访问串行版本中的计数,它将位于寄存器中,并且需要 1 个周期才能递增。在并行版本中,它通常不在缓存中,您必须告诉其他核心使该缓存行无效,获取它(L3 最多需要 66 个周期)递增它,然后将其存储回来。每次 count 从一个 cpu 核心迁移到另一个 CPU 核心时,您的最小周期成本约为 125 个周期,这比 1 差很多。线程将永远无法并行运行,因为它们依赖于 count。

尝试修改您的代码,以便每个线程都有自己的计数,然后最后将所有线程的所有计数值相加,您/可能/会看到加速。

关于c - 为什么使用 OpenMP 会使代码运行速度变慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146527/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com