Adding SetNbThreads in Python to set if compute will be done in parallel

This commit is contained in:
Yoann Audouin 2022-08-30 09:42:21 +02:00
parent 0be5e44fdf
commit 5d3c1425f8
7 changed files with 57 additions and 37 deletions

View File

@ -899,6 +899,10 @@ module SMESH
*/ */
boolean SetMeshOrder(in submesh_array_array theSubMeshArray); boolean SetMeshOrder(in submesh_array_array theSubMeshArray);
/*!
* \brief Set Number of Threads
*/
void SetNbThreads(in long nbThreads);
/*! /*!
* Get mesh description * Get mesh description

View File

@ -385,9 +385,14 @@ class SMESH_EXPORT SMESH_Mesh
std::ostream& Dump(std::ostream & save); std::ostream& Dump(std::ostream & save);
// Data for parallel computation
void Lock() {_my_lock.lock();}; void Lock() {_my_lock.lock();};
void Unlock() {_my_lock.unlock();}; void Unlock() {_my_lock.unlock();};
int GetNbThreads(){return _NbThreads;};
void SetNbThreads(int nbThreads){_NbThreads=nbThreads;};
// Temporary folder used during parallel Computation // Temporary folder used during parallel Computation
boost::filesystem::path tmp_folder; boost::filesystem::path tmp_folder;
@ -439,6 +444,7 @@ protected:
// Mutex for multhitreading write in SMESH_Mesh // Mutex for multhitreading write in SMESH_Mesh
std::mutex _my_lock; std::mutex _my_lock;
int _NbThreads=0;
protected: protected:
SMESH_Mesh(); SMESH_Mesh();

View File

@ -271,8 +271,7 @@ bool SMESH_subMesh::IsMeshComputed() const
TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type ); TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
for ( ; exp.More(); exp.Next() ) for ( ; exp.More(); exp.Next() )
{ {
SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ); if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ) )
if ( smDS )
{ {
bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes(); bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
if ( computed ) if ( computed )
@ -1595,7 +1594,6 @@ bool SMESH_subMesh::ComputeStateEngine(compute_event event)
} }
else else
{ {
// TODO: Replace by call to ParallelCompute
ret = algo->Compute((*_father), shape); ret = algo->Compute((*_father), shape);
} }
// algo can set _computeError of submesh // algo can set _computeError of submesh
@ -1656,7 +1654,6 @@ bool SMESH_subMesh::ComputeStateEngine(compute_event event)
bool isComputeErrorSet = !checkComputeError( algo, ret, shape ); bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
if ( isComputeErrorSet ) if ( isComputeErrorSet )
ret = false; ret = false;
// TODO: See why IsMeshCompited() returns false
// check if anything was built // check if anything was built
TopExp_Explorer subS(shape, _subShape.ShapeType()); TopExp_Explorer subS(shape, _subShape.ShapeType());
if ( ret ) if ( ret )
@ -2053,9 +2050,7 @@ bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
if ( !_computeError || _computeError->IsOK() ) if ( !_computeError || _computeError->IsOK() )
{ {
// no error description is set to this sub-mesh, check if any mesh is computed // no error description is set to this sub-mesh, check if any mesh is computed
//TODO: See why this does not work _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
//_computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
_computeState = COMPUTE_OK;
if ( _computeState != COMPUTE_OK ) if ( _computeState != COMPUTE_OK )
{ {
if ( _subShape.ShapeType() == TopAbs_EDGE && if ( _subShape.ShapeType() == TopAbs_EDGE &&

View File

@ -2212,7 +2212,7 @@ bool _pyMesh::NeedMeshAccess( const Handle(_pyCommand)& theCommand )
"GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces", "GetElemNode","IsMediumNode","IsMediumNodeOfAnyElem","ElemNbEdges","ElemNbFaces",
"GetElemFaceNodes", "GetFaceNormal", "FindElementByNodes", "GetElemFaceNodes", "GetFaceNormal", "FindElementByNodes",
"IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor", "IsPoly","IsQuadratic","BaryCenter","GetHypothesisList", "SetAutoColor", "GetAutoColor",
"Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder" "Clear", "ConvertToStandalone", "GetMeshOrder", "SetMeshOrder", "SetNbThreads"
,"" }; // <- mark of end ,"" }; // <- mark of end
sameMethods.Insert( names ); sameMethods.Insert( names );
} }

View File

@ -7037,6 +7037,16 @@ TListOfListOfInt SMESH_Mesh_i::findConcurrentSubMeshes()
return res; return res;
} }
//=============================================================================
/*!
* \brief Set the number of threads for a parallel computation
*/
//=============================================================================
void SMESH_Mesh_i::SetNbThreads(int nbThreads){
_impl->SetNbThreads(nbThreads);
}
//============================================================================= //=============================================================================
/*! /*!
* \brief Convert submesh ids into submesh interfaces * \brief Convert submesh ids into submesh interfaces

View File

@ -673,6 +673,8 @@ private:
SMESH::submesh_array_array& theSubMeshOrder, SMESH::submesh_array_array& theSubMeshOrder,
const bool theIsDump); const bool theIsDump);
void SetNbThreads(int nbThreads);
/*! /*!
* \brief Finds concurrent sub-meshes * \brief Finds concurrent sub-meshes
*/ */

View File

@ -1864,7 +1864,7 @@ class Mesh(metaclass = MeshMeta):
return self.smeshpyD.Evaluate(self.mesh, geom) return self.smeshpyD.Evaluate(self.mesh, geom)
def Compute(self, geom=0, discardModifs=False, refresh=False): def Compute(self, geom=0, discardModifs=False, refresh=False, nbThreads=0):
""" """
Compute the mesh and return the status of the computation Compute the mesh and return the status of the computation
@ -1874,6 +1874,7 @@ class Mesh(metaclass = MeshMeta):
a last total re-compute and that may prevent successful partial re-compute, a last total re-compute and that may prevent successful partial re-compute,
then the mesh is cleaned before Compute() then the mesh is cleaned before Compute()
refresh: if *True*, Object Browser is automatically updated (when running in GUI) refresh: if *True*, Object Browser is automatically updated (when running in GUI)
nbThreads: Number of threads to use for a parallel computation
Returns: Returns:
True or False True or False
@ -1885,6 +1886,8 @@ class Mesh(metaclass = MeshMeta):
try: try:
if discardModifs and self.mesh.HasModificationsToDiscard(): # issue 0020693 if discardModifs and self.mesh.HasModificationsToDiscard(): # issue 0020693
self.mesh.Clear() self.mesh.Clear()
# Setting parallel parameters
self.mesh.SetNbThreads(nbThreads)
ok = self.smeshpyD.Compute(self.mesh, geom) ok = self.smeshpyD.Compute(self.mesh, geom)
except SALOME.SALOME_Exception as ex: except SALOME.SALOME_Exception as ex:
print("Mesh computation failed, exception caught:") print("Mesh computation failed, exception caught:")