#ifndef LIBNEST2D_PARALLEL_HPP #define LIBNEST2D_PARALLEL_HPP #include #include #include #ifdef LIBNEST2D_THREADING_tbb #include #endif #ifdef LIBNEST2D_THREADING_omp #include #endif namespace libnest2d { namespace __parallel { template using TIteratorValue = typename std::iterator_traits::value_type; template inline void enumerate( Iterator from, Iterator to, std::function, size_t)> fn, std::launch policy = std::launch::deferred | std::launch::async) { using TN = size_t; auto iN = to-from; TN N = iN < 0? 0 : TN(iN); #ifdef LIBNEST2D_THREADING_tbb if((policy & std::launch::async) == std::launch::async) { tbb::parallel_for(0, N, [from, fn] (TN n) { fn(*(from + n), n); } ); } else { for(TN n = 0; n < N; n++) fn(*(from + n), n); } #endif #ifdef LIBNEST2D_THREADING_omp if((policy & std::launch::async) == std::launch::async) { #pragma omp parallel for for(int n = 0; n < int(N); n++) fn(*(from + n), TN(n)); } else { for(TN n = 0; n < N; n++) fn(*(from + n), n); } #endif #ifdef LIBNEST2D_THREADING_std std::vector> rets(N); auto it = from; for(TN b = 0; b < N; b++) { rets[b] = std::async(policy, fn, *it++, unsigned(b)); } for(TN fi = 0; fi < N; ++fi) rets[fi].wait(); #endif } }} #endif //LIBNEST2D_PARALLEL_HPP