Cbc  2.9.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CbcThread.hpp
Go to the documentation of this file.
1 /* $Id: CbcThread.hpp 2022 2014-03-25 11:18:50Z forrest $ */
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"
11 class OsiObject;
12 class OsiCuts;
13 #ifdef CBC_THREAD
14 class CbcThread;
15 // Use pthreads
16 #define CBC_PTHREAD
17 #ifdef CBC_PTHREAD
18 #include <pthread.h>
19 typedef struct {
20  pthread_t thr;
21  long status;
22 } Coin_pthread_t;
23 #endif
24 //#define THREAD_DEBUG 1
33 class CbcSpecificThread {
34 public:
35  // Default Constructor
36  CbcSpecificThread ();
37 
38  // Useful Constructor
39  CbcSpecificThread (CbcSpecificThread * master, pthread_mutex_t * masterMutex);
40 
41  virtual ~CbcSpecificThread();
42 
43  // Useful stuff
44  void setUsefulStuff (CbcSpecificThread * master,
45  void *& masterMutex);
50  void lockThread();
54  void unlockThread();
56  void lockThread2(bool doAnyway = false);
58  void unlockThread2(bool doAnyway = false);
60  void signal();
62  void timedWait(int time);
64  void startThread(void * (*routine ) (void *), CbcThread * thread);
66  int exit();
68  void exitThread();
70  int status() const;
72  void setStatus(int value);
73  //}
74 
75 
76 public: // private:
77  CbcSpecificThread * basePointer_; // for getting main mutex and threadid of base
78 #ifdef CBC_PTHREAD
79  pthread_mutex_t *masterMutex_; // for synchronizing
80  pthread_mutex_t mutex2_; // for waking up threads
81  pthread_cond_t condition2_; // for waking up thread
82  Coin_pthread_t threadId_;
83 #endif
84  bool locked_; // For mutex2
85 };
89 class CbcThread {
90 private:
91  void gutsOfDelete();
92  void gutsOfCopy(const CbcThread & rhs);
93 
94 public:
95  // Default Constructor
96  CbcThread ();
97 
98  virtual ~CbcThread();
99 
101  void setUsefulStuff (CbcModel * model, int deterministic,
102  CbcModel * baseModel,
103  CbcThread * master,
104  void *& masterMutex);
109  void lockThread();
113  void unlockThread();
114 
116  inline bool isLocked() const {
117  return locked_;
118  }
123  bool wait(int type, int currentCode);
125  void waitNano(int time);
127  void signal();
129  void lockFromMaster();
131  void unlockFromMaster();
133  void lockFromThread();
135  void unlockFromThread();
137  int exit();
139  void exitThread();
141  void waitThread();
143  inline int status() const {
144  return threadStuff_.status();
145  }
147  inline void setStatus(int value) {
148  threadStuff_.setStatus( value);
149  }
151  inline int returnCode() const {
152  return returnCode_;
153  }
155  inline void setReturnCode(int value) {
156  returnCode_ = value;
157  }
159  inline CbcModel * baseModel() const {
160  return baseModel_;
161  }
163  inline CbcModel * thisModel() const {
164  return thisModel_;
165  }
167  inline CbcNode * node() const {
168  return node_;
169  }
171  inline void setNode(CbcNode * node) {
172  node_ = node;
173  }
175  inline CbcNode * createdNode() const {
176  return createdNode_;
177  }
179  inline void setCreatedNode(CbcNode * node) {
180  createdNode_ = node;
181  }
183  inline int dantzigState() const {
184  return dantzigState_;
185  }
187  inline void setDantzigState(int value) {
188  dantzigState_ = value;
189  }
191  inline double timeInThread() const {
192  return timeInThread_;
193  }
195  inline void incrementTimeInThread(double value) {
196  timeInThread_ += value;
197  }
199  inline double timeWaitingToStart() const {
200  return timeWaitingToStart_;
201  }
203  inline void incrementTimeWaitingToStart(double value) {
204  timeWaitingToStart_ += value;
205  }
207  inline double timeLocked() const {
208  return timeLocked_;
209  }
211  inline void incrementTimeLocked(double value) {
212  timeLocked_ += value;
213  }
215  inline double timeWaitingToLock() const {
216  return timeWaitingToLock_;
217  }
219  inline void incrementTimeWaitingToLock(double value) {
220  timeWaitingToLock_ += value;
221  }
223  inline int deterministic() const {
224  return deterministic_;
225  }
227  inline int maxDeleteNode() const {
228  return maxDeleteNode_;
229  }
231  inline void setMaxDeleteNode(int value) {
232  maxDeleteNode_ = value;
233  }
235  inline int nDeleteNode() const {
236  return nDeleteNode_;
237  }
239  inline void setNDeleteNode(int value) {
240  nDeleteNode_ = value;
241  }
243  inline void clearDelNode() {
244  delete delNode_;
245  delNode_ = NULL;
246  }
248  inline void fakeDelNode(CbcNode ** delNode) {
249  delNode_ = delNode;
250  }
252  inline CbcNode ** delNode() const {
253  return delNode_;
254  }
256  inline void setDelNode(CbcNode ** delNode) {
257  delNode_ = delNode;
258  }
260  inline int numberTimesLocked() const {
261  return numberTimesLocked_;
262  }
264  inline int numberTimesUnlocked() const {
265  return numberTimesUnlocked_;
266  }
268  inline int nodesThisTime() const {
269  return nodesThisTime_;
270  }
272  inline void setNodesThisTime(int value) {
273  nodesThisTime_ = value;
274  }
276  inline int iterationsThisTime() const {
277  return iterationsThisTime_;
278  }
280  inline void setIterationsThisTime(int value) {
281  iterationsThisTime_ = value;
282  }
284  inline int * saveStuff() {
285  return saveStuff_;
286  }
288  inline bool locked() const {
289  return locked_;
290  }
291 
292 public: // private:
293  CbcSpecificThread threadStuff_;
294  CbcModel * baseModel_;
295  CbcModel * thisModel_;
296  CbcNode * node_; // filled in every time
297  CbcNode * createdNode_; // filled in every time on return
298  CbcThread * master_; // points back to master thread
299  int returnCode_; // -1 available, 0 busy, 1 finished , 2??
300  double timeLocked_;
301  double timeWaitingToLock_;
302  double timeWaitingToStart_;
303  double timeInThread_;
304  double timeWhenLocked_; // time when thread got lock (in seconds)
305  int numberTimesLocked_;
306  int numberTimesUnlocked_;
307  int numberTimesWaitingToStart_;
308  int saveStuff_[2];
309  int dantzigState_; // 0 unset, -1 waiting to be set, 1 set
310  bool locked_;
311  int nDeleteNode_;
312  CbcNode ** delNode_;
313  int maxDeleteNode_;
314  int nodesThisTime_;
315  int iterationsThisTime_;
316  int deterministic_;
317 #ifdef THREAD_DEBUG
318 public:
319  int threadNumber_;
320  int lockCount_;
321 #endif
322 };
326 class CbcBaseModel {
327 public:
328  // Default Constructor
329  CbcBaseModel ();
330 
339  CbcBaseModel (CbcModel & model, int type);
340 
341  virtual ~CbcBaseModel();
342 
347  void stopThreads(int type);
348 
355  int waitForThreadsInTree(int type);
356 
361  void waitForThreadsInCuts(int type, OsiCuts * eachCuts, int whichGenerator);
362 
364  void deterministicParallel();
369  inline void lockThread() {
370  children_[numberThreads_].lockThread();
371  }
375  inline void unlockThread() {
376  children_[numberThreads_].unlockThread();
377  }
378 
380  inline bool isLocked() const {
381  return children_[numberThreads_].locked();
382  }
383 
385  CbcThread * masterThread() const;
386 
388  inline CbcModel * model(int i) const {
389  return threadModel_[i];
390  }
391 
393  inline CbcThread * child(int thread) const
394  { return children_+thread;}
395 
397  inline int numberThreads() const
398  { return numberThreads_;}
399 
401  void setDantzigState();
402 
403 private:
404 
406  int numberThreads_;
408  CbcThread * children_;
412  int type_;
413  int * threadCount_;
414  CbcModel ** threadModel_;
415  int numberObjects_;
416  OsiObject ** saveObjects_;
417  int threadStats_[6];
418  int defaultParallelIterations_;
419  int defaultParallelNodes_;
420 };
421 #else
422 // Dummy threads
426 class CbcThread {
427 public:
428  // Default Constructor
429  CbcThread () {}
430 
431  virtual ~CbcThread() {}
432 
433 };
438 public:
439  // Default Constructor (not declared here so that CbcThread.cpp not empty)
440  CbcBaseModel ();
441 
442  virtual ~CbcBaseModel() {}
443 
444 };
445 #endif
446 
447 #endif
448 
Collections of row cuts and column cuts.
Definition: OsiCuts.hpp:19
Information required while the node is live.
Definition: CbcNode.hpp:49
Base model.
Definition: CbcThread.hpp:437
virtual ~CbcThread()
Definition: CbcThread.hpp:431
Abstract base class for `objects&#39;.
virtual ~CbcBaseModel()
Definition: CbcThread.hpp:442
Simple Branch and bound class.
Definition: CbcModel.hpp:101
A class to encapsulate thread stuff.
Definition: CbcThread.hpp:426