[OpenMP] Parameter packs

Should this compile?

#include <tuple>

constexpr std::size_t N = 100;

double A[N];

template<typename ...Args>
void loop(Args ...args) {
#pragma omp parallel firstprivate(args...)
  for (std::size_t i = 0; i < N; ++i) {
    A[i] = (i + ... + args);
  }
}

int main(void) {
  std::apply( (const auto & ...args) { loop(args...); },
                 std::tuple<int, double, float>(10, 2.6, 3.4));
  std::apply( (const auto & ...args) { loop(args...); },
                 std::tuple<int, long, char, double, short>(10, 100, 'b', 2.6, 3));
  return 0;
}

clang++ errors with:

firstprivate.cpp:10:39: error: expected ',' or ')' in 'firstprivate' clause
#pragma omp parallel firstprivate(args...)
                                      ^
firstprivate.cpp:10:39: error: expected expression
2 errors generated.

g++ gives a similar error. I was not terribly surprised by this.
However, it sure would be nice if it worked.

                      -David

The OpenMP specification has the following restriction:

“Variadic parameter packs cannot be expanded into a directive or its clauses except as part of an expression argument to be evaluated by the base language, such as into a function call inside an if clause.”

The language committee has some discussions on it in the last face-to-face meeting. We will look into relaxing this restriction to provide better usage with C++ templates.

Kelvin

Good to hear. Is it technically difficult to do in clang? If not,
would an experimental implementation help the committee?

                               -David

Kelvin Li via Openmp-dev <openmp-dev@lists.llvm.org> writes:

The OpenMP specification has the following restriction:

"Variadic parameter packs cannot be expanded into a directive or its clauses except as part of an expression argument to be evaluated by the base language, such as into a function call inside an if clause."

Glad to see that this is covered by spec text. Unfortunately, searching the document for "parameter pack" has no match, because of the line break between the two words :confused:

I do not think it is very difficult. The experts on the list can correct me if there
are some complications involved.

A reference implementation can always help the standardization process.

Kelvin