C++ OpenMP计算每个线程在循环中的迭代次数

C++ OpenMP计算每个线程在循环中的迭代次数,c++,visual-studio-2010,openmp,C++,Visual Studio 2010,Openmp,我决定计算每个线程在循环中的迭代次数。 所以我必须声明变量并获得每个迭代的线程数,对吗? 我得到的线程数与(0,1,2,3)4个线程一样。但是当我创建变量来计算每个线程的总和时,我遇到了一个问题 #include <iostream> #include <time.h> #include <math.h> #include "fact.h" #include <cstdlib>; #include <conio.h>; #include

我决定计算每个线程在循环中的迭代次数。 所以我必须声明变量并获得每个迭代的线程数,对吗? 我得到的线程数与(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;}
#包括
#包括
#包括
#包括“事实h”
#包括,;
#包括,;
#包括
使用名称空间std;
int main()
{
时钟t1,t2;
int n;
长双经验=0;
长双y;
INTP;
内斧;
coutn;
t1=时钟();
int a=0,b=0,c=0,d=0;
#用于num_线程的pragma omp并行(4)缩减(+:exp)

对于(int i=1;i您需要使用
if(l==0)
等,而不是
if(l=0)
。后者将0分配给
l
,而不是将
l
与0进行比较。

您需要使用
if(l==0)
等,而不是
if(l=0)
。后者将0分配给
l
,而不是将
l
与0进行比较。

此外,我是在计算每个线程在循环中的迭代次数???还是我只是计算与循环并行时使用的线程数?我想你在计算每个线程执行的迭代次数,但我没有使用OpenMP,所以我不完全确定。不过,这很容易检查:如果
a+b+c+d==n
,您可能做得对:-)@Olexanderhaly:顺便说一句,应该避免使用单个字母
l
作为变量名,因为它很容易与数字
1
混淆。此外,我是在计算每个线程在循环中的迭代次数???还是我只计算并行循环中使用的线程数?我想你在计算每个线程执行的迭代次数,但我有一段时间没有使用OpenMP了,所以我不完全确定。但是,这很容易检查:如果
a+b+c+d==n
,您可能做得对:-)@olexanderhaly:顺便说一句,应该避免使用单个字母
l
作为变量名,因为它很容易与编号
1