outsourced some functionality
authorSebastian Hack <hack@cs.uni-sb.de>
Thu, 16 Jun 2011 13:50:22 +0000 (15:50 +0200)
committerSebastian Hack <hack@cs.uni-sb.de>
Thu, 16 Jun 2011 13:51:16 +0000 (15:51 +0200)
adapted to changes

ir/be/becopyilp.c
ir/be/bemain.c
ir/lpp/lpp.c
ir/lpp/lpp.h
ir/lpp/lpp_cplex.c
ir/lpp/lpp_server.c
ir/lpp/lpp_solvers.c [new file with mode: 0644]
ir/lpp/lpp_solvers.h [new file with mode: 0644]

index 87646de..a64884a 100644 (file)
@@ -34,6 +34,8 @@
 #include "bemodule.h"
 #include "error.h"
 
+#include "lpp.h"
+
 #include "lc_opts.h"
 #include "lc_opts_enum.h"
 
@@ -229,7 +231,7 @@ lpp_sol_state_t ilp_go(ilp_env_t *ienv)
        if (solve_log)
                lpp_set_log(ienv->lp, stdout);
 
-       lpp_solve_net(ienv->lp, options->ilp_server, options->ilp_solver);
+       lpp_solve(ienv->lp, options->ilp_server, options->ilp_solver);
 
        //be_stat_ev_dbl("co_ilp_objval",     ienv->lp->objval);
        //be_stat_ev_dbl("co_ilp_best_bound", ienv->lp->best_bound);
index 0eafb66..959a379 100644 (file)
@@ -86,7 +86,7 @@ static be_options_t be_options = {
        0,                                 /* try to omit frame pointer */
        0,                                 /* create PIC code */
        BE_VERIFY_WARN,                    /* verification level: warn */
-       "i44pc52.info.uni-karlsruhe.de",   /* ilp server */
+       "",                                /* ilp server */
        "cplex",                           /* ilp solver */
        0,                                 /* enable statistic event dumping */
        "",                                /* print stat events */
index af1bd2d..7ef0eba 100644 (file)
@@ -26,6 +26,7 @@
 #include "mps.h"
 #include "lpp_t.h"
 #include "lpp_comm.h"
+#include "lpp_solvers.h"
 
 #define HASH_NAME_T(n) HASH_STR((n)->name, strlen((n)->name))
 
@@ -582,3 +583,17 @@ void lpp_deserialize_stats(lpp_comm_t *comm, lpp_t *lpp)
        lpp->objval     = lpp_readd(comm);
        lpp->best_bound = lpp_readd(comm);
 }
+
+void lpp_solve(lpp_t *lpp, const char* host, const char* solver)
+{
+       if (host == NULL || strlen(host) == 0) {
+               lpp_solver_func_t* f = lpp_find_solver(solver);
+               if (f != NULL)
+                       f(lpp);
+       }
+
+       else {
+               lpp_solve_net(lpp, host, solver);
+       }
+}
+
index 1fd6bb4..2091b2b 100644 (file)
@@ -300,4 +300,12 @@ void lpp_dump_plain(lpp_t *lpp, FILE *f);
  */
 #define lpp_unset_bound(lpp)          ((lpp)->set_bound = 0)
 
+/**
+ * Solve an ILP.
+ * @param lpp    The problem.
+ * @param host   The host to solve on.
+ * @param solver The solver to use.
+ */
+void lpp_solve(lpp_t *lpp, const char* host, const char* solver);
+
 #endif /* _LPP_H */
index f6270fa..8986d87 100644 (file)
@@ -213,6 +213,9 @@ static void cpx_solve(cpx_t *cpx) {
                                        ? CPX_PARAM_OBJLLIM : CPX_PARAM_OBJULIM), lpp->bound);
        }
 
+       /* turn on the fancy messages :) */
+       // CPXsetintparam (cpx->env, CPX_PARAM_SCRIND, CPX_ON);
+
        /* solve */
        gettimeofday(&tvb, NULL);
        cpx->status = CPXmipopt(cpx->env, cpx->prob);
index bae79c0..bd7393b 100644 (file)
@@ -44,7 +44,7 @@
 
 #include "lpp_t.h"
 #include "lpp_comm.h"
-#include "lpp_cplex.h"
+#include "lpp_solvers.h"
 
 #define MAX_JOBS 128
 
@@ -65,8 +65,6 @@ static int n_children = 0;
 
 extern char** environ;
 
-typedef void (solver_func_t)(lpp_t *lpp);
-
 typedef struct _job_t {
        struct list_head list;
        int id;
@@ -74,7 +72,7 @@ typedef struct _job_t {
        pthread_t session;
        lpp_comm_t *comm;
        lpp_t *lpp;
-       solver_func_t *solver_func;
+       lpp_solver_func_t *solver_func;
        time_t received;
        int csock;
 } job_t;
@@ -114,7 +112,7 @@ static void initproctitle(int argc, char **argv) {
        --title_length;
 }
 
-static void job_init(job_t *job, lpp_comm_t *comm, lpp_t *lpp, solver_func_t *solver_func)
+static void job_init(job_t *job, lpp_comm_t *comm, lpp_t *lpp, lpp_solver_func_t *solver_func)
 {
   /* TODO MAXJOBS */
   memset(job, 0, sizeof(job[0]));
@@ -161,64 +159,6 @@ static int passive_tcp(uint16_t port, int queue_len)
   return s;
 }
 
-static void dummy_solver(lpp_t *lpp)
-{
-  int i;
-
-  for(i = 0; i < lpp->var_next; ++i) {
-    lpp->vars[i]->value = i;
-    lpp->vars[i]->value_kind = lpp_value_solution;
-  }
-
-  if(lpp->log)
-         fprintf(lpp->log, "dummy solver exiting now.\n");
-
-  sleep(1);
-  lpp->sol_time = 0.0;
-  lpp->iterations = 0;
-  lpp->sol_state = lpp_optimal;
-}
-
-static void segv_solver(lpp_t *lpp)
-{
-  int i;
-
-  for(i = 0; i < lpp->var_next; ++i) {
-    lpp->vars[i]->value = i;
-    lpp->vars[i]->value_kind = lpp_value_solution;
-  }
-
-  if(lpp->log)
-         fprintf(lpp->log, "segv dummy solver exiting now.\n");
-
-  sleep(1);
-  *((int *) 0) = 1;
-}
-
-#define DEFAULT_SOLVER lpp_solve_cplex
-
-struct {
-  solver_func_t *solver;
-  const char    *name;
-  int           n_instances;
-} solvers[] = {
-  { lpp_solve_cplex,   "cplex",   1 },
-  { dummy_solver,      "dummy",   2 },
-  { segv_solver,       "segv",    2 },
-};
-
-
-static solver_func_t *find_solver(const char *name)
-{
-  unsigned i;
-
-  for(i = 0; i < sizeof(solvers) / sizeof(solvers[0]); ++i)
-    if(strcmp(solvers[i].name, name) == 0)
-      return solvers[i].solver;
-
-  return NULL;
-}
-
 static void *solver_thread(void * data)
 {
        job_t *job = data;
@@ -345,8 +285,8 @@ static int solve(lpp_comm_t *comm, job_t *job)
 
 static void *session(int fd)
 {
-       solver_func_t *solver = DEFAULT_SOLVER;
-       lpp_comm_t *comm      = lpp_comm_new(fd, LPP_BUFSIZE);
+       lpp_solver_func_t *solver = lpp_find_solver("dummy");
+       lpp_comm_t *comm          = lpp_comm_new(fd, LPP_BUFSIZE);
 
        DBG((dbg, LEVEL_1, "starting session thread pid %d tid %d\n", getpid(), pthread_self()));
        setproctitle("lpp_server [child]");
@@ -393,17 +333,20 @@ static void *session(int fd)
 
                        case LPP_CMD_SOLVER:
                                lpp_readbuf(comm, buf, sizeof(buf));
-                               solver = find_solver(buf);
+                               solver = lpp_find_solver(buf);
                                DBG((dbg, LEVEL_2, "setting solver to: %s\n", buf));
                                //lpp_send_res(comm, solver != NULL, "could not find solver: %s", buf);
                                break;
 
                        case LPP_CMD_SOLVERS:
                                {
-                                       int i, n = ARRAY_SIZE(solvers);
-                                       lpp_writel(comm, n);
-                                       for(i = 0; i < n; ++i)
-                                               lpp_writes(comm, solvers[i].name);
+                                       int i;
+
+                                       for(i = 0; lpp_solvers[i].solver != NULL; i++);
+                                       lpp_writel(comm, i);
+
+                                       for(i = 0; lpp_solvers[i].solver != NULL; i++)
+                                               lpp_writes(comm, lpp_solvers[i].name);
                                        lpp_flush(comm);
                                }
                                break;
diff --git a/ir/lpp/lpp_solvers.c b/ir/lpp/lpp_solvers.c
new file mode 100644 (file)
index 0000000..1cfc52a
--- /dev/null
@@ -0,0 +1,67 @@
+/**
+ * @file   lpp_solvers.c
+ * @date   16.06.2011
+ * @author Sebastian Hack
+ *
+ * Copyright (C) 2011 Saarland University
+ * Released under the LGPL
+ */
+
+#include <unistd.h>
+
+#include "lpp_cplex.h"
+#include "lpp_solvers.h"
+
+static void dummy_solver(lpp_t *lpp)
+{
+  int i;
+
+  for(i = 0; i < lpp->var_next; ++i) {
+    lpp->vars[i]->value = i;
+    lpp->vars[i]->value_kind = lpp_value_solution;
+  }
+
+  if(lpp->log)
+         fprintf(lpp->log, "dummy solver exiting now.\n");
+
+  sleep(1);
+  lpp->sol_time = 0.0;
+  lpp->iterations = 0;
+  lpp->sol_state = lpp_optimal;
+}
+
+static void segv_solver(lpp_t *lpp)
+{
+  int i;
+
+  for(i = 0; i < lpp->var_next; ++i) {
+    lpp->vars[i]->value = i;
+    lpp->vars[i]->value_kind = lpp_value_solution;
+  }
+
+  if(lpp->log)
+         fprintf(lpp->log, "segv dummy solver exiting now.\n");
+
+  sleep(1);
+  *((int *) 0) = 1;
+}
+
+lpp_solver_t lpp_solvers[] = {
+  { lpp_solve_cplex,   "cplex",   1 },
+  { dummy_solver,      "dummy",   2 },
+  { segv_solver,       "segv",    2 },
+  { NULL,              NULL,      0 }
+};
+
+lpp_solver_func_t *lpp_find_solver(const char *name)
+{
+  int i;
+
+  for(i = 0; lpp_solvers[i].solver != NULL; i++)
+    if(strcmp(lpp_solvers[i].name, name) == 0)
+      return lpp_solvers[i].solver;
+
+  return NULL;
+}
+
+
diff --git a/ir/lpp/lpp_solvers.h b/ir/lpp/lpp_solvers.h
new file mode 100644 (file)
index 0000000..1ef7333
--- /dev/null
@@ -0,0 +1,30 @@
+/**
+ * @file   lpp_solvers.h
+ * @date   16.06.2011
+ * @author Sebastian Hack
+ *
+ * Copyright (C) 2011 Saarland University
+ * Released under the LGPL
+ */
+
+#ifndef LPP_SOLVER_H
+#define LPP_SOLVER_H
+
+#include "lpp.h"
+
+typedef void (lpp_solver_func_t)(lpp_t *lpp);
+
+typedef struct {
+  lpp_solver_func_t *solver;
+  const char        *name;
+  int                n_instances;
+} lpp_solver_t;
+
+extern lpp_solver_t lpp_solvers[];
+
+/**
+ * Find a solver for a given name.
+ */
+lpp_solver_func_t *lpp_find_solver(const char *name);
+
+#endif /* LPP_SOLVER_H */