Ignore:
Timestamp:
Dec 21, 2009 11:59:56 AM (10 years ago)
Author:
forrest
Message:

first attempt at cleaning up threads

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sandbox/Cbc/src/CbcThread.hpp

    r1404 r1409  
    88#include "CbcNode.hpp"
    99class OsiObject;
     10class OsiCuts;
     11#ifdef CBC_THREAD
     12// Threads
     13#include <pthread.h>
     14typedef struct {
     15    pthread_t   thr;
     16    long                status;
     17} Coin_pthread_t;
     18
     19#ifdef HAVE_CLOCK_GETTIME
     20inline int my_gettime(struct timespec* tp)
     21{
     22    return clock_gettime(CLOCK_REALTIME, tp);
     23}
     24#else
     25#ifndef _MSC_VER
     26inline int my_gettime(struct timespec* tp)
     27{
     28    struct timeval tv;
     29    int ret = gettimeofday(&tv, NULL);
     30    tp->tv_sec = tv.tv_sec;
     31    tp->tv_nsec = tv.tv_usec * 1000;
     32    return ret;
     33}
     34#else
     35inline int my_gettime(struct timespec* tp)
     36{
     37    double t = CoinGetTimeOfDay();
     38    tp->tv_sec = (int)floor(t);
     39    tp->tv_nsec = (int)((tp->tv_sec - floor(t)) / 1000000.0);
     40    return 0;
     41}
     42#endif
     43#endif
     44/** A class to encapsulate phread stuff */
     45
     46
     47class CbcPthread {
     48public:
     49    // Default Constructor
     50    CbcPthread ();
     51
     52    // Constructor with base model
     53    CbcPthread (CbcModel & model, int deterministic, CbcModel * baseModel);
     54
     55    // Copy constructor
     56    CbcPthread ( const CbcPthread &);
     57
     58    virtual ~CbcPthread();
     59
     60    /// Assignment operator
     61    CbcPthread & operator=(const CbcPthread& rhs);
     62    /**
     63       Locks a thread if parallel so that stuff like cut pool
     64       can be updated and/or used.
     65    */
     66    void lockThread();
     67    /**
     68       Unlocks a thread if parallel to say cut pool stuff not needed
     69    */
     70    void unlockThread();
     71
     72    /// Returns true if locked
     73    inline bool isLocked() const {
     74        return locked_;
     75    }
     76
     77public:
     78    CbcPthread * basePointer_; // for getting main mutex and threadid of base
     79    pthread_mutex_t mutex_; // for waking up threads
     80    pthread_cond_t condition_; // for waking up thread
     81    Coin_pthread_t threadId_;
     82    bool locked_;
     83};
    1084
    1185/** A class to encapsulate thread stuff */
     86
    1287
    1388class CbcThread {
     
    30105    /// Assignment operator
    31106    CbcThread & operator=(const CbcThread& rhs);
    32 };
    33 
     107    /**
     108       Locks a thread if parallel so that stuff like cut pool
     109       can be updated and/or used.
     110    */
     111    void lockThread();
     112    /**
     113       Unlocks a thread if parallel to say cut pool stuff not needed
     114    */
     115    void unlockThread();
     116
     117    /// Returns true if locked
     118    inline bool isLocked() const {
     119        return locked_;
     120    }
     121
     122public:
     123    CbcModel * baseModel_;
     124    CbcModel * thisModel_;
     125    CbcNode * node_; // filled in every time
     126    CbcNode * createdNode_; // filled in every time on return
     127    Coin_pthread_t threadIdOfBase_;
     128    pthread_mutex_t * mutex_; // for locking data
     129    pthread_mutex_t * mutex2_; // for waking up threads
     130    CbcThread * master_; // points back to master thread
     131    pthread_cond_t * condition2_; // for waking up thread
     132    int returnCode_; // -1 available, 0 busy, 1 finished , 2??
     133    double timeLocked_;
     134    double timeWaitingToLock_;
     135    double timeWaitingToStart_;
     136    double timeInThread_;
     137    int numberTimesLocked_;
     138    int numberTimesUnlocked_;
     139    int numberTimesWaitingToStart_;
     140    int saveStuff_[2];
     141    int dantzigState_; // 0 unset, -1 waiting to be set, 1 set
     142    struct timespec absTime_;
     143    bool locked_;
     144    int nDeleteNode_;
     145    CbcNode ** delNode_;
     146    int maxDeleteNode_;
     147    int nodesThisTime_;
     148    int iterationsThisTime_;
     149    int deterministic_;
     150};
     151/** Base model */
     152
     153
     154class CbcBaseModel {
     155public:
     156    // Default Constructor
     157    CbcBaseModel ();
     158
     159    /** Constructor with model
     160        type -1 cuts
     161              0 opportunistic
     162              1 deterministic */
     163    // CbcBaseModel (CbcModel & model, int numberThreads,
     164    //  void *(* function) (void *),
     165    //  int type);
     166    /** Constructor with model
     167        type -1 cuts
     168              0 opportunistic
     169              1 deterministic */
     170    CbcBaseModel (CbcModel & model, int type);
     171
     172    // Copy constructor
     173    CbcBaseModel ( const CbcBaseModel &);
     174
     175    virtual ~CbcBaseModel();
     176
     177    /// Assignment operator
     178    CbcBaseModel & operator=(const CbcBaseModel& rhs);
     179
     180    /// Stop all threads
     181    void stopThreads();
     182
     183    /** Wait for threads in tree
     184        type 0 - tree looks empty - see if any nodes outstanding
     185             1 - tree not empty
     186         2 - finish and do statistics
     187        returns non-zero if keep going
     188    */
     189    int waitForThreadsInTree(int type);
     190
     191    /** Wait for threads n parallel cuts
     192        type 0 - parallel cuts
     193         1 - finishing parallel cuts
     194    */
     195    void waitForThreadsInCuts(int type, OsiCuts * eachCuts, int whichGenerator);
     196
     197    /// Split model and do work in deterministic parallel
     198    void  deterministicParallel();
     199    /**
     200       Locks a thread if parallel so that stuff like cut pool
     201       can be updated and/or used.
     202    */
     203    inline void lockThread() {
     204        children_[numberThreads_].lockThread();
     205    }
     206    /**
     207       Unlocks a thread if parallel to say cut pool stuff not needed
     208    */
     209    inline void unlockThread() {
     210        children_[numberThreads_].unlockThread();
     211    }
     212
     213    /// Returns true if locked
     214    inline bool isLocked() const {
     215        return children_[numberThreads_].locked_;
     216    }
     217
     218    /// Returns pointer to master thread
     219    inline CbcThread * masterThread() const {
     220        return children_ + numberThreads_;
     221    }
     222
     223    /// Returns pointer to a thread model
     224    inline CbcModel * model(int i) const {
     225        return threadModel_[i];
     226    }
     227
     228    /// Sets Dantzig state in children
     229    void setDantzigState();
     230
     231private:
     232
     233    /// Number of children
     234    int numberThreads_;
     235    /// Child models (with base model at end)
     236    CbcThread * children_;
     237    /** type -1 cuts
     238              0 opportunistic
     239              1 deterministic */
     240    int type_;
     241    pthread_mutex_t mutex_main_;
     242    pthread_cond_t condition_main_;
     243    pthread_mutex_t condition_mutex_;
     244    Coin_pthread_t * threadId_;
     245    int * threadCount_;
     246    CbcModel ** threadModel_;
     247    pthread_mutex_t * mutex2_;
     248    pthread_cond_t * condition2_;
     249    int numberObjects_;
     250    OsiObject ** saveObjects_;
     251    int threadStats_[6];
     252    int defaultParallelIterations_;
     253    int defaultParallelNodes_;
     254};
     255
     256/** Simple general method - just passed n bundles of data and a function */
     257
     258
     259class CbcSimpleThread {
     260public:
     261    // Default Constructor
     262    CbcSimpleThread ();
     263
     264    // Constructor with stuff
     265    CbcSimpleThread (int numberThreads,
     266                     void *(* function) (void *),
     267                     int sizeOfData,
     268                     void * data);
     269    // Constructor with stuff (type 0 -> doHeurThread)
     270    CbcSimpleThread (int numberThreads,
     271                     int type,
     272                     int sizeOfData,
     273                     void * data);
     274
     275    virtual ~CbcSimpleThread();
     276
     277protected:
     278
     279    /// Number of children
     280    int numberThreads_;
     281    /// data
     282    void * argBundle_;
     283};
     284#else
     285// Dummy threads
     286/** A class to encapsulate thread stuff */
     287
     288
     289class CbcThread {
     290public:
     291    // Default Constructor
     292    CbcThread () {}
     293
     294    virtual ~CbcThread() {}
     295
     296};
     297/** Base model */
     298
     299
     300class CbcBaseModel {
     301public:
     302    // Default Constructor (not declared here so that CbcThread.cpp not empty)
     303    CbcBaseModel ();
     304
     305    virtual ~CbcBaseModel() {}
     306
     307};
    34308#endif
     309
     310#endif
Note: See TracChangeset for help on using the changeset viewer.