Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

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

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?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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 Application Program Interface Version 3.0 says:

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++ ----


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...