TBB parallel_reduce - 需要更好地理解"Developer Reference"的例子
TBB parallel_reduce - need better understanding of example from the "Developer Reference"
我正在查看页面上的示例,该示例显示了如何以命令式形式调用parallel_reduce
。此示例的副本如下:
struct Sum {
float value;
Sum() : value(0) {}
Sum( Sum& s, split ) {value = 0;}
void operator()( const blocked_range<float*>& r ) {
float temp = value;
for( float* a=r.begin(); a!=r.end(); ++a ) {
temp += *a;
}
value = temp;
}
void join( Sum& rhs ) {value += rhs.value;}
};
float ParallelSum( float array[], size_t n ) {
Sum total;
parallel_reduce( blocked_range<float*>( array, array+n ),
total );
return total.value;
}
我的问题是 - 为什么我们需要operator()
体中的变量float temp
?如果求和直接与value
数据成员一起使用,则会发生什么情况:
for( float* a=r.begin(); a!=r.end(); ++a ) value += *a;
直接应用时它将起作用,因为类的每个实例同时由一个线程使用。
但是直接使用此变量可能会阻止编译器对循环进行矢量化。我会通过缓存 r.end() 来进一步处理这个逻辑,因为如果它没有正确内联,它也会破坏矢量化。虽然,它与TBB本身没有直接关系,只是一般的C++优化技巧。
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 不断"Attempting to reference a deleted function"
- std::iterator::reference 必须是引用吗?
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- C++返回类型 T(&)[] 与使用 reference = T(&)[] 作为返回类型
- const auto & 和 auto & if reference 对象之间的区别是 const
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 以前的'namespace reference { }'声明
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- C ++引用函数参数似乎包含原始对象的副本,而不是充当"real reference"
- TBB parallel_reduce - 需要更好地理解"Developer Reference"的例子