0
每当我运行此代码时,它都会显示并行部分所执行的不同运行时间。根据我的核心,我尝试了不断的线程数,但仍然是徒劳无功。该计划是计算pi的价值。编为gcc -fopenmp
。改变OpenMP并行区域的运行时间
#include <stdio.h>
#include <omp.h>
static long num_steps = 100000; double step;
//double omp_get_wtime(void);
int main(){
int i;
double x,pi,max_threads,start,time;
double sum=0.0;
step = 1.0/(double) num_steps;
//omp_set_num_threads(4);
omp_get_max_threads();
start=omp_get_wtime();
#pragma omp parallel
{
#pragma omp for reduction(+:sum) schedule(static) private(x) //reduction to get local copy
for (i=0;i<num_steps;i++){
x=(i+0.5)*step;
sum += 4.0/(1.0+x*x);
}
//max_threads=omp_get_max_threads();
}
time=omp_get_wtime()-start;
pi=step*sum;
printf("pi=(%f)\t run_time(%f)\n",pi,time);//,max_threads);
return 0;
}
欢迎来到Stack Overflow!你的问题的题目是非常通用的,绝不反映实际问题。请花点时间在帮助部分阅读[我如何提出一个好问题?](http://stackoverflow.com/help/how-to-ask)。 –
对于一致的运行时间,一个典型的要求是固定线程并避免来自其他任务的竞争。如果你正在做一个简单的总和减少和防止simd优化,这个任务是没有意义的。 – tim18