• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

/tmp/buildd/coinor-cbc-2.5.0/Cbc/src/CbcThread.hpp

Go to the documentation of this file.
00001 /* $Id: CbcThread.hpp 1221 2009-09-03 15:43:48Z forrest $ */
00002 // Copyright (C) 2009, International Business Machines
00003 // Corporation and others.  All Rights Reserved.
00004 #ifndef CbcThread_H
00005 #define CbcThread_H
00006 
00007 #include "CbcModel.hpp"
00008 #include "CbcNode.hpp"
00009 class OsiObject;
00010 class OsiCuts;
00011 #ifdef CBC_THREAD
00012 class CbcThread;
00013 // Use pthreads
00014 #define CBC_PTHREAD
00015 #ifdef CBC_PTHREAD
00016 #include <pthread.h>
00017 typedef struct {
00018     pthread_t   thr;
00019     long                status;
00020 } Coin_pthread_t;
00021 #endif
00022 //#define THREAD_DEBUG 1
00031 class CbcSpecificThread {
00032 public:
00033     // Default Constructor
00034     CbcSpecificThread ();
00035 
00036     // Useful Constructor
00037     CbcSpecificThread (CbcSpecificThread * master, pthread_mutex_t * masterMutex);
00038 
00039     virtual ~CbcSpecificThread();
00040 
00041     // Useful stuff
00042     void setUsefulStuff (CbcSpecificThread * master,
00043                          void *& masterMutex);
00048     void lockThread();
00052     void unlockThread();
00054     void lockThread2(bool doAnyway = false);
00056     void unlockThread2(bool doAnyway = false);
00058     void signal();
00060     void timedWait(int time);
00062     void startThread(void * (*routine ) (void *), CbcThread * thread);
00064     int exit();
00066     void exitThread();
00068     int status() const;
00070     void setStatus(int value);
00071     //}
00072 
00073 
00074 public: // private:
00075     CbcSpecificThread * basePointer_; // for getting main mutex and threadid of base
00076 #ifdef CBC_PTHREAD
00077     pthread_mutex_t *masterMutex_; // for synchronizing
00078     pthread_mutex_t mutex2_; // for waking up threads
00079     pthread_cond_t condition2_; // for waking up thread
00080     Coin_pthread_t threadId_;
00081 #endif
00082     bool locked_; // For mutex2
00083 };
00087 class CbcThread {
00088 private:
00089     void gutsOfDelete();
00090     void gutsOfCopy(const CbcThread & rhs);
00091 
00092 public:
00093     // Default Constructor
00094     CbcThread ();
00095 
00096     virtual ~CbcThread();
00097 
00099     void setUsefulStuff (CbcModel * model, int deterministic,
00100                          CbcModel * baseModel,
00101                          CbcThread * master,
00102                          void *& masterMutex);
00107     void lockThread();
00111     void unlockThread();
00112 
00114     inline bool isLocked() const {
00115         return locked_;
00116     }
00121     bool wait(int type, int currentCode);
00123     void waitNano(int time);
00125     void signal();
00127     void lockFromMaster();
00129     void unlockFromMaster();
00131     void lockFromThread();
00133     void unlockFromThread();
00135     int exit();
00137     void exitThread();
00139     void waitThread();
00141     inline int status() const {
00142         return threadStuff_.status();
00143     }
00145     inline void setStatus(int value) {
00146         threadStuff_.setStatus( value);
00147     }
00149     inline int returnCode() const {
00150         return returnCode_;
00151     }
00153     inline void setReturnCode(int value) {
00154         returnCode_ = value;
00155     }
00157     inline CbcModel * baseModel() const {
00158         return baseModel_;
00159     }
00161     inline CbcModel * thisModel() const {
00162         return thisModel_;
00163     }
00165     inline CbcNode * node() const {
00166         return node_;
00167     }
00169     inline void setNode(CbcNode * node) {
00170         node_ = node;
00171     }
00173     inline CbcNode * createdNode() const {
00174         return createdNode_;
00175     }
00177     inline void setCreatedNode(CbcNode * node) {
00178         createdNode_ = node;
00179     }
00181     inline int dantzigState() const {
00182         return dantzigState_;
00183     }
00185     inline void setDantzigState(int value) {
00186         dantzigState_ = value;
00187     }
00189     inline double timeInThread() const {
00190         return timeInThread_;
00191     }
00193     inline void incrementTimeInThread(double value) {
00194         timeInThread_ += value;
00195     }
00197     inline double timeWaitingToStart() const {
00198         return timeWaitingToStart_;
00199     }
00201     inline void incrementTimeWaitingToStart(double value) {
00202         timeWaitingToStart_ += value;
00203     }
00205     inline double timeLocked() const {
00206         return timeLocked_;
00207     }
00209     inline void incrementTimeLocked(double value) {
00210         timeLocked_ += value;
00211     }
00213     inline double timeWaitingToLock() const {
00214         return timeWaitingToLock_;
00215     }
00217     inline void incrementTimeWaitingToLock(double value) {
00218         timeWaitingToLock_ += value;
00219     }
00221     inline int deterministic() const {
00222         return deterministic_;
00223     }
00225     inline int maxDeleteNode() const {
00226         return maxDeleteNode_;
00227     }
00229     inline void setMaxDeleteNode(int value) {
00230         maxDeleteNode_ = value;
00231     }
00233     inline int nDeleteNode() const {
00234         return nDeleteNode_;
00235     }
00237     inline void setNDeleteNode(int value) {
00238         nDeleteNode_ = value;
00239     }
00241     inline void clearDelNode() {
00242         delete delNode_;
00243         delNode_ = NULL;
00244     }
00246     inline void fakeDelNode(CbcNode ** delNode) {
00247         delNode_ = delNode;
00248     }
00250     inline CbcNode ** delNode() const {
00251         return delNode_;
00252     }
00254     inline void setDelNode(CbcNode ** delNode) {
00255         delNode_ = delNode;
00256     }
00258     inline int numberTimesLocked() const {
00259         return numberTimesLocked_;
00260     }
00262     inline int numberTimesUnlocked() const {
00263         return numberTimesUnlocked_;
00264     }
00266     inline int nodesThisTime() const {
00267         return nodesThisTime_;
00268     }
00270     inline void setNodesThisTime(int value) {
00271         nodesThisTime_ = value;
00272     }
00274     inline int iterationsThisTime() const {
00275         return iterationsThisTime_;
00276     }
00278     inline void setIterationsThisTime(int value) {
00279         iterationsThisTime_ = value;
00280     }
00282     inline int * saveStuff() {
00283         return saveStuff_;
00284     }
00286     inline bool locked() const {
00287         return locked_;
00288     }
00289 
00290 public: // private:
00291     CbcSpecificThread threadStuff_;
00292     CbcModel * baseModel_;
00293     CbcModel * thisModel_;
00294     CbcNode * node_; // filled in every time
00295     CbcNode * createdNode_; // filled in every time on return
00296     CbcThread * master_; // points back to master thread
00297     int returnCode_; // -1 available, 0 busy, 1 finished , 2??
00298     double timeLocked_;
00299     double timeWaitingToLock_;
00300     double timeWaitingToStart_;
00301     double timeInThread_;
00302     double timeWhenLocked_; // time when thread got lock (in seconds)
00303     int numberTimesLocked_;
00304     int numberTimesUnlocked_;
00305     int numberTimesWaitingToStart_;
00306     int saveStuff_[2];
00307     int dantzigState_; // 0 unset, -1 waiting to be set, 1 set
00308     bool locked_;
00309     int nDeleteNode_;
00310     CbcNode ** delNode_;
00311     int maxDeleteNode_;
00312     int nodesThisTime_;
00313     int iterationsThisTime_;
00314     int deterministic_;
00315 #ifdef THREAD_DEBUG
00316 public:
00317     int threadNumber_;
00318     int lockCount_;
00319 #endif
00320 };
00324 class CbcBaseModel {
00325 public:
00326     // Default Constructor
00327     CbcBaseModel ();
00328 
00337     CbcBaseModel (CbcModel & model, int type);
00338 
00339     virtual ~CbcBaseModel();
00340 
00345     void stopThreads(int type);
00346 
00353     int waitForThreadsInTree(int type);
00354 
00359     void waitForThreadsInCuts(int type, OsiCuts * eachCuts, int whichGenerator);
00360 
00362     void  deterministicParallel();
00367     inline void lockThread() {
00368         children_[numberThreads_].lockThread();
00369     }
00373     inline void unlockThread() {
00374         children_[numberThreads_].unlockThread();
00375     }
00376 
00378     inline bool isLocked() const {
00379         return children_[numberThreads_].locked();
00380     }
00381 
00383     CbcThread * masterThread() const;
00384 
00386     inline CbcModel * model(int i) const {
00387         return threadModel_[i];
00388     }
00389 
00391     void setDantzigState();
00392 
00393 private:
00394 
00396     int numberThreads_;
00398     CbcThread * children_;
00402     int type_;
00403     int * threadCount_;
00404     CbcModel ** threadModel_;
00405     int numberObjects_;
00406     OsiObject ** saveObjects_;
00407     int threadStats_[6];
00408     int defaultParallelIterations_;
00409     int defaultParallelNodes_;
00410 };
00411 #else
00412 // Dummy threads
00416 class CbcThread {
00417 public:
00418     // Default Constructor
00419     CbcThread () {}
00420 
00421     virtual ~CbcThread() {}
00422 
00423 };
00427 class CbcBaseModel {
00428 public:
00429     // Default Constructor (not declared here so that CbcThread.cpp not empty)
00430     CbcBaseModel ();
00431 
00432     virtual ~CbcBaseModel() {}
00433 
00434 };
00435 #endif
00436 
00437 #endif
00438 

Generated on Tue Sep 7 2010 20:23:52 by  doxygen 1.7.1