VTK
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 =========================================================================*/
00028 #ifndef __vtkSMPTools_h__
00029 #define __vtkSMPTools_h__
00030 
00031 #include "vtkCommonCoreModule.h" // For export macro
00032 #include "vtkObject.h"
00033 
00034 #include "vtkSMPThreadLocal.h" // For Initialized
00035 
00036 class vtkSMPTools;
00037 
00038 #include "vtkSMPToolsInternal.h"
00039 
00040 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00041 #ifndef __WRAP__
00042 namespace vtk
00043 {
00044 namespace detail
00045 {
00046 namespace smp
00047 {
00048 template <typename T>
00049 class vtkSMPTools_Has_Initialize
00050 {
00051   typedef char (&no_type)[1];
00052   typedef char (&yes_type)[2];
00053   template <typename U, void (U::*)()> struct V {};
00054   template <typename U> static yes_type check(V<U, &U::Initialize>*);
00055   template <typename U> static no_type check(...);
00056 public:
00057   static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
00058 };
00059 
00060 template <typename T>
00061 class vtkSMPTools_Has_Initialize_const
00062 {
00063   typedef char (&no_type)[1];
00064   typedef char (&yes_type)[2];
00065   template <typename U, void (U::*)() const> struct V {};
00066   template <typename U> static yes_type check(V<U, &U::Initialize>*);
00067   template <typename U> static no_type check(...);
00068 public:
00069   static bool const value = sizeof(check<T>(0)) == sizeof(yes_type);
00070 };
00071 
00072 template <typename Functor, bool Init>
00073 struct vtkSMPTools_FunctorInternal;
00074 
00075 template <typename Functor>
00076 struct vtkSMPTools_FunctorInternal<Functor, false>
00077 {
00078   Functor& F;
00079   vtkSMPTools_FunctorInternal(Functor& f): F(f) {}
00080   void Execute(vtkIdType first, vtkIdType last)
00081   {
00082     this->F(first, last);
00083   }
00084   void For(vtkIdType first, vtkIdType last, vtkIdType grain)
00085   {
00086     vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
00087   }
00088   vtkSMPTools_FunctorInternal<Functor, false>& operator=(
00089     const vtkSMPTools_FunctorInternal<Functor, false>&);
00090   vtkSMPTools_FunctorInternal<Functor, false>(
00091     const vtkSMPTools_FunctorInternal<Functor, false>&);
00092 };
00093 
00094 template <typename Functor>
00095 struct vtkSMPTools_FunctorInternal<Functor, true>
00096 {
00097   Functor& F;
00098   vtkSMPThreadLocal<unsigned char> Initialized;
00099   vtkSMPTools_FunctorInternal(Functor& f): F(f), Initialized(0) {}
00100   void Execute(vtkIdType first, vtkIdType last)
00101   {
00102     unsigned char& inited = this->Initialized.Local();
00103     if (!inited)
00104       {
00105       this->F.Initialize();
00106       inited = 1;
00107       }
00108     this->F(first, last);
00109   }
00110   void For(vtkIdType first, vtkIdType last, vtkIdType grain)
00111   {
00112     vtk::detail::smp::vtkSMPTools_Impl_For(first, last, grain, *this);
00113     this->F.Reduce();
00114   }
00115   vtkSMPTools_FunctorInternal<Functor, true>& operator=(
00116     const vtkSMPTools_FunctorInternal<Functor, true>&);
00117   vtkSMPTools_FunctorInternal<Functor, true>(
00118     const vtkSMPTools_FunctorInternal<Functor, true>&);
00119 };
00120 
00121 template <typename Functor>
00122 class vtkSMPTools_Lookup_For
00123 {
00124   static bool const init = vtkSMPTools_Has_Initialize<Functor>::value;
00125 public:
00126   typedef vtkSMPTools_FunctorInternal<Functor, init> type;
00127 };
00128 
00129 template <typename Functor>
00130 class vtkSMPTools_Lookup_For<Functor const>
00131 {
00132   static bool const init = vtkSMPTools_Has_Initialize_const<Functor>::value;
00133 public:
00134   typedef vtkSMPTools_FunctorInternal<Functor const, init> type;
00135 };
00136 } // namespace smp
00137 } // namespace detail
00138 } // namespace vtk
00139 #endif // __WRAP__
00140 #endif // DOXYGEN_SHOULD_SKIP_THIS
00141 
00142 class VTKCOMMONCORE_EXPORT vtkSMPTools
00143 {
00144 public:
00145 
00147 
00153   template <typename Functor>
00154   static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor& f)
00155   {
00156     typename vtk::detail::smp::vtkSMPTools_Lookup_For<Functor>::type fi(f);
00157     fi.For(first, last, grain);
00158   }
00160 
00162 
00168   template <typename Functor>
00169   static void For(vtkIdType first, vtkIdType last, vtkIdType grain, Functor const& f)
00170   {
00171     typename vtk::detail::smp::vtkSMPTools_Lookup_For<Functor const>::type fi(f);
00172     fi.For(first, last, grain);
00173   }
00175 
00177 
00183   template <typename Functor>
00184   static void For(vtkIdType first, vtkIdType last, Functor& f)
00185   {
00186     vtkSMPTools::For(first, last, 0, f);
00187   }
00189 
00191 
00197   template <typename Functor>
00198   static void For(vtkIdType first, vtkIdType last, Functor const& f)
00199   {
00200     vtkSMPTools::For(first, last, 0, f);
00201   }
00203 
00205 
00212   static void Initialize(int numThreads=0);
00213 };
00215 
00216 #endif
00217 // VTK-HeaderTest-Exclude: vtkSMPTools.h