Main Page | Class List | File List | Class Members | File Members

ranking.c

Go to the documentation of this file.
00001 // Tools to compute the ranking function of an iteration J : the number of integer points in P that are lexicographically inferior to J
00002 // B. Meister 6/2005
00003 // LSIIT-ICPS, UMR 7005 CNRS Université Louis Pasteur
00004 // HiPEAC Network
00005 
00006 #include <polylib/polylib.h>
00007 #include <polylib/ranking.h>
00008 
00009 /*
00010  * Returns a list of polytopes needed to compute
00011  * the number of points in P that are lexicographically
00012  * smaller than a given point in D.
00013  * Only the first dim dimensions are taken into account
00014  * for computing the lexsmaller relation.
00015  * The remaining variables are assumed to be extra
00016  * existential/control variables.
00017  * When P == D, this is the conventional ranking function.
00018  * P and D are assumed to have the same parameter domain C.
00019  *
00020  * The first polyhedron in the list returned is the
00021  * updated context: a combination of D and C or an extended C.
00022  *
00023  * The order of the variables in the remaining polyhedra is
00024  * - first dim variables of P
00025  * - existential variables of P
00026  * - existential variables of D
00027  * - first dim variables of D
00028  * - the parameters
00029  */
00030 Polyhedron *LexSmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
00031                         Polyhedron *C, unsigned MAXRAYS)
00032 {
00033   unsigned i, j, k, r;
00034   unsigned nb_parms = C->Dimension;
00035   unsigned nb_vars = dim;
00036   unsigned P_extra = P->Dimension - nb_vars - nb_parms;
00037   unsigned D_extra = D->Dimension - nb_vars - nb_parms;
00038   unsigned nb_new_parms;
00039   unsigned ncons;
00040   Matrix * cur_element, * C_times_J, * Klon;
00041   Polyhedron * P1, *C1;
00042   Polyhedron * lexico_lesser_union = NULL;
00043 
00044   POL_ENSURE_INEQUALITIES(C);
00045   POL_ENSURE_INEQUALITIES(D);
00046   POL_ENSURE_INEQUALITIES(P);
00047 
00048   assert(P->Dimension >= C->Dimension + dim);
00049   assert(D->Dimension >= C->Dimension + dim);
00050   nb_new_parms = nb_vars;
00051 
00052   // the number of variables must be positive
00053   if (nb_vars<=0) {
00054     printf("\nRanking > No variables, returning NULL.\n"); 
00055     return NULL;
00056   }
00057   /*
00058    * if D has extra variables, then we can't squeeze the contraints
00059    * of D in the new context, so we simply add them to each element.
00060    */
00061   if (D_extra)
00062     cur_element = Matrix_Alloc(P->NbConstraints+D->NbConstraints+nb_new_parms, 
00063                                P->Dimension+D_extra+nb_new_parms+2);
00064   else
00065     cur_element = Matrix_Alloc(P->NbConstraints+nb_new_parms, 
00066                                P->Dimension+D_extra+nb_new_parms+2);
00067 
00068 
00069   // 0- Put P in the first rows of cur_element
00070   for (i=0; i < P->NbConstraints; i++) {
00071     Vector_Copy(P->Constraint[i], cur_element->p[i], nb_vars+P_extra+1);
00072     Vector_Copy(P->Constraint[i]+1+nb_vars+P_extra, 
00073                 cur_element->p[i]+1+nb_vars+P_extra+D_extra+nb_new_parms, 
00074                 nb_parms+1);
00075   }
00076   ncons = P->NbConstraints;
00077   if (D_extra) {
00078     for (i=0; i < D->NbConstraints; i++) {
00079       r = P->NbConstraints + i;
00080       Vector_Copy(D->Constraint[i], cur_element->p[r], 1);
00081       Vector_Copy(D->Constraint[i]+1, 
00082                   cur_element->p[r]+1+nb_vars+P_extra+D_extra, nb_new_parms);
00083       Vector_Copy(D->Constraint[i]+1+nb_new_parms, 
00084                   cur_element->p[r]+1+nb_vars+P_extra, D_extra);
00085       Vector_Copy(D->Constraint[i]+1+nb_new_parms+D_extra, 
00086                   cur_element->p[r]+1+nb_vars+P_extra+D_extra+nb_new_parms, 
00087                   nb_parms+1);
00088     }
00089     ncons += D->NbConstraints;
00090   }
00091 
00092   // 1- compute the Ehrhart polynomial of each disjoint polyhedron defining the lexicographic order
00093   for (k=0, r = ncons; k < nb_vars; k++, r++) {
00094 
00095     // a- build the corresponding matrix
00096     // the nb of rows of cur_element is fake, so that we do not have to re-allocate it.
00097     cur_element->NbRows = r+1;
00098 
00099     // convert the previous (strict) inequality into an equality
00100     if (k>=1) {
00101       value_set_si(cur_element->p[r-1][0], 0);
00102       value_set_si(cur_element->p[r-1][cur_element->NbColumns-1], 0);
00103     }
00104     // build the k-th inequality from P
00105     value_set_si(cur_element->p[r][0], 1);
00106     value_set_si(cur_element->p[r][k+1], -1);
00107     value_set_si(cur_element->p[r][nb_vars+P_extra+D_extra+k+1], 1);
00108     // we want a strict inequality
00109     value_set_si(cur_element->p[r][cur_element->NbColumns-1], -1);
00110 #ifdef ERDEBUG
00111     show_matrix(cur_element);
00112 #endif
00113 
00114     // b- add it to the current union
00115     // as Constraints2Polyhedron modifies its input, we must clone cur_element
00116     Klon = Matrix_Copy(cur_element);
00117     P1 = Constraints2Polyhedron(Klon, MAXRAYS);
00118     Matrix_Free(Klon);
00119     P1->next = lexico_lesser_union;
00120     lexico_lesser_union = P1;
00121   }
00122   
00123   // 2- as we introduce n parameters, we must introduce them into the context as well
00124   // The added constraints are P.M.(J N 1 )^T >=0
00125   if (D_extra)
00126     C_times_J = Matrix_Alloc(C->NbConstraints, nb_new_parms+nb_parms+2);
00127   else
00128     C_times_J = Matrix_Alloc(C->NbConstraints + D->NbConstraints, D->Dimension+2);
00129   // copy the initial context while adding the new parameters
00130   for (i = 0; i < C->NbConstraints; i++) {
00131     value_assign(C_times_J->p[i][0], C->Constraint[i][0]);
00132     Vector_Copy(C->Constraint[i]+1, C_times_J->p[i]+1+nb_new_parms, nb_parms+1);
00133   }
00134 
00135   /* copy constraints from evaluation domain */
00136   if (!D_extra)
00137     for (i = 0; i < D->NbConstraints; i++)
00138       Vector_Copy(D->Constraint[i], C_times_J->p[C->NbConstraints+i], 
00139                   D->Dimension+2);
00140 
00141 #ifdef ERDEBUG
00142   show_matrix(C_times_J);
00143 #endif
00144   C1 = Constraints2Polyhedron(C_times_J, POL_NO_DUAL);
00145 
00146   // 4- clean up
00147   Matrix_Free(cur_element);
00148   Matrix_Free(C_times_J);
00149 
00150   C1->next = P1;
00151 
00152   return C1;
00153 } // Ranking
00154 
00155 /*
00156  * Returns the number of points in P that are lexicographically
00157  * smaller than a given point in D.
00158  * Only the first dim dimensions are taken into account
00159  * for computing the lexsmaller relation.
00160  * The remaining variables are assumed to be extra
00161  * existential/control variables.
00162  * When P == D, this is the conventional ranking function.
00163  * P and D are assumed to have the same parameter domain C.
00164  * The variables in the Enumeration correspond to the first dim variables
00165  * in D followed by the parameters of D (the variables of C).
00166  */
00167 Enumeration *Polyhedron_LexSmallerEnumerate(Polyhedron *P, Polyhedron *D, 
00168                                             unsigned dim,
00169                                             Polyhedron *C, unsigned MAXRAYS)
00170 {
00171   Enumeration * ranking;
00172   Polyhedron *RC, *RD;
00173 
00174   RC = LexSmaller(P, D, dim, C, MAXRAYS);
00175   RD = RC->next;
00176   RC->next = NULL;
00177 
00178   // 3- Compute the ranking, which is the sum of the Ehrhart polynomials of the n disjoint polyhedra we just put in P1.
00179   // OPT : our polyhdera are (already) disjoint, so Domain_Enumerate does probably too much work uselessly
00180   ranking = Domain_Enumerate(RD, RC, MAXRAYS, NULL);
00181 
00182   Domain_Free(RD);
00183   Polyhedron_Free(RC);
00184 
00185   return ranking;
00186 }
00187 
00188 /*
00189  * Returns a function that assigns a unique number to each point in the
00190  * polytope P ranging from zero to (number of points in P)-1.
00191  * The order of the numbers corresponds to the lexicographical order.
00192  *
00193  * C is the parameter context of the polytope
00194  */
00195 Enumeration *Polyhedron_Ranking(Polyhedron *P, Polyhedron *C, unsigned MAXRAYS)
00196 {
00197     return Polyhedron_LexSmallerEnumerate(P, P, P->Dimension-C->Dimension, 
00198                                           C, MAXRAYS);
00199 }

Generated on Mon Sep 12 15:15:11 2005 for polylib by doxygen 1.3.5