我正在进行简单的Pi计算,其中我并行化生成随机数的循环并且计数递增。串行(非OpenMP)代码比OpenMP代码执行得更好。以下是我采取的一些测量。这两个代码也在下面提供。
将序列号编译为:gcc pi.c -O3
将OpenMP代码编译为:gcc pi_omp.c -O3 -fopenmp
可能是什么问题?
# Iterations = 60000000
Serial Time = 0.893912
OpenMP 1 Threads Time = 0.876654
OpenMP 2 Threads Time = 23.8537
OpenMP 4 Threads Time = 7.72415
序列号:
/* Program to compute Pi using Monte Carlo methods */
/* from: http://www.dartmouth.edu/~rc/classes/soft_dev/C_simple_ex.html */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define SEED 35791246
int main(int argc, char* argv)
{
int niter=0;
double x,y;
int i;
long count=0; /* # of points in the 1st quadrant of unit circle */
double z;
double pi;
printf("Enter the number of iterations used to estimate pi: ");
scanf("%d",&niter);
/* initialize random numbers */
srand(SEED);
count=0;
struct timeval start, end;
gettimeofday(&start, NULL);
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;
gettimeofday(&end, NULL);
double t2 = end.tv_sec + (end.tv_usec/1000000.0);
double t1 = start.tv_sec + (start.tv_usec/1000000.0);
printf("Time: %lg\n", t2 - t1);
printf("# of trials= %d , estimate of pi is %lg \n",niter,pi);
return 0;
}
OpenMP并行代码:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define SEED 35791246
/*
from: http://www.dartmouth.edu/~rc/classes/soft_dev/C_simple_ex.html
*/
#define CHUNKSIZE 500
int main(int argc, char *argv[]) {
int chunk = CHUNKSIZE;
int niter=0;
double x,y;
int i;
long count=0; /* # of points in the 1st quadrant of unit circle */
double z;
double pi;
int nthreads, tid;
printf("Enter the number of iterations used to estimate pi: ");
scanf("%d",&niter);
/* initialize random numbers */
srand(SEED);
struct timeval start, end;
gettimeofday(&start, NULL);
#pragma omp parallel shared(chunk) private(tid,i,x,y,z) reduction(+:count)
{
/* Obtain and print thread id */
tid = omp_get_thread_num();
//printf("Hello World from thread = %d\n", tid);
/* Only master thread does this */
if (tid == 0)
{
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
}
#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++;
}
}
gettimeofday(&end, NULL);
double t2 = end.tv_sec + (end.tv_usec/1000000.0);
double t1 = start.tv_sec + (start.tv_usec/1000000.0);
printf("Time: %lg\n", t2 - t1);
pi=(double)count/niter*4;
printf("# of trials= %d, threads used: %d, estimate of pi is %lg \n",niter,nthreads, pi);
return 0;
}
答案 0 :(得分:1)
在这种特殊情况下,有很多可能性,因为openMP需要10K - 100K周期才能启动循环,使用openMP进行性能改进并非易事。
在此之后我们还有另一个问题,即rand不是重入http://man7.org/linux/man-pages/man3/rand.3.html
所以最有可能的rand一次只能被一个线程调用,因此你的开放式MP版本基本上是单线程的,因为你的循环没什么其他的,每次调用rand时额外的争用开销 - 因此显着减速。
答案 1 :(得分:1)
rand()
不可重入。它将无法正常工作,崩溃,或者只能一次从一个线程调用。像glibc这样的库通常会为传统的非重入函数序列化或使用TLS,而不是让它们在多线程代码中使用时随机崩溃。
尝试可重复使用的表单rand_r()
:
tid = omp_get_thread_num();
unsigned int seed = tid;
...
x = (double)rand_r(&seed)/RAND_MAX;
我认为你会发现它的速度要快得多。
注意我如何将种子设置为tid。您可能会想,为什么不将种子初始化为SEED
?给定相同的种子,rand_r()
将产生相同的数字序列。如果每个线程使用相同系列的伪随机数,那么它就会失去进行更多迭代的重点!您必须让每个线程使用不同的号码。