MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
rtsolver.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/rtsolver.cc,v 1.19 2017/01/12 14:46:10 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-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 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include "myassert.h"
35 #include "solver.h"
36 #include "solver_impl.h"
37 #include "rtsolver.h"
38 #include "rtaisolver.h"
39 #include "rtposixsolver.h"
40 #include "ac/sys_sysinfo.h"
41 
42 /* RTSolverBase - begin */
43 
45  RTMode eRTMode,
46  unsigned long lRTPeriod,
47  unsigned long RTStackSize,
48  bool bRTAllowNonRoot,
49  int RTCpuMap,
50  bool bNoOutput)
51 : pS(pS),
52 eRTMode(eRTMode),
53 lRTPeriod(lRTPeriod),
54 RTStackSize(RTStackSize),
55 bRTAllowNonRoot(bRTAllowNonRoot),
56 RTCpuMap(RTCpuMap),
57 bNoOutput(bNoOutput)
58 {
59  ASSERT(RTStackSize > 0);
60  ASSERT(lRTPeriod > 0);
61 }
62 
64 {
65  NO_OP;
66 }
67 
68 void
70 {
71  /* if using real-time, clear out any type of output */
72  if (bNoOutput) {
73  pS->SetNoOutput();
74  }
75 
77 }
78 
79 // check whether stop is commanded by real-time
80 bool
82 {
83  return false;
84 }
85 
86 /* RTSolverBase - end */
87 
88 void
90  RTSolverBase::RTMode& eRTMode,
91  unsigned long& lRTPeriod,
92  unsigned long& RTStackSize,
93  bool& bRTAllowNonRoot,
94  int& RTCpuMap)
95 {
97  /* FIXME: use a safe default? */
98  if (HP.IsKeyWord("mode")) {
99  if (HP.IsKeyWord("period")) {
101 
102  } else if (HP.IsKeyWord("semaphore")) {
103  /* FIXME: not implemented yet ... */
105 
106  } else if (HP.IsKeyWord("io")) {
107  eRTMode = RTSolverBase::MBRT_IO;
108 
109  } else {
110  silent_cerr("RTSolver: unknown realtime mode "
111  "at line " << HP.GetLineData()
112  << std::endl);
114  }
115  }
116 
117  switch (eRTMode) {
120  silent_cerr("RTSolver: unknown realtime mode; "
121  "assuming periodic "
122  "at line " << HP.GetLineData()
123  << std::endl);
124  // fallthru
125 
127  lRTPeriod = -1;
128  if (HP.IsKeyWord("time" "step")) {
129  long long p = HP.GetInt();
130 
131  if (p <= 0) {
132  silent_cerr("RTSolver: illegal time step "
133  << p << " at line "
134  << HP.GetLineData()
135  << std::endl);
137  }
138 
139  lRTPeriod = p;
140 
141  } else {
142  silent_cerr("RTSolver: need a time step for real time "
143  "at line " << HP.GetLineData()
144  << std::endl);
146  }
147  break;
148 
150  // impossible, right now
151  break;
152 
154  break;
155 
156  default:
158  }
159 
160  RTStackSize = 1024;
161  if (HP.IsKeyWord("reserve" "stack")) {
162  long size = HP.GetInt();
163 
164  if (size <= 0) {
165  silent_cerr("RTSolver: illegal stack size "
166  << size << " at line "
167  << HP.GetLineData()
168  << std::endl);
170  }
171 
172  RTStackSize = size;
173  }
174 
175  bRTAllowNonRoot = false;
176  if (HP.IsKeyWord("allow" "nonroot")) {
177  bRTAllowNonRoot = true;
178  }
179 
180  RTCpuMap = 0xFF;
181  if (HP.IsKeyWord("cpu" "map")) {
182 #ifndef HAVE_SCHED_SETAFFINITY
183  silent_cerr("warning: \"cpu map\" unsupported (ignored)" << std::endl);
184 #endif // ! HAVE_SCHED_SETAFFINITY
185 
186  int cpumap = HP.GetInt();
187  // NOTE: there is a hard limit at 4 CPU
188  int ncpu = std::min(get_nprocs(), 4);
189  int newcpumap = (2 << (ncpu - 1)) - 1;
190 
191  /* i bit non legati ad alcuna cpu sono posti a zero */
192  newcpumap &= cpumap;
193  if (newcpumap < 1 || newcpumap > 0xFF) {
194  char buf[16];
195  snprintf(buf, sizeof(buf), "0x%02X", (unsigned)cpumap);
196  silent_cerr("RTSolver: illegal cpu map " << buf
197  << " at line " << HP.GetLineData()
198  << std::endl);
200  }
201  RTCpuMap = newcpumap;
202  }
203 }
204 
205 RTSolverBase *
207 {
208  if (HP.IsKeyWord("POSIX")) {
209  return ReadRTPOSIXSolver(pS, HP);
210  }
211 
212 #ifdef USE_RTAI
213  if (!HP.IsKeyWord("RTAI")) {
214  pedantic_cerr("ReadRTSolver: missing real-time model; "
215  "assuming RTAI at line " << HP.GetLineData()
216  << std::endl);
217  }
218 
219  return ReadRTAISolver(pS, HP);
220 
221 #else // ! USE_RTAI
222  if (HP.IsKeyWord("RTAI")) {
223  silent_cerr("ReadRTSolver: need to configure --with-rtai "
224  "to use RTAI real-time solver "
225  "at line " << HP.GetLineData() << std::endl);
226 
227  } else {
228  silent_cerr("ReadRTSolver: need to configure --with-rtai "
229  "to use default RTAI real-time solver "
230  "at line " << HP.GetLineData() << std::endl);
231  }
232 
234 #endif // ! USE_RTAI
235 }
236 
237 /* RTSolver - end */
238 
RTSolverBase * ReadRTSolver(Solver *pS, MBDynParser &HP)
Definition: rtsolver.cc:206
RTSolverBase * ReadRTPOSIXSolver(Solver *pS, MBDynParser &HP)
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
virtual void Init(void)
Definition: rtsolver.cc:69
unsigned long RTStackSize
Definition: rtsolver.h:59
bool bNoOutput
Definition: rtsolver.h:63
#define NO_OP
Definition: myassert.h:74
RTSolverBase * ReadRTAISolver(Solver *pS, MBDynParser &HP)
Definition: rtaisolver.cc:377
virtual ~RTSolverBase(void)
Definition: rtsolver.cc:63
int mbdyn_reserve_stack(unsigned long size)
Definition: solver.cc:254
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual bool IsStopCommanded(void)
Definition: rtsolver.cc:81
#define ASSERT(expression)
Definition: colamd.c:977
void ReadRTParams(Solver *pS, MBDynParser &HP, RTSolverBase::RTMode &eRTMode, unsigned long &lRTPeriod, unsigned long &RTStackSize, bool &bRTAllowNonRoot, int &RTCpuMap)
Definition: rtsolver.cc:89
Definition: solver.h:78
RTSolverBase(Solver *pS, RTMode eRTMode, unsigned long lRTPeriod, unsigned long RTStackSize, bool bRTAllowNonRoot, int RTCpuMap, bool bNoOutput=true)
Definition: rtsolver.cc:44
Solver * pS
Definition: rtsolver.h:52
int get_nprocs(void)
Definition: get_nprocs.c:44
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697