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)
129 const auto & no_op = [](
const size_t ){};
131 const auto & wrapper = [&func](Index i,
size_t ){ func(i); };
132 return parallel_for(loop_size,no_op,wrapper,no_op,min_parallel);
137 typename PreFunctionType,
138 typename FunctionType,
139 typename AccumFunctionType>
141 const Index loop_size,
142 const PreFunctionType & prep_func,
143 const FunctionType & func,
144 const AccumFunctionType & accum_func,
145 const size_t min_parallel)
147 assert(loop_size>=0);
148 if(loop_size==0)
return false;
151#ifdef IGL_PARALLEL_FOR_FORCE_SERIAL
152 const size_t nthreads = 1;
156 if(loop_size<min_parallel || nthreads<=1)
160 for(Index i = 0;i<loop_size;i++) func(i,0);
168 (Index)std::round((loop_size+1)/
static_cast<double>(nthreads)),(Index)1);
171 const auto & range = [&func](
const Index k1,
const Index k2,
const size_t t)
173 for(Index k = k1; k < k2; k++) func(k,t);
177 std::vector<std::thread> pool;
178 pool.reserve(nthreads);
181 Index i2 = std::min(0 + slice, loop_size);
184 for (; t+1 < nthreads && i1 < loop_size; ++t)
186 pool.emplace_back(range, i1, i2, t);
188 i2 = std::min(i2 + slice, loop_size);
192 pool.emplace_back(range, i1, loop_size, t);
196 for (std::thread &t : pool)
if (t.joinable()) t.join();
198 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.