CbcThread.hpp
Go to the documentation of this file.
1 /* $Id: CbcThread.hpp 1221 2009-09-03 15:43:48Z forrest $ */
2 // Copyright (C) 2009, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CbcThread_H
5 #define CbcThread_H
6 
7 #include "CbcModel.hpp"
8 #include "CbcNode.hpp"
9 class OsiObject;
10 class OsiCuts;
11 #ifdef CBC_THREAD
12 class CbcThread;
13 // Use pthreads
14 #define CBC_PTHREAD
15 #ifdef CBC_PTHREAD
16 #include <pthread.h>
17 typedef struct {
18  pthread_t thr;
19  long status;
20 } Coin_pthread_t;
21 #endif
22 //#define THREAD_DEBUG 1
31 class CbcSpecificThread {
32 public:
33  // Default Constructor
34  CbcSpecificThread ();
35 
36  // Useful Constructor
37  CbcSpecificThread (CbcSpecificThread * master, pthread_mutex_t * masterMutex);
38 
39  virtual ~CbcSpecificThread();
40 
41  // Useful stuff
42  void setUsefulStuff (CbcSpecificThread * master,
43  void *& masterMutex);
48  void lockThread();
52  void unlockThread();
54  void lockThread2(bool doAnyway = false);
56  void unlockThread2(bool doAnyway = false);
58  void signal();
60  void timedWait(int time);
62  void startThread(void * (*routine ) (void *), CbcThread * thread);
64  int exit();
66  void exitThread();
68  int status() const;
70  void setStatus(int value);
71  //}
72 
73 
74 public: // 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 };
87 class CbcThread {
88 private:
89  void gutsOfDelete();
90  void gutsOfCopy(const CbcThread & rhs);
91 
92 public:
93  // Default Constructor
94  CbcThread ();
95 
96  virtual ~CbcThread();
97 
99  void setUsefulStuff (CbcModel * model, int deterministic,
100  CbcModel * baseModel,
101  CbcThread * master,
102  void *& masterMutex);
107  void lockThread();
111  void unlockThread();
112 
114  inline bool isLocked() const {
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  return threadStuff_.status();
143  }
145  inline void setStatus(int value) {
146  threadStuff_.setStatus( value);
147  }
149  inline int returnCode() const {
150  return returnCode_;
151  }
153  inline void setReturnCode(int value) {
154  returnCode_ = value;
155  }
157  inline CbcModel * baseModel() const {
158  return baseModel_;
159  }
161  inline CbcModel * thisModel() const {
162  return thisModel_;
163  }
165  inline CbcNode * node() const {
166  return node_;
167  }
169  inline void setNode(CbcNode * node) {
170  node_ = node;
171  }
173  inline CbcNode * createdNode() const {
174  return createdNode_;
175  }
177  inline void setCreatedNode(CbcNode * node) {
178  createdNode_ = node;
179  }
181  inline int dantzigState() const {
182  return dantzigState_;
183  }
185  inline void setDantzigState(int value) {
186  dantzigState_ = value;
187  }
189  inline double timeInThread() const {
190  return timeInThread_;
191  }
193  inline void incrementTimeInThread(double value) {
194  timeInThread_ += value;
195  }
197  inline double timeWaitingToStart() const {
198  return timeWaitingToStart_;
199  }
201  inline void incrementTimeWaitingToStart(double value) {
202  timeWaitingToStart_ += value;
203  }
205  inline double timeLocked() const {
206  return timeLocked_;
207  }
209  inline void incrementTimeLocked(double value) {
210  timeLocked_ += value;
211  }
213  inline double timeWaitingToLock() const {
214  return timeWaitingToLock_;
215  }
217  inline void incrementTimeWaitingToLock(double value) {
218  timeWaitingToLock_ += value;
219  }
221  inline int deterministic() const {
222  return deterministic_;
223  }
225  inline int maxDeleteNode() const {
226  return maxDeleteNode_;
227  }
229  inline void setMaxDeleteNode(int value) {
230  maxDeleteNode_ = value;
231  }
233  inline int nDeleteNode() const {
234  return nDeleteNode_;
235  }
237  inline void setNDeleteNode(int value) {
238  nDeleteNode_ = value;
239  }
241  inline void clearDelNode() {
242  delete delNode_;
243  delNode_ = NULL;
244  }
246  inline void fakeDelNode(CbcNode ** delNode) {
247  delNode_ = delNode;
248  }
250  inline CbcNode ** delNode() const {
251  return delNode_;
252  }
254  inline void setDelNode(CbcNode ** delNode) {
255  delNode_ = delNode;
256  }
258  inline int numberTimesLocked() const {
259  return numberTimesLocked_;
260  }
262  inline int numberTimesUnlocked() const {
263  return numberTimesUnlocked_;
264  }
266  inline int nodesThisTime() const {
267  return nodesThisTime_;
268  }
270  inline void setNodesThisTime(int value) {
271  nodesThisTime_ = value;
272  }
274  inline int iterationsThisTime() const {
275  return iterationsThisTime_;
276  }
278  inline void setIterationsThisTime(int value) {
279  iterationsThisTime_ = value;
280  }
282  inline int * saveStuff() {
283  return saveStuff_;
284  }
286  inline bool locked() const {
287  return locked_;
288  }
289 
290 public: // private:
291  CbcSpecificThread threadStuff_;
292  CbcModel * baseModel_;
293  CbcModel * thisModel_;
294  CbcNode * node_; // filled in every time
295  CbcNode * createdNode_; // filled in every time on return
296  CbcThread * master_; // points back to master thread
297  int returnCode_; // -1 available, 0 busy, 1 finished , 2??
298  double timeLocked_;
299  double timeWaitingToLock_;
300  double timeWaitingToStart_;
301  double timeInThread_;
302  double timeWhenLocked_; // time when thread got lock (in seconds)
303  int numberTimesLocked_;
304  int numberTimesUnlocked_;
305  int numberTimesWaitingToStart_;
306  int saveStuff_[2];
307  int dantzigState_; // 0 unset, -1 waiting to be set, 1 set
308  bool locked_;
309  int nDeleteNode_;
310  CbcNode ** delNode_;
311  int maxDeleteNode_;
312  int nodesThisTime_;
313  int iterationsThisTime_;
314  int deterministic_;
315 #ifdef THREAD_DEBUG
316 public:
317  int threadNumber_;
318  int lockCount_;
319 #endif
320 };
324 class CbcBaseModel {
325 public:
326  // Default Constructor
327  CbcBaseModel ();
328 
337  CbcBaseModel (CbcModel & model, int type);
338 
339  virtual ~CbcBaseModel();
340 
345  void stopThreads(int type);
346 
353  int waitForThreadsInTree(int type);
354 
359  void waitForThreadsInCuts(int type, OsiCuts * eachCuts, int whichGenerator);
360 
362  void deterministicParallel();
367  inline void lockThread() {
368  children_[numberThreads_].lockThread();
369  }
373  inline void unlockThread() {
374  children_[numberThreads_].unlockThread();
375  }
376 
378  inline bool isLocked() const {
379  return children_[numberThreads_].locked();
380  }
381 
383  CbcThread * masterThread() const;
384 
386  inline CbcModel * model(int i) const {
387  return threadModel_[i];
388  }
389 
391  void setDantzigState();
392 
393 private:
394 
396  int numberThreads_;
398  CbcThread * children_;
402  int type_;
403  int * threadCount_;
404  CbcModel ** threadModel_;
405  int numberObjects_;
406  OsiObject ** saveObjects_;
407  int threadStats_[6];
408  int defaultParallelIterations_;
409  int defaultParallelNodes_;
410 };
411 #else
412 // Dummy threads
416 class CbcThread {
417 public:
418  // Default Constructor
419  CbcThread () {}
420 
421  virtual ~CbcThread() {}
422 
423 };
428 public:
429  // Default Constructor (not declared here so that CbcThread.cpp not empty)
430  CbcBaseModel ();
431 
432  virtual ~CbcBaseModel() {}
433 
434 };
435 #endif
436 
437 #endif
438