ParallelFor Class Reference 线程

#include <parallelfor.h>

详细描述

This class contains of several methods that implement a for loop which is distributing the work load on multiple threads. You can choose between static or dynamic distribution and optionally specify objects to initialize and finalize per worker thread data.

Classes

class   BaseContext
struct   BreakCondition
class   BreakContext
class   Dummy
struct   DynamicContext
class   DynamicJob
struct   ForAlignedContext
struct   ForAlignedContext< FORCONTEXT, INDEXTYPE, Dummy, Dummy >
struct   ForState
class   InvokeSelector
class   NoBreakContext
class   NoContext
class   NoContextSelector
class   ParallelForJob
struct   StaticContext
class   StaticJob

静态公共成员函数

template<typename FROMTYPE , typename INDEXTYPE , typename LOOP >
static auto  Dynamic (FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int granularity= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT ) -> decltype(obj(to))
template<typename CONTEXT , typename FROMTYPE , typename INDEXTYPE , typename LOOP >
static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType  Dynamic (FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int granularity= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT )
template<typename CONTEXT , typename FROMTYPE , typename INDEXTYPE , typename LOOP , typename INIT , typename FINALIZE >
static std::enable_if<! STD_IS_REPLACEMENT (convertible, typename std::remove_reference< LOOP >::type, maxon::Int ), typename CONTEXT::ResultValueType >::type  Dynamic (FROMTYPE from, INDEXTYPE to, const INIT &init, const LOOP &obj, const FINALIZE &finalize, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int granularity= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT )
template<typename CONTEXT , PARALLELFORFLAGS FLAGS, typename FROMTYPE , typename INDEXTYPE , typename LOOP , typename INIT , typename FINALIZE >
static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType  Dynamic (FROMTYPE from, INDEXTYPE to, const INIT &init, const LOOP &obj, const FINALIZE &finalize, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int granularity= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT )
template<typename FROMTYPE , typename INDEXTYPE , typename LOOP >
static auto  Static (FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int minChunkSize= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT ) -> decltype(obj(to))
template<typename CONTEXT , typename FROMTYPE , typename INDEXTYPE , typename LOOP >
static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType  Static (FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int minChunkSize= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT )
template<typename CONTEXT , typename FROMTYPE , typename INDEXTYPE , typename LOOP , typename INIT , typename FINALIZE >
static std::enable_if<! STD_IS_REPLACEMENT (convertible, typename std::remove_reference< LOOP >::type, maxon::Int ), typename CONTEXT::ResultValueType >::type  Static (FROMTYPE from, INDEXTYPE to, const INIT &init, const LOOP &obj, const FINALIZE &finalize, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int minChunkSize= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT )
template<typename CONTEXT , PARALLELFORFLAGS FLAGS, typename FROMTYPE , typename INDEXTYPE , typename LOOP , typename INIT , typename FINALIZE >
static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType  Static (FROMTYPE from, INDEXTYPE to, const INIT &init, const LOOP &obj, const FINALIZE &finalize, Int threadCnt= PARALLELFOR_USEMAXIMUMTHREADS , const Int minChunkSize= PARALLELFOR_DEFAULTGRANULARITY , JobQueueInterface *queue= JOBQUEUE_CURRENT )

Static Private Member Functions

template<typename INDEXTYPE >
static INDEXTYPE  CalculateStaticChunkSize ( UInt cnt, Int &threadCnt, Int minChunkSize)

成员函数文档编制

◆  Dynamic() [1/4]

static auto Dynamic ( FROMTYPE  from ,
INDEXTYPE  to ,
const LOOP &  obj ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   granularity = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
) -> decltype(obj(to)) static

Runs a parallelized for (i = from; i < to; i++) loop using dynamic distribution.

ParallelFor::Dynamic (from, to, []( Int i) { // ... do something ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] obj Lambda or object with operator (), object will be referenced.
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] granularity The number of iterations after which a thread shares work with idle threads (1 means best distribution at the cost of higher synchronization, PARALLELFOR_DEFAULTGRANULARITY is the default).
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE) used for the loop.
返回
Depending on the type of #LOOP either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Dynamic() [2/4]

static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType Dynamic ( FROMTYPE  from ,
INDEXTYPE  to ,
const LOOP &  obj ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   granularity = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
)
static

Runs a parallelized for (i = from; i < to; i++) loop using dynamic distribution.

ParallelFor::Dynamic<ParallelFor::BreakContext>(from, to, [&shouldWeBreak]( Int i, ParallelFor::BreakContext& context) { // ... example for a break condition ... if (shouldWeBreak) { // tell ParallelFor that it should cancel the loop and return from the closure context.Break(); return ; } // ... do something ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] obj Lambda or object with operator (), object will be referenced.
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] granularity The number of iterations after which a thread shares work with idle threads (1 means best distribution at the cost of higher synchronization, PARALLELFOR_DEFAULTGRANULARITY is the default).
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
CONTEXT A class derived from ParallelFor::BaseContext .
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE, CONTEXT&) used for the loop.
返回
Depending on the type of #LOOP and #CONTEXT either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Dynamic() [3/4]

static std::enable_if<! STD_IS_REPLACEMENT (convertible, typename std::remove_reference< LOOP >::type, maxon::Int ), typename CONTEXT::ResultValueType >::type Dynamic ( FROMTYPE  from ,
INDEXTYPE  to ,
const INIT &  init ,
const LOOP &  obj ,
const FINALIZE &  finalize ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   granularity = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
)
static

Runs a parallelized for (i = from; i < to; i++) loop using dynamic distribution. The init method is called concurrently per worker thread whereas the finalize method is called synchronously (no concurrency) after the loop has finished.

struct MyContext : public ParallelFor::BaseContext { // your thread local data ... }; ParallelFor::Dynamic<MyContext>(from, to, [](MyContext& context) { // ... init thread local data ... }, []( Int i, MyContext& context) { // ... computation in the loop body ... }, [](MyContext& context) { // ... finalize thread local data ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] init Lambda being called before the loop starts (threaded)
[in] obj Lambda or object with operator (), object will be referenced.
[in] finalize Lambda being called after the loop has finished (synchronous)
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] granularity The number of iterations after which a thread shares work with idle threads (1 means best distribution at the cost of higher synchronization, PARALLELFOR_DEFAULTGRANULARITY is the default).
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
CONTEXT A class derived from ParallelFor::BaseContext for thread local data storage.
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE, CONTEXT&) used for the loop.
INIT A class containing an operator ()(CONTEXT&) for per-thread initialization before the loop.
FINALIZE A class containing an operator ()(CONTEXT&) for per-thread cleanup after the loop.
返回
Depending on the type of #LOOP and #CONTEXT either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Dynamic() [4/4]

static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType Dynamic ( FROMTYPE  from ,
INDEXTYPE  to ,
const INIT &  init ,
const LOOP &  obj ,
const FINALIZE &  finalize ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   granularity = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
)
static

Runs a parallelized for (i = from; i < to; i++) loop using dynamic distribution. The FLAGS template parameter can be used to specify if the init or finalize closure are called serialized or threaded.

struct MyContext : public ParallelFor::BaseContext { // your thread local data ... }; ParallelFor::Dynamic<MyContext, PARALLELFORFLAGS::INITTHREADED_FINALIZESYNC>(from, to, [](MyContext& context) { // ... init thread local data ... }, []( Int i, MyContext& context) { // ... computation in the loop body ... }, [](MyContext& context) { // ... finalize thread local data ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] init Lambda being called before the loop starts (threaded by default)
[in] obj Lambda or object with operator (), object will be referenced.
[in] finalize Lambda being called after the loop has finished (synchronous by default)
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] granularity The number of iterations after which a thread shares work with idle threads (1 means best distribution at the cost of higher synchronization, PARALLELFOR_DEFAULTGRANULARITY is the default).
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
CONTEXT A class derived from ParallelFor::BaseContext for thread local data storage.
FLAGS Flags that specify whether init or finalize will be called threaded or synchronously.
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE, CONTEXT&) used for the loop.
INIT A class containing an operator ()(CONTEXT&) for per-thread initialization before the loop.
FINALIZE A class containing an operator ()(CONTEXT&) for per-thread cleanup after the loop.
返回
Depending on the type of #LOOP and #CONTEXT either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Static() [1/4]

static auto Static ( FROMTYPE  from ,
INDEXTYPE  to ,
const LOOP &  obj ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   minChunkSize = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
) -> decltype(obj(to)) static

Runs a parallelized for (i = from; i < to; i++) loop using static distribution.

ParallelFor::Static (from, to, []( Int i) { // ... do something ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] obj Lambda or object with operator (), object will be referenced.
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] minChunkSize Minimum number of loops that one thread should handle (default is PARALLELFOR_DEFAULTGRANULARITY)
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE) used for the loop.
返回
Depending on the type of #LOOP either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Static() [2/4]

static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType Static ( FROMTYPE  from ,
INDEXTYPE  to ,
const LOOP &  obj ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   minChunkSize = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
)
static

Runs a parallelized for (i = from; i < to; i++) loop using static distribution.

ParallelFor::Static<ParallelFor::BreakContext>(from, to, [&shouldWeBreak]( Int i, ParallelFor::BreakContext& context) { // ... example for a break condition ... if (shouldWeBreak) { // tell ParallelFor that it should cancel the loop and return from the closure context.Break(); return ; } // ... do something ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] obj Lambda or object with operator (), object will be referenced.
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] minChunkSize Minimum number of loops that one thread should handle (default is PARALLELFOR_DEFAULTGRANULARITY)
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
CONTEXT A class derived from ParallelFor::BaseContext .
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE) used for the loop.
返回
Depending on the type of #LOOP and #CONTEXT either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Static() [3/4]

static std::enable_if<! STD_IS_REPLACEMENT (convertible, typename std::remove_reference< LOOP >::type, maxon::Int ), typename CONTEXT::ResultValueType >::type Static ( FROMTYPE  from ,
INDEXTYPE  to ,
const INIT &  init ,
const LOOP &  obj ,
const FINALIZE &  finalize ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   minChunkSize = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
)
static

Runs a parallelized for (i = from; i < to; i++) loop using static distribution. The init method is called concurrently per worker thread whereas the finalize method is called synchronously (no concurrency) after the loop has finished.

struct MyContext : public ParallelFor::BaseContext { // your thread local data ... }; ParallelFor::Static<MyContext>(from, to, [](MyContext& context) { // ... init thread local data ... }, []( Int i, MyContext& context) { // ... computation in the loop body ... }, [](MyContext& context) { // ... finalize thread local data ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] init Lambda being called before the loop starts (threaded)
[in] obj Lambda or object with operator (), object will be referenced.
[in] finalize Lambda being called after the loop has finished (synchronously)
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] minChunkSize Minimum number of loops that one thread should handle (default is PARALLELFOR_DEFAULTGRANULARITY)
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
CONTEXT A class derived from ParallelFor::BaseContext for thread local data storage.
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE, CONTEXT&) used for the loop.
INIT A class containing an operator ()(CONTEXT&) for per-thread initialization before the loop.
FINALIZE A class containing an operator ()(CONTEXT&) for per-thread cleanup after the loop.
返回
Depending on the type of #LOOP and #CONTEXT either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  Static() [4/4]

static MAXON_ATTRIBUTE_NO_INLINE CONTEXT::ResultValueType Static ( FROMTYPE  from ,
INDEXTYPE  to ,
const INIT &  init ,
const LOOP &  obj ,
const FINALIZE &  finalize ,
Int   threadCnt = PARALLELFOR_USEMAXIMUMTHREADS ,
const Int   minChunkSize = PARALLELFOR_DEFAULTGRANULARITY ,
JobQueueInterface queue = JOBQUEUE_CURRENT  
)
static

Runs a parallelized for (i = from; i < to; i++) loop using static distribution. The FLAGS template parameter can be used to specify if the init or finalize closure are called serialized or threaded.

struct MyContext : public ParallelFor::BaseContext { // your thread local data ... }; ParallelFor::Dynamic<MyContext, PARALLELFORFLAGS::INITTHREADED_FINALIZESYNC>(from, to, [](MyContext& context) { // ... init thread local data ... }, []( Int i, MyContext& context) { // ... computation in the loop body ... }, [](MyContext& context) { // ... finalize thread local data ... });
参数
[in] from Start index.
[in] to End index (excluded)
[in] init Lambda being called before the loop starts (threaded by default)
[in] obj Lambda or object with operator (), object will be referenced.
[in] finalize Lambda being called after the loop has finished (synchronously by default)
[in] threadCnt PARALLELFOR_USEMAXIMUMTHREADS for default handling (uses the maximum number of threads available), otherwise maximum number of threads to be used. A value of 1 will disable parallelization.
[in] minChunkSize Minimum number of loops that one thread should handle (default is PARALLELFOR_DEFAULTGRANULARITY)
[in] queue Optional queue that the parallel for is executed within.
Template Parameters
CONTEXT A class derived from ParallelFor::BaseContext for thread local data storage.
FLAGS Flags that specify whether init or finalize will be called threaded or synchronously.
FROMTYPE An integral type used for the from variable, might be different than INDEXTYPE but is not allowed to have a bigger range.
INDEXTYPE An integral type used for the index of the loop.
LOOP A class containing an operator ()(INDEXTYPE, CONTEXT&) used for the loop.
INIT A class containing an operator ()(CONTEXT&) for per-thread initialization before the loop.
FINALIZE A class containing an operator ()(CONTEXT&) for per-thread cleanup after the loop.
返回
Depending on the type of #LOOP and #CONTEXT either void or Result<void> (OK on success. Will only return an error if your loop object returns one). Any errors will be returned as an AggregatedError which you can iterate to check for individual errors.

◆  CalculateStaticChunkSize()

static INDEXTYPE CalculateStaticChunkSize ( UInt   cnt ,
Int threadCnt ,
Int   minChunkSize  
)
static private

Computes chunk size for static distribution. If only one thread is used, the index is signed and the range of the loop requires an unsigned counter the return value will overflow, but the loops that assign the chunks to the jobs take care of this.

参数
[in] cnt Number of iterations.
[in,out] threadCnt Number of threads to use.
[in] minChunkSize Minimum chunk size.
Template Parameters
INDEXTYPE An integral type used for the index of the loop.
返回
Size of a chunk.
Int
maxon::Int Int
定义: ge_sys_math.h:62
maxon::ParallelFor::Dynamic
static auto Dynamic(FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt=PARALLELFOR_USEMAXIMUMTHREADS, const Int granularity=PARALLELFOR_DEFAULTGRANULARITY, JobQueueInterface *queue=JOBQUEUE_CURRENT) -> decltype(obj(to))
定义: parallelfor.h:238
maxon::ParallelFor::Static
static auto Static(FROMTYPE from, INDEXTYPE to, const LOOP &obj, Int threadCnt=PARALLELFOR_USEMAXIMUMTHREADS, const Int minChunkSize=PARALLELFOR_DEFAULTGRANULARITY, JobQueueInterface *queue=JOBQUEUE_CURRENT) -> decltype(obj(to))
定义: parallelfor.h:393