Cbc 2.10.5
CbcThread.hpp
Go to the documentation of this file.
1/* $Id$ */
2// Copyright (C) 2009, International Business Machines
3// Corporation and others. All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#ifndef CbcThread_H
7#define CbcThread_H
8
9#include "CbcModel.hpp"
10#include "CbcNode.hpp"
11class OsiObject;
12class OsiCuts;
13#ifdef CBC_THREAD
14class CbcThread;
15// Use pthreads
16#define CBC_PTHREAD
17#ifdef CBC_PTHREAD
18#include <pthread.h>
19typedef struct {
20 pthread_t thr;
21 long status;
22} Coin_pthread_t;
23#endif
24//#define THREAD_DEBUG 1
32class CbcSpecificThread {
33public:
34 // Default Constructor
35 CbcSpecificThread();
36
37 // Useful Constructor
38 CbcSpecificThread(CbcSpecificThread *master, pthread_mutex_t *masterMutex);
39
40 virtual ~CbcSpecificThread();
41
42 // Useful stuff
43 void setUsefulStuff(CbcSpecificThread *master,
44 void *&masterMutex);
49 void lockThread();
53 void unlockThread();
55 void lockThread2(bool doAnyway = false);
57 void unlockThread2(bool doAnyway = false);
59 void signal();
61 void timedWait(int time);
63 void startThread(void *(*routine)(void *), CbcThread *thread);
65 int exit();
67 void exitThread();
69 int status() const;
71 void setStatus(int value);
72 //}
73
74public: // private:
75 CbcSpecificThread *basePointer_; // for getting main mutex and threadid of base
76#ifdef CBC_PTHREAD
77 pthread_mutex_t *masterMutex_; // for synchronizing
78 pthread_mutex_t mutex2_; // for waking up threads
79 pthread_cond_t condition2_; // for waking up thread
80 Coin_pthread_t threadId_;
81#endif
82 bool locked_; // For mutex2
83};
86class CbcThread {
87private:
88 void gutsOfDelete();
89 void gutsOfCopy(const CbcThread &rhs);
90
91public:
92 // Default Constructor
93 CbcThread();
94
95 virtual ~CbcThread();
96
98 void setUsefulStuff(CbcModel *model, int deterministic,
99 CbcModel *baseModel,
100 CbcThread *master,
101 void *&masterMutex);
106 void lockThread();
110 void unlockThread();
111
113 inline bool isLocked() const
114 {
115 return locked_;
116 }
121 bool wait(int type, int currentCode);
123 void waitNano(int time);
125 void signal();
127 void lockFromMaster();
129 void unlockFromMaster();
131 void lockFromThread();
133 void unlockFromThread();
135 int exit();
137 void exitThread();
139 void waitThread();
141 inline int status() const
142 {
143 return threadStuff_.status();
144 }
146 inline void setStatus(int value)
147 {
148 threadStuff_.setStatus(value);
149 }
151 inline int returnCode() const
152 {
153 return returnCode_;
154 }
156 inline void setReturnCode(int value)
157 {
158 returnCode_ = value;
159 }
161 inline CbcModel *baseModel() const
162 {
163 return baseModel_;
164 }
166 inline CbcModel *thisModel() const
167 {
168 return thisModel_;
169 }
171 inline CbcNode *node() const
172 {
173 return node_;
174 }
176 inline void setNode(CbcNode *node)
177 {
178 node_ = node;
179 }
181 inline CbcNode *createdNode() const
182 {
183 return createdNode_;
184 }
186 inline void setCreatedNode(CbcNode *node)
187 {
188 createdNode_ = node;
189 }
191 inline int dantzigState() const
192 {
193 return dantzigState_;
194 }
196 inline void setDantzigState(int value)
197 {
198 dantzigState_ = value;
199 }
201 inline double timeInThread() const
202 {
203 return timeInThread_;
204 }
206 inline void incrementTimeInThread(double value)
207 {
208 timeInThread_ += value;
209 }
211 inline double timeWaitingToStart() const
212 {
213 return timeWaitingToStart_;
214 }
216 inline void incrementTimeWaitingToStart(double value)
217 {
218 timeWaitingToStart_ += value;
219 }
221 inline double timeLocked() const
222 {
223 return timeLocked_;
224 }
226 inline void incrementTimeLocked(double value)
227 {
228 timeLocked_ += value;
229 }
231 inline double timeWaitingToLock() const
232 {
233 return timeWaitingToLock_;
234 }
236 inline void incrementTimeWaitingToLock(double value)
237 {
238 timeWaitingToLock_ += value;
239 }
241 inline int deterministic() const
242 {
243 return deterministic_;
244 }
246 inline int maxDeleteNode() const
247 {
248 return maxDeleteNode_;
249 }
251 inline void setMaxDeleteNode(int value)
252 {
253 maxDeleteNode_ = value;
254 }
256 inline int nDeleteNode() const
257 {
258 return nDeleteNode_;
259 }
261 inline void setNDeleteNode(int value)
262 {
263 nDeleteNode_ = value;
264 }
266 inline void clearDelNode()
267 {
268 delete delNode_;
269 delNode_ = NULL;
270 }
272 inline void fakeDelNode(CbcNode **delNode)
273 {
274 delNode_ = delNode;
275 }
277 inline CbcNode **delNode() const
278 {
279 return delNode_;
280 }
282 inline void setDelNode(CbcNode **delNode)
283 {
284 delNode_ = delNode;
285 }
287 inline int numberTimesLocked() const
288 {
289 return numberTimesLocked_;
290 }
292 inline int numberTimesUnlocked() const
293 {
294 return numberTimesUnlocked_;
295 }
297 inline int nodesThisTime() const
298 {
299 return nodesThisTime_;
300 }
302 inline void setNodesThisTime(int value)
303 {
304 nodesThisTime_ = value;
305 }
307 inline int iterationsThisTime() const
308 {
309 return iterationsThisTime_;
310 }
312 inline void setIterationsThisTime(int value)
313 {
314 iterationsThisTime_ = value;
315 }
317 inline int *saveStuff()
318 {
319 return saveStuff_;
320 }
322 inline bool locked() const
323 {
324 return locked_;
325 }
326
327public: // private:
328 CbcSpecificThread threadStuff_;
329 CbcModel *baseModel_;
330 CbcModel *thisModel_;
331 CbcNode *node_; // filled in every time
332 CbcNode *createdNode_; // filled in every time on return
333 CbcThread *master_; // points back to master thread
334 int returnCode_; // -1 available, 0 busy, 1 finished , 2??
335 double timeLocked_;
336 double timeWaitingToLock_;
337 double timeWaitingToStart_;
338 double timeInThread_;
339 double timeWhenLocked_; // time when thread got lock (in seconds)
340 int numberTimesLocked_;
341 int numberTimesUnlocked_;
342 int numberTimesWaitingToStart_;
343 int saveStuff_[2];
344 int dantzigState_; // 0 unset, -1 waiting to be set, 1 set
345 bool locked_;
346 int nDeleteNode_;
347 CbcNode **delNode_;
348 int maxDeleteNode_;
349 int nodesThisTime_;
350 int iterationsThisTime_;
351 int deterministic_;
352#ifdef THREAD_DEBUG
353public:
354 int threadNumber_;
355 int lockCount_;
356#endif
357};
360class CbcBaseModel {
361public:
362 // Default Constructor
363 CbcBaseModel();
364
373 CbcBaseModel(CbcModel &model, int type);
374
375 virtual ~CbcBaseModel();
376
381 void stopThreads(int type);
382
389 int waitForThreadsInTree(int type);
390
395 void waitForThreadsInCuts(int type, OsiCuts *eachCuts, int whichGenerator);
396
398 void deterministicParallel();
403 inline void lockThread()
404 {
405 children_[numberThreads_].lockThread();
406 }
410 inline void unlockThread()
411 {
412 children_[numberThreads_].unlockThread();
413 }
414
416 inline bool isLocked() const
417 {
418 return children_[numberThreads_].locked();
419 }
420
422 CbcThread *masterThread() const;
423
425 inline CbcModel *model(int i) const
426 {
427 return threadModel_[i];
428 }
429
431 inline CbcThread *child(int thread) const
432 {
433 return children_ + thread;
434 }
435
437 inline int numberThreads() const
438 {
439 return numberThreads_;
440 }
441
443 void setDantzigState();
444
445private:
447 int numberThreads_;
449 CbcThread *children_;
453 int type_;
454 int *threadCount_;
455 CbcModel **threadModel_;
456 int numberObjects_;
457 OsiObject **saveObjects_;
458 int threadStats_[6];
459 int defaultParallelIterations_;
460 int defaultParallelNodes_;
461};
462#else
463// Dummy threads
467public:
468 // Default Constructor
470
471 virtual ~CbcThread() {}
472};
476public:
477 // Default Constructor (not declared here so that CbcThread.cpp not empty)
479
480 virtual ~CbcBaseModel() {}
481};
482#endif
483
484#endif
485
486/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
487*/
Base model.
Definition: CbcThread.hpp:475
virtual ~CbcBaseModel()
Definition: CbcThread.hpp:480
Simple Branch and bound class.
Definition: CbcModel.hpp:100
Information required while the node is live.
Definition: CbcNode.hpp:49
A class to encapsulate thread stuff.
Definition: CbcThread.hpp:466
virtual ~CbcThread()
Definition: CbcThread.hpp:471