JDFTx  1.2.0
Parallelization utilities

Files

file  Thread.h
 Utilities for threading (wrappers around std::thread)
 

Classes

class  AutoThreadCount
 Maintain thread timing statistics and automatically choose the optimum number of threads. More...
 

Functions

bool shouldThreadOperators ()
 
void suspendOperatorThreading ()
 call from multi-threaded top-level code to disable threading within operators called from a parallel section
 
void resumeOperatorThreading ()
 call after a parallel section in top-level code to resume threading within subsequent operator calls
 
template<typename Callable , typename... Args>
void threadLaunch (int nThreads, Callable *func, size_t nJobs, Args...args)
 A simple utility for running muliple threads. More...
 
template<typename Callable , typename... Args>
void threadLaunch (Callable *func, size_t nJobs, Args...args)
 
template<typename Callable , typename... Args>
void threadLaunch (AutoThreadCount *, Callable *func, size_t nJobs, Args...args)
 
template<typename Callable , typename... Args>
void threadedLoop (Callable *func, size_t nIter, Args...args)
 A parallelized loop. More...
 
template<typename Callable , typename... Args>
double threadedAccumulate (Callable *func, size_t nIter, Args...args)
 A parallelized loop with an accumulated return value. More...
 

Variables

int nProcsAvailable
 number of available processors (initialized to number of online processors, can be overriden)
 

Detailed Description

Function Documentation

bool shouldThreadOperators ( )

Operators should run multithreaded if this returns true, and should run in a single thread if this returns false. Note that all the thread launching functions in this file automatically prevent nested threading, so operator codes using those functions need not explicitly check this.

This only affects CPU threading, GPU operators should only be called from a single thread anyway.

template<typename Callable , typename... Args>
double threadedAccumulate ( Callable *  func,
size_t  nIter,
Args...  args 
)

A parallelized loop with an accumulated return value.

Same as threadedLoop, but func() returns double, which is summed over and returned

Returns
Accumulated return value of all calls to func()
template<typename Callable , typename... Args>
void threadedLoop ( Callable *  func,
size_t  nIter,
Args...  args 
)

A parallelized loop.

Given a callable object func and an argument list args, this routine calls func(i, args) for each i in [0:nIter-1], and accumulates the return values.

Note that the calls are made from multiple threads, so func must be thread safe. (Hint: pass mutexes as a part of args if synchronization is required).

As many threads as online processors are launched and the nIter iterations are evenly split between all the threads. Threaded loops will become single threaded if suspendOperatorThreading().

Parameters
funcThe function / object with operator() to be looped over
nIterThe number of loop 'iterations'
argsArguments to pass to func
template<typename Callable , typename... Args>
void threadLaunch ( int  nThreads,
Callable *  func,
size_t  nJobs,
Args...  args 
)

A simple utility for running muliple threads.

Given a callable object func and an argument list args, this routine calls nThreads threads of func invoked as func(iMin, iMax, args). The nJobs jobs are evenly split between all the threads; each instance of func should handle job index i satisfying iMin <= i < iMax.

If nJobs <= 0, the behaviour changes: the function is invoked as func(iThread, nThreads, args) instead, where 0 <= iThread < nThreads. This mode allows for more flexible threading than the evenly split job management indicated above. This could be used as a convenient interface for launching threads for any parallel routine requiring as many threads as processors.

Parameters
nThreadsNumber of threads to launch (if <=0, as many as processors on system)
funcThe function / object with operator() to invoke in a multithreaded fashion
nJobsThe number of jobs to be split between the various func threads
argsArguments to pass to func
template<typename Callable , typename... Args>
void threadLaunch ( Callable *  func,
size_t  nJobs,
Args...  args 
)

Same as threadLaunch(int nThreads, Callable* func, size_t nJobs, Args... args) with nThreads = number of online processors.

template<typename Callable , typename... Args>
void threadLaunch ( AutoThreadCount ,
Callable *  func,
size_t  nJobs,
Args...  args 
)

Same as threadLaunch(int nThreads, Callable* func, size_t nJobs, Args... args) but with nThreads automatically adjusted over multiple runs using an AutoThreadCount object If the AutoThreadCount pointer is null, as many threads as processors will be launched