20 #ifndef JDFTX_CORE_THREAD_H 21 #define JDFTX_CORE_THREAD_H 71 template<
typename Callable,
typename ... Args>
72 void threadLaunch(
int nThreads, Callable* func,
size_t nJobs, Args... args);
78 template<
typename Callable,
typename ... Args>
79 void threadLaunch(Callable* func,
size_t nJobs, Args... args);
90 AutoThreadCount(
int minThreads=1,
int minStats=3,
const char* name=0, FILE* fpLog=stdout);
93 template<
typename Callable,
typename ... Args>
97 void submitTime(
int,
double);
98 double* time;
int* count;
int minThreads, nMax, nOpt, minStats;
bool settled;
99 char* name; FILE* fpLog;
107 template<
typename Callable,
typename ... Args>
128 template<
typename Callable,
typename ... Args>
129 void threadedLoop(Callable* func,
size_t nIter, Args... args);
138 template<
typename Callable,
typename ... Args>
149 template<
typename Callable,
typename ... Args>
150 void threadLaunch(
int nThreads, Callable* func,
size_t nJobs, Args... args)
153 std::thread** tArr =
new std::thread*[nThreads-1];
154 for(
int t=0; t<nThreads; t++)
155 {
size_t i1 = (nJobs>0 ? ( t * nJobs)/nThreads : t);
156 size_t i2 = (nJobs>0 ? ((t+1) * nJobs)/nThreads : nThreads);
157 if(t<nThreads-1) tArr[t] =
new std::thread(func, i1, i2, args...);
158 else (*func)(i1, i2, args...);
160 for(
int t=0; t<nThreads-1; t++)
168 template<
typename Callable,
typename ... Args>
169 void threadLaunch(Callable* func,
size_t nJobs, Args... args)
174 template<
typename Callable,
typename ... Args>
179 {
int nThreads = atc->getThreadCount();
180 double runTime = clock_us();
182 runTime = clock_us() - runTime;
183 atc->submitTime(nThreads, runTime);
188 template<
typename Callable,
typename ... Args>
189 void threadedLoop_sub(
size_t iMin,
size_t iMax, Callable* func, Args... args)
190 {
for(
size_t i=iMin; i<iMax; i++) (*func)(i, args...);
192 template<
typename Callable,
typename ... Args>
193 void threadedLoop(Callable* func,
size_t nIter, Args... args)
194 {
threadLaunch(threadedLoop_sub<Callable,Args...>, nIter, func, args...);
197 template<
typename Callable,
typename ... Args>
198 void threadedAccumulate_sub(
size_t iMin,
size_t iMax, Callable* func,
double* accumTot, std::mutex* m, Args... args)
200 for(
size_t i=iMin; i<iMax; i++) accum += (*func)(i, args...);
201 m->lock(); *accumTot += accum; m->unlock();
203 template<
typename Callable,
typename ... Args>
205 {
double accumTot=0.0;
207 threadLaunch(threadedAccumulate_sub<Callable,Args...>, nIter, func, &accumTot, &m, args...);
212 #endif // JDFTX_CORE_THREAD_H friend void threadLaunch(AutoThreadCount *, Callable *, size_t, Args...args)
bool shouldThreadOperators()
void threadLaunch(int nThreads, Callable *func, size_t nJobs, Args...args)
A simple utility for running muliple threads.
void threadedLoop(Callable *func, size_t nIter, Args...args)
A parallelized loop.
void suspendOperatorThreading()
call from multi-threaded top-level code to disable threading within operators called from a parallel ...
Maintain thread timing statistics and automatically choose the optimum number of threads.
Definition: Thread.h:83
AutoThreadCount(int minThreads=1, int minStats=3, const char *name=0, FILE *fpLog=stdout)
int nProcsAvailable
number of available processors (initialized to number of online processors, can be overriden) ...
double threadedAccumulate(Callable *func, size_t nIter, Args...args)
A parallelized loop with an accumulated return value.
void resumeOperatorThreading()
call after a parallel section in top-level code to resume threading within subsequent operator calls ...