From: apo Date: Tue, 19 Dec 2006 12:26:28 +0000 (+0000) Subject: 1. To implement more accurate cache management X-Git-Tag: WP1_2_3_20-12-2006_bug_fixing~4 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=bd0ad14683efef21aa8a58f2e1aeaea7d33d32d6;p=modules%2Fvisu.git 1. To implement more accurate cache management 2. To improve memory size calculation, by extrapolation of the memory size from existing presentation of the same type with the same input. 3. Implementation of the forced view rendering on successful apply --- diff --git a/src/VISU_I/VISU_ColoredPrs3dCache_i.cc b/src/VISU_I/VISU_ColoredPrs3dCache_i.cc index 7dde86de..b24b2e19 100644 --- a/src/VISU_I/VISU_ColoredPrs3dCache_i.cc +++ b/src/VISU_I/VISU_ColoredPrs3dCache_i.cc @@ -39,12 +39,166 @@ #include "VVTK_ViewWindow.h" #include "SUIT_ResourceMgr.h" +#include + #ifdef _DEBUG_ static int MYDEBUG = 0; #else static int MYDEBUG = 0; #endif +namespace +{ + //---------------------------------------------------------------------------- + inline + bool + IsSameField(const VISU::ColoredPrs3dHolder::BasicInput& theReferenceInput, + const VISU::ColoredPrs3dHolder::BasicInput& thePrs3dInput) + { + return thePrs3dInput.myResult->_is_equivalent(theReferenceInput.myResult) && + thePrs3dInput.myEntity == theReferenceInput.myEntity && + !strcmp(thePrs3dInput.myFieldName.in(), theReferenceInput.myFieldName.in()); + } + + + //---------------------------------------------------------------------------- + inline + bool + IsSameTimeStamp(const VISU::ColoredPrs3dHolder::BasicInput& theReferenceInput, + const VISU::ColoredPrs3dHolder::BasicInput& thePrs3dInput) + { + return IsSameField(theReferenceInput, thePrs3dInput) && + thePrs3dInput.myTimeStampNumber == theReferenceInput.myTimeStampNumber; + } + + + //---------------------------------------------------------------------------- + VISU::TPrs3dPtr + FindSameFieldPrs(const VISU::TColoredPrs3dHolderMap& theHolderMap, + const VISU::ColoredPrs3dHolder::BasicInput& theInput, + VISU::VISUType theType) + { + VISU::TColoredPrs3dHolderMap::const_iterator aHolderIter = theHolderMap.begin(); + VISU::TColoredPrs3dHolderMap::const_iterator aHolderIterEnd = theHolderMap.end(); + for(; aHolderIter != aHolderIterEnd; aHolderIter++){ + const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second; + VISU::TLastVisitedPrsList::const_iterator aPrsIter = aPrsList.begin(); + VISU::TLastVisitedPrsList::const_iterator aPrsIterEnd = aPrsList.end(); + for(; aPrsIter != aPrsIterEnd; aPrsIter++){ + VISU::TPrs3dPtr aPrs3d = *aPrsIter; + if(aPrs3d->GetType() != theType) + break; + VISU::ColoredPrs3dHolder::BasicInput_var anInput = aPrs3d->GetBasicInput(); + if(IsSameField(theInput, anInput)) + return aPrs3d; + } + } + return NULL; + } + + + //---------------------------------------------------------------------------- + CORBA::Float + EstimateMemorySize(const VISU::TColoredPrs3dHolderMap& theHolderMap, + const VISU::ColoredPrs3dHolder::BasicInput& theInput, + VISU::VISUType theType, + const size_t theRawEstimatedMemorySize) + { + VISU::TPrs3dPtr aPrs3d = FindSameFieldPrs(theHolderMap, theInput, theType); + if(aPrs3d.GetPointer()) + return aPrs3d->GetMemorySize(); + return CORBA::Float(theRawEstimatedMemorySize/(1024.0*1024.0)); // convert to Mb + } + + + //---------------------------------------------------------------------------- + bool + SelectPrs3dToBeDeleted(CORBA::Float theRequiredMemory, + const std::string& theActiveHolderEntry, + const VISU::TColoredPrs3dHolderMap& theHolderMap, + VISU::TColoredPrs3dHolderMap& theColoredPrs3dHolderMap) + { + if( theRequiredMemory < 1.0 / VTK_LARGE_FLOAT ) + return false; + + VISU::TColoredPrs3dHolderMap::const_iterator aHolderIter = theHolderMap.begin(); + VISU::TColoredPrs3dHolderMap::const_iterator aHolderIterEnd = theHolderMap.end(); + + // To calculate the maximum length of the cache history among all holders + int anIteration = 0; + for( ; aHolderIter != aHolderIterEnd; aHolderIter++ ){ + if( aHolderIter->first == theActiveHolderEntry ) + continue; + const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second; + anIteration = QMAX( aPrsList.size() - 1, anIteration ); + } + + // To estimate what amount of memory can be obtained + // by cleaning of non-active holder's presentation + CORBA::Float aGatheredMemory = 0.0; + for(; anIteration > 0; anIteration--){ // To take into account only non-devices + aHolderIter = theHolderMap.begin(); + for(; aHolderIter != aHolderIterEnd; aHolderIter++ ){ + const std::string& aHolderEntry = aHolderIter->first; + if( aHolderEntry == theActiveHolderEntry ) + continue; + const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second; + if( anIteration < aPrsList.size() ){ + VISU::TPrs3dPtr aPrs3d = aPrsList[anIteration]; + aGatheredMemory += aPrs3d->GetMemorySize(); + theColoredPrs3dHolderMap[aHolderEntry].push_back(aPrs3d); + if( aGatheredMemory > theRequiredMemory ) + return true; + } + } + } + + // To estimate what amount of memory can be obtained + // by cleaning of active holder's presentation + if( theActiveHolderEntry != "" ){ + aHolderIter = theHolderMap.find( theActiveHolderEntry ); + const VISU::TLastVisitedPrsList& aPrsList = aHolderIter->second; + VISU::TLastVisitedPrsList::const_reverse_iterator aPrsIter = aPrsList.rbegin(); + VISU::TLastVisitedPrsList::const_reverse_iterator aPrsIterEnd = aPrsList.rend(); + for(; aPrsIter != aPrsIterEnd; aPrsIter++){ + VISU::TPrs3dPtr aPrs3d = *aPrsIter; + // To avoid of device destruction + if(aPrs3d.GetPointer() == aPrsList.front().GetPointer()) + return false; + + CORBA::Float aMemory = aPrs3d->GetMemorySize(); + + // To prefere "move" action instead of destroy / create + if(aPrsIter == aPrsList.rbegin()) + if(aMemory >= theRequiredMemory) + return false; + + aGatheredMemory += aMemory; + theColoredPrs3dHolderMap[theActiveHolderEntry].push_back(aPrs3d); + if( aGatheredMemory > theRequiredMemory ) + return true; + } + } + + return false; + } + + + //---------------------------------------------------------------------------- + void + ErasePrs3d(VISU::TLastVisitedPrsList& thePrsList, + const VISU::TPrs3dPtr& thePrs3d) + { + VISU::TLastVisitedPrsList::iterator anIter = thePrsList.begin(); + VISU::TLastVisitedPrsList::iterator aEndIter = thePrsList.end(); + for(; anIter != aEndIter; anIter++){ + VISU::TPrs3dPtr aPrs3d = *anIter; + if(aPrs3d.GetPointer() == thePrs3d.GetPointer()) + thePrsList.erase(anIter); + } + } +} + //---------------------------------------------------------------------------- VISU::ColoredPrs3dCache_i @@ -115,26 +269,30 @@ int VISU::ColoredPrs3dCache_i ::IsPossible(VISU::VISUType theType, const VISU::ColoredPrs3dHolder::BasicInput& theInput, - float& theMemoryToClear) + CORBA::Float& theRequiredMemory, + const std::string theHolderEntry) { - //if(MYDEBUG) cout << "VISU::ColoredPrs3dCache_i::IsPossible" << endl; - size_t aMemory = VISU::CheckIsPossible(theType, theInput, true); - - if(aMemory > 0){ + size_t aRawEstimatedMemorySize = VISU::CheckIsPossible(theType, theInput, true); + if(aRawEstimatedMemorySize > 0){ if(GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED){ CORBA::Float aMemoryUsed = GetMemorySize(); CORBA::Float aMemoryLimit = GetLimitedMemory(); - CORBA::Float aMemoryNeeded = CORBA::Float(aMemory/(1024.0*1024.0)); // convert to Mb - if(MYDEBUG) cout<<"Memory needed: "< 0; + return aRawEstimatedMemorySize > 0; } @@ -172,19 +330,16 @@ VISU::ColoredPrs3dCache_i ::CreateHolder(VISU::VISUType theType, const VISU::ColoredPrs3dHolder::BasicInput& theInput) { - float aMemoryToClear = 0.; - if(IsPossible(theType, theInput, aMemoryToClear)) - { + CORBA::Float aRequiredMemory = 0.0; + if(IsPossible(theType, theInput, aRequiredMemory, "")){ VISU::ColoredPrs3dHolder_i* aHolder = new VISU::ColoredPrs3dHolder_i(theType, theInput, *this); - - if( aMemoryToClear > 1 / VTK_LARGE_FLOAT ) - ClearMemory( aMemoryToClear, aHolder ); - VISU::ColoredPrs3d_i* aColoredPrs3d = CreateColoredPrs3d(theType, theInput); std::string aComment = std::string("myComment=") + aColoredPrs3d->GetComment(); std::string aName = aColoredPrs3d->GenerateName().latin1(); aHolder->PublishInStudy(aName, aComment); RegisterInHolder(aColoredPrs3d, aHolder); + if( aRequiredMemory > 1.0 / VTK_LARGE_FLOAT ) + ClearMemory( aRequiredMemory, aHolder->GetEntry() ); return aHolder->_this(); } return VISU::ColoredPrs3dHolder::_nil(); @@ -332,19 +487,12 @@ VISU::ColoredPrs3dCache_i ::FindPrsByInput(TLastVisitedPrsList& theLastVisitedPrsList, const VISU::ColoredPrs3dHolder::BasicInput& theInput) { - // User reverse iteration to start from last visited items TLastVisitedPrsList::iterator anIter = theLastVisitedPrsList.begin(); TLastVisitedPrsList::iterator aEndIter = theLastVisitedPrsList.end(); - for(; anIter != aEndIter; anIter++) - { + for(; anIter != aEndIter; anIter++){ TPrs3dPtr aPrs3d = *anIter; VISU::ColoredPrs3dHolder::BasicInput_var anInput = aPrs3d->GetBasicInput(); - if(anInput->myResult->_is_equivalent(theInput.myResult) && - !strcmp(anInput->myMeshName.in(), theInput.myMeshName.in()) && - anInput->myEntity == theInput.myEntity && - !strcmp(anInput->myFieldName.in(), theInput.myFieldName.in()) && - anInput->myTimeStampNumber == theInput.myTimeStampNumber ) - { + if(IsSameTimeStamp(theInput, anInput)){ theLastVisitedPrsList.erase(anIter); return aPrs3d; } @@ -366,14 +514,16 @@ VISU::ColoredPrs3dCache_i TLastVisitedPrsList& aLastVisitedPrsList = GetLastVisitedPrsList(theHolder); TPrs3dPtr aPrs3d = FindPrsByInput(aLastVisitedPrsList, theInput); bool anIsCheckPossible = GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED; - float aMemoryToClear = 0.; + std::string aHolderEntry = theHolder->GetEntry(); + VISU::VISUType aPrsType = theHolder->GetPrsType(); + CORBA::Float aRequiredMemory = 0.0; if(aPrs3d.GetPointer()){ aLastVisitedPrsList.push_front(aPrs3d); if(MYDEBUG) cout << "FindPrsByInput " << aPrs3d << endl; - }else if(anIsCheckPossible && IsPossible(theHolder->GetPrsType(), theInput, aMemoryToClear)){ - if( aMemoryToClear > 1 / VTK_LARGE_FLOAT ) - ClearMemory(aMemoryToClear, theHolder); - aPrs3d = CreatePrs(theHolder->GetPrsType(), theInput, theHolder); + }else if(anIsCheckPossible && IsPossible(aPrsType, theInput, aRequiredMemory, aHolderEntry)){ + if( aRequiredMemory > 1.0 / VTK_LARGE_FLOAT ) + ClearMemory(aRequiredMemory, aHolderEntry); + aPrs3d = CreatePrs(aPrsType, theInput, theHolder); if(MYDEBUG) cout << "Created " << aPrs3d << endl; }else{ aPrs3d = aLastVisitedPrsList.back(); @@ -386,7 +536,7 @@ VISU::ColoredPrs3dCache_i aLastVisitedPrsList.push_front(aPrs3d); if(MYDEBUG) cout << "Move only " << aPrs3d << endl; } - if(MYDEBUG) PrintCache(); + //if(MYDEBUG) PrintCache(); aPrs3d->SameAs(thePrs); aPrs3d->UpdateActors(); @@ -415,8 +565,10 @@ VISU::ColoredPrs3dCache_i aNewActor->SetVisibility(true); aNewActor->DeepCopy(anActor); aPrs3d->SetActiveState(true); + //aLastVisitedPrs3d->RemoveActors(); } - aViewWindow->Repaint(); + //aViewWindow->Repaint(); + aViewWindow->getRenderWindow()->Render(); return true; } } @@ -432,119 +584,39 @@ void VISU::ColoredPrs3dCache_i ::ClearCache(float theMemory) { - //if(MYDEBUG) cout << "VISU::ColoredPrs3dCache_i::ClearCache( " << theMemory << " )" << endl; - - float aCurrentMemory = GetMemorySize(); - ClearMemory( aCurrentMemory - theMemory, NULL ); + CORBA::Float aCurrentMemory = GetMemorySize(); + ClearMemory( aCurrentMemory - theMemory, "" ); } //---------------------------------------------------------------------------- bool VISU::ColoredPrs3dCache_i -::ClearMemory(float theMemory, VISU::ColoredPrs3dHolder_i* theActiveHolder, bool theOnlyCheck) +::ClearMemory(CORBA::Float theRequiredMemory, + const std::string& theHolderEntry) { - if(MYDEBUG) - { - cout << "VISU::ColoredPrs3dCache_i::ClearMemory( "; - cout << (int)theOnlyCheck; - cout << " , " << theMemory; - if( theActiveHolder ) - cout << " , " << theActiveHolder->GetEntry().c_str(); - cout << " )" << endl; - } - - if( theMemory < 1 / VTK_LARGE_FLOAT ) - return false; - - if(MYDEBUG) PrintCache(); - - TColoredPrs3dHolderMap::iterator aHolderIter = myHolderMap.begin(); - TColoredPrs3dHolderMap::iterator aHolderIterEnd = myHolderMap.end(); - - std::string anEntry; - if( theActiveHolder )//&& myHolderMap.size() > 1 ) - anEntry = theActiveHolder->GetEntry(); - - int anIteration = 0; - for( ; aHolderIter != aHolderIterEnd; aHolderIter++ ) - { - if( aHolderIter->first == anEntry ) - continue; - TLastVisitedPrsList& aPrsList = aHolderIter->second; - anIteration = QMAX( aPrsList.size() - 1, anIteration ); - } - if(MYDEBUG) cout << "Iteration - " << anIteration << endl; - - float aMemory, aGatheredMemory = 0.; - while( anIteration > 0 ) - { - for( aHolderIter = myHolderMap.begin(); aHolderIter != aHolderIterEnd; aHolderIter++ ) - { - if( aHolderIter->first == anEntry ) - continue; - - TLastVisitedPrsList& aPrsList = aHolderIter->second; - if( anIteration < aPrsList.size() ) - { - TPrs3dPtr aPrs3d = aPrsList[anIteration]; - if(MYDEBUG) cout << aPrs3d.GetPointer() << " "; - if( !aPrs3d.GetPointer() ) - continue; - - aMemory = aPrs3d->GetMemorySize(); - aGatheredMemory += aMemory; - if(MYDEBUG) cout << aMemory << " >> " << aGatheredMemory; - - if( !theOnlyCheck ) - aPrsList.pop_back(); - - if( aGatheredMemory > theMemory ) - { - if(MYDEBUG) cout << " STOP" << endl; - if(MYDEBUG) PrintCache(); - return true; - } - if(MYDEBUG) cout << endl; + CORBA::Float aInitialMemorySize = GetMemorySize(); + TColoredPrs3dHolderMap aHolder2PrsToBeDeletedMap; + SelectPrs3dToBeDeleted(theRequiredMemory, theHolderEntry, myHolderMap, aHolder2PrsToBeDeletedMap); + TColoredPrs3dHolderMap::const_iterator aHolderIter = aHolder2PrsToBeDeletedMap.begin(); + TColoredPrs3dHolderMap::const_iterator anEndHolderIter = aHolder2PrsToBeDeletedMap.end(); + for(; aHolderIter != anEndHolderIter; aHolderIter++){ + const std::string aHolderEntry = aHolderIter->first; + TColoredPrs3dHolderMap::iterator anHolderMapIter = myHolderMap.find(aHolderEntry); + if(anHolderMapIter != myHolderMap.end()){ + TLastVisitedPrsList& aLastVisitedPrsList = anHolderMapIter->second; + + const TLastVisitedPrsList& aPrsToBeDeletedList = aHolderIter->second; + TLastVisitedPrsList::const_iterator anIter = aPrsToBeDeletedList.begin(); + TLastVisitedPrsList::const_iterator aEndIter = aPrsToBeDeletedList.end(); + for(; anIter != aEndIter; anIter++){ + TPrs3dPtr aPrs3d = *anIter; + ErasePrs3d(aLastVisitedPrsList, aPrs3d); } } - anIteration--; } - - // clear active holder in a last order - if( theActiveHolder ) - { - aHolderIter = myHolderMap.find( anEntry ); - - TLastVisitedPrsList& aPrsList = aHolderIter->second; - TLastVisitedPrsList::reverse_iterator aPrsIter = aPrsList.rbegin(); - TLastVisitedPrsList::reverse_iterator aPrsIterEnd = aPrsList.rend(); - for(; aPrsIter != aPrsIterEnd; aPrsIter++) - { - TPrs3dPtr aPrs3d = *aPrsIter; - if(MYDEBUG) cout << aPrs3d.GetPointer() << " "; - if( !aPrs3d.GetPointer() ) - continue; - - aMemory = aPrs3d->GetMemorySize(); - aGatheredMemory += aMemory; - if(MYDEBUG) cout << aMemory << " >> " << aGatheredMemory; - - if( !theOnlyCheck ) - aPrsList.pop_back(); - - if( aGatheredMemory > theMemory ) - { - if(MYDEBUG) cout << " STOP" << endl; - if(MYDEBUG) PrintCache(); - return true; - } - if(MYDEBUG) cout << endl; - } - } - - if(MYDEBUG) PrintCache(); - return false; + CORBA::Float aCurrentMemory = GetMemorySize(); + return (aInitialMemorySize - aCurrentMemory >= theRequiredMemory); } diff --git a/src/VISU_I/VISU_ColoredPrs3dCache_i.hh b/src/VISU_I/VISU_ColoredPrs3dCache_i.hh index df42f7f7..8f5e8a85 100644 --- a/src/VISU_I/VISU_ColoredPrs3dCache_i.hh +++ b/src/VISU_I/VISU_ColoredPrs3dCache_i.hh @@ -41,7 +41,6 @@ namespace VISU typedef std::deque TLastVisitedPrsList; typedef std::string THolderEntry; - //typedef std::deque TLastVisitedPrsList; typedef std::map TColoredPrs3dHolderMap; /*! @@ -168,7 +167,8 @@ namespace VISU virtual int IsPossible(VISU::VISUType theType, const VISU::ColoredPrs3dHolder::BasicInput& theInput, - float& theMemoryToClear); + float& theMemoryToClear, + const std::string theHolderEntry); TPrs3dPtr FindPrsByInput(TLastVisitedPrsList& theLastVisitedPrsList, @@ -179,9 +179,8 @@ namespace VISU ClearCache(float theMemory = 0); bool - ClearMemory(float theMemory, - VISU::ColoredPrs3dHolder_i* theActiveHolder, - bool theOnlyCheck = false); + ClearMemory(CORBA::Float theRequiredMemory, + const std::string& theHolderEntry); void PrintCache();