OpenMP计算每个线程的循环迭代次数
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。
相关文章:
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 使用基于范围的循环迭代矢量时出现问题
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 如何基于循环迭代器选择函数
- 如何使用基于范围的for循环迭代Rapidjson文档(它本身就是一个JSON数组)
- 在循环迭代期间将引用保存到矢量中的空指针
- 在每次循环迭代时停止代码执行毫秒
- 优先级队列(使用 pairs<int,int> ) 根据 for 循环迭代器的顶级元素
- 如何在每次循环迭代期间生成向量,存储数据,然后删除该向量?
- 先前循环迭代对当前迭代执行时间的影响
- C++使用 for 循环迭代 2 个向量
- 为什么循环迭代不打印任何东西?
- 每次循环迭代后递减迭代器都会显示出奇怪的行为
- 虽然循环迭代了C++中的太多元素
- 循环迭代后,C 函数调用不会再次调用
- 指向向量和向量的指针中的循环迭代
- 为矩阵创建动态数组,以循环迭代器命名
- for 循环 - 迭代特定元素
- 通过线程分发循环迭代