8#ifndef IGL_PARALLEL_FOR_H
9#define IGL_PARALLEL_FOR_H
60 template<
typename Index,
typename FunctionType >
62 const Index loop_size,
63 const FunctionType & func,
64 const size_t min_parallel=0);
100 typename PrepFunctionType,
101 typename FunctionType,
102 typename AccumFunctionType
105 const Index loop_size,
106 const PrepFunctionType & prep_func,
107 const FunctionType & func,
108 const AccumFunctionType & accum_func,
109 const size_t min_parallel=0);
122template<
typename Index,
typename FunctionType >
124 const Index loop_size,
125 const FunctionType & func,
126 const size_t min_parallel)
130 const auto & no_op = [](
const size_t ){};
132 const auto & wrapper = [&func](Index i,
size_t ){ func(i); };
133 return parallel_for(loop_size,no_op,wrapper,no_op,min_parallel);
138 typename PreFunctionType,
139 typename FunctionType,
140 typename AccumFunctionType>
142 const Index loop_size,
143 const PreFunctionType & prep_func,
144 const FunctionType & func,
145 const AccumFunctionType & accum_func,
146 const size_t min_parallel)
148 assert(loop_size>=0);
149 if(loop_size==0)
return false;
152#ifdef IGL_PARALLEL_FOR_FORCE_SERIAL
153 const size_t nthreads = 1;
157 if(loop_size<min_parallel || nthreads<=1)
161 for(Index i = 0;i<loop_size;i++) func(i,0);
169 (Index)std::round((loop_size+1)/
static_cast<double>(nthreads)),(Index)1);
172 const auto & range = [&func](
const Index k1,
const Index k2,
const size_t t)
174 for(Index k = k1; k < k2; k++) func(k,t);
178 std::vector<std::thread> pool;
179 pool.reserve(nthreads);
182 Index i2 = std::min(0 + slice, loop_size);
185 for (; t+1 < nthreads && i1 < loop_size; ++t)
187 pool.emplace_back(range, i1, i2, t);
189 i2 = std::min(i2 + slice, loop_size);
193 pool.emplace_back(range, i1, loop_size, t);
197 for (std::thread &t : pool) if (t.joinable()) t.join();
199 for(
size_t t = 0;t<nthreads;t++)
bool parallel_for(const Index loop_size, const FunctionType &func, const size_t min_parallel=0)
Functional implementation of a basic, open-mp style, parallel for loop.
Definition parallel_for.h:123
void slice(const Eigen::SparseMatrix< TX > &X, const Eigen::DenseBase< DerivedR > &R, const Eigen::DenseBase< DerivedC > &C, Eigen::SparseMatrix< TY > &Y)
Act like the matlab X(row_indices,col_indices) operator, where row_indices, col_indices are non-negat...
unsigned int default_num_threads(unsigned int force_num_threads=0)
Returns the default number of threads used in libigl.