X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSMESHDS%2FSMESHDS_GroupOnFilter.cxx;h=d8ea06fdac80901e5ac6c4349b697fb5c3af35ee;hb=HEAD;hp=23c14e996ec36cf166c43e15970aa2f74599d0bc;hpb=251f8c052dd12dd29922210dc901b295fe999a0e;p=modules%2Fsmesh.git diff --git a/src/SMESHDS/SMESHDS_GroupOnFilter.cxx b/src/SMESHDS/SMESHDS_GroupOnFilter.cxx index 23c14e996..d8ea06fda 100644 --- a/src/SMESHDS/SMESHDS_GroupOnFilter.cxx +++ b/src/SMESHDS/SMESHDS_GroupOnFilter.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -26,13 +26,18 @@ #include "SMESHDS_GroupOnFilter.hxx" #include "SMDS_SetIterator.hxx" +#include "ObjectPool.hxx" #include "SMESHDS_Mesh.hxx" #include #include +#include + using namespace std; +//#undef WITH_TBB + //============================================================================= /*! * Creates a group based on thePredicate @@ -43,11 +48,12 @@ SMESHDS_GroupOnFilter::SMESHDS_GroupOnFilter (const int theID, const SMESHDS_Mesh* theMesh, const SMDSAbs_ElementType theType, const SMESH_PredicatePtr& thePredicate) - : SMESHDS_GroupBase(theID,theMesh,theType), + : SMESHDS_GroupBase( theID, theMesh, theType ), + SMDS_ElementHolder( theMesh ), myMeshInfo( SMDSEntity_Last, 0 ), - myMeshModifTime(0), - myPredicateTic(0), - myNbElemToSkip(0) + myMeshModifTime( 0 ), + myPredicateTic( 0 ), + myNbElemToSkip( 0 ) { SetPredicate( thePredicate ); } @@ -73,7 +79,7 @@ void SMESHDS_GroupOnFilter::SetPredicate( const SMESH_PredicatePtr& thePredicate */ //================================================================================ -int SMESHDS_GroupOnFilter::Extent() const +smIdType SMESHDS_GroupOnFilter::Extent() const { update(); return std::accumulate( myMeshInfo.begin(), myMeshInfo.end(), 0 ); @@ -114,7 +120,7 @@ bool SMESHDS_GroupOnFilter::IsEmpty() */ //================================================================================ -bool SMESHDS_GroupOnFilter::Contains (const int theID) +bool SMESHDS_GroupOnFilter::Contains (const smIdType theID) { return myPredicate && myPredicate->IsSatisfy( theID ); } @@ -141,6 +147,7 @@ namespace // Iterator size_t myNbToFind, myNbFound, myTotalNb; vector< const SMDS_MeshElement*>& myFoundElems; bool & myFoundElemsOK; + bool myFoundElemsChecked; TIterator( const SMESH_PredicatePtr& filter, SMDS_ElemIteratorPtr& elems, @@ -155,14 +162,15 @@ namespace // Iterator myNbFound( 0 ), myTotalNb( totalNb ), myFoundElems( foundElems ), - myFoundElemsOK( foundElemsOK ) + myFoundElemsOK( foundElemsOK ), + myFoundElemsChecked( false ) { myFoundElemsOK = false; next(); } ~TIterator() { - if ( !myFoundElemsOK ) + if ( !myFoundElemsChecked && !myFoundElemsOK ) clearVector( myFoundElems ); } virtual bool more() @@ -219,6 +227,8 @@ namespace // Iterator } if ( !myFoundElemsOK ) clearVector( myFoundElems ); + + myFoundElemsChecked = true; // in destructor: not to clearVector() which may already die } }; @@ -245,6 +255,9 @@ SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const { myPredicate->SetMesh( GetMesh() ); // hope myPredicate updates self here if necessary + if ( !IsUpToDate() ) + updateParallel(); + elemIt = GetMesh()->elementsIterator( GetType() ); if ( IsUpToDate() ) { @@ -275,7 +288,7 @@ SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const */ //================================================================================ -std::vector< int > SMESHDS_GroupOnFilter::GetMeshInfo() const +std::vector< smIdType > SMESHDS_GroupOnFilter::GetMeshInfo() const { update(); return myMeshInfo; @@ -302,7 +315,7 @@ int SMESHDS_GroupOnFilter::getElementIds( void* ids, size_t idSize ) const if ( IsUpToDate() ) { for ( ; elIt->more(); curID += idSize ) - (*(int*) curID) = elIt->next()->GetID(); + (*(smIdType*) curID) = elIt->next()->GetID(); } else { @@ -312,11 +325,11 @@ int SMESHDS_GroupOnFilter::getElementIds( void* ids, size_t idSize ) const me->myMeshInfo.assign( SMDSEntity_Last, 0 ); me->myMeshInfo[ firstOkElem->GetEntityType() ]++; - (*(int*) curID) = firstOkElem->GetID(); + (*(smIdType*) curID) = firstOkElem->GetID(); for ( curID += idSize; elIt->more(); curID += idSize ) { const SMDS_MeshElement* e = elIt->next(); - (*(int*) curID) = e->GetID(); + (*(smIdType*) curID) = e->GetID(); me->myMeshInfo[ e->GetEntityType() ]++; } } @@ -360,18 +373,140 @@ void SMESHDS_GroupOnFilter::update() const if ( !IsUpToDate() ) { me->setChanged(); - SMDS_ElemIteratorPtr elIt = GetElements(); - if ( elIt->more() ) { - // find out nb of elements to skip w/o check before the 1st OK element - const SMDS_MeshElement* e = me->setNbElemToSkip( elIt ); - ++me->myMeshInfo[ e->GetEntityType() ]; - while ( elIt->more() ) - ++me->myMeshInfo[ elIt->next()->GetEntityType() ]; + if ( !updateParallel() ) + { + SMDS_ElemIteratorPtr elIt = GetElements(); + if ( elIt->more() ) { + // find out nb of elements to skip w/o check before the 1st OK element + const SMDS_MeshElement* e = me->setNbElemToSkip( elIt ); + ++me->myMeshInfo[ e->GetEntityType() ]; + while ( elIt->more() ) + ++me->myMeshInfo[ elIt->next()->GetEntityType() ]; + } } me->setChanged( false ); } } +//================================================================================ +/*! + * \brief Updates myElements in parallel + */ +//================================================================================ +#ifdef WITH_TBB + +#ifdef WIN32 +// See https://docs.microsoft.com/en-gb/cpp/porting/modifying-winver-and-win32-winnt?view=vs-2019 +// Windows 10 = 0x0A00 +#define WINVER 0x0A00 +#define _WIN32_WINNT 0x0A00 + +#endif + +#include +#include "tbb/enumerable_thread_specific.h" + +// a predicate per a thread +typedef tbb::enumerable_thread_specific TLocalPredicat; + +struct IsSatisfyParallel +{ + vector< char >& myIsElemOK; + SMESH_PredicatePtr myPredicate; + TLocalPredicat& myLocalPredicates; + IsSatisfyParallel( SMESH_PredicatePtr mainPred, TLocalPredicat& locPred, vector< char >& isOk ) + : myIsElemOK(isOk), myPredicate( mainPred ), myLocalPredicates( locPred ) + {} + void operator() ( const tbb::blocked_range& r ) const + { + SMESH_PredicatePtr& pred = myLocalPredicates.local(); + if ( !pred ) + { + if ( r.begin() == 0 ) + pred = myPredicate; + else + pred.reset( myPredicate->clone() ); + } + for ( size_t i = r.begin(); i != r.end(); ++i ) + myIsElemOK[ i ] = char( pred->IsSatisfy( i )); + } +}; + +bool SMESHDS_GroupOnFilter::updateParallel() const +{ + // if ( !getenv("updateParallel")) + // return false; + size_t nbElemsOfType = GetMesh()->GetMeshInfo().NbElements( GetType() ); + if ( nbElemsOfType == 0 ) + return true; + if ( nbElemsOfType < 1000000 ) + return false; // no sense in parallel work + + SMDS_ElemIteratorPtr elemIt = GetMesh()->elementsIterator( GetType() ); + const smIdType minID = elemIt->next()->GetID(); + myPredicate->IsSatisfy( minID ); // make myPredicate fully initialized for clone() + SMESH_PredicatePtr clone( myPredicate->clone() ); + if ( !clone ) + return false; + + TLocalPredicat threadPredicates; + threadPredicates.local() = clone; + + smIdType maxID = ( GetType() == SMDSAbs_Node ) ? GetMesh()->MaxNodeID() : GetMesh()->MaxElementID(); + vector< char > isElemOK( 1 + maxID ); + + tbb::parallel_for ( tbb::blocked_range( 0, isElemOK.size() ), + IsSatisfyParallel( myPredicate, threadPredicates, isElemOK ), + tbb::simple_partitioner()); + + SMESHDS_GroupOnFilter* me = const_cast( this ); + + int nbOkElems = 0; + for ( size_t i = minID; i < isElemOK.size(); ++i ) + nbOkElems += ( isElemOK[ i ]); + me->myElements.resize( nbOkElems ); + + const SMDS_MeshElement* e; + size_t iElem = 0; + if ( GetType() == SMDSAbs_Node ) + { + for ( size_t i = minID; i < isElemOK.size(); ++i ) + if (( isElemOK[ i ] ) && + ( e = GetMesh()->FindNode( i ))) + { + me->myElements[ iElem++ ] = e; + } + me->myMeshInfo[ SMDSEntity_Node ] = myElements.size(); + } + else + { + for ( size_t i = minID; i < isElemOK.size(); ++i ) + if (( isElemOK[ i ] ) && + ( e = GetMesh()->FindElement( i )) && + ( e->GetType() == GetType() )) + { + me->myElements[ iElem++ ] = e; + ++me->myMeshInfo[ e->GetEntityType() ]; + } + } + me->myElementsOK = ( iElem < nbElemsOfType ); + if ( !myElementsOK ) + clearVector( me->myElements ); // all elements satisfy myPredicate + else + me->myElements.resize( iElem ); + + me->setChanged( false ); + return true; +} +#else + +bool SMESHDS_GroupOnFilter::updateParallel() const +{ + return false; +} + +#endif + //================================================================================ /*! * \brief Sets myMeshModifTime and clear fields according to modification state @@ -413,3 +548,38 @@ SMESHDS_GroupOnFilter::setNbElemToSkip( SMDS_ElemIteratorPtr& okElemIt ) } return firstOkElem; } + +//================================================================================ +/*! + * \brief Return elements before mesh compacting + */ +//================================================================================ + +SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::getElements() +{ + return boost::make_shared< SMDS_ElementVectorIterator >( myElements.begin(), myElements.end() ); +} + +//================================================================================ +/*! + * \brief clear myElements before re-filling after mesh compacting + */ +//================================================================================ + +void SMESHDS_GroupOnFilter::tmpClear() +{ + std::vector< const SMDS_MeshElement*> newElems( myElements.size() ); + myElements.swap( newElems ); + myElements.clear(); +} + +//================================================================================ +/*! + * \brief Re-fill myElements after mesh compacting + */ +//================================================================================ + +void SMESHDS_GroupOnFilter::add( const SMDS_MeshElement* element ) +{ + myElements.push_back( element ); +}