[英]C++ parallel_for error
I am trying to learn how to use TBB, so I'm modifying a sample program I found that is designed to compute powers of an array of complex numbers. 我正在尝试学习如何使用TBB,因此我正在修改一个示例程序,该程序旨在计算复数数组的幂。 Originally, it was passing an array into the parallel_for loop, but I am trying to change it so that it passes in a vector.
最初,它是将数组传递给parallel_for循环,但是我试图对其进行更改,以使其传递给向量。 However, I cannot get my code to compile;
但是,我无法编译我的代码。 I get the following error (compiled using g++ -g program_name.cpp -ltbb):
我收到以下错误(使用g ++ -g program_name.cpp -ltbb编译):
error: passing ‘const std::complex<double>’ as ‘this’ argument
discards qualifiers [-fpermissive] result[i] = z;
My code is below: 我的代码如下:
#include <cstdlib>
#include <cmath>
#include <complex>
#include <ctime>
#include <iostream>
#include <iomanip>
#include "tbb/tbb.h"
#include "tbb/blocked_range.h"
#include "tbb/parallel_for.h"
#include "tbb/parallel_for_each.h"
#include "tbb/task_scheduler_init.h"
using namespace std;
using namespace tbb;
typedef complex<double> dcmplx;
dcmplx random_dcmplx ( void )
{
double e = 2*M_PI*((double) rand())/RAND_MAX;
dcmplx c(cos(e),sin(e));
return c;
}
class ComputePowers
{
vector<dcmplx> c; // numbers on input
int d; // degree
vector<dcmplx> result; // output
public:
ComputePowers(vector<dcmplx> x, int deg, vector<dcmplx> y): c(x), d(deg), result(y) { }
void operator() ( const blocked_range<size_t>& r ) const
{
for(int i=r.begin(); i!=r.end(); ++i)
{
dcmplx z(1.0,0.0);
for(int j=0; j < d; j++) {
z = z*c[i];
};
result[i] = z;
}
}
};
int main ( int argc, char *argv[] )
{
int deg = 100;
int dim = 10;
vector<dcmplx> r;
for(int i=0; i<dim; i++)
r.push_back(random_dcmplx());
vector<dcmplx> s(dim);
task_scheduler_init init(task_scheduler_init::automatic);
parallel_for(blocked_range<size_t>(0,dim),
ComputePowers(r,deg,s));
for(int i=0; i<dim; i++)
cout << scientific << setprecision(4)
<< "x[" << i << "] = ( " << s[i].real()
<< " , " << s[i].imag() << ")\n";
return 0;
}
You're trying to modify non- mutable
member field result
in const
-qualified operator()
. 你试图修改非
mutable
成员字段result
在const
-qualified operator()
Resolve this discrepancy. 解决此差异。
Edit #1: I have already mentioned the two keywords above. 编辑#1:我已经提到了上面的两个关键字。 Either:
要么:
Remove const
qualifier from operator()
: 从
operator()
删除const
限定符:
void operator() ( const blocked_range<size_t>& r ) { ... }
Make result
mutable
: 使
result
mutable
:
mutable vector<dcmplx> result;
Additional erorrs may emerge after applying (although strongly preferred ) variant no. 应用(尽管强烈推荐 )变体编号5之后可能会出现其他错误。 1. No. 2 is just for completeness and is used only in marginal situations .
1. 2号仅用于完整性,仅在边缘情况下使用 。
It indeed results in an error with the 1st variant. 实际上,这会导致1st变型出现错误。 This is because
tbb::parallel_for
takes Func
by const&
, so it can call only const
-qualified member functions on your functor. 这是因为
tbb::parallel_for
通过const&
接受Func
,因此它只能在函子上调用const
限定的成员函数。 Why? 为什么? TBB doesn't want to waste performance by copying large functors (STL passes them by value).
TBB不想通过复制大型函子来浪费性能(STL按值传递它们)。
I don't know what's the common practice here, I've never used this library. 我不知道这里有什么普遍做法,我从未使用过这个库。
Edit #2: Probably all you were missing was that result
wasn't a reference: 编辑#2:可能您所缺少的只是
result
不是参考:
vector<dcmplx> &result;
Now you'll be able to modify it, even in const
-qualified operator()
. 现在,即使在
const
限定operator()
,您也可以对其进行修改。 Modifying a member that gets destructed afterwards wouldn't make sense. 修改之后销毁的成员没有任何意义。
Don't forget to change the constructor's signature to pass y
by reference, too. 别忘了更改构造函数的签名以通过引用传递
y
。
Off topic issues in your code: 代码中的主题外问题:
Not included <vector>
header 不包含
<vector>
标头
using namespace bulky_namespace
globally 全局
using namespace bulky_namespace
not using size_t
for i
in the for
-loop 在
for
循环中没有为i
使用size_t
maybe more... 也许更多...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.