3 /* Copyright (c) 2007 Arnaud Legrand, Pedro Velho. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
11 #include "maxmin_private.h"
20 * SDP specific variables.
23 #include <declarations.h>
26 static void create_cross_link(struct constraintmatrix *myconstraints,
29 static void addentry(struct constraintmatrix *constraints,
30 struct blockmatrix *, int matno, int blkno,
31 int indexi, int indexj, double ent, int blocksize);
34 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_sdp, surf,
35 "Logging specific to SURF (sdp)");
36 XBT_LOG_NEW_SUBCATEGORY(surf_sdp_out, surf,
37 "Logging specific to SURF (sdp)");
39 ########################################################################
40 ######################## Simple Proportionnal fairness #################
41 ########################################################################
42 ####### Original problem ##########
55 # We assume in the following that n=2^K
57 ####### Standard SDP form #########
59 # A SDP can be written in the following standard form:
61 # (P) min c1*x1+c2*x2+...+cm*xm
62 # st F1*x1+F2*x2+...+Fm*xm-F0=X
65 # Where F1, F2, ..., Fm are symetric matrixes of size n by n. The
66 # constraint X>0 means that X must be positive semidefinite positive.
68 ########## Equivalent SDP #########
72 # y(k,i) for k in [0,K] and i in [1,2^k]
77 # y(2,1) y(2,2) y(2,3) y(2,4)
78 # y(3,1) y(3,2) y(3,3) y(3,4) y(3,5) y(3,6) y(3,7) y(3,8)
79 # -------------------------------------------------------
80 # x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8
83 # Structure Constraints:
85 # forall k in [0,K-1], and i in [1,2^k]
86 # [ y(k, 2i-1) y(k-1, i) ]
87 # [ y(k-1, i ) y(k, 2i) ]
91 # Capacity Constraints:
93 # -(A.y(K,*))_l >= - b_l
95 # Positivity Constraints:
96 # forall k in [0,K], and i in [1,2^k]
99 # Latency Constraints:
100 # forall i in [1,2^k] and v in [0,m-1]
109 //typedef struct lmm_system {
111 // s_xbt_swag_t variable_set; /* a list of lmm_variable_t */
112 // s_xbt_swag_t constraint_set; /* a list of lmm_constraint_t */
113 // s_xbt_swag_t active_constraint_set; /* a list of lmm_constraint_t */
114 // s_xbt_swag_t saturated_variable_set; /* a list of lmm_variable_t */
115 // s_xbt_swag_t saturated_constraint_set; /* a list of lmm_constraint_t_t */
116 // xbt_mallocator_t variable_mallocator;
119 #define get_y(a,b) (pow(2,a)+b-1)
121 void sdp_solve (lmm_system_t sys)
125 * SDP mapping variables.
132 int nb_cnsts_capacity = 0;
133 int nb_cnsts_struct = 0;
134 int nb_cnsts_positivy = 0;
135 int nb_cnsts_latency = 0;
138 int total_block_size = 0;
140 // FILE *sdpout = fopen("SDPA-printf.tmp","w");
142 double *tempdiag = NULL;
149 * CSDP library specific variables.
151 struct blockmatrix C;
152 struct blockmatrix X, Z;
156 struct constraintmatrix *constraints;
159 * Classic maxmin variables.
161 lmm_constraint_t cnst = NULL;
162 lmm_element_t elem = NULL;
163 xbt_swag_t cnst_list = NULL;
164 xbt_swag_t var_list = NULL;
165 xbt_swag_t elem_list = NULL;
166 lmm_variable_t var = NULL;
169 struct sparseblock *p;
176 if (!(sys->modified))
180 * Initialize the var list variable with only the active variables.
181 * Associate an index in the swag variables.
183 var_list = &(sys->variable_set);
185 xbt_swag_foreach(var, var_list) {
186 if (var->weight != 0.0)
193 DEBUG1("Variable set : %d", xbt_swag_size(var_list));
194 DEBUG1("Flows : %d", flows);
201 xbt_swag_foreach(var, var_list) {
208 cnst_list = &(sys->active_constraint_set);
209 DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
210 DEBUG1("Links : %d", links);
213 * Those fields are the top level description of the platform furnished in the xml file.
215 links = xbt_swag_size(&(sys->active_constraint_set));
218 * This number is found based on the tree structure explained on top.
220 tmp_k = (double) log((double) flows) / log(2.0);
221 K = (int) ceil(tmp_k);
222 //xbt_assert1(K!=0, "Invalide value of K (=%d) aborting.", K);
226 * The number of variables in the SDP program.
228 nb_var = get_y(K, pow(2, K));
229 DEBUG1("Number of variables in the SDP program : %d", nb_var);
233 * Find the size of each group of constraints.
235 nb_cnsts_capacity = links + ((int) pow(2, K)) - flows;
236 nb_cnsts_struct = (int) pow(2, K) - 1;
237 nb_cnsts_positivy = (int) pow(2, K);
238 nb_cnsts_latency = nb_var;
242 * The total number of constraints.
245 nb_cnsts_capacity + nb_cnsts_struct + nb_cnsts_positivy +
247 CDEBUG1(surf_sdp_out, "Number of constraints = %d", nb_cnsts);
248 DEBUG1("Number of constraints in the SDP program : %d", nb_cnsts);
251 * Keep track of which blocks have off diagonal entries.
253 isdiag = (int *) calloc((nb_cnsts + 1), sizeof(int));
254 for (i = 1; i <= nb_cnsts; i++)
257 C.nblocks = nb_cnsts;
259 (struct blockrec *) calloc((C.nblocks + 1), sizeof(struct blockrec));
261 (struct constraintmatrix *) calloc((nb_var + 1),
262 sizeof(struct constraintmatrix));
264 for (i = 1; i <= nb_var; i++) {
265 constraints[i].blocks = NULL;
268 a = (double *) calloc(nb_var + 1, sizeof(double));
277 * For each constraint block do.
279 for (i = 1; i <= nb_cnsts; i++) {
282 * Structured blocks are size 2 and all others are size 1.
284 if (i <= nb_cnsts_struct) {
285 total_block_size += block_size = 2;
288 total_block_size += block_size = 1;
289 CDEBUG0(surf_sdp_out, "1 ");
293 * All blocks are matrices.
295 C.blocks[block_num].blockcategory = MATRIX;
296 C.blocks[block_num].blocksize = block_size;
297 C.blocks[block_num].data.mat =
298 (double *) calloc(block_size * block_size, sizeof(double));
303 CDEBUG0(surf_sdp_out, " ");
307 * Creates de objective function array.
309 a = (double *) calloc((nb_var + 1), sizeof(double));
311 for (i = 1; i <= nb_var; i++) {
312 if (get_y(0, 1) == i) {
313 //CDEBUG0(surf_sdp_out,"-1 ");
316 //CDEBUG0(surf_sdp_out,"0 ");
323 * Structure contraint blocks.
327 for (k = 1; k <= K; k++) {
328 for (i = 1; i <= pow(2, k - 1); i++) {
329 matno = get_y(k, 2 * i - 1);
330 CDEBUG2(surf_sdp_out, "%d %d 1 1 1", matno, block_num);
331 addentry(constraints, &C, matno, block_num, 1, 1, 1.0,
332 C.blocks[block_num].blocksize);
334 matno = get_y(k, 2 * i);
335 CDEBUG2(surf_sdp_out, "%d %d 2 2 1", matno, block_num);
336 addentry(constraints, &C, matno, block_num, 2, 2, 1.0,
337 C.blocks[block_num].blocksize);
339 matno = get_y(k - 1, i);
340 CDEBUG2(surf_sdp_out, "%d %d 1 2 1", matno, block_num);
341 addentry(constraints, &C, matno, block_num, 1, 2, 1.0,
342 C.blocks[block_num].blocksize);
344 matno = get_y(k - 1, i);
345 CDEBUG2(surf_sdp_out, "%d %d 2 1 1", matno, block_num);
346 addentry(constraints, &C, matno, block_num, 2, 1, 1.0,
347 C.blocks[block_num].blocksize);
349 isdiag[block_num] = 0;
356 * Capacity constraint block.
358 xbt_swag_foreach(cnst, cnst_list) {
360 CDEBUG2(surf_sdp_out, "0 %d 1 1 %f", block_num, -(cnst->bound));
361 addentry(constraints, &C, 0, block_num, 1, 1, -(cnst->bound),
362 C.blocks[block_num].blocksize);
364 elem_list = &(cnst->element_set);
365 xbt_swag_foreach(elem, elem_list) {
366 if (elem->variable->weight <= 0)
368 matno = get_y(K, elem->variable->index);
369 CDEBUG3(surf_sdp_out, "%d %d 1 1 %f", matno, block_num,
371 addentry(constraints, &C, matno, block_num, 1, 1, -(elem->value),
372 C.blocks[block_num].blocksize);
380 * Positivy constraint blocks.
382 for (i = 1; i <= pow(2, K); i++) {
384 CDEBUG2(surf_sdp_out, "%d %d 1 1 1", matno, block_num);
385 addentry(constraints, &C, matno, block_num, 1, 1, 1.0,
386 C.blocks[block_num].blocksize);
390 * Latency constraint blocks.
392 xbt_swag_foreach(var, var_list) {
394 if (var->weight && var->bound > 0) {
395 matno = get_y(K, var->index);
396 CDEBUG3(surf_sdp_out, "%d %d 1 1 %f", matno, block_num, var->bound);
397 addentry(constraints, &C, matno, block_num, 1, 1, var->bound,
398 C.blocks[block_num].blocksize);
403 * At this point, we'll stop to recognize whether any of the blocks
404 * are "hidden LP blocks" and correct the block type if needed.
406 for (i = 1; i <= nb_cnsts; i++) {
407 if ((C.blocks[i].blockcategory != DIAG) &&
408 (isdiag[i] == 1) && (C.blocks[i].blocksize > 1)) {
410 * We have a hidden diagonal block!
413 blocksz = C.blocks[i].blocksize;
414 tempdiag = (double *) calloc((blocksz + 1), sizeof(double));
415 for (j = 1; j <= blocksz; j++)
416 tempdiag[j] = C.blocks[i].data.mat[ijtok(j, j, blocksz)];
417 free(C.blocks[i].data.mat);
418 C.blocks[i].data.vec = tempdiag;
419 C.blocks[i].blockcategory = DIAG;
425 * Next, setup issparse and NULL out all nextbyblock pointers.
428 for (i = 1; i <= k; i++) {
429 p = constraints[i].blocks;
432 * First, set issparse.
434 if (((p->numentries) > 0.25 * (p->blocksize))
435 && ((p->numentries) > 15)) {
441 if (C.blocks[p->blocknum].blockcategory == DIAG)
445 * Setup the cross links.
448 p->nextbyblock = NULL;
455 * Create cross link reference.
457 create_cross_link(constraints, nb_var);
461 * Debuging print problem in SDPA format.
463 if (XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
464 DEBUG0("Printing SDPA...");
465 tmp = strdup("SURF-PROPORTIONNAL.sdpa");
466 write_prob(tmp, total_block_size, nb_var, C, a, constraints);
470 * Initialize parameters.
472 DEBUG0("Initializing solution...");
473 initsoln(total_block_size, nb_var, C, a, constraints, &X, &y, &Z);
480 DEBUG0("Calling the solver...");
482 stdout = fopen("/dev/null", "w");
484 easy_sdp(total_block_size, nb_var, C, a, constraints, 0.0, &X, &y,
492 DEBUG0("SUCCESS The problem is primal infeasible");
496 DEBUG0("SUCCESS The problem is dual infeasible");
501 ("Partial SUCCESS A solution has been found, but full accuracy was not achieved. One or more of primal infeasibility, dual infeasibility, or relative duality gap are larger than their tolerances, but by a factor of less than 1000.");
505 DEBUG0("Failure. Maximum number of iterations reached.");
509 DEBUG0("Failure. Stuck at edge of primal feasibility.");
514 if (XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
515 tmp = strdup("SURF-PROPORTIONNAL.sol");
516 write_sol(tmp, total_block_size, nb_var, X, y, Z);
520 * Write out the solution if necessary.
522 xbt_swag_foreach(cnst, cnst_list) {
524 elem_list = &(cnst->element_set);
525 xbt_swag_foreach(elem, elem_list) {
526 if (elem->variable->weight <= 0)
529 i = (int) get_y(K, elem->variable->index);
530 elem->variable->value = y[i];
539 free_prob(total_block_size, nb_var, C, a, constraints, X, y, Z);
547 if (XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
555 * Create the cross_link reference in order to have a byblock list.
557 void create_cross_link(struct constraintmatrix *myconstraints, int k)
562 struct sparseblock *p;
563 struct sparseblock *q;
565 struct sparseblock *prev;
571 for (i = 1; i <= k; i++) {
572 p = myconstraints[i].blocks;
574 if (p->nextbyblock == NULL) {
578 * link in the remaining blocks.
580 for (j = i + 1; j <= k; j++) {
581 q = myconstraints[j].blocks;
584 if (q->blocknum == p->blocknum) {
585 if (p->nextbyblock == NULL) {
587 q->nextbyblock = NULL;
590 prev->nextbyblock = q;
591 q->nextbyblock = NULL;
608 void addentry(struct constraintmatrix *constraints,
609 struct blockmatrix *C,
611 int blkno, int indexi, int indexj, double ent, int blocksize)
613 struct sparseblock *p;
614 struct sparseblock *p_sav;
616 p = constraints[matno].blocks;
621 * We haven't yet allocated any blocks.
623 p = (struct sparseblock *) calloc(1, sizeof(struct sparseblock));
625 //two entries because this library ignores indices starting in zerox
626 p->constraintnum = matno;
631 p->entries = calloc(p->numentries + 1, sizeof(double));
632 p->iindices = calloc(p->numentries + 1, sizeof(int));
633 p->jindices = calloc(p->numentries + 1, sizeof(int));
635 p->entries[p->numentries] = ent;
636 p->iindices[p->numentries] = indexi;
637 p->jindices[p->numentries] = indexj;
639 p->blocksize = blocksize;
641 constraints[matno].blocks = p;
644 * We have some existing blocks. See whether this block is already
648 if (p->blocknum == blkno) {
650 * Found the right block.
652 p->constraintnum = matno;
654 p->numentries = p->numentries + 1;
657 realloc(p->entries, (p->numentries + 1) * sizeof(double));
659 realloc(p->iindices, (p->numentries + 1) * sizeof(int));
661 realloc(p->jindices, (p->numentries + 1) * sizeof(int));
663 p->entries[p->numentries] = ent;
664 p->iindices[p->numentries] = indexi;
665 p->jindices[p->numentries] = indexj;
674 * If we get here, we have a non-empty structure but not the right block
675 * inside hence create a new structure.
678 p = (struct sparseblock *) calloc(1, sizeof(struct sparseblock));
680 //two entries because this library ignores indices starting in zerox
681 p->constraintnum = matno;
686 p->entries = calloc(p->numentries + 1, sizeof(double));
687 p->iindices = calloc(p->numentries + 1, sizeof(int));
688 p->jindices = calloc(p->numentries + 1, sizeof(int));
690 p->entries[p->numentries] = ent;
691 p->iindices[p->numentries] = indexi;
692 p->jindices[p->numentries] = indexj;
694 p->blocksize = blocksize;
700 int blksz = C->blocks[blkno].blocksize;
701 if (C->blocks[blkno].blockcategory == DIAG) {
702 C->blocks[blkno].data.vec[indexi] = ent;
704 C->blocks[blkno].data.mat[ijtok(indexi, indexj, blksz)] = ent;
705 C->blocks[blkno].data.mat[ijtok(indexj, indexi, blksz)] = ent;