X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Flpp%2Flpp.c;h=9ac76f4cf4f3c42eba24a56d5e00c4bb6af8817f;hb=2cfb4be35e6255d7cd59824e9b7a5eea39705227;hp=82ea74cbbf84644f9def6400af029531ec612476;hpb=c86aa375b9fe725d85d012dd1231727a03a16585;p=libfirm diff --git a/ir/lpp/lpp.c b/ir/lpp/lpp.c index 82ea74cbb..9ac76f4cf 100644 --- a/ir/lpp/lpp.c +++ b/ir/lpp/lpp.c @@ -1,11 +1,26 @@ -/** - * Author: Daniel Grund - * Date: Fri 13.05.2005 - * Copyright: (c) Universitaet Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. - * CVS-Id: $Id: lpp.c 27353 2010-04-07 13:33:16Z matze $ +/* + * Copyright (C) 2005-2011 University of Karlsruhe. All right reserved. + * + * This file is part of libFirm. + * + * This file may be distributed and/or modified under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation and appearing in the file LICENSE.GPL included in the + * packaging of this file. + * + * Licensees holding valid libFirm Professional Edition licenses may use + * this file in accordance with the libFirm Commercial License. + * Agreement provided with the Software. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. */ +/** + * @file + * @author Daniel Grund + */ #include "config.h" #include @@ -17,6 +32,7 @@ #include "hashptr.h" #include "debug.h" #include "set.h" +#include "debug.h" #include "sp_matrix.h" #include "mps.h" @@ -25,20 +41,20 @@ #include "lpp_solvers.h" #include "lpp_net.h" -#define HASH_NAME_T(n) HASH_STR((n)->name, strlen((n)->name)) +#define HASH_NAME_T(n) hash_str((n)->name) -static firm_dbg_module_t *dbg = NULL; +DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) static inline char *obst_xstrdup(struct obstack *obst, const char *str) { - return obstack_copy0(obst, str, strlen(str)); + return (char*)obstack_copy0(obst, str, strlen(str)); } static int cmp_name_t(const void *x, const void *y, size_t size) { - const lpp_name_t *n = x; - const lpp_name_t *m = y; - (void)size; /* stop warnings */ + const lpp_name_t *n = (const lpp_name_t*)x; + const lpp_name_t *m = (const lpp_name_t*)y; + (void)size; return strcmp(n->name, m->name); } @@ -52,20 +68,19 @@ static void update_stats(lpp_t *lpp) lpp->density = (double)lpp->n_elems / (double)(lpp->cst_next * lpp->var_next) * 100.0; } -#define INITIAL_SIZE 64 - -lpp_t *new_lpp(const char *name, lpp_opt_t opt_type) +lpp_t *lpp_new(const char *name, lpp_opt_t opt_type) { - return new_lpp_userdef(name, opt_type, INITIAL_SIZE, INITIAL_SIZE, 2.0); + return lpp_new_userdef(name, opt_type, 64, 64, 2.0); } -lpp_t *new_lpp_userdef(const char *name, lpp_opt_t opt_type, - int estimated_vars, int estimated_csts, double grow_factor) +lpp_t *lpp_new_userdef(const char *name, lpp_opt_t opt_type, + int estimated_vars, int estimated_csts, + double grow_factor) { lpp_t *lpp; int idx; - dbg = firm_dbg_register("lpp"); + DEBUG_ONLY(dbg = firm_dbg_register("lpp");) lpp = XMALLOCZ(lpp_t); obstack_init(&lpp->obst); @@ -88,13 +103,13 @@ lpp_t *new_lpp_userdef(const char *name, lpp_opt_t opt_type, return lpp; } -void free_lpp_matrix(lpp_t *lpp) +void lpp_free_matrix(lpp_t *lpp) { del_matrix(lpp->m); lpp->m = NULL; } -void free_lpp(lpp_t *lpp) +void lpp_free(lpp_t *lpp) { obstack_free(&lpp->obst, NULL); @@ -121,20 +136,27 @@ void lpp_set_fix_costs(lpp_t *lpp, double value) matrix_set(lpp->m, 0, 0, value); } -static inline int name2nr(set *where, const char *name) +static int name2nr(set *where, const char *name) { lpp_name_t find, *found; find.name = name; - found = set_find(where, &find, sizeof(find), HASH_NAME_T(&find)); + found = set_find(lpp_name_t, where, &find, sizeof(find), HASH_NAME_T(&find)); return (found ? found->nr : -1); } -#define cst_nr(lpp, name) name2nr(lpp->cst2nr, name) -#define var_nr(lpp, name) name2nr(lpp->var2nr, name) +static int cst_nr(const lpp_t *lpp, const char *name) +{ + return name2nr(lpp->cst2nr, name); +} + +static int var_nr(const lpp_t *lpp, const char *name) +{ + return name2nr(lpp->var2nr, name); +} static inline char *get_next_name(lpp_t *lpp) { - char *res = obstack_alloc(&lpp->obst, 12); + char *res = OALLOCN(&lpp->obst, char, 12); snprintf(res, 12, "_%u", lpp->next_name_number++); return res; } @@ -154,7 +176,7 @@ int lpp_add_cst(lpp_t *lpp, const char *cst_name, lpp_cst_t cst_type, double rhs n.name = get_next_name(lpp); n.nr = -1; - inner = set_insert(lpp->cst2nr, &n, sizeof(n), HASH_NAME_T(&n)); + inner = set_insert(lpp_name_t, lpp->cst2nr, &n, sizeof(n), HASH_NAME_T(&n)); assert(inner); if (inner->nr == -1) { @@ -184,7 +206,7 @@ int lpp_add_cst_uniq(lpp_t *lpp, const char *cst_name, lpp_cst_t cst_type, doubl n.name = cst_name; n.nr = -1; - assert(!set_find(lpp->cst2nr, &n, sizeof(n), HASH_NAME_T(&n)) && + assert(!set_find(lpp_name_t, lpp->cst2nr, &n, sizeof(n), HASH_NAME_T(&n)) && "constraint already exists"); } return lpp_add_cst(lpp, cst_name, cst_type, rhs); @@ -229,12 +251,12 @@ int lpp_add_var(lpp_t *lpp, const char *var_name, lpp_var_t var_type, double obj n.name = get_next_name(lpp); n.nr = -1; - inner = set_insert(lpp->var2nr, &n, sizeof(n), HASH_NAME_T(&n)); + inner = set_insert(lpp_name_t, lpp->var2nr, &n, sizeof(n), HASH_NAME_T(&n)); assert(inner); if (inner->nr == -1) { inner->nr = lpp->var_next; - inner->value_kind = 0; + inner->value_kind = lpp_none; inner->value = 0; inner->type.var_type = var_type; @@ -342,14 +364,14 @@ void lpp_check_startvals(lpp_t *lpp) fprintf(stderr, "constraint %s unsatisfied: %g != %g\n", cst->name, sum, cst_val); } break; - case lpp_less: + case lpp_less_equal: if(sum > cst_val) { - fprintf(stderr, "constraint %s unsatisfied: %g > %g\n", cst->name, sum, cst_val); + fprintf(stderr, "constraint %s unsatisfied: %g >= %g\n", cst->name, sum, cst_val); } break; - case lpp_greater: + case lpp_greater_equal: if(sum < cst_val) { - fprintf(stderr, "constraint %s unsatisfied: %g < %g\n", cst->name, sum, cst_val); + fprintf(stderr, "constraint %s unsatisfied: %g <= %g\n", cst->name, sum, cst_val); } break; default: @@ -377,9 +399,9 @@ static const char *lpp_cst_op_to_str(lpp_cst_t cst) switch(cst) { case lpp_equal: return "="; - case lpp_less: + case lpp_less_equal: return "<="; - case lpp_greater: + case lpp_greater_equal: return ">="; default: return ""; @@ -390,21 +412,35 @@ void lpp_dump_plain(lpp_t *lpp, FILE *f) { int i; + fprintf(f, lpp->opt_type == lpp_minimize ? "Minimize\n" : "Maximize\n"); for(i = 0; i < lpp->cst_next; ++i) { - const matrix_elem_t *elm; lpp_name_t *cst = lpp->csts[i]; + fprintf(f, "%16s: ", cst->name); matrix_foreach_in_row(lpp->m, cst->nr, elm) { lpp_name_t *var = lpp->vars[elm->col]; /* TODO Perhaps better a define LPP_COL_RHS */ if(elm->col > 0) - fprintf(f, "%+4.1f*%-16s ", elm->val, var->name); + fprintf(f, "%+4.1f %-16s ", elm->val, var->name); + } + + if (i == 0) { + fprintf(f, "\nSubject To\n"); + continue; } fprintf(f, "%3s %+4.1f\n", lpp_cst_op_to_str(cst->type.cst_type), matrix_get(lpp->m, cst->nr, 0)); } + + fprintf(f, "Binary\n"); + for(i = 0; i < lpp->var_next; ++i) { + lpp_name_t *var = lpp->vars[i]; + if (var->type.var_type == lpp_binary) + fprintf(f, "%16s\n", var->name); + } + fprintf(f, "End\n"); } /** @@ -426,7 +462,7 @@ void lpp_serialize(lpp_comm_t *comm, const lpp_t *lpp, int with_names) lpp_writel(comm, lpp->set_bound); lpp_writed(comm, lpp->bound); lpp_writed(comm, lpp->time_limit_secs); - lpp_writed(comm, lpp->emphasis); + lpp_writel(comm, lpp->emphasis); for(i = 0; i < lpp->cst_next; ++i) { lpp_name_t *name = lpp->csts[i]; @@ -448,25 +484,19 @@ void lpp_serialize(lpp_comm_t *comm, const lpp_t *lpp, int with_names) lpp_writes(comm, name->name); } - { - const matrix_elem_t *elm; - n = 0; - - matrix_foreach(lpp->m, elm) - n++; + n = 0; + matrix_foreach(lpp->m, elm) + n++; - assert(n == matrix_get_entries(lpp->m)); - lpp_writel(comm, n); - matrix_foreach(lpp->m, elm) { - lpp_writel(comm, elm->row); - lpp_writel(comm, elm->col); - lpp_writed(comm, elm->val); - } + assert(n == matrix_get_entries(lpp->m)); + lpp_writel(comm, n); + matrix_foreach(lpp->m, elm) { + lpp_writel(comm, elm->row); + lpp_writel(comm, elm->col); + lpp_writed(comm, elm->val); } } -#define NAMELEN 64 - /** * Deserialize an lpp from a file descriptor. * @param comm The file descriptor. @@ -483,14 +513,14 @@ lpp_t *lpp_deserialize(lpp_comm_t *comm) with_names = lpp_readl(comm); lpp->cst_next = lpp_readl(comm); lpp->var_next = lpp_readl(comm); - lpp->opt_type = lpp_readl(comm); + lpp->opt_type = (lpp_opt_t)lpp_readl(comm); lpp->name = lpp_reads(comm); /* read options */ lpp->set_bound = lpp_readl(comm); lpp->bound = lpp_readd(comm); lpp->time_limit_secs = lpp_readd(comm); - lpp->emphasis = lpp_readd(comm); + lpp->emphasis = (lpp_emphasis_t)lpp_readl(comm); lpp->cst_size = lpp->cst_next; lpp->var_size = lpp->var_next; @@ -506,18 +536,18 @@ lpp_t *lpp_deserialize(lpp_comm_t *comm) lpp_name_t name, *res; name.nr = lpp_readl(comm); - name.value_kind = lpp_readl(comm); - name.type.cst_type = lpp_readl(comm); + name.value_kind = (lpp_value_kind_t)lpp_readl(comm); + name.type.cst_type = (lpp_cst_t)lpp_readl(comm); if(with_names) { name.name = lpp_reads(comm); } else { - char* buf = XMALLOCN(char, NAMELEN); - snprintf(buf, NAMELEN, "c%d\n", name.nr); + char* buf = XMALLOCN(char, 32); + snprintf(buf, 32, "c%d\n", name.nr); name.name = buf; } - res = set_insert(lpp->cst2nr, &name, sizeof(name), HASH_NAME_T(&name)); + res = set_insert(lpp_name_t, lpp->cst2nr, &name, sizeof(name), HASH_NAME_T(&name)); lpp->csts[name.nr] = res; } @@ -525,18 +555,18 @@ lpp_t *lpp_deserialize(lpp_comm_t *comm) lpp_name_t name, *res; name.nr = lpp_readl(comm); - name.value_kind = lpp_readl(comm); - name.type.var_type = lpp_readl(comm); + name.value_kind = (lpp_value_kind_t)lpp_readl(comm); + name.type.var_type = (lpp_var_t)lpp_readl(comm); if(with_names) { name.name = lpp_reads(comm); } else { - char* buf = XMALLOCN(char, NAMELEN); - snprintf(buf, NAMELEN, "v%d\n", name.nr); + char* buf = XMALLOCN(char, 32); + snprintf(buf, 32, "v%d\n", name.nr); name.name = buf; } - res = set_insert(lpp->var2nr, &name, sizeof(name), HASH_NAME_T(&name)); + res = set_insert(lpp_name_t, lpp->var2nr, &name, sizeof(name), HASH_NAME_T(&name)); lpp->vars[name.nr] = res; } @@ -599,7 +629,7 @@ void lpp_serialize_stats(lpp_comm_t *comm, const lpp_t *lpp) void lpp_deserialize_stats(lpp_comm_t *comm, lpp_t *lpp) { - lpp->sol_state = lpp_readl(comm); + lpp->sol_state = (lpp_sol_state_t)lpp_readl(comm); lpp->iterations = lpp_readl(comm); lpp->sol_time = lpp_readd(comm); lpp->objval = lpp_readd(comm);