polylib 5.22.8
ranking.c
Go to the documentation of this file.
1/**
2 * Tools to compute the ranking function of an iteration J: the number of
3 * integer points in P that are lexicographically inferior to J
4 * B. Meister
5 * 6/2005
6 * LSIIT-ICPS, UMR 7005 CNRS Universite Louis Pasteur
7 * HiPEAC Network
8 */
9
10#include <polylib/polylib.h>
11#include <polylib/ranking.h>
12
13/**
14 * Returns a list of polytopes needed to compute
15 * the number of points in P that are lexicographically
16 * smaller than a given point in D.
17 * Only the first dim dimensions are taken into account
18 * for computing the lexsmaller relation.
19 * The remaining variables are assumed to be extra
20 * existential/control variables.
21 * When P == D, this is the conventional ranking function.
22 * P and D are assumed to have the same parameter domain C.
23 *
24 * The first polyhedron in the list returned is the
25 * updated context: a combination of D and C or an extended C.
26 *
27 * The order of the variables in the remaining polyhedra is
28 * - first dim variables of P
29 * - existential variables of P
30 * - existential variables of D
31 * - first dim variables of D
32 * - the parameters
33 */
35 Polyhedron *C, unsigned MAXRAYS) {
36 unsigned i, j, k, r;
37 unsigned nb_parms = C->Dimension;
38 unsigned nb_vars = dim;
39 unsigned P_extra = P->Dimension - nb_vars - nb_parms;
40 unsigned D_extra = D->Dimension - nb_vars - nb_parms;
41 unsigned nb_new_parms;
42 unsigned ncons;
43 Matrix *cur_element, *C_times_J, *Klon;
44 Polyhedron *P1, *C1;
45 Polyhedron *lexico_lesser_union = NULL;
46
50
51 assert(P->Dimension >= C->Dimension + dim);
52 assert(D->Dimension >= C->Dimension + dim);
53 nb_new_parms = nb_vars;
54
55 /* the number of variables must be positive */
56 if (nb_vars <= 0) {
57 printf("\nRanking > No variables, returning NULL.\n");
58 return NULL;
59 }
60 /*
61 * if D has extra variables, then we can't squeeze the contraints
62 * of D in the new context, so we simply add them to each element.
63 */
64 if (D_extra)
65 cur_element =
66 Matrix_Alloc(P->NbConstraints + D->NbConstraints + nb_new_parms,
67 P->Dimension + D_extra + nb_new_parms + 2);
68 else
69 cur_element = Matrix_Alloc(P->NbConstraints + nb_new_parms,
70 P->Dimension + D_extra + nb_new_parms + 2);
71
72 /* 0- Put P in the first rows of cur_element */
73 for (i = 0; i < P->NbConstraints; i++) {
74 Vector_Copy(P->Constraint[i], cur_element->p[i], nb_vars + P_extra + 1);
75 Vector_Copy(P->Constraint[i] + 1 + nb_vars + P_extra,
76 cur_element->p[i] + 1 + nb_vars + P_extra + D_extra +
77 nb_new_parms,
78 nb_parms + 1);
79 }
80 ncons = P->NbConstraints;
81 if (D_extra) {
82 for (i = 0; i < D->NbConstraints; i++) {
83 r = P->NbConstraints + i;
84 Vector_Copy(D->Constraint[i], cur_element->p[r], 1);
85 Vector_Copy(D->Constraint[i] + 1,
86 cur_element->p[r] + 1 + nb_vars + P_extra + D_extra,
87 nb_new_parms);
88 Vector_Copy(D->Constraint[i] + 1 + nb_new_parms,
89 cur_element->p[r] + 1 + nb_vars + P_extra, D_extra);
90 Vector_Copy(D->Constraint[i] + 1 + nb_new_parms + D_extra,
91 cur_element->p[r] + 1 + nb_vars + P_extra + D_extra +
92 nb_new_parms,
93 nb_parms + 1);
94 }
95 ncons += D->NbConstraints;
96 }
97
98 /* 1- compute the Ehrhart polynomial of each disjoint polyhedron defining the
99 lexicographic order */
100 for (k = 0, r = ncons; k < nb_vars; k++, r++) {
101
102 /* a- build the corresponding matrix
103 * the nb of rows of cur_element is fake, so that we do not have to
104 * re-allocate it. */
105 cur_element->NbRows = r + 1;
106
107 /* convert the previous (strict) inequality into an equality */
108 if (k >= 1) {
109 value_set_si(cur_element->p[r - 1][0], 0);
110 value_set_si(cur_element->p[r - 1][cur_element->NbColumns - 1], 0);
111 }
112 /* build the k-th inequality from P */
113 value_set_si(cur_element->p[r][0], 1);
114 value_set_si(cur_element->p[r][k + 1], -1);
115 value_set_si(cur_element->p[r][nb_vars + P_extra + D_extra + k + 1], 1);
116 /* we want a strict inequality */
117 value_set_si(cur_element->p[r][cur_element->NbColumns - 1], -1);
118#ifdef ERDEBUG
119 show_matrix(cur_element);
120#endif
121
122 /* b- add it to the current union
123 as Constraints2Polyhedron modifies its input, we must clone cur_element
124 */
125 Klon = Matrix_Copy(cur_element);
127 Matrix_Free(Klon);
128 P1->next = lexico_lesser_union;
129 lexico_lesser_union = P1;
130 }
131
132 /* 2- as we introduce n parameters, we must introduce them into the context
133 * as well.
134 * The added constraints are P.M.(J N 1 )^T >=0 */
135 if (D_extra)
136 C_times_J = Matrix_Alloc(C->NbConstraints, nb_new_parms + nb_parms + 2);
137 else
138 C_times_J =
140 /* copy the initial context while adding the new parameters */
141 for (i = 0; i < C->NbConstraints; i++) {
142 value_assign(C_times_J->p[i][0], C->Constraint[i][0]);
143 Vector_Copy(C->Constraint[i] + 1, C_times_J->p[i] + 1 + nb_new_parms,
144 nb_parms + 1);
145 }
146
147 /* copy constraints from evaluation domain */
148 if (!D_extra)
149 for (i = 0; i < D->NbConstraints; i++)
150 Vector_Copy(D->Constraint[i], C_times_J->p[C->NbConstraints + i],
151 D->Dimension + 2);
152
153#ifdef ERDEBUG
154 show_matrix(C_times_J);
155#endif
156 C1 = Constraints2Polyhedron(C_times_J, POL_NO_DUAL);
157
158 /* 4- clean up */
159 Matrix_Free(cur_element);
160 Matrix_Free(C_times_J);
161
162 C1->next = P1;
163
164 return C1;
165} /* LexSmaller */
166
167/**
168 * Returns the number of points in P that are lexicographically
169 * smaller than a given point in D.
170 * Only the first dim dimensions are taken into account
171 * for computing the lexsmaller relation.
172 * The remaining variables are assumed to be extra
173 * existential/control variables.
174 * When P == D, this is the conventional ranking function.
175 * P and D are assumed to have the same parameter domain C.
176 * The variables in the Enumeration correspond to the first dim variables
177 * in D followed by the parameters of D (the variables of C).
178 */
180 unsigned dim, Polyhedron *C,
181 unsigned MAXRAYS) {
182 Enumeration *ranking;
183 Polyhedron *RC, *RD;
184
185 RC = LexSmaller(P, D, dim, C, MAXRAYS);
186 RD = RC->next;
187 RC->next = NULL;
188
189 /* Compute the ranking, which is the sum of the Ehrhart polynomials of the n
190 disjoint polyhedra we just put in P1. */
191 /* OPT : our polyhdera are (already) disjoint, so Domain_Enumerate does
192 probably too much work uselessly */
193 ranking = Domain_Enumerate(RD, RC, MAXRAYS, NULL);
194
195 Domain_Free(RD);
196 Polyhedron_Free(RC);
197
198 return ranking;
199}
200
201/*
202 * Returns a function that assigns a unique number to each point in the
203 * polytope P ranging from zero to (number of points in P)-1.
204 * The order of the numbers corresponds to the lexicographical order.
205 *
206 * C is the parameter context of the polytope
207 */
209 unsigned MAXRAYS) {
210 return Polyhedron_LexSmallerEnumerate(P, P, P->Dimension - C->Dimension, C,
211 MAXRAYS);
212}
Matrix * Matrix_Copy(Matrix const *Src)
Definition: Matop.c:98
#define value_assign(v1, v2)
Definition: arithmetique.h:482
#define value_set_si(val, i)
Definition: arithmetique.h:483
#define assert(ex)
Definition: assert.h:16
Enumeration * Domain_Enumerate(Polyhedron *D, Polyhedron *C, unsigned MAXRAYS, const char **pn)
Definition: ext_ehrhart.c:671
Matrix * Matrix_Alloc(unsigned NbRows, unsigned NbColumns)
Definition: matrix.c:24
void Matrix_Free(Matrix *Mat)
Definition: matrix.c:71
#define show_matrix(M)
Polylib matrix addons Mainly, deals with polyhedra represented in implicit form (set of constraints).
Definition: matrix_addon.h:15
void Polyhedron_Free(Polyhedron *Pol)
Definition: polyhedron.c:1614
Polyhedron * Constraints2Polyhedron(Matrix *Constraints, unsigned NbMaxRays)
Given a matrix of constraints ('Constraints'), construct and return a polyhedron.
Definition: polyhedron.c:1905
void Domain_Free(Polyhedron *Pol)
Definition: polyhedron.c:1626
#define POL_ENSURE_INEQUALITIES(P)
Definition: polyhedron.h:5
Polyhedron * LexSmaller(Polyhedron *P, Polyhedron *D, unsigned dim, Polyhedron *C, unsigned MAXRAYS)
Tools to compute the ranking function of an iteration J: the number of integer points in P that are l...
Definition: ranking.c:34
Enumeration * Polyhedron_Ranking(Polyhedron *P, Polyhedron *C, unsigned MAXRAYS)
Definition: ranking.c:208
Enumeration * Polyhedron_LexSmallerEnumerate(Polyhedron *P, Polyhedron *D, unsigned dim, Polyhedron *C, unsigned MAXRAYS)
Returns the number of points in P that are lexicographically smaller than a given point in D.
Definition: ranking.c:179
Definition: types.h:75
unsigned NbRows
Definition: types.h:76
Value ** p
Definition: types.h:77
unsigned NbColumns
Definition: types.h:76
unsigned Dimension
Definition: types.h:94
unsigned NbConstraints
Definition: types.h:94
struct polyhedron * next
Definition: types.h:99
Value ** Constraint
Definition: types.h:95
#define POL_NO_DUAL
Definition: types.h:66
void Vector_Copy(Value *p1, Value *p2, unsigned length)
Definition: vector.c:256
#define MAXRAYS
Definition: verif_ehrhart.c:20