27 static LoggerPtr logger(Logger::getLogger(
"pz.strmatrix.tpzpairstructmatrix"));
28 static LoggerPtr loggerel(Logger::getLogger(
"pz.strmatrix.element"));
37 std::cout <<
"fNumThreads e "<< fNumThreads <<
" gNumThreads esta setando para " <<
gNumThreads << endl;
53 class StageOne_t :
public tbb::filter
60 std::vector<PipeItem_t> items;
61 unsigned current_item;
63 std::set<int>& fMaterialIds;
65 bool ShouldCompute(
int matid)
67 return fMaterialIds.size()==0 || fMaterialIds.find(matid) != fMaterialIds.end();
72 StageOne_t(
unsigned tokens,
TPZCompMesh& _mesh, std::set<int>& materialIds) :
73 tbb::filter(
true), current_iel(0),
75 items(tokens), current_item(0), fMaterialIds(materialIds)
78 unsigned n_tokens() {
return items.size();}
80 void* operator()(
void*)
85 while(current_iel < nelem)
87 el = elementvec[current_iel++];
91 if(fMaterialIds.size() == 0) {
111 int matid = mat->
Id();
112 if(this->ShouldCompute(matid) ==
false)
continue;
121 PipeItem_t& item = items[current_item];
122 current_item = (current_item+1) % items.size();
136 class StageTwo_t:
public tbb::filter
143 StageTwo_t(
TPZCompMesh* _mesh,
int _mineq,
int _maxeq) :
144 tbb::filter(
false), mesh(_mesh), mineq(_mineq), maxeq(_maxeq)
147 void* operator()(
void* item_p)
149 PipeItem_t& item = *
static_cast<PipeItem_t*
>(item_p);
154 item.el->CalcStiff(item.ek,item.ef);
158 if (item.el->HasDependency())
161 item.ek.ApplyConstraints();
162 item.ef.ApplyConstraints();
165 item.ek.ComputeDestinationIndices();
167 if(mineq != -1 && maxeq != -1)
177 class StageThree_t:
public tbb::filter
184 tbb::filter(
true), fGlobMatrix1(_fGlobMatrix1), fGlobRhs(_fGlobRhs)
187 void* operator()(
void* item_p)
189 PipeItem_t& item = *
static_cast<PipeItem_t*
>(item_p);
211 class StageFour_t:
public tbb::filter
220 for(iel = 0; iel<nel; iel++)
221 index[iel] = fPermuteScatter[index[iel]];
226 tbb::filter(
true), fGlobMatrix2(_fGlobMatrix2),
230 void* operator()(
void* item_p)
232 PipeItem_t& item = *
static_cast<PipeItem_t*
>(item_p);
264 for(iel = 0; iel<nel; iel++)
285 work_items.push_back(el);
292 typedef spin_mutex MatrixMutex_t;
293 MatrixMutex_t Matrix1Mutex;
294 MatrixMutex_t Matrix2Mutex;
299 void run_parallel_for()
303 parallel_for(blocked_range<size_t>(0,work_items.size(), 1 ), *
this);
307 void operator()(
const blocked_range<size_t>& range)
309 MatrixMutex_t::scoped_lock lock;
313 for(
size_t i=range.begin(); i!=range.end(); ++i ) {
329 if(mineq != -1 && maxeq != -1)
333 lock.acquire(Matrix1Mutex);
348 lock.acquire(Matrix2Mutex);
363 std::vector<TPZCompEl*>::iterator it = work_items.begin();
364 for(; it != work_items.end(); it++)
374 ek.ApplyConstraints();
375 ef.ApplyConstraints();
378 ek.ComputeDestinationIndices();
385 first->AddKel(ek.fMat,ek.fSourceIndex,ek.fDestinationIndex);
386 rhs.AddFel(ef.fMat,ek.fSourceIndex,ek.fDestinationIndex);
390 first->AddKel(ek.fConstrMat,ek.fSourceIndex,ek.fDestinationIndex);
391 rhs.AddFel(ef.fConstrMat,ek.fSourceIndex,ek.fDestinationIndex);
396 second->AddKel(ek.fMat,ek.fSourceIndex,ek.fDestinationIndex);
408 first(_first), second(_second),
409 rhs(_rhs), fPermuteScatter(permuteScatter), fStrMatrix(strmat)
424 cerr <<
"TPZPairStructMatrix::TBBAssemble() invoked, but TBB " 425 <<
"was not linked. Executing SerialAssemble!" << endl;
428 #else // if USING_TBB 435 tbb::pipeline pipeline;
438 StageOne_t filter1(tbb_pair_pipe_tokens.
get_value() , mesh, fMaterialIds);
439 StageTwo_t filter2(fMesh, mineq, maxeq);
440 StageThree_t<STATE> filter3(*first, rhs);
443 pipeline.add_filter(filter1);
444 pipeline.add_filter(filter2);
445 pipeline.add_filter(filter3);
446 pipeline.add_filter(filter4);
449 pipeline.run(filter1.n_tokens() );
462 TPZTimer calcstiff(
"Computing the stiffness matrices");
463 TPZTimer assemble(
"Assembling the stiffness matrices");
466 for(iel=0; iel < nelem; iel++) {
477 ek.ComputeDestinationIndices();
480 first->
AddKel(ek.fMat,ek.fSourceIndex,ek.fDestinationIndex);
481 rhs.
AddFel(ef.
fMat,ek.fSourceIndex,ek.fDestinationIndex);
483 second->
AddKel(ek.fMat,ek.fSourceIndex,ek.fDestinationIndex);
485 if(loggerel->isDebugEnabled())
487 std::stringstream sout;
488 ek.fMat.Print(
"Element stiffness matrix",sout);
489 ef.
fMat.
Print(
"Element right hand side", sout);
495 ek.ApplyConstraints();
497 ek.ComputeDestinationIndices();
500 first->
AddKel(ek.fConstrMat,ek.fSourceIndex,ek.fDestinationIndex);
504 second->
AddKel(ek.fMat,ek.fSourceIndex,ek.fDestinationIndex);
511 std::stringstream sout;
512 sout <<
"Number of equations " << first->
Rows() << std::endl;
513 sout << calcstiff.
processName() <<
" " << calcstiff << std::endl;
517 if (logger->isDebugEnabled())
530 for(iel = 0; iel<nel; iel++)
539 for(iel = 0; iel<nel; iel++)
548 std::cout <<
"TPZPairStructMatrix::Assemble = Assembling the system of equations with " << fNumThreads
549 <<
" threads (TPZPairStructMatrix::gNumThreads = " << TPZPairStructMatrix::gNumThreads <<
")\n";
558 else if(numthreads > 0)
569 std::vector<pthread_t> allthreads(numthreads+1);
584 for(itr=0; itr<numthreads+1; itr++)
593 fGlobMatrix1(&mat1), fGlobMatrix2(&mat2), fGlobRhs(&rhs),fNextElement(0)
629 if(loggerel->isDebugEnabled())
631 std::stringstream sout;
632 ek->
fMat.
Print(
"Element stiffness matrix",sout);
633 ef->
fMat.
Print(
"Element right hand side", sout);
661 for(iel = 0; iel<nel; iel++)
670 for(iel = 0; iel<nel; iel++)
686 while(nextel < nel || numprocessed)
689 std::set<int>::iterator itprocess;
690 bool keeplooking =
false;
695 if(itavail->first == *itprocess)
705 int iel = *itprocess;
706 std::stringstream sout;
707 sout <<
"Assembling element " << iel;
708 if (logger->isDebugEnabled())
733 LOGPZ_DEBUG(logger,
"Going to sleep within assembly")
756 while(nextel < nel || numprocessed)
758 std::map<int, TPZAutoPointer<TPZElementMatrix> >::iterator itavail;
759 std::set<int>::iterator itprocess;
760 bool keeplooking =
false;
765 if(itavail->first == *itprocess)
774 int iel = *itprocess;
775 std::stringstream sout;
776 sout <<
"Assembling element " << iel;
777 if (logger->isDebugEnabled())
803 LOGPZ_DEBUG(logger,
"Going to sleep within assembly")
841 int matid = mat->
Id();
856 std::stringstream sout;
857 sout << __PRETTY_FUNCTION__ <<
" returning " << nextel <<
" fNextElement " <<
fNextElement;
858 if (logger->isDebugEnabled())
885 std::set<int>::const_iterator it;
886 std::stringstream sout;
887 sout <<
"setting input material ids ";
888 for(it=materialids.begin(); it!= materialids.end(); it++)
892 if (logger->isDebugEnabled())
int64_t NElements() const
Number of computational elements allocated.
#define PZ_PTHREAD_MUTEX_UNLOCK(mutex, fn)
TPZSemaphore fAssembly2
Semaphore (to wake up the second assembly thread)
The timer class. Utility.
Contains TPZAnalysis class which implements the sequence of actions to perform a finite element analy...
~ThreadData()
Destroy the mutex semaphores and others.
void push_work_item(TPZCompEl *el)
virtual void SetEquationRange(int64_t mineq, int64_t maxeq)
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
Implements a vector class which allows to use external storage provided by the user. Utility.
pthread_mutex_t fAccessElement
Mutexes (to choose which element is next)
Timing class. Absolutely copied from GNU time. Take a look at
virtual TPZCompMesh * Mesh() const
Access method for the mesh pointer.
Contains the thread data for matrices divided in sub structures.
TPZPairStructMatrix(TPZCompMesh *mesh, TPZVec< int > &permutescatter)
void AddFel(TPZFMatrix< TVar > &rhs, TPZVec< int64_t > &destination)
Performs a right hand side assemblage.
void TBBAssemble(TPZMatrix< STATE > *first, TPZMatrix< STATE > *second, TPZFMatrix< STATE > &rhs)
static void * ThreadWork(void *threaddata)
The function which will compute the matrices.
clarg::argInt tbb_pair_pipe_tokens("-tbb_pair_ntokens", "# tokens during assemble TPZPairStructMatrix (level 2) using TBB", 128)
TPZFNMatrix< 1000, STATE > fMat
Pointer to a blocked matrix object.
Contains declaration of TPZCompEl class which defines the interface of a computational element...
std::set< int > fProcessed2
Elements which are being processed maintained by the second global matrix.
virtual void CalcStiff(TPZElementMatrix &ek, TPZElementMatrix &ef)
Computes the element stifness matrix and right hand side.
TPZMatrix< STATE > * fGlobMatrix2
Global matrix2.
ThreadData(TPZStructMatrix *strmatrix, TPZMatrix< STATE > &mat1, TPZMatrix< STATE > &mat2, TPZFMatrix< STATE > &rhs)
Initialize the mutex semaphores and others.
int64_t NElements() const
Access method to query the number of elements of the vector.
Implements a chunk vector with free store administration. Utility.
TPZFMatrix< STATE > & rhs
Contains the TPZStructMatrixOR class which responsible for a interface among Matrix and Finite Elemen...
void PermuteScatter(TPZVec< int > &index)
parallel_assemble_task_t(TPZStructMatrix *strmat, int _mineq, int _maxeq, TPZMatrix< STATE > *_first, TPZMatrix< STATE > *_second, TPZFMatrix< STATE > &_rhs, TPZVec< int > &permuteScatter)
static void * ThreadAssembly2(void *threaddata)
The function which will compute the assembly.
Iterações do laço podem ser executadas em paralelo. .
std::map< int, TPZAutoPointer< TPZElementMatrix > > fSubmitted2
List of computed element matrices (autopointers?)
TPZManVector< int64_t > fDestinationIndex
void ComputeDestinationIndices()
Refines geometrical mesh (all the elements) num times.
This abstract class defines the behaviour which each derived class needs to implement.
Contains declaration of TPZElementMatrix struct which associates an element matrix with the coeficien...
#define PZ_PTHREAD_JOIN(thread, val, fn)
TPZFMatrix< STATE > * fGlobRhs
Global rhs.
virtual const std::set< int > & MaterialIds()
Returns the material ids.
virtual TPZMaterial * Material() const
Identify the material object associated with the element.
#define PZ_PTHREAD_MUTEX_LOCK(mutex, fn)
std::string & processName()
Gets the process name (for reporting purposes).
void start()
Turns the timer on.
Implements a group of computational elements as a mesh and an element. Computational Mesh...
virtual void SetNumThreads(int n)
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
TPZMatrix< STATE > * second
void parallel_for(int n, body_t &obj)
int64_t Rows() const
Returns number of rows.
TPZVec< int > fPermuteScatter
virtual void AddKel(TPZFMatrix< TVar > &elmat, TPZVec< int64_t > &destinationindex)
Add a contribution of a stiffness matrix.
void MultiThread_Assemble(TPZMatrix< STATE > *first, TPZMatrix< STATE > *second, TPZFMatrix< STATE > &rhs)
Contains the TPZPairStructMatrix class.
TPZStructMatrix * fStrMatrix
#define PZ_PTHREAD_MUTEX_DESTROY(mutex, fn)
TPZVec< int > fPermuteScatter
Vector which defines the permutation of all equations to internal equations.
TPZManVector< int64_t > fSourceIndex
void PermuteScatter(TPZVec< int > &index)
TPZMatrix< STATE > * fGlobMatrix1
Global matrix1.
Full matrix class. Matrix.
std::vector< TPZCompEl * > work_items
TPZStructMatrix fStrMatrix
void PermuteScatter(TPZVec< int > &index)
bool NeedsComputing(const std::set< int > &matids) override
Verifies if any element needs to be computed corresponding to the material ids.
std::set< int > fProcessed1
Elements which are being processed maintained by the first global matrix.
void SerialAssemble(TPZMatrix< STATE > *first, TPZMatrix< STATE > *second, TPZFMatrix< STATE > &rhs)
This class associates an element matrix with the coeficients of its contribution in the global stiffn...
TPZFNMatrix< 1000, STATE > fConstrMat
Pointer to the constrained matrix object.
void ApplyConstraints()
Apply the constraints applied to the nodes by transforming the tangent matrix and right hand side...
void Assemble(TPZMatrix< STATE > *first, TPZMatrix< STATE > *second, TPZFMatrix< STATE > &rhs)
TPZMatrix< STATE > * first
Contains declaration of TPZSubCompMesh class which implements a group of computational elements as a ...
bool ShouldCompute(int matid)
Establish whether the element should be computed.
virtual void SetMaterialIds(const std::set< int > &materialids)
Set the set of material ids which will be considered when assembling the system.
int NextElement()
Look for an element index which needs to be computed and put it on the stack.
void SetMaterialIds(const std::set< int > &materialids)
Set the set of material ids which will be considered when assembling the system.
Implements computational mesh. Computational Mesh.
const T & get_value() const
TPZAdmChunkVector< TPZCompEl * > & ElementVec()
Returns a reference to the element pointers vector.
#define PZ_PTHREAD_CREATE(thread, attr, routine, args, fn)
virtual int GetNumThreads() const
TPZVec< int > & fPermuteScatter
int fNextElement
Current element.
TPZSemaphore fAssembly1
Semaphore (to wake up the first assembly thread)
virtual void FilterEquations(TPZVec< int64_t > &origindex, TPZVec< int64_t > &destindex) const
Filter out the equations which are out of the range.
virtual void Print(std::ostream &out) const
void stop()
Turns the timer off, and computes the elapsed time.
int64_t NElements() const
Returns the number of elements of the vector.
static void * ThreadAssembly1(void *threaddata)
The function which will compute the assembly.
#define PZ_PTHREAD_MUTEX_INIT(mutex, attr, fn)
Defines the interface of a computational element. Computational Element.
TPZStructMatrix * fStrMatrix
Current structmatrix object.
virtual int HasDependency()
Returns 1 if the element has at least one dependent node. Returns 0 otherwise.
bool HasDependency()
Returns true if the element has at least one dependent node. Returns false otherwise.
std::map< int, std::pair< TPZAutoPointer< TPZElementMatrix >, TPZAutoPointer< TPZElementMatrix > > > fSubmitted1
List of computed element matrices (autopointers?)
Root matrix class (abstract). Matrix.
void ComputedElementMatrix(int iel, TPZAutoPointer< TPZElementMatrix > &ek, TPZAutoPointer< TPZElementMatrix > &ef)
Put the computed element matrices in the map.
This class implements a reference counter mechanism to administer a dynamically allocated object...