使用模板类型减少OpenMP [英] OpenMP reduction with template type

查看:75
本文介绍了使用模板类型减少OpenMP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
}

我可以使用MSVC和gcc编译并运行以上代码.是的,太好了!

I can compile and run above code with MSVC and gcc. Yes, it's excellent!

但是我的问题在代码注释中. 由于在预处理'#pragma'时未确定结果(T)的类型,因此编译器如何验证结果类型是否适合OpenMP缩减?" .

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?".

我确定如果T = double,可以确定,如果T = std :: string,则可以确定为NG,但是预处理程序如何知道T的类型?

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?

我记得很久以前我无法使用一些较小的c ++编译器来编译以上代码.

I remember I couldn't compile the above code with some minor c++ compiler a long time ago.

让我问一下在C ++/OpenMP规范中哪种行为(可编译或不可编译)是正确的.

Let me ask which behavior (compilable or uncompilable) is correct in the context of C++/OpenMP specifications.

推荐答案

它是未指定(对于OpenMP 3.0或更高版本)或未定义(对于OpenMP 2.5)

It's unspecified (for OpenMP 3.0 or later) or undefined (for OpenMP 2.5)

reduction是数据共享属性子句之一,并且 OpenMP应用程序接口版本2.5 说:

reduction is one of data-sharing attribute clauses, and OpenMP Application Program Interface Version 2.5 says:

2.8.3数据共享属性子句
---- C/C ++ ----
如果在数据共享属性子句中引用的变量具有从模板派生的类型,并且程序中没有对该变量的其他引用,则与该变量相关的任何行为均未定义.
---- C/C ++ ----

2.8.3 Data-Sharing Attribute Clauses
---- 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++ ----

OpenMP应用程序 程序界面版本3.0 说:

OpenMP Application Program Interface Version 3.0 says:

2.9.3数据共享属性子句
---- C/C ++ ----
如果数据共享属性子句中引用的变量具有从模板派生的类型,并且程序中没有对该变量的其他引用,则未指定与该变量有关的任何行为.
---- C/C ++ ----

2.9.3 Data-Sharing Attribute Clauses
---- 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++ ----

这篇关于使用模板类型减少OpenMP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆