I have a function that contains an OpenMP-parallelized for loop, which calls a callback at each iteration, similar to this:
template<class Callback>
void iterate(const Callback& callback, bool parallel) {
#pragma omp parallel for if(parallel)
for(int i = 0; i < 1000; ++i) {
callback(i);
}
}
The caller can control whether the loop is parallelized using the parallel argument.
Is there a way to allow the caller to specify variables on which reduction should be done, like this:
int counter = 0;
#pragma omp ??? reduction(+ : counter)
iterate([&counter](int i) {
counter++;
}, true);
One way seems to be to put #pragma omp parallel outside the function at the call site, and #pragma omp for inside the function. But this restricts how the function can be used, and seems to be non-standard.
Is there way to do this using nested parallel constructs, the loop contruct with its bind clause, or possibly another openmp feature?
std::atomic<int>might help here.counter) to be defined at runtime (and even de number/types)?pragma omp parallel for) is implemented inside of a function that returns to the caller via a callback. So its implementation is not accessible from the caller. But the variable that should be reduced is at the caller.iterate()at different places and with different callbacks, and do different reductions every time. (Not runtime-determined, but different for the different places where iterate() is used)