Changeset 1998


Ignore:
Timestamp:
Dec 19, 2013 1:11:05 PM (5 years ago)
Author:
forrest
Message:

for threadsafe

Location:
trunk/Cbc
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cbc/src/CbcSolver.cpp

    r1954 r1998  
    815815#endif
    816816
    817 static double totalTime = 0.0;
    818817static void statistics(ClpSimplex * originalModel, ClpSimplex * model);
    819818static bool maskMatches(const int * starts, char ** masks,
     
    976975FILE * CbcOrClpReadCommand = stdin;
    977976extern int CbcOrClpEnvironmentIndex;
    978 static bool noPrinting = false;
    979 
    980 
    981977
    982978/*
     
    10761072}
    10771073
     1074static CbcSolverUsefulData staticParameterData;
    10781075int callCbc1(const char * input2, CbcModel & model,
    10791076             int callBack(CbcModel * currentSolver, int whereFrom))
     
    11231120    argv[n+1] = CoinStrdup("-quit");
    11241121    free(input);
    1125     totalTime = 0.0;
     1122    // allow interrupts and printing
     1123    staticParameterData.noPrinting_ = false;
     1124    staticParameterData.useSignalHandler_=true;
    11261125    currentBranchModel = NULL;
    11271126    CbcOrClpRead_mode = 1;
    11281127    CbcOrClpReadCommand = stdin;
    1129     noPrinting = false;
    11301128    int returnCode = CbcMain1(n + 2, const_cast<const char **>(argv),
    1131                               model, callBack);
     1129                              model, callBack,staticParameterData);
    11321130    for (int k = 0; k < n + 2; k++)
    11331131        free(argv[k]);
     
    11361134}
    11371135
    1138 static CbcOrClpParam parameters[CBCMAXPARAMETERS];
    1139 static int numberParameters = 0 ;
    11401136CglPreProcess * cbcPreProcessPointer=NULL;
    11411137
     
    11501146}
    11511147
     1148#ifdef CBC_THREAD_SAFE
     1149// Copies of some input decoding
     1150
     1151static std::string
     1152CoinReadGetCommand(int &whichArgument, int argc, const char *argv[])
     1153{
     1154  std::string field;
     1155  if (whichArgument < argc)
     1156    field = argv[whichArgument++];
     1157  else
     1158    field = "quit";
     1159  if (field[0] == '-')
     1160    field = field.substr(1);
     1161  return field;
     1162}
     1163static std::string
     1164CoinReadGetString(int &whichArgument, int argc, const char *argv[])
     1165{
     1166  std::string field;
     1167  if (whichArgument < argc)
     1168    field = argv[whichArgument++];
     1169  else
     1170    field = "";
     1171  return field;
     1172}
     1173// valid 0 - okay, 1 bad, 2 not there
     1174static int
     1175CoinReadGetIntField(int &whichArgument, int argc, const char *argv[], int * valid)
     1176{
     1177  std::string field;
     1178  if (whichArgument < argc)
     1179    field = argv[whichArgument++];
     1180  else
     1181    field = "0";
     1182  long int value = 0;
     1183  const char * start = field.c_str();
     1184  char * endPointer = NULL;
     1185  // check valid
     1186  value =  strtol(start, &endPointer, 10);
     1187  if (*endPointer == '\0') {
     1188    *valid = 0;
     1189  } else {
     1190    *valid = 1;
     1191    std::cout << "String of " << field;
     1192  }
     1193  return static_cast<int>(value);
     1194}
     1195static double
     1196CoinReadGetDoubleField(int &whichArgument, int argc, const char *argv[], int * valid)
     1197{
     1198  std::string field;
     1199  if (whichArgument < argc)
     1200    field = argv[whichArgument++];
     1201  else
     1202    field = "0.0";
     1203  double value = 0.0;
     1204  const char * start = field.c_str();
     1205  char * endPointer = NULL;
     1206  // check valid
     1207  value =  strtod(start, &endPointer);
     1208  if (*endPointer == '\0') {
     1209    *valid = 0;
     1210  } else {
     1211    *valid = 1;
     1212    std::cout << "String of " << field;
     1213  }
     1214  return value;
     1215}
     1216// Redefine all
     1217#define CoinReadGetCommand(x,y) CoinReadGetCommand(whichArgument,x,y)
     1218#define CoinReadGetString(x,y) CoinReadGetString(whichArgument,x,y)
     1219#define CoinReadGetIntField(x,y,z) CoinReadGetIntField(whichArgument,x,y,z)
     1220#define CoinReadGetDoubleField(x,y,z) CoinReadGetDoubleField(whichArgument,x,y,z)
     1221#endif
     1222// Default Constructor
     1223CbcSolverUsefulData::CbcSolverUsefulData()
     1224{
     1225  totalTime_ = 0.0;
     1226  noPrinting_ = true;
     1227  useSignalHandler_ = false;
     1228  establishParams(numberParameters_,parameters_);
     1229}
     1230
     1231/* Copy constructor .
     1232 */
     1233CbcSolverUsefulData::CbcSolverUsefulData(const CbcSolverUsefulData & rhs)
     1234{
     1235  totalTime_ = rhs.totalTime_;
     1236  noPrinting_ = rhs.noPrinting_;
     1237  useSignalHandler_ = rhs.useSignalHandler_;
     1238  numberParameters_ = rhs.numberParameters_;
     1239  memcpy(parameters_,rhs.parameters_,sizeof(parameters_));
     1240}
     1241
     1242// Assignment operator
     1243CbcSolverUsefulData & CbcSolverUsefulData::operator=(const CbcSolverUsefulData& rhs)
     1244{
     1245  if (this != &rhs) {
     1246    totalTime_ = rhs.totalTime_;
     1247    noPrinting_ = rhs.noPrinting_;
     1248    useSignalHandler_ = rhs.useSignalHandler_;
     1249    numberParameters_ = rhs.numberParameters_;
     1250    memcpy(parameters_,rhs.parameters_,sizeof(parameters_));
     1251  }
     1252  return *this;
     1253}
     1254
     1255// Destructor
     1256CbcSolverUsefulData::~CbcSolverUsefulData ()
     1257{
     1258}
    11521259
    11531260/*
     
    11651272              int callBack(CbcModel * currentSolver, int whereFrom))
    11661273{
    1167     CbcOrClpParam * parameters_ = parameters;
    1168     int numberParameters_ = numberParameters;
     1274  // allow interrupts and printing
     1275  staticParameterData.noPrinting_ = false;
     1276  staticParameterData.useSignalHandler_=true;
     1277  return CbcMain1(argc,argv,model,callBack,staticParameterData);
     1278}
     1279/*
     1280  Meaning of whereFrom:
     1281    1 after initial solve by dualsimplex etc
     1282    2 after preprocessing
     1283    3 just before branchAndBound (so user can override)
     1284    4 just after branchAndBound (before postprocessing)
     1285    5 after postprocessing
     1286    6 after a user called heuristic phase
     1287*/
     1288int CbcMain1 (int argc, const char *argv[],
     1289              CbcModel  & model,
     1290              int callBack(CbcModel * currentSolver, int whereFrom),
     1291              CbcSolverUsefulData & parameterData)
     1292{
     1293    CbcOrClpParam * parameters_ = parameterData.parameters_;
     1294    int numberParameters_ = parameterData.numberParameters_;
     1295    double totalTime = parameterData.totalTime_;
     1296    bool noPrinting = parameterData.noPrinting_;
     1297    bool useSignalHandler = parameterData.useSignalHandler_;
    11691298    CbcModel & model_ = model;
     1299#ifdef CBC_THREAD_SAFE
     1300    // Initialize argument
     1301    int whichArgument=1;
     1302#endif
    11701303#ifdef CBC_USE_INITIAL_TIME
    11711304    if (model_.useElapsedTime())
     
    12511384        //CoinSighandler_t saveSignal=signal(SIGINT,signal_handler);
    12521385#if CBC_QUIET < 2
    1253         signal(SIGINT, signal_handler);
     1386        if (useSignalHandler)
     1387          signal(SIGINT, signal_handler);
    12541388#endif
    12551389        // Set up all non-standard stuff
     
    21112245                                  addFlags = 1048576*(value/10);
    21122246                                  value = value % 10;
    2113                                   parameters[whichParam(CBC_PARAM_INT_EXPERIMENT, numberParameters, parameters)].setIntValue(value);
     2247                                  parameters_[whichParam(CBC_PARAM_INT_EXPERIMENT, numberParameters_, parameters_)].setIntValue(value);
    21142248                                }
    21152249                                if (value >= 1) {
     
    21232257                                    }
    21242258                                    int extra4 = values[value-1]+addFlags;
    2125                                     parameters[whichParam(CBC_PARAM_INT_EXTRA4, numberParameters, parameters)].setIntValue(extra4);
     2259                                    parameters_[whichParam(CBC_PARAM_INT_EXTRA4, numberParameters_, parameters_)].setIntValue(extra4);
    21262260                                    if (!noPrinting_) {
    21272261                                        generalMessageHandler->message(CLP_GENERAL, generalMessages)
     
    21362270                                        << CoinMessageEol;
    21372271                                    }
    2138                                     parameters[whichParam(CBC_PARAM_STR_PROBINGCUTS, numberParameters, parameters)].setCurrentOption("forceOnStrong");
     2272                                    parameters_[whichParam(CBC_PARAM_STR_PROBINGCUTS, numberParameters_, parameters_)].setCurrentOption("forceOnStrong");
    21392273                                    probingAction = 8;
    21402274                                    parameters_[whichParam(CBC_PARAM_STR_GOMORYCUTS, numberParameters_, parameters_)].setCurrentOption("onGlobal");
     
    21452279                                    lpSolver->factorization()->forceOtherFactorization(3);
    21462280                                    parameters_[whichParam(CBC_PARAM_INT_MAXHOTITS, numberParameters_, parameters_)].setIntValue(100);
    2147                                     parameters[whichParam(CBC_PARAM_INT_CUTPASS, numberParameters, parameters)].setIntValue(1000);
     2281                                    parameters_[whichParam(CBC_PARAM_INT_CUTPASS, numberParameters_, parameters_)].setIntValue(1000);
    21482282                                    cutPass = 1000;
    2149                                     parameters[whichParam(CBC_PARAM_STR_RENS, numberParameters, parameters)].setCurrentOption("on");
     2283                                    parameters_[whichParam(CBC_PARAM_STR_RENS, numberParameters_, parameters_)].setCurrentOption("on");
    21502284                                }
    21512285                            } else if (parameters_[iParam].type() == CBC_PARAM_INT_STRATEGY) {
     
    91309264                               field = CoinReadGetString(argc, argv);
    91319265                               if (field == "$") {
    9132                                     field = parameters[iParam].stringValue();
     9266                                    field = parameters_[iParam].stringValue();
    91339267                               } else if (field == "EOL") {
    9134                                     parameters[iParam].printString();
     9268                                    parameters_[iParam].printString();
    91359269                                    break;
    91369270                               } else {
    9137                                     parameters[iParam].setStringValue(field);
     9271                                    parameters_[iParam].setStringValue(field);
    91389272                               }
    91399273                               std::string fileName;
     
    92669400}
    92679401
    9268 
    92699402void CbcMain0 (CbcModel  & model)
    92709403{
     9404  CbcMain0(model,staticParameterData);
     9405}
     9406void CbcMain0 (CbcModel  & model,
     9407               CbcSolverUsefulData & parameterData)
     9408{
     9409    CbcOrClpParam * parameters = parameterData.parameters_;
     9410    int numberParameters = parameterData.numberParameters_;
    92719411#ifndef CBC_OTHER_SOLVER
    92729412    OsiClpSolverInterface * originalSolver = dynamic_cast<OsiClpSolverInterface *> (model.solver());
     
    92889428    lpSolver->messageHandler()->setPrefix(false);
    92899429#endif
    9290     establishParams(numberParameters, parameters) ;
     9430    //establishParams(numberParameters, parameters) ;
    92919431    const char dirsep =  CoinFindDirSeparator();
    92929432    std::string directory;
  • trunk/Cbc/src/CbcSolver.hpp

    r1899 r1998  
    1010
    1111    This class is currently an orphan. With the removal of all code flagged
    12     with the NEWS_STYLE_SOLVER, this class is never instantiated (and cannot
     12    with the NEW_STYLE_SOLVER, this class is never instantiated (and cannot
    1313    be instantiated). It is available to be coopted as a top-level object
    1414    wrapping the current CbcMain0 and CbcMain1, should that appear to be a
     
    251251    // Up pseudo costs
    252252    double * pseudoUp_;
    253 } CbcSolverUsefulData;
    254 
     253} CbcSolverUsefulData2;
     254
     255//#############################################################################
     256
     257/**
     258   The CbcSolver class was taken out at a 9/12/09 meeting
     259   This is a feeble replacement.
     260   At present everything is public
     261*/
     262class CbcSolverUsefulData {
     263
     264public:
     265    ///@name Constructors and destructors etc
     266    //@{
     267    /// Default Constructor
     268    CbcSolverUsefulData();
     269
     270    /** Copy constructor .
     271     */
     272    CbcSolverUsefulData(const CbcSolverUsefulData & rhs);
     273
     274    /// Assignment operator
     275    CbcSolverUsefulData & operator=(const CbcSolverUsefulData& rhs);
     276
     277    /// Destructor
     278    ~CbcSolverUsefulData ();
     279    //@}
     280
     281    ///@name Member data
     282    //@{
     283    // For time
     284    double totalTime_;
     285    // Parameters
     286    CbcOrClpParam parameters_[CBCMAXPARAMETERS];
     287    // Printing
     288    bool noPrinting_;
     289    // Whether to use signal handler
     290    bool useSignalHandler_;
     291    // Number of Parameters
     292    int numberParameters_;
     293    // Default pump tuning
     294    int initialPumpTune_;
     295    //@}
     296};
     297/// And this uses it
     298// When we want to load up CbcModel with options first
     299void CbcMain0 (CbcModel & babSolver,CbcSolverUsefulData & solverData);
     300int CbcMain1 (int argc, const char *argv[], CbcModel & babSolver, int (CbcModel * currentSolver, int whereFrom),CbcSolverUsefulData & solverData);
     301
     302//#############################################################################
    255303
    256304/*! \brief A class to allow the use of unknown user functionality
Note: See TracChangeset for help on using the changeset viewer.