简体   繁体   English

OpenMP计算使每个线程处于循环状态的迭代次数

[英]OpenMP count the number of iteration in cycle which is making each thread

I decided to count the number of iteration in cycle which is making each thread. 我决定计算构成每个线程的循环迭代次数。 So i must to declare variable and get the thread number of each iteration right? 因此,我必须声明变量并获得每次迭代的线程号正确吗? i got the number of threads just like ( 0,1,2,3) 4 threads. 我得到的线程数就像(0,1,2,3)4个线程。 but when i created variables to calculate the sum of each thread i got a problem. 但是当我创建变量来计算每个线程的总和时,我遇到了问题。

#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;}

I am not getting errors but it shows me not the proper number of iteration in cycle which each thread is making. 我没有收到错误,但是它没有显示每个线程进行的正确的循环迭代次数。 In this work i calculated exponent. 在这项工作中,我计算了指数。 2.71 2.71

You need to use if (l == 0) etc. instead of if (l = 0) . 您需要使用if (l == 0)等,而不是if (l = 0) The latter assigns 0 to l rather than comparing l to 0. 后者将0分配给l而不是将l与0进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM