#include<iostream>
#include<fstream>
#include<time.h>
#include<omp.h>
using namespace std;
static long num_steps = 100;
#define NUM 8
double step;
void main()
{
clock_t time =clock();
ofstream result;
result.open ("Result.txt");
int a[100];
double pi, sum=0.0;
step = 1.0/(double) num_steps;
#pragma omp parallel num_threads(NUM)
{
int i, ID;
double x, psum= 0.0;
int nthreads = omp_get_num_threads();
ID = omp_get_thread_num();
for (i=ID;i<= num_steps; i+=nthreads)
{
x = (i+0.5)*step;
psum += 4.0/(1.0+x*x);
}
#pragma omp critical
sum += psum;
}
pi = step * sum;
for (int j=0;j<100;j++)
result<<a[j]<<endl;
time = clock() - time;
result << "Time Elapsed: " << (((double)time)/CLOCKS_PER_SEC) << endl;
result <<"======================================================================================="<<endl;
result.close();
}
问题是:
for (i=ID;i<= num_steps; i+=nthreads)
以下 for 循环按以下顺序执行线程:01234567 01234567 01234567 等...任务是将 for 循环更改为,以便线程均匀分布,而不是以舍入的方式分布。首先是 0,然后是 1,然后是 2.... 然后是 sevens 我应该如何更改 forloop?