Exemple #1
0
MStatus lockEvent::doIt( const MArgList &args ) 
//
// Description: 
//  Entry point 
//
{
	MStatus status; 
	int result = 0;
	
	// First check our arguments 
	//
	if ( !parseArgs( args ) ) { 
		return MS::kFailure; 
	}

	if ( fAttach ) { 
		MItSelectionList iter( theList, MFn::kDependencyNode, &status ); 
		for ( ; status && !iter.isDone(); iter.next() ) { 
			MCallbackId id = installCallback( iter ); 
			if ( id ) { 
				result ++; 
			} else { 
				status = MS::kFailure; 
			}
		}
	} else if ( fOverrideFlag ) { 
		// What to do when callback occurs. 
		overrideMode = fOverrideVal; 
	} else if ( fClearCB ) { 
		clearCallbackIds(); 
		result++; 
	}	 

	clearResult(); 
	// Let the caller know if the operation was successful. 
	// We just use an integer value here.  Anything > 0 is 
	// a success. 
	//
	setResult( result ); 

	return status; 
}
Exemple #2
0
int PMAPI PM_setMouseHandler(int mask, PM_mouseHandler mh)
{
    RMREGS      regs;
    RMSREGS     sregs;
    uint    rseg,roff;

    lockPMHandlers();           /* Ensure our handlers are locked   */

    if (!installCallback(_PM_mouseISR, &mouseSel, &mouseOff, &rseg, &roff))
	return 0;
    _PM_mouseHandler = mh;

    /* Install the real mode mouse handler  */
    sregs.es = rseg;
    regs.x.dx = roff;
    regs.x.cx = _PM_mouseMask = mask;
    regs.x.ax = 0xC;
    PM_int86x(0x33, &regs, &regs, &sregs);
    return 1;
}
Exemple #3
0
int
main(int argc, char **argv)
{
   int status;
   CPXENVptr *env;
   CPXLPptr *lp;
   char const *modelfile = NULL;
   CPXASYNCptr *handle;
   CPXENVGROUPptr group;
   int i;
   int jobs = 0;
   int active;
   int *finished;
   char const **machine;
   char cwd[MAX_PATH_LEN];
   char usrfunc[MAX_PATH_LEN];
   int frequency;
   double absgap = 1e-6;
   int bestidx;
   CPXDIM c, cols;
   double *x;
   enum {
      OUTPUT_SILENT, OUTPUT_PREFIXED, OUTPUT_LOG
   } output = OUTPUT_SILENT;

#if defined(USE_MPI)
   int numprocs, rank;

   MPI_Init(&argc, &argv);
   MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
   if ( numprocs < 3 ) {
      fprintf (stderr, "Invalid number of processors (%d)\n", numprocs);
      abort ();
   }
   MPI_Comm_rank (MPI_COMM_WORLD, &rank);
   if ( rank != 0 ) {
      fprintf (stderr, "Master must have rank 0!\n");
      MPI_Finalize ();
      abort ();
   }
   machine = malloc (sizeof (*machine) * numprocs);
   if ( machine == NULL ) {
      fprintf (stderr, "Out of memory!\n");
      abort ();
   }
   for (i = 0; i < numprocs; ++i)
      machine[i] = "mpimachine";
   jobs = numprocs - 1;
#elif defined(USE_PROCESS)
   char const *bin = "./cplex";

   machine = malloc (sizeof (*machine) * argc);
   if ( machine == NULL ) {
      fprintf (stderr, "Out of memory!\n");
      abort ();
   }
#elif defined(USE_TCPIP)
   machine = malloc (sizeof (*machine) * argc);
   if ( machine == NULL ) {
      fprintf (stderr, "Out of memory!\n");
      abort ();
   }
#else
#   error "No transport type selected"
#endif


   /* Parse the command line. */
   for (i = 1; i < argc; ++i) {
      if ( strncmp (argv[i], "-model=", 7) == 0 )
         modelfile = argv[i] + 7;
#if defined(USE_MPI)
#elif defined(USE_PROCESS)
      else if ( strncmp (argv[i], "-machine=", 9) == 0 )
         machine[jobs++] = argv[i] + 9;
      else if ( strncmp (argv[i], "-bin=", 5) == 0 )
         bin = argv[i] + 5;
#elif defined(USE_TCPIP)
      else if ( strncmp (argv[i], "-address=", 9) == 0 )
         machine[jobs++] = argv[i];
#endif
      else if ( strncmp (argv[i], "-absgap=", 8) == 0 )
         absgap = strtod (argv[i] + 8, NULL);
      else if ( strcmp (argv[i], "-output-prefixed") == 0 )
         output = OUTPUT_PREFIXED;
      else if ( strcmp (argv[i], "-output-log") == 0 )
         output = OUTPUT_LOG;
   }

   /* Validate arguments.
    */
   if ( modelfile == NULL ) {
      fprintf (stderr, "No model file specified with -model=<modelfile>\n");
      abort ();
   }
   if ( jobs < 1 ) {
     fprintf (stderr, "Invalid job count %d\n", jobs);
     abort ();
   }

   /* Allocate working arrays. */
   if ( (env = malloc (sizeof (*env) * jobs)) == NULL ||
        (handle = malloc (sizeof (*handle) * jobs)) == NULL ||
        (lp = malloc (sizeof (*lp) * jobs)) == NULL ||
        (finished = calloc (jobs, sizeof (*finished))) == NULL ||
        (remotestats = calloc (jobs, sizeof (*remotestats))) == NULL )
   {
      fprintf (stderr, "Out of memory!\n");
      abort ();
   }

   /* Find the place at which to find the shared object that implements
    * the user function. On Windows the path to the current directory is
    * likely to contain blanks, so better quote it.
    */
   getcwd (cwd, sizeof (cwd));
   usrfunc[0] = 0;
#ifdef _WIN32
   strcat (usrfunc, "-libpath=\"");
   strcat (usrfunc, cwd);
   strcat (usrfunc, "\"");
#else
   strcat (usrfunc, "-libpath=");
   strcat (usrfunc, cwd);
#endif

   /* Create a remote object instances. */
   for (i = 0; i < jobs; ++i) {
      /* These values define how we connect to the remote object. It is
       * important to use a transport configuration that actually supports
       * disconnect/reconnect. For the "processtransport" this means to use
       * named pipes instead of anonymous pipes or stdio.
       */
      char const *transport;
      char const *args[16];
      int nextarg = 0;
      char *logpath = NULL;

#if defined(USE_MPI)
      char rankbuf[256];

      sprintf (rankbuf, "-remoterank=%d", i + 1);
      transport = "mpitransport";
      args[nextarg++] = rankbuf;
#elif defined(USE_PROCESS)
      char logbuf[1024];
      transport = "processtransport";
      /* If the machine is not "localhost" then use ssh to connect to
       * this machine. Otherwise just fork a process on the local
       * machine.
       */
      if ( machine[i] != NULL && strcmp (machine[i], "localhost") != 0 ) {
         args[nextarg++] = "/usr/bin/ssh";
         args[nextarg++] = machine[i];
      }
      args[nextarg++] = bin;
      args[nextarg++] = "-worker=process";
      if ( machine[i] != NULL )
         args[nextarg++] = "-stdio";
      else
         args[nextarg++] = "-namedpipes=.";
      args[nextarg++] = usrfunc;
      args[nextarg++] = "-userfunction=parmipopt_userfunction=REGISTER_USERFUNCTION";
      sprintf (logbuf, "-logfile=server%d.log", i);
      if ( (args[nextarg] = logpath = strdup (logbuf)) != NULL )
         ++nextarg;
#elif defined(USE_TCPIP)
      transport = "tcpiptransport";
      args[nextarg++] = machine[i];
#endif


      printf ("Creating env on %s\n", machine[i]);
      env[i] = CPXXopenCPLEXremote(transport, nextarg, args, &status);
      if ( status || env[i] == NULL ) {
         fprintf (stderr, "CPXXopenCPLEXremote: %d\n", status);
         abort ();
      }
      free (logpath);

      /* Enable output */
      switch (output) {
      case OUTPUT_SILENT:
         /* nothing */
         break;
      case OUTPUT_PREFIXED:
         {
            CPXCHANNELptr cres, cwar, cerr, clog;

            if ( (status = CPXXgetchannels (env[i], &cres, &cwar, &cerr, &clog)) != 0 ) {
               fprintf (stderr, "CPXXgetchannels: %d\n", status);
               abort ();
            }
            if ( (status = CPXXaddfuncdest (env[i], cres, env[i], printer)) != 0 ||
                 (status = CPXXaddfuncdest (env[i], cwar, env[i], printer)) != 0 ||
                 (status = CPXXaddfuncdest (env[i], clog, env[i], printer)) != 0 )
            {
               fprintf (stderr, "CPXXaddfpdest: %d\n", status);
               abort ();
            }
         }
         break;
      case OUTPUT_LOG:
         {
            if ( (status = CPXXsetintparam (env[i], CPXPARAM_ScreenOutput, CPX_ON)) != 0 ) {
               fprintf (stderr, "CPXXgetchannels: %d\n", status);
               abort ();
            }
         }
         break;
      }

      /* Create empty problem object for this remote solver. */
      printf ("Creating LP %d\n", i);
      lp[i] = CPXXcreateprob(env[i], &status, "problem");
      if ( status || lp[i] == NULL ) {
         fprintf (stderr, "CPXXcreateprob: %d\n", status);
         abort ();
      }

      /* Install and configure callbacks. */
      remotestats[i].env = env[i];
      remotestats[i].idx = i;
      if ( (status = CPXXsetinfohandler (env[i], infohandler, &remotestats[i])) != 0 ) {
         fprintf (stderr, "CPXXsetinfohandler: %d\n", status);
         abort ();
      }

      if ( (status = changeObjdiff (env[i], 1e-5)) != 0 ) {
         fprintf (stderr, "changeObjdiff: %d\n", status);
         abort ();
      }

      if ( (status = installCallback (env[i])) != 0 ) {
         fprintf (stderr, "installCallback: %d\n", status);
         abort ();
      }

      /* Apply predefined perameter settings for this solver. */
      applySettings (env[i], i);
   }

   /* Put all environments into one group so that we can use multicasts
    * on operations that are the same for all solvers and/or imply lots
    * of data exchange.
    */
   status = CPXXcreateenvgroup (&group, jobs, env);
   if ( status != 0 ) {
      fprintf (stderr, "CPXXcreateenvgroup: %d\n", status);
      abort ();
   }

   /* Read the model into all remote solver. */
   status = CPXXreadcopyprob_multicast (group, modelfile, NULL);
   if ( status != 0 ) {
      fprintf (stderr, "CPXXreadcopyprob_multicast: %d\n", status);
      abort ();
   }
   objsen = CPXXgetobjsen (env[0], lp[0]);

   /* We set the thread count for each solver to 1 so that we do not
    * run into problems if multiple solves are performed on the same
    * machine.
    */
   status = CPXXsetintparam_multicast (group, CPXPARAM_Threads, 1);
   if ( status != 0 ) {
      fprintf (stderr, "CPXXsetintparam_multicast: %d\n", status);
      abort ();
   }

   /* Start an asynchronous solve on each remote solver. */
   for (i = 0; i < jobs; ++i) {
      printf ("Solving %d\n", i);
      if ( (status = CPXXmipopt_async (env[i], lp[i], &handle[i])) != 0 ) {
         fprintf (stderr, "CPXXmipopt_async: %d\n", status);
         abort ();
      }
   }

   /* All solves are started. Loop until the stopping criterion is met. */
   active = jobs;
   frequency = 10000; /* Print current bounds every two seconds. */
   while (active > 0) {
      int running = 0;
      /* Check if we shold stop all solves.
       * We stop them if the absolute mipgap is reached.
       */
      if ( primal.valid && dual.valid &&
           ((objsen == CPX_MIN && dual.bound + absgap >= primal.bound) ||
            (objsen == CPX_MAX && dual.bound - absgap <= primal.bound)) )
      {
         printf ("Stopping criterion reached. Stopping all pending solves.\n");
         for (i = 0; i < jobs; ++i) {
            if ( !finished[i] )
               CPXXasynckill (handle[i]);
         }
         break;
      }
      if ( --frequency == 0 ) {
         printf ("dual=%f, primal=%f\n", dual.bound, primal.bound);
         frequency = 10000;
      }

      /* Loop over all solvers and test if they are still running. */
      for (i = 0; i < jobs; ++i) {
         if ( finished[i] )
            continue;
         CPXXasynctest (handle[i], &running);
         if ( !running ) {
            /* The job is finished. We have a solution, so kill all
             * others. */
            int j;
            --active;
            finished[i] = 1;
            printf ("First job (%d) is finished, killing the rest\n", i);
            for (j = 0; j < jobs; ++j) {
               if ( j != i )
                  CPXXasynckill (handle[j]);
            }
            break;
         }
      }
      millisleep (10);
   }

   /* All solves have finished. Join them. */
   for (i = 0; i < jobs; ++i) {
      double obj = -CPX_INFBOUND;
      int stat;
      
      status = CPXXmipopt_join (&handle[i]);
      if ( status ) {
         fprintf (stderr, "CPXXmipopt_join: %d\n", status);
         abort ();
      }

      status = CPXXgetobjval (env[i], lp[i], &obj);
      if ( status == CPXERR_NO_SOLN ) {
         /* No feasible solution found (yet) on this machine.
          * Just set objective function to a very big value
          */
         obj = (objsen == CPX_MIN) ? CPX_INFBOUND : -CPX_INFBOUND;
      }
      else if ( status ) {
         fprintf (stderr, "CPXXgetobjval: %d\n", status);
         abort ();
      }
      stat = CPXXgetstat (env[i], lp[i]);
      printf ("Job %d: %f, stat %d\n", i, obj, stat);
      printf ("\t%f, %f, %f\n", remotestats[i].dettime,
              remotestats[i].dual, remotestats[i].primal);

      if ( (status = removeCallback (env[i])) != 0 ) {
         fprintf (stderr, "removeCallback: %d\n", status);
         abort ();
      }
   }

   /* Fetch the x vector from the solver that produced the best
    * primal bound. */
   bestidx = primal.idx;
   cols = CPXXgetnumcols (env[bestidx], lp[bestidx]);
   if ( (x = malloc (cols * sizeof (*x))) == NULL ) {
      fprintf (stderr, "Out of memory!\n");
      abort ();
   }
   status = CPXXgetx (env[bestidx], lp[bestidx], x, 0, cols - 1);
   if ( status ) {
      fprintf (stderr, "CPXXgetx: %d\n", status);
      abort ();
   }
   printf ("Optimal solution:\n");
   for (c = 0; c < cols; ++c)
      printf ("x[%5d]: %f\n", c, x[c]);
   free (x);

   CPXXfreeenvgroup (&group);

   /* Close the CPLEX objects in _reverse_ order. */
   for (i = jobs - 1; i >= 0; --i)
      CPXXcloseCPLEX (&env[i]);
   free (remotestats);
   free (env);
   free (lp);
   free (handle);
   free (finished);
   free ((char **)machine);

#ifdef USE_MPI
   MPI_Finalize ();
#endif


   return 0;
}