[英]OpenMP count the number of iteration in cycle which is making each thread
我決定計算構成每個線程的循環迭代次數。 因此,我必須聲明變量並獲得每次迭代的線程號正確嗎? 我得到的線程數就像(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
您需要使用if (l == 0)
等,而不是if (l = 0)
。 后者將0分配給l
而不是將l
與0進行比較。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.