diff --git a/dune/alugrid/3d/grid.hh b/dune/alugrid/3d/grid.hh index 670421af1851383d4578c28fa3d5b9313461fe11..f8dbea30957f5da28757c3a3c7884afd7ddaf7fe 100644 --- a/dune/alugrid/3d/grid.hh +++ b/dune/alugrid/3d/grid.hh @@ -756,7 +756,7 @@ namespace Dune LBHandle lbHandle( *this, dataHandle ); return loadBalance( lbHandle ); } - + /** \brief ghostSize is one for codim 0 and zero otherwise for this grid */ int ghostSize (int level, int codim) const; @@ -781,6 +781,10 @@ namespace Dune void communicate (CommDataHandleIF<DataHandleImp,DataTypeImp> & data, InterfaceType iftype, CommunicationDirection dir) const; + protected: + // load balance and compress memory if possible + void finalizeGridCreation(); + private: typedef ALU3DSPACE GatherScatter GatherScatterType; diff --git a/dune/alugrid/3d/grid_inline.hh b/dune/alugrid/3d/grid_inline.hh index bcf43c5d4f103763d4a5cf70c8e38f18c7d8b0fc..c8e88b93c4343dec915c2530a26e2e992663a533 100644 --- a/dune/alugrid/3d/grid_inline.hh +++ b/dune/alugrid/3d/grid_inline.hh @@ -659,6 +659,20 @@ namespace Dune return ALU3dGridCommHelper< elType, Comm >::loadBalance( *this, data ); } + template< ALU3dGridElementType elType, class Comm > + inline void ALU3dGrid< elType, Comm >::finalizeGridCreation() + { + // distribute the grid + loadBalance(); + + // free memory by reinitializing the grid + mygrid_ = GitterImplType :: compress( mygrid_ ); + + // reset wasRefined flags + postAdapt(); + // update additional information on grid + calcExtras(); + } // communicate level data template< ALU3dGridElementType elType, class Comm > diff --git a/dune/alugrid/3d/gridfactory.cc b/dune/alugrid/3d/gridfactory.cc index ee384dd821c6abb94bf011b22b864ca46aeb0175..7d5746f27af7aa7e9f2bcb13719a0e9a508bca95 100644 --- a/dune/alugrid/3d/gridfactory.cc +++ b/dune/alugrid/3d/gridfactory.cc @@ -432,11 +432,9 @@ namespace Dune grid->duneNotifyMacroGridChanges(); } - // reset wasRefined flags - grid->postAdapt(); - // update additional information on grid - grid->calcExtras(); - + // load balance the grid and free memory if possible + grid->finalizeGridCreation(); + return grid; } diff --git a/dune/alugrid/impl/duneinterface/gitter_dune_impl.h b/dune/alugrid/impl/duneinterface/gitter_dune_impl.h index fff0bfade7b936a11132cc458a9c09b084e44651..38ac668ea282dc2db21526f096212644622e7588 100644 --- a/dune/alugrid/impl/duneinterface/gitter_dune_impl.h +++ b/dune/alugrid/impl/duneinterface/gitter_dune_impl.h @@ -124,17 +124,21 @@ namespace ALUGrid // compress memory of given grid and return new object (holding equivalent information) static GitterDuneImpl* compress( GitterDuneImpl* grd ) { - // backup stream - std::stringstream backup; - // backup grid - grd->duneBackup( backup ); - delete grd; grd = 0; - // free allocated memory (only works if all grids are deleted at this point) - MyAlloc::clearFreeMemory (); - // restore saved grid - grd = new GitterDuneImpl( backup ); - assert( grd ); - grd->duneRestore( backup ); + // only do the backup-restore thing if dlmalloc is enabled + if( MyAlloc :: ALUGridUsesDLMalloc ) + { + // backup stream + std::stringstream backup; + // backup grid + grd->duneBackup( backup ); + delete grd; grd = 0; + // free allocated memory (only works if all grids are deleted at this point) + MyAlloc::clearFreeMemory (); + // restore saved grid + grd = new GitterDuneImpl( backup ); + assert( grd ); + grd->duneRestore( backup ); + } return grd; } diff --git a/dune/alugrid/impl/duneinterface/gitter_dune_pll_impl.h b/dune/alugrid/impl/duneinterface/gitter_dune_pll_impl.h index 12b8f0b0476818d7525f0659bc975717da361d58..d88da12b2eb827c5e65e111f8a7a830a02b36ee3 100644 --- a/dune/alugrid/impl/duneinterface/gitter_dune_pll_impl.h +++ b/dune/alugrid/impl/duneinterface/gitter_dune_pll_impl.h @@ -175,18 +175,22 @@ namespace ALUGrid // compress memory of given grid and return new object (holding equivalent information) static GitterDunePll* compress( GitterDunePll* grd ) { - MpAccessLocal& mpa = grd->mpAccess (); - // backup stream - std::stringstream backup; - // backup grid - grd->duneBackup( backup ); - delete grd; grd = 0; - // free allocated memory (only works if all grids are deleted at this point) - MyAlloc::clearFreeMemory (); - // restore saved grid - grd = new GitterDunePll( backup, mpa ); - assert( grd ); - grd->duneRestore( backup ); + // only do the backup-restore thing if dlmalloc is enabled + if( MyAlloc :: ALUGridUsesDLMalloc ) + { + MpAccessLocal& mpa = grd->mpAccess (); + // backup stream + std::stringstream backup; + // backup grid + grd->duneBackup( backup ); + delete grd; grd = 0; + // free allocated memory (only works if all grids are deleted at this point) + MyAlloc::clearFreeMemory (); + // restore saved grid + grd = new GitterDunePll( backup, mpa ); + assert( grd ); + grd->duneRestore( backup ); + } return grd; } diff --git a/dune/alugrid/impl/serial/myalloc.cc b/dune/alugrid/impl/serial/myalloc.cc index 92349564cab9e6e93947c48cc41575a9a376272c..d6b5d05fab93b43f14b103db8bb1df9c33d00f58 100644 --- a/dune/alugrid/impl/serial/myalloc.cc +++ b/dune/alugrid/impl/serial/myalloc.cc @@ -10,10 +10,12 @@ #include <set> #include <stack> -//#define ONLY_MSPACES 1 - #include "myalloc.h" +#ifdef ALUGRID_USES_DLMALLOC +#define ONLY_MSPACES 1 +#endif + namespace ALUGrid { diff --git a/dune/alugrid/impl/serial/myalloc.h b/dune/alugrid/impl/serial/myalloc.h index 19d172481cf750f6aaf285a43946c24b386bbd08..9084bfe3d3ba86a7412cfd13dfc9d25aa728166f 100644 --- a/dune/alugrid/impl/serial/myalloc.h +++ b/dune/alugrid/impl/serial/myalloc.h @@ -6,9 +6,10 @@ #include <cstddef> +//#define ALUGRID_USES_DLMALLOC + namespace ALUGrid { - #ifndef DONT_USE_ALUGRID_ALLOC class MyAlloc @@ -24,7 +25,14 @@ namespace ALUGrid // if true objects are not free, only pushed to stack static bool _freeAllowed ; - public : + public : + static const bool ALUGridUsesDLMalloc = +#ifdef ALUGRID_USES_DLMALLOC + true ; +#else + false ; +#endif + class Initializer { // initializer versucht, die statischen Objekte der Speicherverwaltung @@ -75,6 +83,9 @@ namespace ALUGrid class MyAlloc { public: + // this is false here anyway + static const bool ALUGridUsesDLMalloc = false ; + // if called, freeing objects is allowed again inline static void unlockFree(void *) {}