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 "xbt/mallocator.h"
12 #include "maxmin_private.h"
20 * SDP specific variables.
22 #include <declarations.h>
25 static void create_cross_link(struct constraintmatrix *myconstraints, int k);
27 static void addentry(struct constraintmatrix *constraints,
28 struct blockmatrix *, int matno, int blkno, int indexi, int indexj, double ent, int blocksize);
31 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_sdp, surf,
32 "Logging specific to SURF (sdp)");
33 XBT_LOG_NEW_SUBCATEGORY(surf_sdp_out, surf,
34 "Logging specific to SURF (sdp)");
36 ########################################################################
37 ######################## Simple Proportionnal fairness #################
38 ########################################################################
39 ####### Original problem ##########
52 # We assume in the following that n=2^K
54 ####### Standard SDP form #########
56 # A SDP can be written in the following standard form:
58 # (P) min c1*x1+c2*x2+...+cm*xm
59 # st F1*x1+F2*x2+...+Fm*xm-F0=X
62 # Where F1, F2, ..., Fm are symetric matrixes of size n by n. The
63 # constraint X>0 means that X must be positive semidefinite positive.
65 ########## Equivalent SDP #########
69 # y(k,i) for k in [0,K] and i in [1,2^k]
74 # y(2,1) y(2,2) y(2,3) y(2,4)
75 # y(3,1) y(3,2) y(3,3) y(3,4) y(3,5) y(3,6) y(3,7) y(3,8)
76 # -------------------------------------------------------
77 # x_1 x_2 x_3 x_4 x_5 x_6 x_7 x_8
80 # Structure Constraints:
82 # forall k in [0,K-1], and i in [1,2^k]
83 # [ y(k, 2i-1) y(k-1, i) ]
84 # [ y(k-1, i ) y(k, 2i) ]
88 # Capacity Constraints:
90 # -(A.y(K,*))_l >= - b_l
92 # Positivity Constraints:
93 # forall k in [0,K], and i in [1,2^k]
96 # Latency Constraints:
97 # forall i in [1,2^k] and v in [0,m-1]
106 //typedef struct lmm_system {
108 // s_xbt_swag_t variable_set; /* a list of lmm_variable_t */
109 // s_xbt_swag_t constraint_set; /* a list of lmm_constraint_t */
110 // s_xbt_swag_t active_constraint_set; /* a list of lmm_constraint_t */
111 // s_xbt_swag_t saturated_variable_set; /* a list of lmm_variable_t */
112 // s_xbt_swag_t saturated_constraint_set; /* a list of lmm_constraint_t_t */
113 // xbt_mallocator_t variable_mallocator;
116 #define get_y(a,b) (pow(2,a)+b-1)
118 void sdp_solve(lmm_system_t sys)
122 * SDP mapping variables.
129 int nb_cnsts_capacity=0;
130 int nb_cnsts_struct=0;
131 int nb_cnsts_positivy=0;
132 int nb_cnsts_latency=0;
135 int total_block_size=0;
137 // FILE *sdpout = fopen("SDPA-printf.tmp","w");
139 double *tempdiag = NULL;
146 * CSDP library specific variables.
148 struct blockmatrix C;
149 struct blockmatrix X,Z;
153 struct constraintmatrix *constraints;
156 * Classic maxmin variables.
158 lmm_constraint_t cnst = NULL;
159 lmm_element_t elem = NULL;
160 xbt_swag_t cnst_list = NULL;
161 xbt_swag_t var_list = NULL;
162 xbt_swag_t elem_list = NULL;
163 lmm_variable_t var = NULL;
166 struct sparseblock *p;
173 if ( !(sys->modified))
177 * Initialize the var list variable with only the active variables.
178 * Associate an index in the swag variables.
180 var_list = &(sys->variable_set);
182 xbt_swag_foreach(var, var_list) {
183 if(var->weight != 0.0) i++;
189 DEBUG1("Variable set : %d", xbt_swag_size(var_list));
190 DEBUG1("Flows : %d", flows);
197 xbt_swag_foreach(var, var_list) {
204 cnst_list=&(sys->active_constraint_set);
205 DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
206 DEBUG1("Links : %d", links);
209 * Those fields are the top level description of the platform furnished in the xml file.
211 links = xbt_swag_size(&(sys->active_constraint_set));
214 * This number is found based on the tree structure explained on top.
216 tmp_k = (double) log((double)flows)/log(2.0);
217 K = (int) ceil(tmp_k);
218 //xbt_assert1(K!=0, "Invalide value of K (=%d) aborting.", K);
222 * The number of variables in the SDP program.
224 nb_var = get_y(K, pow(2,K));
225 DEBUG1("Number of variables in the SDP program : %d", nb_var);
229 * Find the size of each group of constraints.
231 nb_cnsts_capacity = links + ((int)pow(2,K)) - flows;
232 nb_cnsts_struct = (int)pow(2,K) - 1;
233 nb_cnsts_positivy = (int)pow(2,K);
234 nb_cnsts_latency = nb_var;
238 * The total number of constraints.
240 nb_cnsts = nb_cnsts_capacity + nb_cnsts_struct + nb_cnsts_positivy + nb_cnsts_latency;
241 CDEBUG1(surf_sdp_out,"Number of constraints = %d", nb_cnsts);
242 DEBUG1("Number of constraints in the SDP program : %d", nb_cnsts);
245 * Keep track of which blocks have off diagonal entries.
247 isdiag=(int *)calloc((nb_cnsts+1), sizeof(int));
248 for (i=1; i<=nb_cnsts; i++)
251 C.nblocks = nb_cnsts;
252 C.blocks = (struct blockrec *) calloc((C.nblocks+1),sizeof(struct blockrec));
253 constraints = (struct constraintmatrix *)calloc((nb_var+1),sizeof(struct constraintmatrix));
255 for(i = 1; i <= nb_var; i++){
256 constraints[i].blocks=NULL;
259 a = (double *)calloc(nb_var+1, sizeof(double));
268 * For each constraint block do.
270 for(i = 1; i <= nb_cnsts; i++){
273 * Structured blocks are size 2 and all others are size 1.
275 if(i <= nb_cnsts_struct){
276 total_block_size += block_size = 2;
279 total_block_size += block_size = 1;
280 CDEBUG0(surf_sdp_out,"1 ");
284 * All blocks are matrices.
286 C.blocks[block_num].blockcategory = MATRIX;
287 C.blocks[block_num].blocksize = block_size;
288 C.blocks[block_num].data.mat = (double *)calloc(block_size * block_size, sizeof(double));
293 CDEBUG0(surf_sdp_out," ");
297 * Creates de objective function array.
299 a = (double *)calloc((nb_var+1), sizeof(double));
301 for(i = 1; i <= nb_var; i++){
303 //CDEBUG0(surf_sdp_out,"-1 ");
306 //CDEBUG0(surf_sdp_out,"0 ");
313 * Structure contraint blocks.
317 for(k = 1; k <= K; k++){
318 for(i = 1; i <= pow(2,k-1); i++){
319 matno=get_y(k,2*i-1);
320 CDEBUG2(surf_sdp_out,"%d %d 1 1 1", matno , block_num);
321 addentry(constraints, &C, matno, block_num, 1, 1, 1.0, C.blocks[block_num].blocksize);
324 CDEBUG2(surf_sdp_out,"%d %d 2 2 1", matno , block_num);
325 addentry(constraints, &C, matno, block_num, 2, 2, 1.0, C.blocks[block_num].blocksize);
328 CDEBUG2(surf_sdp_out,"%d %d 1 2 1", matno , block_num);
329 addentry(constraints, &C, matno, block_num, 1, 2, 1.0, C.blocks[block_num].blocksize);
332 CDEBUG2(surf_sdp_out,"%d %d 2 1 1", matno , block_num);
333 addentry(constraints, &C, matno, block_num, 2, 1, 1.0, C.blocks[block_num].blocksize);
335 isdiag[block_num] = 0;
342 * Capacity constraint block.
344 xbt_swag_foreach(cnst, cnst_list) {
346 CDEBUG2(surf_sdp_out,"0 %d 1 1 %f", block_num, - (cnst->bound));
347 addentry(constraints, &C, 0, block_num, 1, 1, - (cnst->bound) , C.blocks[block_num].blocksize);
349 elem_list = &(cnst->element_set);
350 xbt_swag_foreach(elem, elem_list){
351 if(elem->variable->weight <=0) break;
352 matno=get_y(K,elem->variable->index);
353 CDEBUG3(surf_sdp_out,"%d %d 1 1 %f", matno, block_num, - (elem->value));
354 addentry(constraints, &C, matno, block_num, 1, 1, - (elem->value), C.blocks[block_num].blocksize);
362 * Positivy constraint blocks.
364 for(i = 1; i <= pow(2,K); i++){
366 CDEBUG2(surf_sdp_out,"%d %d 1 1 1", matno, block_num);
367 addentry(constraints, &C, matno, block_num, 1, 1, 1.0, C.blocks[block_num].blocksize);
371 * Latency constraint blocks.
373 xbt_swag_foreach(var, var_list) {
375 if(var->weight && var->bound > 0) {
376 matno = get_y(K,var->index);
377 CDEBUG3(surf_sdp_out,"%d %d 1 1 %f", matno, block_num, var->bound);
378 addentry(constraints, &C, matno, block_num, 1, 1, var->bound, C.blocks[block_num].blocksize);
383 * At this point, we'll stop to recognize whether any of the blocks
384 * are "hidden LP blocks" and correct the block type if needed.
386 for (i=1; i<=nb_cnsts; i++){
387 if ((C.blocks[i].blockcategory != DIAG) &&
388 (isdiag[i]==1) && (C.blocks[i].blocksize > 1)){
390 * We have a hidden diagonal block!
393 blocksz=C.blocks[i].blocksize;
394 tempdiag=(double *)calloc((blocksz+1), sizeof(double));
395 for (j=1; j<=blocksz; j++)
396 tempdiag[j]=C.blocks[i].data.mat[ijtok(j,j,blocksz)];
397 free(C.blocks[i].data.mat);
398 C.blocks[i].data.vec=tempdiag;
399 C.blocks[i].blockcategory=DIAG;
405 * Next, setup issparse and NULL out all nextbyblock pointers.
408 for (i=1; i<=k; i++) {
409 p=constraints[i].blocks;
412 * First, set issparse.
414 if (((p->numentries) > 0.25*(p->blocksize)) && ((p->numentries) > 15)){
420 if (C.blocks[p->blocknum].blockcategory == DIAG)
424 * Setup the cross links.
434 * Create cross link reference.
436 create_cross_link(constraints, nb_var);
440 * Debuging print problem in SDPA format.
442 if(XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
443 DEBUG0("Printing SDPA...");
444 tmp=strdup("SURF-PROPORTIONNAL.sdpa");
445 write_prob(tmp,total_block_size,nb_var,C,a,constraints);
449 * Initialize parameters.
451 DEBUG0("Initializing solution...");
452 initsoln(total_block_size, nb_var, C, a, constraints, &X, &y, &Z);
459 DEBUG0("Calling the solver...");
461 stdout=fopen("/dev/null","w");
462 ret = easy_sdp(total_block_size, nb_var, C, a, constraints, 0.0, &X, &y, &Z, &pobj, &dobj);
468 case 1: DEBUG0("SUCCESS The problem is primal infeasible");
471 case 2: DEBUG0("SUCCESS The problem is dual infeasible");
474 case 3: DEBUG0("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.");
477 case 4: DEBUG0("Failure. Maximum number of iterations reached.");
480 case 5: DEBUG0("Failure. Stuck at edge of primal feasibility.");
485 if(XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
486 tmp=strdup("SURF-PROPORTIONNAL.sol");
487 write_sol(tmp,total_block_size, nb_var, X, y, Z);
491 * Write out the solution if necessary.
493 xbt_swag_foreach(cnst, cnst_list) {
495 elem_list = &(cnst->element_set);
496 xbt_swag_foreach(elem, elem_list) {
497 if(elem->variable->weight <=0) break;
499 i = (int)get_y(K, elem->variable->index);
500 elem->variable->value = y[i];
509 free_prob(total_block_size, nb_var, C, a, constraints, X, y, Z);
517 if(XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
525 * Create the cross_link reference in order to have a byblock list.
527 void create_cross_link(struct constraintmatrix *myconstraints, int k){
531 struct sparseblock *p;
532 struct sparseblock *q;
534 struct sparseblock *prev;
540 for (i=1; i<=k; i++){
541 p=myconstraints[i].blocks;
543 if (p->nextbyblock == NULL){
547 * link in the remaining blocks.
549 for (j=i+1; j<=k; j++){
550 q=myconstraints[j].blocks;
553 if (q->blocknum == p->blocknum){
554 if (p->nextbyblock == NULL){
578 void addentry(struct constraintmatrix *constraints,
579 struct blockmatrix *C,
587 struct sparseblock *p;
588 struct sparseblock *p_sav;
590 p=constraints[matno].blocks;
595 * We haven't yet allocated any blocks.
597 p=(struct sparseblock *)calloc(1, sizeof(struct sparseblock));
599 //two entries because this library ignores indices starting in zerox
600 p->constraintnum=matno;
605 p->entries=calloc(p->numentries+1, sizeof(double));
606 p->iindices=calloc(p->numentries+1, sizeof(int));
607 p->jindices=calloc(p->numentries+1, sizeof(int));
609 p->entries[p->numentries]=ent;
610 p->iindices[p->numentries]=indexi;
611 p->jindices[p->numentries]=indexj;
613 p->blocksize=blocksize;
615 constraints[matno].blocks=p;
618 * We have some existing blocks. See whether this block is already
622 if (p->blocknum == blkno){
624 * Found the right block.
626 p->constraintnum=matno;
628 p->numentries=p->numentries+1;
630 p->entries = realloc(p->entries, (p->numentries+1) * sizeof(double) );
631 p->iindices = realloc(p->iindices, (p->numentries+1) * sizeof(int) );
632 p->jindices = realloc(p->jindices, (p->numentries+1) * sizeof(int) );
634 p->entries[p->numentries]=ent;
635 p->iindices[p->numentries]=indexi;
636 p->jindices[p->numentries]=indexj;
645 * If we get here, we have a non-empty structure but not the right block
646 * inside hence create a new structure.
649 p=(struct sparseblock *)calloc(1, sizeof(struct sparseblock));
651 //two entries because this library ignores indices starting in zerox
652 p->constraintnum=matno;
657 p->entries=calloc(p->numentries+1, sizeof(double));
658 p->iindices=calloc(p->numentries+1, sizeof(int));
659 p->jindices=calloc(p->numentries+1, sizeof(int));
661 p->entries[p->numentries]=ent;
662 p->iindices[p->numentries]=indexi;
663 p->jindices[p->numentries]=indexj;
665 p->blocksize=blocksize;
671 int blksz=C->blocks[blkno].blocksize;
672 if (C->blocks[blkno].blockcategory == DIAG){
673 C->blocks[blkno].data.vec[indexi]=ent;
675 C->blocks[blkno].data.mat[ijtok(indexi,indexj,blksz)]=ent;
676 C->blocks[blkno].data.mat[ijtok(indexj,indexi,blksz)]=ent;