NeoPZ
pzstrmatrixtbb.h
Go to the documentation of this file.
1 
6 #ifndef TPZStructMatrixTBB_H
7 #define TPZStructMatrixTBB_H
8 
9 #include <set>
10 #include <map>
11 #include <semaphore.h>
12 #include "pzvec.h"
13 #include "tpzautopointer.h"
14 #include "pzcmesh.h"
15 #include "pzelmat.h"
16 #include "TPZSemaphore.h"
17 #include "TPZEquationFilter.h"
18 #include "TPZGuiInterface.h"
19 #include <list>
20 #include "pzmatrix.h"
21 #include "pzfmatrix.h"
22 
23 class TPZStructMatrixTBB;
24 #include "TPZStructMatrixBase.h"
25 #ifdef USING_TBB
26 #include "tbb/tbb.h"
27 #include "tbb/flow_graph.h"
28 
29 
34 //void UniformRefine(int num, TPZGeoMesh &m);
35 
40 class TPZStructMatrixTBB : public TPZStructMatrixBase{
41 
42 public:
43 
44  TPZStructMatrixTBB();
45 
46  TPZStructMatrixTBB(TPZCompMesh *, bool onlyrhs = false);
47 
48  TPZStructMatrixTBB(TPZAutoPointer<TPZCompMesh> cmesh, bool onlyrhs = false);
49 
50  TPZStructMatrixTBB(const TPZStructMatrixTBB &copy);
51 
52  virtual ~TPZStructMatrixTBB();
53 
54 
55  virtual TPZMatrix<STATE> * Create() override;
56 
58  unsigned numthreads_assemble, unsigned numthreads_decompose) {
59  SetNumThreads(numthreads_assemble);
60  return CreateAssemble(rhs, guiInterface);
61  }
62 
64 
65  virtual TPZStructMatrixTBB * Clone() override;
66 
68  virtual void Assemble(TPZMatrix<STATE> & mat, TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface) override;
69  virtual void Assemble(TPZMatrix<STATE> & mat, TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface,
70  unsigned numthreads_assemble, unsigned numthreads_decompose) {
71  std::cout << "Nothing to do." << std::endl;
72  }
73 
75  virtual void Assemble(TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface) override;
76 
77  public:
78  int ClassId() const override;
79  void Read(TPZStream &buf, void *context) override;
80  void Write(TPZStream &buf, int withclassid) const override;
81 
82 protected:
83 
84 // /** @brief Assemble the global system of equations into the matrix which has already been created */
85 // virtual void Serial_Assemble(TPZMatrix<STATE> & mat, TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface);
86 //
87 // /** @brief Assemble the global right hand side */
88 // virtual void Serial_Assemble(TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface);
89 
91  virtual void MultiThread_Assemble(TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface);
92 
94  virtual void MultiThread_Assemble(TPZMatrix<STATE> & mat, TPZFMatrix<STATE> & rhs, TPZAutoPointer<TPZGuiInterface> guiInterface);
95 
96 protected:
97 
98 
99  class TPZFlowGraph {
100 
101  struct TPZPointers
102  {
103  TPZElementMatrix *fEk;
104  TPZElementMatrix *fEf;
105 
106  TPZPointers() : fEk(0), fEf(0){}
107  };
108 
109  public:
110  TPZFlowGraph(TPZStructMatrixTBB *strmat, bool onlyrhs);
111  ~TPZFlowGraph();
112  TPZFlowGraph(TPZFlowGraph const &copy);
113 
114  protected:
115 
116  TPZStack<int64_t> fFirstElColor;
117 
118  // vectors for mesh coloring
119  TPZVec<int64_t> fnextBlocked, felSequenceColor, felSequenceColorInv;
120  TPZManVector<int64_t> fElementOrder;
121 
122  TPZManVector<int,20> fNodeDest;
123 
124 
126  TPZCompMesh *fCMesh;
127 
129  tbb::flow::graph fGraph;
130 
132  std::vector<tbb::flow::continue_node<tbb::flow::continue_msg> *> fNodes;
133 
135  TPZVec<TPZPointers> fElMatPointers;
136 
138  bool fOnlyRhs;
139 
140  public:
141 
142 // std::vector<std::list<int64_t> > nextTasks;
143 // std::vector<int64_t> predecessorTasks;
144 
145  void OrderElements();
146  void ElementColoring();
147 
148  void CreateGraph();
149 
150  void CreateGraphRhs();
151 
152  void ExecuteGraph(TPZFMatrix<STATE> *rhs, TPZMatrix<STATE> *matrix);
153 
154  void ExecuteGraph(TPZFMatrix<STATE> *rhs);
155 
156  friend class TPZStructMatrixTBB;
157 
158  protected:
160  TPZStructMatrixTBB *fStruct;
162  TPZAutoPointer<TPZGuiInterface> fGuiInterface;
163 
165  TPZFMatrix<STATE> fRhsFat;
167  TPZMatrix<STATE> *fGlobMatrix;
169  TPZFMatrix<STATE> *fGlobRhs;
170 
171 #ifdef USING_TBB
172 
173  class TPZAssembleTask {
174  public:
175  TPZAssembleTask() : fOrigin(0), fElMat(0), fIel(-1){
176  }
177 
178  TPZAssembleTask(int64_t iel, TPZFlowGraph *origin) : fOrigin(origin), fIel(iel)
179  {
180  fElMat = &origin->fElMatPointers[iel];
181  }
182 
183  ~TPZAssembleTask() {}
184 
185  TPZAssembleTask(const TPZAssembleTask &copy) : fOrigin(copy.fOrigin), fElMat(copy.fElMat), fIel(copy.fIel)
186  {
187 
188  }
189 
190  void operator=(const TPZAssembleTask &copy)
191  {
192  fOrigin = copy.fOrigin;
193  fElMat = copy.fElMat;
194  fIel = copy.fIel;
195  }
196 
197  tbb::flow::continue_msg operator()(const tbb::flow::continue_msg &msg);
198 
200  TPZFlowGraph *fOrigin;
201 
202  TPZPointers *fElMat;
203 
204  int64_t fIel;
205 
206 
207  };
208 
209  class TPZCalcTask {
210  public:
211 
212  TPZCalcTask(TPZFlowGraph *flowgraph) : fFlowGraph(flowgraph) {}
213 
214  void operator()(const tbb::blocked_range<int64_t>& range) const;
215 
216 
217  private:
218  TPZFlowGraph *fFlowGraph;
219  };
220 
221  class TAssembleOneColor {
222  public:
223  TAssembleOneColor(TPZFlowGraph *graph) : fFlowGraph(graph)
224  {
225  }
226 
227  void operator()(const tbb::blocked_range<int64_t> &range) const;
228 
229  private:
230  TPZFlowGraph *fFlowGraph;
231  };
232 
233  class TComputeElementRange {
234 
235  public:
236  TComputeElementRange(TPZFlowGraph *graph, int64_t color) : fFlowGraph(graph), fColor(color)
237  {
238 
239  }
240  void operator()(const tbb::blocked_range<int64_t> &range) const;
241  private:
242  TPZFlowGraph *fFlowGraph;
243  int64_t fColor;
244 
245  };
246 
247  class TSumTwoColors
248  {
249  public:
250  TSumTwoColors(int64_t firstcolumn, int64_t secondcolumn, TPZFMatrix<STATE> *rhs) : fFirstColumn(firstcolumn), fSecondColumn(secondcolumn), fRhs(rhs)
251  {
252  }
253 
254  tbb::flow::continue_msg operator()(const tbb::flow::continue_msg &msg) const
255  {
256  int64_t nrow = fRhs->Rows();
257  for (int64_t ir=0; ir<nrow; ir++) {
258  (*fRhs)(ir,fFirstColumn) += (*fRhs)(ir,fSecondColumn);
259  }
260  return tbb::flow::continue_msg();
261 
262  }
263  private:
264  int64_t fFirstColumn;
265  int64_t fSecondColumn;
266  TPZFMatrix<STATE> *fRhs;
267 
268  };
269 
270  };
271 
272 #endif
273 
274 
275 protected:
276 
277 #ifdef USING_TBB
278  TPZFlowGraph *fFlowGraph;
279 #endif
280 };
281 
282 
283 #endif
284 #endif
virtual TPZMatrix< STATE > * CreateAssemble(TPZFMatrix< STATE > &rhs, TPZAutoPointer< TPZGuiInterface > guiInterface)
Contains declaration of the TPZSemaphore class which implements semaphore to threads.
Templated vector implementation.
Contains declaration of TPZGuiInterface class.
virtual TPZStructMatrixBase * Clone()=0
int ClassId() const override
Define the class id associated with the class.
TPZSkylMatrix< REAL > matrix
Definition: numatst.cpp:255
virtual void Assemble(TPZMatrix< STATE > &stiffness, TPZFMatrix< STATE > &rhs, TPZAutoPointer< TPZGuiInterface > guiInterface)=0
Contains declaration of TPZElementMatrix struct which associates an element matrix with the coeficien...
Contains TPZMatrixclass which implements full matrix (using column major representation).
virtual void SetNumThreads(int n)
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
Contains declaration of the TPZAutoPointer class which has Increment and Decrement actions are mutexe...
Contains declaration of TPZCompMesh class which is a repository for computational elements...
Contains TPZMatrix<TVar>class, root matrix class.
This class associates an element matrix with the coeficients of its contribution in the global stiffn...
Definition: pzelmat.h:30
virtual TPZMatrix< STATE > * Create()=0
Implements computational mesh. Computational Mesh.
Definition: pzcmesh.h:47
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
void Read(TPZStream &buf, void *context) override
read objects from the stream