简体   繁体   中英

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. 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

You need to use if (l == 0) etc. instead of if (l = 0) . The latter assigns 0 to l rather than comparing l to 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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