| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifndef vtkSMPTools_h |
| | # define vtkSMPTools_h |
| |
|
| | # include "vtkCommonCoreModule.h" |
| | # include "vtkObject.h" |
| |
|
| | # include "SMP/Common/vtkSMPToolsAPI.h" |
| | # include "vtkSMPThreadLocal.h" |
| |
|
| | # include <functional> |
| | # include <iterator> |
| | # include <type_traits> |
| |
|
| | # ifndef DOXYGEN_SHOULD_SKIP_THIS |
| | namespace vtk |
| | { |
| | namespace detail |
| | { |
| | namespace smp |
| | { |
| | template<typename T> |
| | class vtkSMPTools_Has_Initialize |
| | { |
| | typedef char (&no_type)[1]; |
| | typedef char (&yes_type)[2]; |
| | template<typename U, void (U::*)()> |
| | struct V |
| | { |
| | }; |
| | template<typename U> |
| | static yes_type check(V<U, &U::Initialize>*); |
| | template<typename U> |
| | static no_type check(...); |
| |
|
| | public: |
| | static bool const value = sizeof(check<T>(nullptr)) == sizeof(yes_type); |
| | }; |
| |
|
| | template<typename T> |
| | class vtkSMPTools_Has_Initialize_const |
| | { |
| | typedef char (&no_type)[1]; |
| | typedef char (&yes_type)[2]; |
| | template<typename U, void (U::*)() const> |
| | struct V |
| | { |
| | }; |
| | template<typename U> |
| | static yes_type check(V<U, &U::Initialize>*); |
| | template<typename U> |
| | static no_type check(...); |
| |
|
| | public: |
| | static bool const value = sizeof(check<T>(0)) == sizeof(yes_type); |
| | }; |
| |
|
| | template<typename Functor, bool Init> |
| | struct vtkSMPTools_FunctorInternal; |
| |
|
| | template<typename Functor> |
| | struct vtkSMPTools_FunctorInternal<Functor, false> |
| | { |
| | Functor& F; |
| | vtkSMPTools_FunctorInternal(Functor& f) |
| | : F(f) |
| | {} |
| | void Execute(vtkIdType first, vtkIdType last) |
| | { |
| | this->F(first, last); |
| | } |
| | void For(vtkIdType first, vtkIdType last, vtkIdType grain) |
| | { |
| | auto& SMPToolsAPI = vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.For(first, last, grain, *this); |
| | } |
| | vtkSMPTools_FunctorInternal<Functor, false>& operator=( |
| | const vtkSMPTools_FunctorInternal<Functor, false>& |
| | ); |
| | vtkSMPTools_FunctorInternal(const vtkSMPTools_FunctorInternal<Functor, false>&); |
| | }; |
| |
|
| | template<typename Functor> |
| | struct vtkSMPTools_FunctorInternal<Functor, true> |
| | { |
| | Functor& F; |
| | vtkSMPThreadLocal<unsigned char> Initialized; |
| | vtkSMPTools_FunctorInternal(Functor& f) |
| | : F(f) |
| | , Initialized(0) |
| | {} |
| | void Execute(vtkIdType first, vtkIdType last) |
| | { |
| | unsigned char& inited = this->Initialized.Local(); |
| | if (!inited) { |
| | this->F.Initialize(); |
| | inited = 1; |
| | } |
| | this->F(first, last); |
| | } |
| | void For(vtkIdType first, vtkIdType last, vtkIdType grain) |
| | { |
| | auto& SMPToolsAPI = vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.For(first, last, grain, *this); |
| | this->F.Reduce(); |
| | } |
| | vtkSMPTools_FunctorInternal<Functor, true>& operator=( |
| | const vtkSMPTools_FunctorInternal<Functor, true>& |
| | ); |
| | vtkSMPTools_FunctorInternal(const vtkSMPTools_FunctorInternal<Functor, true>&); |
| | }; |
| |
|
| | template<typename Functor> |
| | class vtkSMPTools_Lookup_For |
| | { |
| | static bool const init = vtkSMPTools_Has_Initialize<Functor>::value; |
| |
|
| | public: |
| | typedef vtkSMPTools_FunctorInternal<Functor, init> type; |
| | }; |
| |
|
| | template<typename Functor> |
| | class vtkSMPTools_Lookup_For<Functor const> |
| | { |
| | static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value; |
| |
|
| | public: |
| | typedef vtkSMPTools_FunctorInternal<Functor const, init> type; |
| | }; |
| |
|
| | template<typename Iterator, typename Functor, bool Init> |
| | struct vtkSMPTools_RangeFunctor; |
| |
|
| | template<typename Iterator, typename Functor> |
| | struct vtkSMPTools_RangeFunctor<Iterator, Functor, false> |
| | { |
| | Functor& F; |
| | Iterator& Begin; |
| | vtkSMPTools_RangeFunctor(Iterator& begin, Functor& f) |
| | : F(f) |
| | , Begin(begin) |
| | {} |
| | void operator()(vtkIdType first, vtkIdType last) |
| | { |
| | Iterator itFirst(Begin); |
| | std::advance(itFirst, first); |
| | Iterator itLast(itFirst); |
| | std::advance(itLast, last - first); |
| | this->F(itFirst, itLast); |
| | } |
| | }; |
| |
|
| | template<typename Iterator, typename Functor> |
| | struct vtkSMPTools_RangeFunctor<Iterator, Functor, true> |
| | { |
| | Functor& F; |
| | Iterator& Begin; |
| | vtkSMPTools_RangeFunctor(Iterator& begin, Functor& f) |
| | : F(f) |
| | , Begin(begin) |
| | {} |
| | void Initialize() |
| | { |
| | this->F.Initialize(); |
| | } |
| | void operator()(vtkIdType first, vtkIdType last) |
| | { |
| | Iterator itFirst(Begin); |
| | std::advance(itFirst, first); |
| | Iterator itLast(itFirst); |
| | std::advance(itLast, last - first); |
| | this->F(itFirst, itLast); |
| | } |
| | void Reduce() |
| | { |
| | this->F.Reduce(); |
| | } |
| | }; |
| |
|
| | template<typename Iterator, typename Functor> |
| | class vtkSMPTools_Lookup_RangeFor |
| | { |
| | static bool const init = vtkSMPTools_Has_Initialize<Functor>::value; |
| |
|
| | public: |
| | typedef vtkSMPTools_RangeFunctor<Iterator, Functor, init> type; |
| | }; |
| |
|
| | template<typename Iterator, typename Functor> |
| | class vtkSMPTools_Lookup_RangeFor<Iterator, Functor const> |
| | { |
| | static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value; |
| |
|
| | public: |
| | typedef vtkSMPTools_RangeFunctor<Iterator, Functor const, init> type; |
| | }; |
| |
|
| | template<typename T> |
| | using resolvedNotInt = typename std::enable_if<!std::is_integral<T>::value, void>::type; |
| | } |
| | } |
| | } |
| | # endif |
| |
|
| | class VTKCOMMONCORE_EXPORT vtkSMPTools |
| | { |
| | public: |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename Functor> |
| | static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f) |
| | { |
| | typename vtk::detail::smp::vtkSMPTools_Lookup_For<Functor>::type fi(f); |
| | fi.For(first, last, grain); |
| | } |
| |
|
| | template<typename Functor> |
| | static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f) |
| | { |
| | typename vtk::detail::smp::vtkSMPTools_Lookup_For<Functor const>::type fi(f); |
| | fi.For(first, last, grain); |
| | } |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename Functor> |
| | static void For(vtkIdType first, vtkIdType last, Functor& f) |
| | { |
| | vtkSMPTools::For(first, last, 0, f); |
| | } |
| |
|
| | template<typename Functor> |
| | static void For(vtkIdType first, vtkIdType last, Functor const& f) |
| | { |
| | vtkSMPTools::For(first, last, 0, f); |
| | } |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename Iter, typename Functor> |
| | static vtk::detail::smp::resolvedNotInt<Iter> For(Iter begin, Iter end, vtkIdType grain, Functor& f) |
| | { |
| | vtkIdType size = std::distance(begin, end); |
| | typename vtk::detail::smp::vtkSMPTools_Lookup_RangeFor<Iter, Functor>::type fi(begin, f); |
| | vtkSMPTools::For(0, size, grain, fi); |
| | } |
| |
|
| | template<typename Iter, typename Functor> |
| | static vtk::detail::smp::resolvedNotInt<Iter> For( |
| | Iter begin, |
| | Iter end, |
| | vtkIdType grain, |
| | Functor const& f |
| | ) |
| | { |
| | vtkIdType size = std::distance(begin, end); |
| | typename vtk::detail::smp::vtkSMPTools_Lookup_RangeFor<Iter, Functor const>::type fi(begin, f); |
| | vtkSMPTools::For(0, size, grain, fi); |
| | } |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename Iter, typename Functor> |
| | static vtk::detail::smp::resolvedNotInt<Iter> For(Iter begin, Iter end, Functor& f) |
| | { |
| | vtkSMPTools::For(begin, end, 0, f); |
| | } |
| |
|
| | template<typename Iter, typename Functor> |
| | static vtk::detail::smp::resolvedNotInt<Iter> For(Iter begin, Iter end, Functor const& f) |
| | { |
| | vtkSMPTools::For(begin, end, 0, f); |
| | } |
| | |
| |
|
| | |
| | |
| | |
| | static const char* GetBackend(); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | static bool SetBackend(const char* backend); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | static void Initialize(int numThreads = 0); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | static int GetEstimatedNumberOfThreads(); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | static void SetNestedParallelism(bool isNested); |
| |
|
| | |
| | |
| | |
| | static bool GetNestedParallelism(); |
| |
|
| | |
| | |
| | |
| | static bool IsParallelScope(); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | struct Config |
| | { |
| | int MaxNumberOfThreads = 0; |
| | std::string Backend = vtk::detail::smp::vtkSMPToolsAPI::GetInstance().GetBackend(); |
| | bool NestedParallelism = true; |
| |
|
| | Config() |
| | {} |
| | Config(int maxNumberOfThreads) |
| | : MaxNumberOfThreads(maxNumberOfThreads) |
| | {} |
| | Config(std::string backend) |
| | : Backend(backend) |
| | {} |
| | Config(bool nestedParallelism) |
| | : NestedParallelism(nestedParallelism) |
| | {} |
| | Config(int maxNumberOfThreads, std::string backend, bool nestedParallelism) |
| | : MaxNumberOfThreads(maxNumberOfThreads) |
| | , Backend(backend) |
| | , NestedParallelism(nestedParallelism) |
| | {} |
| | # ifndef DOXYGEN_SHOULD_SKIP_THIS |
| | Config(vtk::detail::smp::vtkSMPToolsAPI& API) |
| | : MaxNumberOfThreads(API.GetInternalDesiredNumberOfThread()) |
| | , Backend(API.GetBackend()) |
| | , NestedParallelism(API.GetNestedParallelism()) |
| | {} |
| | # endif |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename T> |
| | static void LocalScope(Config const& config, T&& lambda) |
| | { |
| | auto& SMPToolsAPI = vtk::detail::smp::vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.LocalScope<vtkSMPTools::Config>(config, lambda); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename InputIt, typename OutputIt, typename Functor> |
| | static void Transform(InputIt inBegin, InputIt inEnd, OutputIt outBegin, Functor transform) |
| | { |
| | auto& SMPToolsAPI = vtk::detail::smp::vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.Transform(inBegin, inEnd, outBegin, transform); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename InputIt1, typename InputIt2, typename OutputIt, typename Functor> |
| | static void Transform( |
| | InputIt1 inBegin1, |
| | InputIt1 inEnd, |
| | InputIt2 inBegin2, |
| | OutputIt outBegin, |
| | Functor transform |
| | ) |
| | { |
| | auto& SMPToolsAPI = vtk::detail::smp::vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.Transform(inBegin1, inEnd, inBegin2, outBegin, transform); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename Iterator, typename T> |
| | static void Fill(Iterator begin, Iterator end, const T& value) |
| | { |
| | auto& SMPToolsAPI = vtk::detail::smp::vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.Fill(begin, end, value); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | template<typename RandomAccessIterator> |
| | static void Sort(RandomAccessIterator begin, RandomAccessIterator end) |
| | { |
| | auto& SMPToolsAPI = vtk::detail::smp::vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.Sort(begin, end); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | template<typename RandomAccessIterator, typename Compare> |
| | static void Sort(RandomAccessIterator begin, RandomAccessIterator end, Compare comp) |
| | { |
| | auto& SMPToolsAPI = vtk::detail::smp::vtkSMPToolsAPI::GetInstance(); |
| | SMPToolsAPI.Sort(begin, end, comp); |
| | } |
| | }; |
| |
|
| | #endif |
| | |
| |
|