NeoPZ
TPZFront.h
Go to the documentation of this file.
1 
6 #ifndef TPZFRONT_H
7 #define TPZFRONT_H
8 
9 #include "pzmatrix.h"
10 #include "pzstack.h"
11 #include "pzvec.h"
12 #include "TPZThreadTools.h"
13 
14 template<class TVar>
15 class TPZEqnArray;
16 
25 template<class TVar>
26 class TPZFront : public TPZSavable {
27 
28 public:
29 
31  //static void main();
32 
33  int64_t NElements();
35  virtual ~TPZFront();
37  TPZFront();
39  TPZFront(
40  int64_t GlobalSize
41  );
42 
43  TPZFront(const TPZFront<TVar> &cp);
44 
50  void SymbolicDecomposeEquations(int64_t mineq, int64_t maxeq);
51 
56  void SymbolicAddKel(TPZVec < int64_t > & destinationindex);
57 
58  int Work() {
59  return fWork;
60  }
61 
63  void SetNumRigidBodyModes(int nrigid)
64  {
66 
67  std::cout << " fNextRigidBody Mode neste ponto " << fNextRigidBodyMode<<std::endl;
68  }
69 
70  public:
71 int ClassId() const override;
72 
73 protected:
74  int fWork;
75 private:
76 
83  void FreeGlobal(int64_t global);
84 
89  int Local(int64_t global);
90 
91 public:
93  virtual void ExtractFrontMatrix(TPZFMatrix<TVar> &front) {
94  std::cout << "TPZFront ExtractFrontMatrix should never be called\n";
95  DebugStop();
96  }
97 
100  {
101  return fDecomposeType;
102  }
103 
105  virtual void SetDecomposeType(DecomposeType dectype) = 0;
106 
110  int NonNullFrontSize() const{
111  int maxeq = fLocal.NElements();
112  int mineq = 0;
113  for(mineq=0; mineq<maxeq; mineq++) if(fLocal[mineq] != -1) break;
114  int numeq = maxeq-mineq;
115  return numeq;
116  }
117 
119  virtual int64_t NFree();
121  void Reset(int64_t GlobalSize=0);
122 
124  void Print(const char *name, std::ostream& out) const;
125  void PrintGlobal(const char *name, std::ostream& out = std::cout);
126 
128  int FrontSize()
129  {
130  return fFront;
131  }
132 
133 protected:
134 
137 
146 
153 
155  int64_t fFront;
156 
159 
162 
165 
168 
169 protected:
170 
173 
174 
175 public:
176 
179 
180  typedef std::pair<int,STensorProductMTData * > STensorProductThreadData;
181 
182  public:
183 
188 
191 
194 
197 
199  TVar fDiagonal;
200 
202  int NThreads(){ return fThreads.NElements(); };
203 
204  //vec to storage
206 
209 
210  bool fRunning;
211 
214  if(!frontMat) DebugStop();
215  this->fMat = frontMat;
216  this->fRunning = true;
217 
218  fWorkSem.Resize(nthreads);
219  for(int i = 0; i < nthreads; i++){
220  tht::InitializeSemaphore(fWorkSem[i]);
221  }
222 
223  fThreads.Resize(nthreads);
224  fThreadData.Resize(nthreads);
225  for(int i = 0; i < nthreads; i++){
226  STensorProductThreadData * threadData = new STensorProductThreadData;
227  threadData->first = i;
228  threadData->second = this;
229  fThreadData[i] = threadData;
230  tht::CreateThread( fThreads[i], & Execute, threadData);
231  }
232 
233  tht::InitializeSemaphore(fWorkDoneSem);
234  tht::InitializeCriticalSection(fWorkDoneCS);
235 
236  }
237 
240 
242  this->fRunning = false;
243  const int nthreads = fWorkSem.NElements();
244  for(int i = 0; i < nthreads; i++){
245  tht::SemaphorePost(fWorkSem[i]);
246  }
247  for(int i = 0; i < nthreads; i++){
248  tht::ThreadWaitFor(fThreads[i]);
249  delete fThreadData[i];
250  fThreadData[i] = NULL;
251  }
252 
254  for(int i = 0; i < fWorkSem.NElements(); i++){
255  tht::DeleteSemaphore( fWorkSem[i] );
256  }
257  tht::DeleteSemaphore( fWorkDoneSem );
258  tht::DeleteCriticalSection(fWorkDoneCS);
259 
260  }
261 
262  static void *Execute(void *data){
263  STensorProductThreadData * mypair = static_cast< STensorProductThreadData * >(data);
264  if(!mypair) DebugStop();
265  TPZFront<TVar> * mat = mypair->second->fMat;
266  if(!mat) DebugStop();
267  mat->TensorProductIJ(mypair->first,mypair->second);
268  return NULL;
269  }
270 
271  void WorkDone(){
272  tht::EnterCriticalSection(fWorkDoneCS);
273  fWorkDoneCount++;
274  if(fWorkDoneCount == NThreads()){
275  tht::SemaphorePost(fWorkDoneSem);
276  }
277  tht::LeaveCriticalSection(fWorkDoneCS);
278  }
279 
280  void Run(TPZVec<TVar> &AuxVecCol, TPZVec<TVar> &AuxVecRow){
281  this->fAuxVecCol = &AuxVecCol;
282  this->fAuxVecRow = &AuxVecRow;
283  this->fWorkDoneCount = 0;
284  for(int i = 0; i < NThreads(); i++){
285  tht::SemaphorePost(fWorkSem[i]);
286  }
287  tht::SemaphoreWait( fWorkDoneSem );
288  }
289 
290  };
291 
292 protected:
293 
295 
296 public:
297 
299  fProductMTData = new STensorProductMTData(nthreads, this);
300  }
301 
303  delete this->fProductMTData;
304  }
305 
306  void ProductTensorMT(TPZVec<TVar> &AuxVecCol, TPZVec<TVar> &AuxVecRow){
307  this->fProductMTData->Run(AuxVecCol,AuxVecRow);
308  }
309 
310 public:
311 
313  virtual void TensorProductIJ(int ithread,typename TPZFront<TVar>::STensorProductMTData *data);
314 };
315 
316 template<class TVar>
318  return Hash("TPZFront") ^ ClassIdOrHash<TVar>() << 1;
319 }
320 #endif //TPZFRONT_H
virtual ~TPZFront()
Simple destructor.
Definition: TPZFront.cpp:65
TPZFront()
Simple constructor.
Definition: TPZFront.cpp:21
void DeleteSemaphore(pz_semaphore_t &s)
Implements semaphore to threads. Utility.
Definition: TPZSemaphore.h:15
static void * Execute(void *data)
destrutor
Definition: TPZFront.h:262
TPZVec< TVar > fData
Frontal matrix data.
Definition: TPZFront.h:164
It is an equation array, generally in its decomposed form. Frontal.
Definition: tpzeqnarray.h:36
TPZVec< int64_t > fLocal
Front equation to each global equation.
Definition: TPZFront.h:152
int InitializeSemaphore(pz_semaphore_t &s)
Semaforos.
pthread_mutex_t pz_critical_section_t
TVar fDiagonal
valor da diagonal
Definition: TPZFront.h:199
TPZVec< TVar > * fAuxVecCol
vetores de operacao
Definition: TPZFront.h:196
Templated vector implementation.
TPZVec< TVar > * fAuxVecRow
Definition: TPZFront.h:196
void LeaveCriticalSection(pz_critical_section_t &cs)
struct para paralelizar a decomposicao da matriz
Definition: TPZFront.h:178
int NThreads()
num threads
Definition: TPZFront.h:202
STensorProductMTData * fProductMTData
Definition: TPZFront.h:294
int fWork
Definition: TPZFront.h:74
void EnterCriticalSection(pz_critical_section_t &cs)
void ProductTensorMTInitData(int nthreads)
Definition: TPZFront.h:298
TPZVec< STensorProductThreadData * > fThreadData
Definition: TPZFront.h:202
TPZVec< pz_semaphore_t > fWorkSem
semaforos para sincronizar os threads de calculo
Definition: TPZFront.h:190
Abstract class implements storage and decomposition process of the frontal matrix. Frontal.
void CreateThread(pz_thread_t &thread, void *(*function)(void *param), void *data)
Threads.
void ProductTensorMT(TPZVec< TVar > &AuxVecCol, TPZVec< TVar > &AuxVecRow)
void
Definition: TPZFront.h:306
virtual void ExtractFrontMatrix(TPZFMatrix< TVar > &front)
Extracts the so far condensed matrix.
Definition: TPZFront.h:93
void FreeGlobal(int64_t global)
Sets the global equation as freed, allowing the space used by this equation to be used...
Definition: TPZFront.cpp:137
DecomposeType fDecomposeType
Used Decomposition method.
Definition: TPZFront.h:172
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object reallocating the necessary storage, copying the existing objects to the new...
Definition: pzvec.h:373
void SymbolicAddKel(TPZVec< int64_t > &destinationindex)
Add a contribution of a stiffness matrix using the indexes to compute the frontwidth.
Definition: TPZFront.cpp:176
void InitializeCriticalSection(pz_critical_section_t &cs)
Critical sections.
void ThreadWaitFor(pz_thread_t &thread)
int nthreads
Definition: numatst.cpp:315
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
DecomposeType GetDecomposeType() const
Returns decomposition type. Default LU.
Definition: TPZFront.h:99
void ProductTensorMTFinish()
void
Definition: TPZFront.h:302
int FrontSize()
returns the actual front size
Definition: TPZFront.h:128
virtual int64_t NFree()
Returns the number of free equations.
Definition: TPZFront.cpp:313
std::pair< int, STensorProductMTData *> STensorProductThreadData
Definition: TPZFront.h:180
pz_semaphore_t fWorkDoneSem
dados para sincronizar o thread principal
Definition: TPZFront.h:185
int fExpandRatio
Expansion Ratio of frontal matrix.
Definition: TPZFront.h:167
int Local(int64_t global)
return a local index corresponding to a global equation number
Definition: TPZFront.cpp:151
void SetNumRigidBodyModes(int nrigid)
Indicate the first equation dedicated to rigid body modes.
Definition: TPZFront.h:63
~STensorProductMTData()
construtor
Definition: TPZFront.h:239
int64_t fNextRigidBodyMode
Equation where rigid body modes can be stored.
Definition: TPZFront.h:158
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
void PrintGlobal(const char *name, std::ostream &out=std::cout)
Definition: TPZFront.cpp:69
A simple stack.
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
Contains TPZMatrix<TVar>class, root matrix class.
int64_t fFront
Actual front size.
Definition: TPZFront.h:155
virtual void SetDecomposeType(DecomposeType dectype)=0
Set the decomposition type.
STensorProductMTData(int nthreads, TPZFront< TVar > *frontMat)
construtor padrao
Definition: TPZFront.h:213
int NonNullFrontSize() const
Return the number of equations in the condensed front matrix It would be equal to FrontSize if the fr...
Definition: TPZFront.h:110
void SemaphoreWait(pz_semaphore_t &s)
void SemaphorePost(pz_semaphore_t &s)
void Print(const char *name, std::ostream &out) const
It prints TPZFront data.
Definition: TPZFront.cpp:93
int fMaxFront
Maximum size of the front.
Definition: TPZFront.h:136
void Reset(int64_t GlobalSize=0)
Definition: TPZFront.cpp:292
virtual void TensorProductIJ(int ithread, typename TPZFront< TVar >::STensorProductMTData *data)
void
Definition: TPZFront.cpp:327
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
TPZVec< pz_thread_t > fThreads
array de threads
Definition: TPZFront.h:193
void SymbolicDecomposeEquations(int64_t mineq, int64_t maxeq)
Decompose these equations in a symbolic way and store freed indexes in fFree.
Definition: TPZFront.cpp:190
int64_t NElements()
Static main used for testing.
Definition: TPZFront.cpp:307
void Run(TPZVec< TVar > &AuxVecCol, TPZVec< TVar > &AuxVecRow)
Definition: TPZFront.h:280
This class defines the interface to save and restore objects from TPZStream objects. Persistency.
Definition: TPZSavable.h:67
int Work()
Definition: TPZFront.h:58
TPZStack< int > fFree
Colection of already decomposed equations still on the front.
Definition: TPZFront.h:161
TPZManVector< int64_t > fGlobal
Global equation associated to each front equation.
Definition: TPZFront.h:145
pz_critical_section_t fWorkDoneCS
Definition: TPZFront.h:187
TPZFront< TVar > * fMat
matriz TPZFront
Definition: TPZFront.h:208
int ClassId() const override
Define the class id associated with the class.
Definition: TPZFront.h:317
DecomposeType
Defines decomposition type for any matrix classes.
Definition: pzmatrix.h:52
void DeleteCriticalSection(pz_critical_section_t &cs)