I have a parallel utility class that I would like to be able to use future or openmp, depending on what's available on the system. The future code works fine, but when I pass a function pointer into openmp, I get a linker error, (but if I apply openmp for loop directly to a function there is no error. Here is the code to the parallel loop function from the utility file:
#include "ParallelUtils.h"
#define _OPENMP // just for testing, openmp is installed and verified working
#if defined(_OPENMP)
#include <omp.h>
#elif defined(_WIN32)
#include <Windows.h>
#else
#include <future>
#endif
void ParallelUtils::ParallelFor(size_t From, size_t To, const std::function<void(size_t)> &F)
{
#if defined(_OPENMP)
#pragma omp parallel for num_threads(To)
{
size_t i = omp_get_thread_num();
F(i);
}
#elif defined(_WIN32)
concurrency::parallel_for(From, To, [&](size_t i)
{
F(i);
});
#else
std::vector<std::future<void>> futures;
for (size_t i = From; i < To; ++i)
{
auto fut = std::async([i, F]()
{
F(i);
});
futures.push_back(std::move(fut));
}
for (size_t i = 0; i < futures.size(); ++i)
futures[i].wait();
futures.clear();
#endif
}
Again, if I apply it directly to the function it works fine, ex.
#pragma omp parallel shared(this, &Input, InOffset, blkLen), num_threads(m_treeParams.ThreadDepth())
{
size_t i = omp_get_thread_num();
ProcessLeaf(Input, InOffset + (i * blkLen), m_hashChain, i * CHAIN_SIZE, blkLen);
}
So two questions, what am I doing wrong, and, is there anything I need to do to add the shared variables to the openmp version of the function?