VTK  9.4.20241217
Classes | Static Public Member Functions | Static Public Attributes | List of all members
vtkSMPTools Class Reference

A set of parallel (multi-threaded) utility functions. More...

#include <vtkSMPTools.h>

Classes

struct  Config
 Structure used to specify configuration for LocalScope() method. More...
 

Static Public Member Functions

static const char * GetBackend ()
 Get the backend in use.
 
static bool SetBackend (const char *backend)
 /!\ This method is not thread safe.
 
static void Initialize (int numThreads=0)
 /!\ This method is not thread safe.
 
static int GetEstimatedNumberOfThreads ()
 Get the estimated number of threads being used by the backend.
 
static int GetEstimatedDefaultNumberOfThreads ()
 Get the estimated number of threads being used by the backend by default.
 
static void SetNestedParallelism (bool isNested)
 /!\ This method is not thread safe.
 
static bool GetNestedParallelism ()
 Get true if the nested parallelism is enabled.
 
static bool IsParallelScope ()
 Return true if it is called from a parallel scope.
 
static bool GetSingleThread ()
 Returns true if the given thread is specified thread for single scope.
 
template<typename T >
static void LocalScope (Config const &config, T &&lambda)
 /!\ This method is not thread safe.
 
template<typename InputIt , typename OutputIt , typename Functor >
static void Transform (InputIt inBegin, InputIt inEnd, OutputIt outBegin, Functor transform)
 A convenience method for transforming data.
 
template<typename InputIt1 , typename InputIt2 , typename OutputIt , typename Functor >
static void Transform (InputIt1 inBegin1, InputIt1 inEnd, InputIt2 inBegin2, OutputIt outBegin, Functor transform)
 A convenience method for transforming data.
 
template<typename Iterator , typename T >
static void Fill (Iterator begin, Iterator end, const T &value)
 A convenience method for filling data.
 
template<typename RandomAccessIterator >
static void Sort (RandomAccessIterator begin, RandomAccessIterator end)
 A convenience method for sorting data.
 
template<typename RandomAccessIterator , typename Compare >
static void Sort (RandomAccessIterator begin, RandomAccessIterator end, Compare comp)
 A convenience method for sorting data.
 
template<typename Functor >
static void For (vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
 Execute a for operation in parallel.
 
template<typename Functor >
static void For (vtkIdType first, vtkIdType last, vtkIdType grain, Functor const &f)
 Execute a for operation in parallel.
 
template<typename Functor >
static void For (vtkIdType first, vtkIdType last, Functor &f)
 Execute a for operation in parallel.
 
template<typename Functor >
static void For (vtkIdType first, vtkIdType last, Functor const &f)
 Execute a for operation in parallel.
 
template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > For (Iter begin, Iter end, vtkIdType grain, Functor &f)
 Execute a for operation in parallel.
 
template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > For (Iter begin, Iter end, vtkIdType grain, Functor const &f)
 Execute a for operation in parallel.
 
template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > For (Iter begin, Iter end, Functor &f)
 Execute a for operation in parallel.
 
template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > For (Iter begin, Iter end, Functor const &f)
 Execute a for operation in parallel.
 

Static Public Attributes

static constexpr vtkIdType THRESHOLD = 100000
 This is an empirically determined threshold used by various cases to switch between serial and threaded execution.
 

Detailed Description

A set of parallel (multi-threaded) utility functions.

vtkSMPTools provides a set of utility functions that can be used to parallelize parts of VTK code using multiple threads. There are several back-end implementations of parallel functionality (currently Sequential, TBB, OpenMP and STDThread) that actual execution is delegated to.

See also
vtkSMPThreadLocal vtkSMPThreadLocalObject
Examples:
vtkSMPTools (Examples)
Tests:
vtkSMPTools (Tests)

Definition at line 223 of file vtkSMPTools.h.

Member Function Documentation

◆ For() [1/8]

template<typename Functor >
static void vtkSMPTools::For ( vtkIdType  first,
vtkIdType  last,
vtkIdType  grain,
Functor &  f 
)
inlinestatic

Execute a for operation in parallel.

First and last define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. The grain gives the parallel engine a hint about the coarseness over which to parallelize the function (as defined by last-first of each execution of operator() ).

Definition at line 237 of file vtkSMPTools.h.

◆ For() [2/8]

template<typename Functor >
static void vtkSMPTools::For ( vtkIdType  first,
vtkIdType  last,
vtkIdType  grain,
Functor const &  f 
)
inlinestatic

Execute a for operation in parallel.

First and last define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. The grain gives the parallel engine a hint about the coarseness over which to parallelize the function (as defined by last-first of each execution of operator() ).

Definition at line 244 of file vtkSMPTools.h.

◆ For() [3/8]

template<typename Functor >
static void vtkSMPTools::For ( vtkIdType  first,
vtkIdType  last,
Functor &  f 
)
inlinestatic

Execute a for operation in parallel.

First and last define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. The grain gives the parallel engine a hint about the coarseness over which to parallelize the function (as defined by last-first of each execution of operator() ). Uses a default value for the grain.

Definition at line 262 of file vtkSMPTools.h.

◆ For() [4/8]

template<typename Functor >
static void vtkSMPTools::For ( vtkIdType  first,
vtkIdType  last,
Functor const &  f 
)
inlinestatic

Execute a for operation in parallel.

First and last define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. The grain gives the parallel engine a hint about the coarseness over which to parallelize the function (as defined by last-first of each execution of operator() ). Uses a default value for the grain.

Definition at line 268 of file vtkSMPTools.h.

◆ For() [5/8]

template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > vtkSMPTools::For ( Iter  begin,
Iter  end,
vtkIdType  grain,
Functor &  f 
)
inlinestatic

Execute a for operation in parallel.

Begin and end iterators define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. The grain gives the parallel engine a hint about the coarseness over which to parallelize the function (as defined by last-first of each execution of operator() ).

Usage example:

template<class IteratorT>
class ExampleFunctor
{
void operator()(IteratorT begin, IteratorT end)
{
for (IteratorT it = begin; it != end; ++it)
{
// Do stuff
}
}
};
ExampleFunctor<std::set<int>::iterator> worker;
vtkSMPTools::For(container.begin(), container.end(), 5, worker);
static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor &f)
Execute a for operation in parallel.

Lambda are also supported through Functor const& function overload:

vtkSMPTools::For(container.begin(), container.end(), 5,
[](std::set<int>::iterator begin, std::set<int>::iterator end) {
// Do stuff
});

Definition at line 311 of file vtkSMPTools.h.

◆ For() [6/8]

template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > vtkSMPTools::For ( Iter  begin,
Iter  end,
vtkIdType  grain,
Functor const &  f 
)
inlinestatic

Execute a for operation in parallel.

Begin and end iterators define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. The grain gives the parallel engine a hint about the coarseness over which to parallelize the function (as defined by last-first of each execution of operator() ).

Usage example:

template<class IteratorT>
class ExampleFunctor
{
void operator()(IteratorT begin, IteratorT end)
{
for (IteratorT it = begin; it != end; ++it)
{
// Do stuff
}
}
};
ExampleFunctor<std::set<int>::iterator> worker;
vtkSMPTools::For(container.begin(), container.end(), 5, worker);

Lambda are also supported through Functor const& function overload:

vtkSMPTools::For(container.begin(), container.end(), 5,
[](std::set<int>::iterator begin, std::set<int>::iterator end) {
// Do stuff
});

Definition at line 320 of file vtkSMPTools.h.

◆ For() [7/8]

template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > vtkSMPTools::For ( Iter  begin,
Iter  end,
Functor &  f 
)
inlinestatic

Execute a for operation in parallel.

Begin and end iterators define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. Uses a default value for the grain.

Usage example:

template<class IteratorT>
class ExampleFunctor
{
void operator()(IteratorT begin, IteratorT end)
{
for (IteratorT it = begin; it != end; ++it)
{
// Do stuff
}
}
};
ExampleFunctor<std::set<int>::iterator> worker;
vtkSMPTools::For(container.begin(), container.end(), worker);

Lambda are also supported through Functor const& function overload:

vtkSMPTools::For(container.begin(), container.end(),
[](std::set<int>::iterator begin, std::set<int>::iterator end) {
// Do stuff
});

Definition at line 364 of file vtkSMPTools.h.

◆ For() [8/8]

template<typename Iter , typename Functor >
static vtk::detail::smp::resolvedNotInt< Iter > vtkSMPTools::For ( Iter  begin,
Iter  end,
Functor const &  f 
)
inlinestatic

Execute a for operation in parallel.

Begin and end iterators define the range over which to operate (which is defined by the operator). The operation executed is defined by operator() of the functor object. Uses a default value for the grain.

Usage example:

template<class IteratorT>
class ExampleFunctor
{
void operator()(IteratorT begin, IteratorT end)
{
for (IteratorT it = begin; it != end; ++it)
{
// Do stuff
}
}
};
ExampleFunctor<std::set<int>::iterator> worker;
vtkSMPTools::For(container.begin(), container.end(), worker);

Lambda are also supported through Functor const& function overload:

vtkSMPTools::For(container.begin(), container.end(),
[](std::set<int>::iterator begin, std::set<int>::iterator end) {
// Do stuff
});

Definition at line 370 of file vtkSMPTools.h.

◆ GetBackend()

static const char * vtkSMPTools::GetBackend ( )
static

Get the backend in use.

◆ SetBackend()

static bool vtkSMPTools::SetBackend ( const char *  backend)
static

/!\ This method is not thread safe.

Change the backend in use. The options can be: "Sequential", "STDThread", "TBB" or "OpenMP"

VTK_SMP_BACKEND_IN_USE env variable can also be used to set the default SMPTools backend, in that case SetBackend() doesn't need to be called. The backend selected with SetBackend() have the priority over VTK_SMP_BACKEND_IN_USE.

SetBackend() will return true if the backend was found and available.

◆ Initialize()

static void vtkSMPTools::Initialize ( int  numThreads = 0)
static

/!\ This method is not thread safe.

Initialize the underlying libraries for execution. This is not required as it is automatically defined by the libraries. However, it can be used to control the maximum number of thread used. Make sure to call it before the parallel operation.

If Initialize is called without argument it will reset to the maximum number of threads or use the VTK_SMP_MAX_THREADS env variable if it is defined.

Note: If VTK_SMP_MAX_THREADS env variable is defined the SMPTools will try to use it to set the maximum number of threads. Initialize() doesn't need to be called.

◆ GetEstimatedNumberOfThreads()

static int vtkSMPTools::GetEstimatedNumberOfThreads ( )
static

Get the estimated number of threads being used by the backend.

This should be used as just an estimate since the number of threads may vary dynamically and a particular task may not be executed on all the available threads.

◆ GetEstimatedDefaultNumberOfThreads()

static int vtkSMPTools::GetEstimatedDefaultNumberOfThreads ( )
static

Get the estimated number of threads being used by the backend by default.

This should be used as just an estimate since the number of threads may vary dynamically and a particular task may not be executed on all the available threads.

◆ SetNestedParallelism()

static void vtkSMPTools::SetNestedParallelism ( bool  isNested)
static

/!\ This method is not thread safe.

If true enable nested parallelism for underlying backends. When enabled the comportement is different for each backend:

  • TBB support nested parallelism using a single thread pool
  • For OpenMP, set omp_set_nested to the value of isNested.
  • For STDThread nested parallelism implies creating new threads pools.
  • For Sequential nothing changes.

Default to false except for TBB.

◆ GetNestedParallelism()

static bool vtkSMPTools::GetNestedParallelism ( )
static

Get true if the nested parallelism is enabled.

By default, nested parallelism is enabled only for TBB.

◆ IsParallelScope()

static bool vtkSMPTools::IsParallelScope ( )
static

Return true if it is called from a parallel scope.

◆ GetSingleThread()

static bool vtkSMPTools::GetSingleThread ( )
static

Returns true if the given thread is specified thread for single scope.

Returns false otherwise.

◆ LocalScope()

template<typename T >
static void vtkSMPTools::LocalScope ( Config const &  config,
T &&  lambda 
)
inlinestatic

/!\ This method is not thread safe.

Change the number of threads locally within this scope and call a functor which should contains a vtkSMPTools method.

Usage example:

vtkSMPTools::Config{ 4, "OpenMP", false }, [&]() { vtkSMPTools::For(0, size, worker); });
static void LocalScope(Config const &config, T &&lambda)
/!\ This method is not thread safe.
Structure used to specify configuration for LocalScope() method.

Definition at line 535 of file vtkSMPTools.h.

◆ Transform() [1/2]

template<typename InputIt , typename OutputIt , typename Functor >
static void vtkSMPTools::Transform ( InputIt  inBegin,
InputIt  inEnd,
OutputIt  outBegin,
Functor  transform 
)
inlinestatic

A convenience method for transforming data.

It is a drop in replacement for std::transform(), it does a unary operation on the input ranges. The data array must have the same length. The performed transformation is defined by operator() of the functor object.

Usage example with vtkDataArray:

const auto range0 = vtk::DataArrayValueRange<1>(array0);
auto range1 = vtk::DataArrayValueRange<1>(array1);
range0.cbegin(), range0.cend(), range1.begin(), [](double x) { return x - 1; });
static void Transform(InputIt inBegin, InputIt inEnd, OutputIt outBegin, Functor transform)
A convenience method for transforming data.

Please visit vtkDataArrayRange.h documentation for more information and optimisation.

Definition at line 557 of file vtkSMPTools.h.

◆ Transform() [2/2]

template<typename InputIt1 , typename InputIt2 , typename OutputIt , typename Functor >
static void vtkSMPTools::Transform ( InputIt1  inBegin1,
InputIt1  inEnd,
InputIt2  inBegin2,
OutputIt  outBegin,
Functor  transform 
)
inlinestatic

A convenience method for transforming data.

It is a drop in replacement for std::transform(), it does a binary operation on the input ranges. The data array must have the same length. The performed transformation is defined by operator() of the functor object.

Usage example with vtkDataArray:

const auto range0 = vtk::DataArrayValueRange<1>(array0);
auto range1 = vtk::DataArrayValueRange<1>(array1);
range0.cbegin(), range0.cend(), range1.cbegin(), range1.begin(),
[](double x, double y) { return x * y; });

Please visit vtkDataArrayRange.h documentation for more information and optimisation.

Definition at line 580 of file vtkSMPTools.h.

◆ Fill()

template<typename Iterator , typename T >
static void vtkSMPTools::Fill ( Iterator  begin,
Iterator  end,
const T &  value 
)
inlinestatic

A convenience method for filling data.

It is a drop in replacement for std::fill(), it assign the given value to the element in ranges.

Usage example with vtkDataArray:

// Fill range with its first tuple value
auto range = vtk::DataArrayTupleRange<1>(array);
const auto value = *range.begin();
vtkSMPTools::Fill(range.begin(), range.end(), value);
static void Fill(Iterator begin, Iterator end, const T &value)
A convenience method for filling data.

Please visit vtkDataArrayRange.h documentation for more information and optimisation.

Definition at line 602 of file vtkSMPTools.h.

◆ Sort() [1/2]

template<typename RandomAccessIterator >
static void vtkSMPTools::Sort ( RandomAccessIterator  begin,
RandomAccessIterator  end 
)
inlinestatic

A convenience method for sorting data.

It is a drop in replacement for std::sort(). Under the hood different methods are used. For example, tbb::parallel_sort is used in TBB.

Definition at line 614 of file vtkSMPTools.h.

◆ Sort() [2/2]

template<typename RandomAccessIterator , typename Compare >
static void vtkSMPTools::Sort ( RandomAccessIterator  begin,
RandomAccessIterator  end,
Compare  comp 
)
inlinestatic

A convenience method for sorting data.

It is a drop in replacement for std::sort(). Under the hood different methods are used. For example, tbb::parallel_sort is used in TBB. This version of Sort() takes a comparison class.

Definition at line 627 of file vtkSMPTools.h.

Member Data Documentation

◆ THRESHOLD

constexpr vtkIdType vtkSMPTools::THRESHOLD = 100000
staticconstexpr

This is an empirically determined threshold used by various cases to switch between serial and threaded execution.

There is a startup cost to threading which is not worth it for small numbers of elements. This variable should be used only for small/light computations.

The developer can use this value by choosing one of the following options:

  1. In the first option, the value is used as the grain size for the parallel operation:
    static constexpr vtkIdType THRESHOLD
    This is an empirically determined threshold used by various cases to switch between serial and thread...
  2. In the second option, an explicit branch allows for backend control
    {
    // Do the work in serial
    }
    else
    {
    // Do the work in parallel
    }

Definition at line 521 of file vtkSMPTools.h.


The documentation for this class was generated from the following file: