MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
parnaivewrap.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbwrap/parnaivewrap.h,v 1.24 2017/01/12 14:44:25 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 2004-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 /*
32  * Copyright (C) 2004-2017
33  *
34  * Marco Morandini
35  *
36  */
37 
38 /*****************************************************************************
39  * *
40  * ParNaive C++ WRAPPER *
41  * *
42  *****************************************************************************/
43 
44 
45 #ifndef PARNAIVEWRAP_H
46 #define PARNAIVEWRAP_H
47 
48 #ifdef USE_NAIVE_MULTITHREAD
49 
50 #include <atomic_ops.h>
51 
52 #include <iostream>
53 #include "ac/pthread.h"
54 #include <vector>
55 
56 #include "myassert.h"
57 #include "mynewmem.h"
58 #include "except.h"
59 #include "solman.h"
60 #include "submat.h"
61 #include "naivemh.h"
62 #include "ls.h"
63 
64 /* ParNaiveSolver - begin */
65 
66 /*
67  * Solutore LU per matrici sparse. usa spazio messo a disposizione da altri
68  * e si aspetta le matrici gia' bell'e preparate
69  */
70 
71 struct ParNaiveSolverData;
72 
73 class ParNaiveSolver : public LinearSolver {
74 public:
75 private:
76 
77  integer iSize;
78 
79  doublereal dMinPiv;
80  mutable std::vector<integer> piv;
81  integer **ppril;
82  integer *pnril;
83  mutable std::vector<doublereal> fwd;
84  std::vector<integer> todo;
85  mutable std::vector<AO_t> row_locks;
86  mutable std::vector<AO_TS_t> col_locks;
87 
89 
90  unsigned nThreads;
91 
92  enum Op {
93  FACTOR,
94  SOLVE,
95  EXIT
96  };
97 
98  struct thread_data_t {
99  pthread_t thread;
100  ParNaiveSolver *pSLUS;
101  unsigned threadNumber;
102  sem_t sem;
103  int retval;
104  } *thread_data;
105 
106  mutable Op thread_operation;
107  mutable unsigned thread_count;
108  mutable pthread_mutex_t thread_mutex;
109  mutable pthread_cond_t thread_cond;
110 
111  /* Thread process */
112  static void *thread_op(void *arg);
113 
114  /* Fattorizza la matrice */
115  void Factor(void);
116 
117  void EndOfOp(void);
118 
119 public:
120  /* Costruttore: si limita ad allocare la memoria */
121  ParNaiveSolver(unsigned nt, const integer &size,
122  const doublereal& dMP,
123  NaiveMatrixHandler *const a);
124 
125  /* Distruttore */
126  ~ParNaiveSolver(void);
127 
128 #ifdef DEBUG
129  void IsValid(void) const;
130 #endif /* DEBUG */
131 
132  /* Risolve */
133  void Solve(void) const;
134 
135  void SetMat(NaiveMatrixHandler *const a);
136 };
137 
138 /* ParNaiveSolver - end */
139 
140 
141 
142 /* ParNaiveSparseSolutionManager - begin */
143 
144 /*
145  * Gestisce la soluzione del problema; alloca le matrici occorrenti
146  * e gli oggetti dedicati alla gestione delle matrici ed alla soluzione
147  */
148 
149 class ParNaiveSparseSolutionManager : public SolutionManager {
150 protected:
151  mutable NaiveMatrixHandler *A;
152 
153  mutable MyVectorHandler VH;
154  mutable MyVectorHandler XH;
155 
156  /* Fattorizza la matrice (non viene mai chiamato direttamente,
157  * ma da Solve se la matrice ancora non e' stata fattorizzata) */
158  void Factor(void);
159 
160 #ifdef DEBUG
161  /* Usata per il debug */
162  virtual void IsValid(void) const;
163 #endif /* DEBUG */
164 
165 public:
166  /* Costruttore: usa e mette a disposizione matrici che gli sono date */
167  ParNaiveSparseSolutionManager(unsigned nt,
168  const integer Dim,
169  const doublereal dMP = 1.e-9);
170 
171  /* Distruttore: dealloca le matrici e distrugge gli oggetti propri */
172  ~ParNaiveSparseSolutionManager(void);
173 
174  /* Inizializza il gestore delle matrici */
175  void MatrReset(void);
176 
177  /* Risolve il sistema */
178  void Solve(void);
179 
180  /* Rende disponibile l'handler per la matrice */
181  MatrixHandler* pMatHdl(void) const;
182 
183  /* Rende disponibile l'handler per il termine noto */
184  VectorHandler* pResHdl(void) const;
185 
186  /* Rende disponibile l'handler per la soluzione */
187  VectorHandler* pSolHdl(void) const;
188 };
189 
190 /* ParNaiveSparseSolutionManager - end */
191 
192 /* ParNaiveSparsePermSolutionManager - begin */
193 
194 class ParNaiveSparsePermSolutionManager: public ParNaiveSparseSolutionManager {
195 private:
196  const doublereal dMinPiv;
197  void ComputePermutation();
198  void BackPerm();
199 
200 protected:
201  /* See NaiveSparsePermSolutionManager */
202  enum {
203  PERM_NO,
204  PERM_INTERMEDIATE,
205  PERM_READY
206  } ePermState;
207 
208  mutable std::vector<integer> perm;
209  mutable std::vector<integer> invperm;
210 
211  virtual void MatrReset(void);
212 
213 public:
214  ParNaiveSparsePermSolutionManager(unsigned nt,
215  const integer Dim,
216  const doublereal dMP = 1.e-9);
217  virtual ~ParNaiveSparsePermSolutionManager(void);
218 
219  /* Risolve il sistema Backward Substitution; fattorizza se necessario */
220  virtual void Solve(void);
221 
222  /* Inizializzatore "speciale" */
223  virtual void MatrInitialize(void);
224 };
225 
226 /* ParNaiveSparsePermSolutionManager - end */
227 
228 #endif /* USE_NAIVE_MULTITHREAD */
229 
230 #endif /* PARNAIVEWRAP_H */
231 
virtual VectorHandler * pResHdl(void) const =0
virtual MatrixHandler * pMatHdl(void) const =0
octave_value_list retval
virtual void MatrReset(void)=0
virtual void Solve(void)=0
virtual void Solve(void) const =0
static const doublereal a
Definition: hfluid_.h:289
virtual VectorHandler * pSolHdl(void) const =0
double doublereal
Definition: colamd.c:52
long int integer
Definition: colamd.c:51