Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Shut libcsdp mouth!
[simgrid.git] / src / surf / sdp.c
index 8029077..35a49bf 100644 (file)
@@ -30,6 +30,8 @@ static void addentry(struct constraintmatrix *constraints,
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_sdp, surf,
                                "Logging specific to SURF (sdp)");
+XBT_LOG_NEW_SUBCATEGORY(surf_sdp_out, surf,
+                               "Logging specific to SURF (sdp)");
 /*
 ########################################################################
 ######################## Simple Proportionnal fairness #################
@@ -121,7 +123,7 @@ void sdp_solve(lmm_system_t sys)
   int block_size; 
   int total_block_size=0;
   int *isdiag=NULL;
-  FILE *sdpout = fopen("SDPA-printf.tmp","w");
+  //  FILE *sdpout = fopen("SDPA-printf.tmp","w");
   int blocksz = 0;
   double *tempdiag = NULL;
   int matno=0;
@@ -155,22 +157,24 @@ void sdp_solve(lmm_system_t sys)
    * Initialize the var list variable with only the active variables. 
    * Associate an index in the swag variables.
    */
-  i = 1;
   var_list = &(sys->variable_set);
+  i=0;
+  xbt_swag_foreach(var, var_list) {
+    if(var->weight) i++;
+  }
+  flows=i;
   DEBUG1("Variable set : %d", xbt_swag_size(var_list));
-
   xbt_swag_foreach(var, var_list) {
     var->value = 0.0;
-    if(var->weight) var->index = i++;
+    if(var->weight) var->index = i--;
   }
 
-  cnst_list=&(sys->saturated_constraint_set);  
+  cnst_list=&(sys->active_constraint_set); 
   DEBUG1("Active constraints : %d", xbt_swag_size(cnst_list));
 
   /*
    * Those fields are the top level description of the platform furnished in the xml file.
    */
-  flows = i-1;
   links = xbt_swag_size(&(sys->active_constraint_set));
 
   /* 
@@ -186,7 +190,8 @@ void sdp_solve(lmm_system_t sys)
    * The number of variables in the SDP program. 
    */
   nb_var = get_y(K, pow(2,K));
-  fprintf(sdpout,"%d\n", nb_var);
+  DEBUG1("Number of variables in the SDP program : %d", nb_var);
+  CDEBUG1(surf_sdp_out,"%d", nb_var);
  
   /*
    * Find the size of each group of constraints.
@@ -199,7 +204,7 @@ void sdp_solve(lmm_system_t sys)
    * The total number of constraints.
    */
   nb_cnsts = nb_cnsts_capacity + nb_cnsts_struct + nb_cnsts_positivy;
-  fprintf(sdpout,"%d\n", nb_cnsts);
+  //fprintf(sdpout,"%d\n", nb_cnsts);
 
   /*
    * Keep track of which blocks have off diagonal entries. 
@@ -234,10 +239,10 @@ void sdp_solve(lmm_system_t sys)
      */
     if(i <= nb_cnsts_struct){
       total_block_size += block_size = 2;
-      fprintf(sdpout,"2 ");
+      //fprintf(sdpout,"2 ");
     }else{
       total_block_size += block_size = 1;
-      fprintf(sdpout,"1 ");
+      //fprintf(sdpout,"1 ");
     }
     
     /*
@@ -250,7 +255,7 @@ void sdp_solve(lmm_system_t sys)
     block_num++;
   }
 
-  fprintf(sdpout,"\n");
+  //fprintf(sdpout,"\n");
 
 
   /*
@@ -260,14 +265,14 @@ void sdp_solve(lmm_system_t sys)
   
   for(i = 1; i <= nb_var; i++){
     if(get_y(0,1)==i){
-      fprintf(sdpout,"-1 ");
+      //fprintf(sdpout,"-1 ");
       a[i]=-1;
     }else{
-      fprintf(sdpout,"0 ");
+      //fprintf(sdpout,"0 ");
       a[i]=0;
     }
   }
-  fprintf(sdpout,"\n");
+  //fprintf(sdpout,"\n");
 
 
   /*
@@ -278,19 +283,19 @@ void sdp_solve(lmm_system_t sys)
   for(k = 1; k <= K; k++){
     for(i = 1; i <= pow(2,k-1); i++){
       matno=get_y(k,2*i-1);
-      fprintf(sdpout,"%d %d 1 1 1\n", matno  , block_num);
+      //fprintf(sdpout,"%d %d 1 1 1\n", matno  , block_num);
       addentry(constraints, &C, matno, block_num, 1, 1, 1.0, C.blocks[block_num].blocksize);
 
       matno=get_y(k,2*i);
-      fprintf(sdpout,"%d %d 2 2 1\n", matno  , block_num);
+      //fprintf(sdpout,"%d %d 2 2 1\n", matno  , block_num);
       addentry(constraints, &C, matno, block_num, 2, 2, 1.0, C.blocks[block_num].blocksize);
 
       matno=get_y(k-1,i);
-      fprintf(sdpout,"%d %d 1 2 1\n", matno  , block_num);
+      //fprintf(sdpout,"%d %d 1 2 1\n", matno  , block_num);
       addentry(constraints, &C, matno, block_num, 1, 2, 1.0, C.blocks[block_num].blocksize);      
       
       matno=get_y(k-1,i);
-      fprintf(sdpout,"%d %d 2 1 1\n", matno  , block_num);
+      //fprintf(sdpout,"%d %d 2 1 1\n", matno  , block_num);
       addentry(constraints, &C, matno, block_num, 2, 1, 1.0, C.blocks[block_num].blocksize);
       
       isdiag[block_num] = 0;
@@ -304,15 +309,16 @@ void sdp_solve(lmm_system_t sys)
    */
   xbt_swag_foreach(cnst, cnst_list) {
 
-    fprintf(sdpout,"0 %d 1 1 %d\n", block_num,  (int) - (cnst->bound));    
+    //fprintf(sdpout,"0 %d 1 1 %f\n", block_num,  - (cnst->bound));    
     addentry(constraints, &C, 0, block_num, 1, 1, - (cnst->bound) , C.blocks[block_num].blocksize);    
 
 
     elem_list = &(cnst->element_set);
     xbt_swag_foreach(elem, elem_list) {
       if(elem->variable->weight <=0) break;
-      fprintf(sdpout,"%d %d 1 1 %d\n", elem->variable->index, block_num, (int) - (elem->variable->value)); 
-      addentry(constraints, &C, elem->variable->index, block_num, 1, 1, - (elem->value), C.blocks[block_num].blocksize);
+      matno=get_y(K,elem->variable->index);
+      //fprintf(sdpout,"%d %d 1 1 %f\n", elem->variable->index, block_num, - (elem->value)); 
+      addentry(constraints, &C, matno, block_num, 1, 1, - (elem->value), C.blocks[block_num].blocksize);
  
     }
     block_num++;
@@ -324,7 +330,7 @@ void sdp_solve(lmm_system_t sys)
    */
   for(i = 1; i <= pow(2,K); i++){
     matno=get_y(K, i);
-    fprintf(sdpout,"%d %d 1 1 1\n", matno, block_num);
+    //fprintf(sdpout,"%d %d 1 1 1\n", matno, block_num);
     addentry(constraints, &C, matno, block_num, 1, 1, 1.0, C.blocks[block_num].blocksize);
     block_num++;
   }
@@ -341,7 +347,7 @@ void sdp_solve(lmm_system_t sys)
        * We have a hidden diagonal block!
        */
       
-      printf("Block %d is actually diagonal.\n",i);
+      //printf("Block %d is actually diagonal.\n",i);
       
       blocksz=C.blocks[i].blocksize;
       tempdiag=(double *)calloc((blocksz+1), sizeof(double));
@@ -392,18 +398,17 @@ void sdp_solve(lmm_system_t sys)
   /*
    * Debuging print problem in SDPA format.
    */
-  printf("Printing SDPA...\n");
   if(XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
-    char *tmp=strdup("SDPA.tmp");
-    write_prob(tmp, nb_cnsts, nb_var, C, a, constraints);  
-    //int write_prob(char *fname, int n, int k, struct blockmatrix C, double *a, struct constraintmatrix *constraints);
+    DEBUG0("Printing SDPA...\n");
+    char *tmp=strdup("SURF-PROPORTIONNAL.sdpa");
+    write_prob(tmp,total_block_size,nb_var,C,a,constraints);
     free(tmp);
   }
 
   /*
    * Initialize parameters.
    */
-  printf("Initializing solution...\n");
+  DEBUG0("Initializing solution...\n");
   initsoln(total_block_size, nb_var, C, a, constraints, &X, &y, &Z);  
   
 
@@ -411,28 +416,37 @@ void sdp_solve(lmm_system_t sys)
   /*
    * Call the solver.
    */
-  printf("Calling the solver...\n");
+  DEBUG0("Calling the solver...\n");
+  FILE *stdout_sav=stdout;
+  stdout=fopen("/dev/null","w");
   int ret = easy_sdp(total_block_size, nb_var, C, a, constraints, 0.0, &X, &y, &Z, &pobj, &dobj);
+  fclose(stdout);
+  stdout=stdout_sav;
 
   switch(ret){
   case 0:
-  case 1: printf("SUCCESS The problem is primal infeasible\n");
+  case 1: DEBUG0("SUCCESS The problem is primal infeasible\n");
           break;
 
-  case 2: printf("SUCCESS The problem is dual infeasible\n");
+  case 2: DEBUG0("SUCCESS The problem is dual infeasible\n");
           break;
  
-  case 3: printf("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.\n");
+  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.\n");
           break;
 
-  case 4: printf("Failure. Maximum number of iterations reached.");
+  case 4: DEBUG0("Failure. Maximum number of iterations reached.");
           break;
 
-  case 5: printf("Failure. Stuck at edge of primal feasibility.");
+  case 5: DEBUG0("Failure. Stuck at edge of primal feasibility.");
           break;
 
   }
 
+  if(XBT_LOG_ISENABLED(surf_sdp, xbt_log_priority_debug)) {
+    char *tmp=strdup("SURF-PROPORTIONNAL.sol");
+    write_sol(tmp,total_block_size, nb_var, X, y, Z);
+    free(tmp);
+  }
 
   /*
    * Write out the solution if necessary.
@@ -455,7 +469,7 @@ void sdp_solve(lmm_system_t sys)
    */
   free_prob(total_block_size, nb_var, C, a, constraints, X, y, Z);
 
-  fclose(sdpout);
+  //  fclose(sdpout);
   free(isdiag);
   sys->modified = 0;