dune-grid
2.2beta1
|
00001 #ifndef DUNE_ALUGRIDOBJECTFACTORY_HH 00002 #define DUNE_ALUGRIDOBJECTFACTORY_HH 00003 00004 #include <dune/grid/alugrid/common/memory.hh> 00005 00006 #if defined USE_PTHREADS || defined _OPENMP 00007 #define USE_SMP_PARALLEL 00008 #endif 00009 00010 #ifdef _OPENMP 00011 #include <omp.h> 00012 #endif 00013 00014 #if HAVE_DUNE_FEM 00015 #include <dune/fem/misc/threadmanager.hh> 00016 #endif 00017 00018 namespace Dune 00019 { 00020 template <class InterfaceType> 00021 struct MakeableInterfaceObject ; 00022 00023 template <class GridImp> 00024 class ALUGridObjectFactory 00025 { 00026 template <class OF, int codim> 00027 struct ALUGridEntityFactory; 00028 00030 // 00031 // partial specialization of method getNewEntity 00032 // 00034 template <class GridObjectFactory> 00035 struct ALUGridEntityFactory<GridObjectFactory,0> 00036 { 00037 enum { codim = 0 }; 00038 typedef typename GridImp :: template Codim<codim> :: Entity Entity; 00039 typedef MakeableInterfaceObject<Entity> EntityObject; 00040 typedef typename EntityObject :: ImplementationType EntityImp; 00041 00042 inline static EntityObject * 00043 getNewEntity (const GridObjectFactory& factory, int level) 00044 { 00045 return factory.entityProvider_.getEntityObject( factory, level, (EntityImp *) 0); 00046 } 00047 00048 inline static void freeEntity(const GridObjectFactory& factory, EntityObject * e ) 00049 { 00050 factory.entityProvider_.freeObject( e ); 00051 } 00052 }; 00053 00054 template <class GridObjectFactory> 00055 struct ALUGridEntityFactory<GridObjectFactory,1> 00056 { 00057 enum { codim = 1 }; 00058 typedef typename GridImp :: template Codim<codim> :: Entity Entity; 00059 typedef MakeableInterfaceObject<Entity> EntityObject; 00060 typedef typename EntityObject :: ImplementationType EntityImp; 00061 00062 inline static EntityObject * 00063 getNewEntity (const GridObjectFactory& factory, int level) 00064 { 00065 return factory.faceProvider_.getEntityObject( factory, level, (EntityImp *) 0); 00066 } 00067 00068 inline static void freeEntity(const GridObjectFactory& factory, EntityObject * e ) 00069 { 00070 factory.faceProvider_.freeObject( e ); 00071 } 00072 }; 00073 00074 template <class GridObjectFactory> 00075 struct ALUGridEntityFactory<GridObjectFactory,2> 00076 { 00077 enum { codim = 2 }; 00078 typedef typename GridImp :: template Codim<codim> :: Entity Entity; 00079 typedef MakeableInterfaceObject<Entity> EntityObject; 00080 typedef typename EntityObject :: ImplementationType EntityImp; 00081 00082 inline static EntityObject * 00083 getNewEntity (const GridObjectFactory& factory, int level) 00084 { 00085 return factory.edgeProvider_.getEntityObject( factory, level, (EntityImp *) 0); 00086 } 00087 00088 inline static void freeEntity(const GridObjectFactory& factory, EntityObject * e ) 00089 { 00090 factory.edgeProvider_.freeObject( e ); 00091 } 00092 }; 00093 00094 template <class GridObjectFactory> 00095 struct ALUGridEntityFactory<GridObjectFactory,3> 00096 { 00097 enum { codim = 3 }; 00098 typedef typename GridImp :: template Codim<codim> :: Entity Entity; 00099 typedef MakeableInterfaceObject<Entity> EntityObject; 00100 typedef typename EntityObject :: ImplementationType EntityImp; 00101 00102 inline static EntityObject * 00103 getNewEntity (const GridObjectFactory& factory, int level) 00104 { 00105 return factory.vertexProvider_.getEntityObject( factory, level, (EntityImp *) 0); 00106 } 00107 00108 inline static void freeEntity(const GridObjectFactory& factory, EntityObject * e ) 00109 { 00110 factory.vertexProvider_.freeObject( e ); 00111 } 00112 }; // end of ALUGridEntityFactory 00113 00114 enum { vxCodim = GridImp :: dimension }; 00115 public: 00116 typedef GridImp GridType; 00117 typedef ALUGridObjectFactory FactoryType; 00118 00119 typedef MakeableInterfaceObject<typename GridType :: Traits::template Codim<0>::Entity> EntityObject; 00120 typedef MakeableInterfaceObject<typename GridType :: Traits::template Codim<1>::Entity> FaceObject; 00121 typedef MakeableInterfaceObject<typename GridType :: Traits::template Codim<2>::Entity> EdgeObject; 00122 typedef MakeableInterfaceObject<typename GridType :: Traits::template Codim< vxCodim >::Entity> VertexObject; 00123 00124 typedef typename GridType :: LeafIntersectionIteratorImp LeafIntersectionIteratorImp ; 00125 typedef typename GridType :: LevelIntersectionIteratorImp LevelIntersectionIteratorImp ; 00126 00127 // declare friendship 00128 friend class ALUGridEntityFactory<FactoryType,0>; 00129 friend class ALUGridEntityFactory<FactoryType,1>; 00130 friend class ALUGridEntityFactory<FactoryType,2>; 00131 friend class ALUGridEntityFactory<FactoryType,3>; 00132 00133 protected: 00134 typedef ALUMemoryProvider< EntityObject > EntityProvider; 00135 typedef ALUMemoryProvider< FaceObject > FaceProvider; 00136 typedef ALUMemoryProvider< EdgeObject > EdgeProvider; 00137 typedef ALUMemoryProvider< VertexObject > VertexProvider; 00138 00139 mutable EntityProvider entityProvider_; 00140 mutable FaceProvider faceProvider_; 00141 mutable EdgeProvider edgeProvider_; 00142 mutable VertexProvider vertexProvider_; 00143 00144 typedef ALUMemoryProvider< LeafIntersectionIteratorImp > LeafIntersectionIteratorProviderType; 00145 typedef ALUMemoryProvider< LevelIntersectionIteratorImp > LevelIntersectionIteratorProviderType; 00146 00147 mutable LeafIntersectionIteratorProviderType leafInterItProvider_; 00148 mutable LevelIntersectionIteratorProviderType levelInterItProvider_; 00149 00150 const GridType& grid_ ; 00151 00152 #ifdef USE_SMP_PARALLEL 00153 public: 00154 #endif 00155 ALUGridObjectFactory( const ALUGridObjectFactory& other ) : grid_( other.grid_ ) {} 00156 00157 public: 00158 const GridType& grid() const { return grid_; } 00159 00160 ALUGridObjectFactory( const GridType& grid ) : grid_( grid ) {} 00161 00162 template <int codim> 00163 inline MakeableInterfaceObject<typename GridType :: Traits::template Codim<codim>::Entity> * 00164 getNewEntity ( int level = -1 ) const 00165 { 00166 return ALUGridEntityFactory<FactoryType,codim>::getNewEntity( *this, level); 00167 } 00168 00169 template <int codim> 00170 inline void freeEntity (MakeableInterfaceObject<typename GridType :: Traits::template Codim<codim>::Entity> * en) const 00171 { 00172 ALUGridEntityFactory<FactoryType,codim>::freeEntity(*this, en); 00173 } 00174 00175 LeafIntersectionIteratorImp& getIntersection( const int wLevel, const LeafIntersectionIteratorImp* ) const 00176 { 00177 return * (leafInterItProvider_.getObject( *this, wLevel )); 00178 } 00179 00180 LevelIntersectionIteratorImp& getIntersection(const int wLevel, const LevelIntersectionIteratorImp* ) const 00181 { 00182 return * (levelInterItProvider_.getObject( *this, wLevel )); 00183 } 00184 00186 void freeIntersection(LeafIntersectionIteratorImp & it) const { leafInterItProvider_.freeObject( &it ); } 00187 void freeIntersection(LevelIntersectionIteratorImp & it) const { levelInterItProvider_.freeObject( &it ); } 00188 00189 // return thread number 00190 static inline int threadNumber() 00191 { 00192 #ifdef _OPENMP 00193 return omp_get_thread_num(); 00194 #elif HAVE_DUNE_FEM 00195 return Fem :: ThreadManager :: thread() ; 00196 #else 00197 return 0; 00198 #endif 00199 } 00200 00201 // return maximal possible number of threads 00202 static inline int maxThreads() { 00203 #ifdef _OPENMP 00204 return omp_get_max_threads(); 00205 #elif HAVE_DUNE_FEM 00206 return Fem :: ThreadManager :: maxThreads() ; 00207 #else 00208 return 1; 00209 #endif 00210 } 00211 }; 00212 00213 } // end namespace Dune 00214 #endif