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"
19 * SDP specific variables.
22 #include <declarations.h>
25 static void create_cross_link(struct constraintmatrix *myconstraints,
28 static void addentry(struct constraintmatrix *constraints,
29 struct blockmatrix *, int matno, int blkno,
30 int indexi, int indexj, double ent, int blocksize);
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_sdp, surf,
34 "Logging specific to SURF (sdp)");
35 XBT_LOG_NEW_SUBCATEGORY(surf_sdp_out, surf,
36 "Logging specific to SURF (sdp)");
38 ########################################################################
39 ######################## Simple Proportionnal fairness #################
40 ########################################################################
41 ####### Original problem ##########
54 # We assume in the following that n=2^K
56 ####### Standard SDP form #########
58 # A SDP can be written in the following standard form:
60 # (P) min c1*x1+c2*x2+...+cm*xm
61 # st F1*x1+F2*x2+...+Fm*xm-F0=X
64 # Where F1, F2, ..., Fm are symetric matrixes of size n by n. The
65 # constraint X>0 means that X must be positive semidefinite positive.
67 ########## Equivalent SDP #########
71 # y(k,i) for k in [0,K] and i in [1,2^k]
76 # y(2,1) y(2,2) y(2,3) y(2,4)
77 # y(3,1) y(3,2) y(3,3) y(3,4) y(3,5) y(3,6) y(3,7) y(3,8)
78 # -------------------------------------------------------
79 # x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8
82 # Structure Constraints:
84 # forall k in [0,K-1], and i in [1,2^k]
85 # [ y(k, 2i-1) y(k-1, i) ]
86 # [ y(k-1, i ) y(k, 2i) ]
90 # Capacity Constraints:
92 # -(A.y(K,*))_l >= - b_l
94 # Positivity Constraints:
95 # forall k in [0,K], and i in [1,2^k]
98 # Latency Constraints:
99 # forall i in [1,2^k] and v in [0,m-1]
108 //typedef struct lmm_system {
110 // s_xbt_swag_t variable_set; /* a list of lmm_variable_t */
111 // s_xbt_swag_t constraint_set; /* a list of lmm_constraint_t */
112 // s_xbt_swag_t active_constraint_set; /* a list of lmm_constraint_t */
113 // s_xbt_swag_t saturated_variable_set; /* a list of lmm_variable_t */
114 // s_xbt_swag_t saturated_constraint_set; /* a list of lmm_constraint_t_t */
115 // xbt_mallocator_t variable_mallocator;
118 #define get_y(a,b) (pow(2,a)+b-1)
120 void sdp_solve(lmm_system_t sys)
124 * SDP mapping variables.
131 int nb_cnsts_capacity = 0;
132 int nb_cnsts_struct = 0;
133 int nb_cnsts_positivy = 0;
134 int nb_cnsts_latency = 0;
137 int total_block_size = 0;
139 // FILE *sdpout = fopen("SDPA-printf.tmp","w");
141 double *tempdiag = NULL;
148 * CSDP library specific variables.
150 struct blockmatrix C;
151 struct blockmatrix X, Z;
155 struct constraintmatrix *constraints;
158 * Classic maxmin variables.
160 lmm_constraint_t cnst = NULL;
161 lmm_element_t elem = NULL;
162 xbt_swag_t cnst_list = NULL;
163 xbt_swag_t var_list = NULL;
164 xbt_swag_t elem_list = NULL;
165 lmm_variable_t var = NULL;
168 struct sparseblock *p;
175 if (!(sys->modified))
179 * Initialize the var list variable with only the active variables.
180 * Associate an index in the swag variables.
182 var_list = &(sys->variable_set);
184 xbt_swag_foreach(var, var_list) {
185 if (var->weight != 0.0)
192 DEBUG1("Variable set : %d", xbt_swag_size(var_list));
193 DEBUG1("Flows : %d", flows);
200 xbt_swag_foreach(var, var_list) {
207 cnst_list = &(sys->active_constraint_set);
208 DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
209 DEBUG1("Links : %d", links);
212 * Those fields are the top level description of the platform furnished in the xml file.
214 links = xbt_swag_size(&(sys->active_constraint_set));
217 * This number is found based on the tree structure explained on top.
219 tmp_k = (double) log((double) flows) / log(2.0);
220 K = (int) ceil(tmp_k);
221 //xbt_assert1(K!=0, "Invalide value of K (=%d) aborting.", K);
225 * The number of variables in the SDP program.
227 nb_var = get_y(K, pow(2, K));
228 DEBUG1("Number of variables in the SDP program : %d", nb_var);
232 * Find the size of each group of constraints.
234 nb_cnsts_capacity = links + ((int) pow(2, K)) - flows;
235 nb_cnsts_struct = (int) pow(2, K) - 1;
236 nb_cnsts_positivy = (int) pow(2, K);
237 nb_cnsts_latency = nb_var;
241 * The total number of constraints.
244 nb_cnsts_capacity + nb_cnsts_struct + nb_cnsts_positivy +
246 CDEBUG1(surf_sdp_out, "Number of constraints = %d", nb_cnsts);
247 DEBUG1("Number of constraints in the SDP program : %d", nb_cnsts);
250 * Keep track of which blocks have off diagonal entries.
252 isdiag = (int *) calloc((nb_cnsts + 1), sizeof(int));
253 for (i = 1; i <= nb_cnsts; i++)
256 C.nblocks = nb_cnsts;
258 (struct blockrec *) calloc((C.nblocks + 1), sizeof(struct blockrec));
260 (struct constraintmatrix *) calloc((nb_var + 1),
261 sizeof(struct constraintmatrix));
263 for (i = 1; i <= nb_var; i++) {
264 constraints[i].blocks = NULL;
267 a = (double *) calloc(nb_var + 1, sizeof(double));
276 * For each constraint block do.
278 for (i = 1; i <= nb_cnsts; i++) {
281 * Structured blocks are size 2 and all others are size 1.
283 if (i <= nb_cnsts_struct) {
284 total_block_size += block_size = 2;
287 total_block_size += block_size = 1;
288 CDEBUG0(surf_sdp_out, "1 ");
292 * All blocks are matrices.
294 C.blocks[block_num].blockcategory = MATRIX;
295 C.blocks[block_num].blocksize = block_size;
296 C.blocks[block_num].data.mat =
297 (double *) calloc(block_size * block_size, sizeof(double));
302 CDEBUG0(surf_sdp_out, " ");
306 * Creates de objective function array.
308 a = (double *) calloc((nb_var + 1), sizeof(double));
310 for (i = 1; i <= nb_var; i++) {
311 if (get_y(0, 1) == i) {
312 //CDEBUG0(surf_sdp_out,"-1 ");
315 //CDEBUG0(surf_sdp_out,"0 ");
322 * Structure contraint blocks.
326 for (k = 1; k <= K; k++) {
327 for (i = 1; i <= pow(2, k - 1); i++) {
328 matno = get_y(k, 2 * i - 1);
329 CDEBUG2(surf_sdp_out, "%d %d 1 1 1", matno, block_num);
330 addentry(constraints, &C, matno, block_num, 1, 1, 1.0,
331 C.blocks[block_num].blocksize);
333 matno = get_y(k, 2 * i);
334 CDEBUG2(surf_sdp_out, "%d %d 2 2 1", matno, block_num);
335 addentry(constraints, &C, matno, block_num, 2, 2, 1.0,
336 C.blocks[block_num].blocksize);
338 matno = get_y(k - 1, i);
339 CDEBUG2(surf_sdp_out, "%d %d 1 2 1", matno, block_num);
340 addentry(constraints, &C, matno, block_num, 1, 2, 1.0,
341 C.blocks[block_num].blocksize);
343 matno = get_y(k - 1, i);
344 CDEBUG2(surf_sdp_out, "%d %d 2 1 1", matno, block_num);
345 addentry(constraints, &C, matno, block_num, 2, 1, 1.0,
346 C.blocks[block_num].blocksize);
348 isdiag[block_num] = 0;
355 * Capacity constraint block.
357 xbt_swag_foreach(cnst, cnst_list) {
359 CDEBUG2(surf_sdp_out, "0 %d 1 1 %f", block_num, -(cnst->bound));
360 addentry(constraints, &C, 0, block_num, 1, 1, -(cnst->bound),
361 C.blocks[block_num].blocksize);
363 elem_list = &(cnst->element_set);
364 xbt_swag_foreach(elem, elem_list) {
365 if (elem->variable->weight <= 0)
367 matno = get_y(K, elem->variable->index);
368 CDEBUG3(surf_sdp_out, "%d %d 1 1 %f", matno, block_num,
370 addentry(constraints, &C, matno, block_num, 1, 1, -(elem->value),
371 C.blocks[block_num].blocksize);
379 * Positivy constraint blocks.
381 for (i = 1; i <= pow(2, K); i++) {
383 CDEBUG2(surf_sdp_out, "%d %d 1 1 1", matno, block_num);
384 addentry(constraints, &C, matno, block_num, 1, 1, 1.0,
385 C.blocks[block_num].blocksize);
389 * Latency constraint blocks.
391 xbt_swag_foreach(var, var_list) {
393 if (var->weight && var->bound > 0) {
394 matno = get_y(K, var->index);
395 CDEBUG3(surf_sdp_out, "%d %d 1 1 %f", matno, block_num, var->bound);
396 addentry(constraints, &C, matno, block_num, 1, 1, var->bound,
397 C.blocks[block_num].blocksize);
402 * At this point, we'll stop to recognize whether any of the blocks
403 * are "hidden LP blocks" and correct the block type if needed.
405 for (i = 1; i <= nb_cnsts; i++) {
406 if ((C.blocks[i].blockcategory != DIAG) &&
407 (isdiag[i] == 1) && (C.blocks[i].blocksize > 1)) {
409 * We have a hidden diagonal block!
412 blocksz = C.blocks[i].blocksize;
413 tempdiag = (double *) calloc((blocksz + 1), sizeof(double));
414 for (j = 1; j <= blocksz; j++)
415 tempdiag[j] = C.blocks[i].data.mat[ijtok(j, j, blocksz)];
416 free(C.blocks[i].data.mat);
417 C.blocks[i].data.vec = tempdiag;
418 C.blocks[i].blockcategory = DIAG;
424 * Next, setup issparse and NULL out all nextbyblock pointers.
427 for (i = 1; i <= k; i++) {
428 p = constraints[i].blocks;
431 * First, set issparse.
433 if (((p->numentries) > 0.25 * (p->blocksize))
434 && ((p->numentries) > 15)) {
440 if (C.blocks[p->blocknum].blockcategory == DIAG)
444 * Setup the cross links.
447 p->nextbyblock = NULL;
454 * Create cross link reference.
456 create_cross_link(constraints, nb_var);
460 * Debuging print problem in SDPA format.
462 if (XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
463 DEBUG0("Printing SDPA...");
464 tmp = strdup("SURF-PROPORTIONNAL.sdpa");
465 write_prob(tmp, total_block_size, nb_var, C, a, constraints);
469 * Initialize parameters.
471 DEBUG0("Initializing solution...");
472 initsoln(total_block_size, nb_var, C, a, constraints, &X, &y, &Z);
479 DEBUG0("Calling the solver...");
481 stdout = fopen("/dev/null", "w");
483 easy_sdp(total_block_size, nb_var, C, a, constraints, 0.0, &X, &y,
491 DEBUG0("SUCCESS The problem is primal infeasible");
495 DEBUG0("SUCCESS The problem is dual infeasible");
500 ("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.");
504 DEBUG0("Failure. Maximum number of iterations reached.");
508 DEBUG0("Failure. Stuck at edge of primal feasibility.");
513 if (XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
514 tmp = strdup("SURF-PROPORTIONNAL.sol");
515 write_sol(tmp, total_block_size, nb_var, X, y, Z);
519 * Write out the solution if necessary.
521 xbt_swag_foreach(cnst, cnst_list) {
523 elem_list = &(cnst->element_set);
524 xbt_swag_foreach(elem, elem_list) {
525 if (elem->variable->weight <= 0)
528 i = (int) get_y(K, elem->variable->index);
529 elem->variable->value = y[i];
538 free_prob(total_block_size, nb_var, C, a, constraints, X, y, Z);
546 if (XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
554 * Create the cross_link reference in order to have a byblock list.
556 void create_cross_link(struct constraintmatrix *myconstraints, int k)
561 struct sparseblock *p;
562 struct sparseblock *q;
564 struct sparseblock *prev;
570 for (i = 1; i <= k; i++) {
571 p = myconstraints[i].blocks;
573 if (p->nextbyblock == NULL) {
577 * link in the remaining blocks.
579 for (j = i + 1; j <= k; j++) {
580 q = myconstraints[j].blocks;
583 if (q->blocknum == p->blocknum) {
584 if (p->nextbyblock == NULL) {
586 q->nextbyblock = NULL;
589 prev->nextbyblock = q;
590 q->nextbyblock = NULL;
607 void addentry(struct constraintmatrix *constraints,
608 struct blockmatrix *C,
610 int blkno, int indexi, int indexj, double ent, int blocksize)
612 struct sparseblock *p;
613 struct sparseblock *p_sav;
615 p = constraints[matno].blocks;
620 * We haven't yet allocated any blocks.
622 p = (struct sparseblock *) calloc(1, sizeof(struct sparseblock));
624 //two entries because this library ignores indices starting in zerox
625 p->constraintnum = matno;
630 p->entries = calloc(p->numentries + 1, sizeof(double));
631 p->iindices = calloc(p->numentries + 1, sizeof(int));
632 p->jindices = calloc(p->numentries + 1, sizeof(int));
634 p->entries[p->numentries] = ent;
635 p->iindices[p->numentries] = indexi;
636 p->jindices[p->numentries] = indexj;
638 p->blocksize = blocksize;
640 constraints[matno].blocks = p;
643 * We have some existing blocks. See whether this block is already
647 if (p->blocknum == blkno) {
649 * Found the right block.
651 p->constraintnum = matno;
653 p->numentries = p->numentries + 1;
656 realloc(p->entries, (p->numentries + 1) * sizeof(double));
658 realloc(p->iindices, (p->numentries + 1) * sizeof(int));
660 realloc(p->jindices, (p->numentries + 1) * sizeof(int));
662 p->entries[p->numentries] = ent;
663 p->iindices[p->numentries] = indexi;
664 p->jindices[p->numentries] = indexj;
673 * If we get here, we have a non-empty structure but not the right block
674 * inside hence create a new structure.
677 p = (struct sparseblock *) calloc(1, sizeof(struct sparseblock));
679 //two entries because this library ignores indices starting in zerox
680 p->constraintnum = matno;
685 p->entries = calloc(p->numentries + 1, sizeof(double));
686 p->iindices = calloc(p->numentries + 1, sizeof(int));
687 p->jindices = calloc(p->numentries + 1, sizeof(int));
689 p->entries[p->numentries] = ent;
690 p->iindices[p->numentries] = indexi;
691 p->jindices[p->numentries] = indexj;
693 p->blocksize = blocksize;
699 int blksz = C->blocks[blkno].blocksize;
700 if (C->blocks[blkno].blockcategory == DIAG) {
701 C->blocks[blkno].data.vec[indexi] = ent;
703 C->blocks[blkno].data.mat[ijtok(indexi, indexj, blksz)] = ent;
704 C->blocks[blkno].data.mat[ijtok(indexj, indexi, blksz)] = ent;