2013-05-14 87 views
1

我決定統計正在製作每個線程的循環中的迭代次數。 所以我必須聲明變量並獲得每個迭代的線程號? 我得到了像(0,1,2,3)4個線程一樣的線程數。但是當我創建變量來計算每個線程的總和時,我得到了一個問題。OpenMP計算正在製作每個線程的循環中的迭代次數

#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

回答

1

您需要使用if (l == 0)等而不是if (l = 0)。後者將0至l,而不是比較l 0我的

+0

沒有看到,謝謝:) – 2013-05-14 10:42:05

+0

之外,我在計算迭代的週期,這是使每個線程的數量???或者我只是計算並行我的週期中使用的線程數量? – 2013-05-14 10:43:51

+0

我想你會計算每個線程執行的迭代次數,但是我有一段時間沒有使用OpenMP,所以我不完全確定。 – 2013-05-14 10:58:54