Skip to content
Snippets Groups Projects
Commit 4800e373 authored by Xavier Valls Pla's avatar Xavier Valls Pla Committed by Danilo Piparo
Browse files

Add a Foreach signature to TThreadExecutor

This will allow the parallel execution of void returning functions
parent 4047a9e1
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,17 @@ namespace ROOT {
TThreadExecutor(TThreadExecutor &) = delete;
TThreadExecutor &operator=(TThreadExecutor &) = delete;
template<class F>
void Foreach(F func, unsigned nTimes);
template<class F, class INTEGER>
void Foreach(F func, ROOT::TSeq<INTEGER> args);
/// \cond
template<class F, class T>
void Foreach(F func, std::initializer_list<T> args);
/// \endcond
template<class F, class T>
void Foreach(F func, std::vector<T> &args);
template<class F, class Cond = noReferenceCond<F>>
auto Map(F func, unsigned nTimes) -> std::vector<typename std::result_of<F()>::type>;
template<class F, class INTEGER, class Cond = noReferenceCond<F, INTEGER>>
......@@ -91,6 +102,43 @@ namespace ROOT {
/************ TEMPLATE METHODS IMPLEMENTATION ******************/
//////////////////////////////////////////////////////////////////////////
/// Execute func (with no arguments) nTimes in parallel.
/// Functions that take more than zero arguments can be executed (with
/// fixed arguments) by wrapping them in a lambda or with std::bind.
template<class F>
void TThreadExecutor::Foreach(F func, unsigned nTimes) {
ParallelFor(0U, nTimes, 1, [&](unsigned int i){func();});
}
//////////////////////////////////////////////////////////////////////////
/// Execute func in parallel, taking an element of a
/// sequence as argument.
template<class F, class INTEGER>
void TThreadExecutor::Foreach(F func, ROOT::TSeq<INTEGER> args) {
ParallelFor(*args.begin(), *args.end(), 1, [&](unsigned int i){func(i);});
}
/// \cond
//////////////////////////////////////////////////////////////////////////
/// Execute func in parallel, taking an element of a
/// initializer_list as argument.
template<class F, class T>
void TThreadExecutor::Foreach(F func, std::initializer_list<T> args) {
std::vector<T> vargs(std::move(args));
Foreach(func, vargs);
}
/// \endcond
//////////////////////////////////////////////////////////////////////////
/// Execute func in parallel, taking an element of an
/// std::vector as argument.
template<class F, class T>
void TThreadExecutor::Foreach(F func, std::vector<T> &args) {
unsigned int nToProcess = args.size();
ParallelFor(0U, nToProcess, 1, [&](unsigned int i){func(args[i]);});
}
//////////////////////////////////////////////////////////////////////////
/// Execute func (with no arguments) nTimes in parallel.
/// A vector containg executions' results is returned.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment