When using OpenMP, I would like to declare an user-defined reduction for a class template.
#include <omp.h>
#include <iostream>
template<typename T>
class Foo
{
public:
T Data_;
template<typename U> friend Foo<U> operator+( const Foo<U>& lhs, const Foo<U>& rhs );
};
template<typename U>
Foo<U> operator+( const Foo<U>& lhs, const Foo<U>& rhs )
{
Foo<U> Addition;
Addition.Data_ = lhs.Data_ + rhs.Data_;
return Addition;
}
#pragma omp declare reduction( + : template<typename U> Foo<U> : omp_out = omp_out + omp_in ) initializer (omp_priv=omp_orig)
int main( int argc, char* argv[] )
{
Foo<int> Array[100];
for ( int i = 0 ; i < 100 ; ++i )
{
Array[i].Data_ = i;
}
Foo<int> Sum {0};
#pragma omp parallel for num_threads(4) reduction( + : Sum )
for ( int i = 0 ; i < 100 ; ++i )
{
Sum.Data_ += Array[i].Data_;
}
std::cout << Sum.Data_ << std::endl;
return 0;
}
But I got the following errors:
error: expected type-specifier before 'template'
#pragma omp declare reduction( + : template<typename U> Foo<U> : omp_out = omp_out + omp_in ) initializer (omp_priv=omp_orig)
^~~~~~~~
I can fix the error by replacing template<typename U> Foo<U> with Foo<int>.
But I would like to know is there any solution by keeping using template.
Thanks.