[英]OpenMP reduction with template type
template <typename T, std::size_t N>
static T sum(const std::array<T, N>& a)
{
T result;
// type of result (T) is not determined when pre-process?
#pragma omp parallel for reduction(+: result)
for(int i = 0; i < static_cast<int>(N); i++)
{
result += a[i];
}
return result;
}
I can compile and run above code with MSVC and gcc. 我可以用MSVC和gcc编译并运行上面的代码。 Yes, it's excellent! 是的,这太棒了!
But my question is in the code comment; 但我的问题是在代码评论中; "Because the type of result (T) is not determined while pre-processing '#pragma', how does the compiler validate that the type of result is suited to OpenMP reduction?" “因为在预处理'#pragma'时没有确定结果类型(T),编译器如何验证结果类型是否适合OpenMP减少?” . 。
I'm sure it's OK if T=double and NG if T=std::string, but how does the pre-processor know the type of T? 我确定如果T = double,那就没关系,如果T = std :: string则是NG,但是预处理器如何知道T的类型?
I remember I couldn't compile the above code with some minor c++ compiler a long time ago. 我记得很久以前我用一些小的c ++编译器无法编译上面的代码。
Let me ask which behavior (compilable or uncompilable) is correct in the context of C++/OpenMP specifications. 让我问一下在C ++ / OpenMP规范的上下文中哪种行为(可编译或不可编译)是正确的。
It's unspecified (for OpenMP 3.0 or later) or undefined (for OpenMP 2.5) 它未指定 (对于OpenMP 3.0或更高版本)或未定义 (对于OpenMP 2.5)
reduction
is one of data-sharing attribute clauses, and OpenMP Application Program Interface Version 2.5 says: reduction
是数据共享属性子句之一, OpenMP应用程序接口版本2.5说:
2.8.3 Data-Sharing Attribute Clauses 2.8.3数据共享属性子句
---- C/C++ ---- ---- C / C ++ ----
If a variable referenced in a data-sharing attribute clause has a type derived from a template, and there are no other references to that variable in the program, then any behavior related to that variable is undefined. 如果数据共享属性子句中引用的变量具有从模板派生的类型,并且程序中没有对该变量的其他引用,则与该变量相关的任何行为都是未定义的。
---- C/C++ ---- ---- C / C ++ ----
OpenMP Application Program Interface Version 3.0 says: OpenMP应用程序接口版本3.0说:
2.9.3 Data-Sharing Attribute Clauses 2.9.3数据共享属性子句
---- C/C++ ---- ---- C / C ++ ----
If a variable referenced in a data-sharing attribute clause has a type derived from a template, and there are no other references to that variable in the program, then any behavior related to that variable is unspecified. 如果数据共享属性子句中引用的变量具有从模板派生的类型,并且程序中没有对该变量的其他引用,则未指定与该变量相关的任何行为。
---- C/C++ ---- ---- C / C ++ ----
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.