我决定计算每个线程的循环迭代次数。所以我必须声明变量并获取每次迭代的线程号对吗?我得到的线程数就像(0,1,2,3)4个线程。但是当我创建变量来计算每个线程的总和时,我遇到了问题。
#include <iostream>
#include <time.h>
#include <math.h>
#include "fact.h"
#include <cstdlib>;
#include <conio.h>;
#include <omp.h>
using namespace std;
int main()
{
clock_t t1,t2;
int n;
long double exp=0;
long double y;
int p;
int axe;
cout<<"Enter n:";
cin>>n;
t1=clock();
int a=0,b=0,c=0,d=0;
#pragma omp parallel for num_threads(4) reduction (+:exp)
for(int i=1; i<n; i++)
{
int l=omp_get_thread_num();
cout<<l<<endl;
if (l=0) {a++;}
else if (l=1) {b++;}
else if (l=2) {c++;}
else {d++;}
p=i+1;
exp=exp+(1/((fact(p))));
}
t2=clock();
double total_clock;
total_clock=t2-t1;
long double total_exp;
total_exp=exp+2;
cout<<endl;
cout<<endl;
cout<<total_clock<<"\t the time is used for parralel calculations"<<endl;
cout<<total_exp<<endl;
cout<<a<<" thread one"<<endl;
cout<<b<<"thread two"<<endl;
cout<<c<<"thread three"<<endl;
cout<<d<<"Thread fourth"<<endl;
return 0;}
我没有收到错误,但它向我显示了每个线程正在进行的循环中不正确的迭代次数。在这项工作中,我计算了指数。2.71