VTK
/Users/kitware/Dashboards/MyTests/VTK_BLD_Release_docs/Utilities/Doxygen/dox/Common/Core/vtkSMPTools.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkSMPTools.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00031 #ifndef vtkSMPTools_h__
00032 #define vtkSMPTools_h__
00033 
00034 #include "vtkCommonCoreModule.h" // For export macro
00035 #include "vtkObject.h"
00036 
00037 #include "vtkSMPThreadLocal.h" // For Initialized
00038 
00039 class vtkSMPTools;
00040 
00041 #include "vtkSMPToolsInternal.h"
00042 
00043 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00044 #ifndef __WRAP__
00045 namespace vtk
00046 {
00047 namespace detail
00048 {
00049 namespace smp
00050 {
00051 template <typename T>
00052 class vtkSMPTools_Has_Initialize
00053 {
00054   typedef char (&no_type)[1];
00055   typedef char (&yes_type)[2];
00056   template <typename U, void (U::*)()> struct V {};
00057   template <typename U> static yes_type check(V<U, &U::Initialize>*);
00058   template <typename U> static no_type check(...);
00059 public:
00060   static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
00061 };
00062 
00063 template <typename T>
00064 class vtkSMPTools_Has_Initialize_const
00065 {
00066   typedef char (&no_type)[1];
00067   typedef char (&yes_type)[2];
00068   template <typename U, void (U::*)() const> struct V {};
00069   template <typename U> static yes_type check(V<U, &U::Initialize>*);
00070   template <typename U> static no_type check(...);
00071 public:
00072   static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
00073 };
00074 
00075 template <typename Functor, bool Init>
00076 struct vtkSMPTools_FunctorInternal;
00077 
00078 template <typename Functor>
00079 struct vtkSMPTools_FunctorInternal<Functor, false>
00080 {
00081   Functor& F;
00082   vtkSMPTools_FunctorInternal(Functor& f): F(f) {}
00083   void Execute(vtkIdType first, vtkIdType last)
00084   {
00085     this->F(first, last);
00086   }
00087   void For(vtkIdType first, vtkIdType last, vtkIdType grain)
00088   {
00089     vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
00090   }
00091   vtkSMPTools_FunctorInternal<Functor, false>& operator=(
00092     const vtkSMPTools_FunctorInternal<Functor, false>&);
00093   vtkSMPTools_FunctorInternal<Functor, false>(
00094     const vtkSMPTools_FunctorInternal<Functor, false>&);
00095 };
00096 
00097 template <typename Functor>
00098 struct vtkSMPTools_FunctorInternal<Functor, true>
00099 {
00100   Functor& F;
00101   vtkSMPThreadLocal<unsigned char> Initialized;
00102   vtkSMPTools_FunctorInternal(Functor& f): F(f), Initialized(0) {}
00103   void Execute(vtkIdType first, vtkIdType last)
00104   {
00105     unsigned char& inited = this->Initialized.Local();
00106     if (!inited)
00107       {
00108       this->F.Initialize();
00109       inited = 1;
00110       }
00111     this->F(first, last);
00112   }
00113   void For(vtkIdType first, vtkIdType last, vtkIdType grain)
00114   {
00115     vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
00116     this->F.Reduce();
00117   }
00118   vtkSMPTools_FunctorInternal<Functor, true>& operator=(
00119     const vtkSMPTools_FunctorInternal<Functor, true>&);
00120   vtkSMPTools_FunctorInternal<Functor, true>(
00121     const vtkSMPTools_FunctorInternal<Functor, true>&);
00122 };
00123 
00124 template <typename Functor>
00125 class vtkSMPTools_Lookup_For
00126 {
00127   static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
00128 public:
00129   typedef vtkSMPTools_FunctorInternal<Functor, init> type;
00130 };
00131 
00132 template <typename Functor>
00133 class vtkSMPTools_Lookup_For<Functor const>
00134 {
00135   static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
00136 public:
00137   typedef vtkSMPTools_FunctorInternal<Functor const, init> type;
00138 };
00139 } // namespace smp
00140 } // namespace detail
00141 } // namespace vtk
00142 #endif // __WRAP__
00143 #endif // DOXYGEN_SHOULD_SKIP_THIS
00144 
00145 class VTKCOMMONCORE_EXPORT vtkSMPTools
00146 {
00147 public:
00148 
00150 
00156   template <typename Functor>
00157   static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
00158   {
00159     typename vtk::detail::smp::vtkSMPTools_Lookup_For<Functor>::type fi(f);
00160     fi.For(first, last, grain);
00161   }
00163 
00165 
00171   template <typename Functor>
00172   static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
00173   {
00174     typename vtk::detail::smp::vtkSMPTools_Lookup_For<Functor const>::type fi(f);
00175     fi.For(first, last, grain);
00176   }
00178 
00180 
00186   template <typename Functor>
00187   static void For(vtkIdType first, vtkIdType last, Functor& f)
00188   {
00189     vtkSMPTools::For(first, last, 0, f);
00190   }
00192 
00194 
00200   template <typename Functor>
00201   static void For(vtkIdType first, vtkIdType last, Functor const& f)
00202   {
00203     vtkSMPTools::For(first, last, 0, f);
00204   }
00206 
00214   static void Initialize(int numThreads=0);
00215 
00217 
00221   static int GetEstimatedNumberOfThreads();
00222 };
00224 
00225 #endif
00226 // VTK-HeaderTest-Exclude: vtkSMPTools.h