gs_smart_mapping_graph.c

Go to the documentation of this file.
00001 
00013 #include "config.h"
00014 #ifdef GS_SMART_GRIDSOLVE
00015 #include "gs_smart_mapping_graph.h"
00016 #include "gs_smart_mapping_heuristics.h"
00017 #include "gs_smart_task_graph.h"
00018 #include "gs_smart_netpm.h"
00019 #include "comm_data.h"
00020 #include "gs_smart_app_pm.h"
00021 #include "gs_smart_mapping_solution.h"
00022 #include "math.h"
00023 
00043 int
00044 gs_smart_map_common(char * input_mapper_type,  gs_smart_tg * tg, 
00045                   gs_smart_netpm * netpm, int * total_nb_mapped_tasks){
00046 
00047   int i, mapper_found=0;
00048   if( (!input_mapper_type) || (!tg) || (!netpm) ) return -1;
00049 
00050   best_mg= (gs_smart_mg * )calloc(1, sizeof(gs_smart_mg));
00051   if(!best_mg) return -1;
00052   
00053   for(i=0;i<gs_smart_get_nb_mappers(); i++){
00054     if(strcmp(gs_smart_get_mapping_name(i), input_mapper_type)==0){
00055       if(gs_smart_exec_mapper(tg, netpm, &best_mg, i)<0){
00056         ERRPRINTF("SMART : Error performing mapping heuristic %s\n", input_mapper_type);
00057         return -1;
00058       }
00059       mapper_found=1;
00060     }
00061   }
00062 
00063 /*
00064  
00065   if(gs_smart_mg_print_to_dotgraph(best_mg, "smart_graphs/best_map_graph.dot")<0){
00066       ERRPRINTF("SMART : Error printing dotgraph\n ");
00067       return -1;
00068     }
00069     system("dot -Tjpg smart_graphs/best_map_graph.dot -o smart_graphs/best_map_graph.jpg");
00070 */
00071 
00072 
00073 
00074   if(mapper_found==0){
00075     ERRPRINTF("SMART: Unknown mapper\n");
00076     return -1;
00077   }
00078   *total_nb_mapped_tasks=best_mg->nb_mg_rem_nodes;
00079   return 0;
00080 }
00081 
00082 
00083 
00084 
00085 
00117 int gs_smart_generate_mapping_graph(gs_smart_tg * tg, gs_smart_netpm * netpm, 
00118                                           int * mapping_vec, gs_smart_mg *mg, int * _valid_graph){
00119 
00120   gs_smart_tg_remote_task_node * tg_rem_task_node;
00121   gs_smart_mg_task_node * mg_task_node=NULL;
00122   gs_smart_mg_node * mg_node;
00123   gs_smart_mg_obj_node * mg_obj_node;
00124   int i, j, k;
00125   int mg_nb_obj_for_links, sent_remotely;
00126   int tg_nb_obj_for_links;
00127   int this_mg_o_f_link_cnt, next_mg_o_f_link_cnt;
00128   int valid_graph;
00129 
00130   if( (!tg) || (!netpm) || (!mapping_vec) ) return -1;
00131 
00132   *_valid_graph=1;
00133 
00134   if(gs_smart_mg_construct_frame(tg, netpm, mapping_vec, mg, &valid_graph)<0){
00135     ERRPRINTF("SMART : Error constructing mapping graph frame\n");
00136     return -1;
00137   }
00138 
00139   if(!mg) return -1;
00140 
00141   if(!valid_graph){
00142     *_valid_graph=valid_graph;
00143     return 0;
00144   }
00145   for(i=0;i<mg->nb_mg_nodes;i++){
00146     if(!mg->mg_nodes[i]) return -1;
00147     mg_node=mg->mg_nodes[i];   
00148     if(mg_node->node_type==GS_SMART_MG_TASK_NODE){
00149       if(!mg_node->mg_task_node) return -1;
00150       mg_task_node=mg_node->mg_task_node;
00151       if(!tg->task_nodes[i]) return -1;
00152       if(!tg->task_nodes[i]->tg_rem_task_node) return -1;
00153       tg_rem_task_node=tg->task_nodes[i]->tg_rem_task_node;
00154       for(j=0;j<mg_task_node->nb_back_links;j++){
00155         if(!mg_task_node->back_link_set) return -1;
00156         /*
00157          * Set the back link object nodes that have not already been set.
00158          * Back link object nodes could be other tasks forward link object nodes
00159          * when there is a dependency and are therefore set when setting forward
00160          * link object nodes.
00161          */ 
00162         if(mg_task_node->back_link_set[j]==0){
00163 
00164           /*
00165            * Counts the number of object forward links of object node
00166            * on the end of back links from task nodes.
00167            */ 
00168           if(gs_smart_mg_count_c_comm_for_obj_links(tg, netpm, mg,i, j, 
00169                                        &mg_nb_obj_for_links, &sent_remotely)<0){
00170             ERRPRINTF("SMART : Error counting task %d  object node %d for links\n", i, j);
00171             return -1;
00172           }
00173 
00174 
00175           /*
00176            * Constructs back link object nodes and points them to the previous
00177            * client.
00178            */ 
00179           if(gs_smart_mg_construct_c_comm_obj_back_links(tg, netpm,mg, i, j, mg_nb_obj_for_links)<0){
00180             ERRPRINTF("SMART : Error constructing task backward links\n");
00181             return -1;
00182           }
00183           if(!mg_task_node->mg_node_back_links) return -1;
00184           if(!mg_task_node->mg_node_back_links[j]) return -1;
00185           if(!mg_task_node->mg_node_back_links[j]->mg_obj_node_ptr) return -1;
00186           mg_obj_node=mg_task_node->mg_node_back_links[j]->mg_obj_node_ptr;          
00187           mg_obj_node->sent_remotely=sent_remotely;
00188 
00189           if(!mg_obj_node->mapped_obj_node) return -1;
00190           if(!mg_obj_node->mapped_obj_node->nb_obj_frwd_links) return -1;
00191           tg_nb_obj_for_links=mg_obj_node->mapped_obj_node->nb_obj_frwd_links;
00192           this_mg_o_f_link_cnt=1;
00193           k=1;
00194           for(k=1;((k<tg_nb_obj_for_links) && ( this_mg_o_f_link_cnt<mg_nb_obj_for_links)) ;k++){
00195             /*
00196              * Points each for obj link of the back link's obj node 
00197              * to the destination nodes.  The back link of the destination node
00198              * is connected to the obj for link and the element of back_link_set array 
00199              * corresponding to the back link is set to one.  This ensures
00200              * that that back link is not set again.
00201              */ 
00202              if(gs_smart_mg_set_c_comm_dep(tg, netpm,mg, i, j, k, this_mg_o_f_link_cnt, &next_mg_o_f_link_cnt)<0){
00203                ERRPRINTF("SMART : Error constructing task backward links\n");
00204                return -1;
00205              }
00206              this_mg_o_f_link_cnt=next_mg_o_f_link_cnt;
00207           }
00208 
00209         }
00210       }
00211 
00212       for(j=0;j<mg_task_node->nb_for_links;j++){
00213         if(!mg_task_node->mg_node_for_links) return -1;
00214         if(!mg_task_node->mg_node_for_links[j]) return -1;
00215         if(!mg_task_node->mg_node_for_links[j]->mg_obj_node_ptr) return -1;
00216 
00217         mg_obj_node=mg_task_node->mg_node_for_links[j]->mg_obj_node_ptr;
00218         /*
00219          * Counts the number of object forward links of object node
00220          * on the end of forward links from task nodes.
00221          */ 
00222         if(gs_smart_mg_count_s_comm_for_obj_links(tg, netpm, mg,i, j, 
00223                                        &mg_nb_obj_for_links, &sent_remotely)<0){
00224           ERRPRINTF("SMART : Error counting task %d  object node %d for links\n", i, j);
00225           return -1;
00226        }
00227 
00228        /*
00229         * Constructs forward link object nodes and points back to the task.
00230         */ 
00231 
00232        if(gs_smart_mg_construct_s_comm_obj_for_links(tg,netpm, mg, i, j, mg_nb_obj_for_links)<0){
00233          ERRPRINTF("SMART : Error constructing smart task forward links\n");
00234          return -1;
00235        }
00236        mg_obj_node->sent_remotely=sent_remotely;
00237 
00238 
00239        tg_nb_obj_for_links=mg_obj_node->mapped_obj_node->nb_obj_frwd_links;
00240        this_mg_o_f_link_cnt=0;
00241 
00242        for(k=0;((k<tg_nb_obj_for_links) && ( this_mg_o_f_link_cnt<mg_nb_obj_for_links)) ;k++){
00243          /*
00244           * Points each for obj link of the tasks forward link's obj node 
00245           * to the destination nodes.  The back link of the destination node
00246           * is connected to the object forward link and the element of back_link_set array 
00247           * corresponding to the back link is set to one.  This ensures
00248           * that that back link is not set again.
00249           */ 
00250          if(gs_smart_mg_set_s_comm(tg, netpm, mg,  i, j, k, this_mg_o_f_link_cnt, &next_mg_o_f_link_cnt)<0){
00251            ERRPRINTF("SMART : Error setting obj for link dependency of task %d for link %d\n", i, j);
00252            return -1;
00253          }
00254          this_mg_o_f_link_cnt=next_mg_o_f_link_cnt;
00255        }
00256       }
00257     }
00258   }
00259   for(i=0;i<netpm->nb_nodes;i++){
00260     netpm->netpm_nodes[i]->server->task_workload=0;
00261   }
00262   return 0;
00263 }
00264 
00265 
00266 /*
00267  *    This function creates a mapping task node for each task node in the 
00268  *    task graph (tg) and a client map node for every client node in the 
00269  *    task graph. It assigns the node of the network performance model(netpm_node) 
00270  *    which is outlined in the vector (mapping vec) to each task map node.
00271  *    In this function each task map node (which specifies a task node 
00272  *    to netpm node assignment) in the mapping graph is also timed.
00273  *    In addition the forward links, the object nodes and back links of the 
00274  *    mapping task nodes are created.
00275  *   
00276  *    
00277  *    @param tg - (in) Task Graph of a group of tasks 
00278  *    @param netpm -- (in) Network performance model
00279  *    @param mapping_vec -- (in) Mapping vec which outlines the task to
00280  *    server assignment
00281  *    @param mg -- (out) mapping graph
00282  *    @param valid_graph -- (out) 0 if mg is not a valid solution
00283  *    
00284  *    @returns -1 if there is an error
00285  *
00286  */
00287 
00288 
00289 int gs_smart_mg_construct_frame(gs_smart_tg * tg, gs_smart_netpm * netpm, 
00290                       int * mapping_vec,gs_smart_mg *mg, int *valid_graph){
00291 
00292   gs_smart_tg_remote_task_node * tg_rem_task_node;
00293   gs_smart_tg_client_node * tg_client_node;
00294   gs_smart_netpm_node * netpm_node;
00295   gs_smart_mg_task_node * mg_task_node=NULL;
00296   gs_smart_mg_client_node * mg_client_node;
00297   gs_smart_mg_link * mg_for_link;
00298   gs_smart_mg_obj_node * mg_obj_node;
00299   int i, j, map_vec_pos, server_pos;
00300   int valid_assignment=0;
00301   int timing_nbl_tasks=0;
00302   double exe_time;
00303   double max_time_nbl_tasks=0.0;
00304   
00305   if( (!tg) || (!netpm) || (!mapping_vec) ) return -1;
00306 
00307   *valid_graph=1;
00308   mg->total_time=0.0;
00309   mg->nb_mg_nodes=tg->nb_nodes;
00310   mg->nb_mg_rem_nodes=tg->nb_rem_nodes;
00311   mg->mg_nodes=(gs_smart_mg_node **)
00312                 calloc(mg->nb_mg_nodes, sizeof(gs_smart_mg_node *));
00313   if(!mg->mg_nodes) return -1;
00314   map_vec_pos=0;
00315   for(i=0;i<mg->nb_mg_nodes;i++){
00316     mg->mg_nodes[i]=(gs_smart_mg_node *)calloc(1, sizeof(gs_smart_mg_node ));
00317     if(!mg->mg_nodes[i]) return -1;
00318     mg->mg_nodes[i]->id=i;
00319     if(tg->task_nodes[i]->node_type==GS_SMART_TG_START_NODE){
00320      /*
00321       * First node is a start node
00322       */ 
00323       mg->mg_nodes[i]->node_type=GS_SMART_MG_START_NODE;
00324     }
00325     else if(tg->task_nodes[i]->node_type==GS_SMART_TG_REM_TASK_NODE){
00326      /*
00327       *  Create a mapping task node
00328       */  
00329       server_pos=mapping_vec[map_vec_pos];
00330       mg->mg_nodes[i]->node_type=GS_SMART_MG_TASK_NODE;
00331       mg->mg_nodes[i]->mg_task_node=(gs_smart_mg_task_node *)
00332                                       calloc(1, sizeof(gs_smart_mg_task_node));
00333 
00334       if(!mg->mg_nodes[i]->mg_task_node) return -1;
00335 
00336       /*
00337        * Assign a netpm node to the mapping task node
00338        * which is outlined in mapping_vec
00339        */ 
00340       if(!mg->mg_nodes[i]->mg_task_node) return -1;
00341       mg_task_node=mg->mg_nodes[i]->mg_task_node;
00342 
00343       if(!tg->task_nodes) return -1;
00344       if(!tg->task_nodes[i]) return -1;
00345       if(!tg->task_nodes[i]->tg_rem_task_node) return -1;
00346       tg_rem_task_node=tg->task_nodes[i]->tg_rem_task_node;
00347       mg_task_node->mapped_task_node=tg_rem_task_node;
00348  
00349       if(!netpm->netpm_nodes) return -1;
00350       if(!netpm->netpm_nodes[server_pos]) return -1;
00351       netpm_node=netpm->netpm_nodes[server_pos];
00352       mg_task_node->mapped_server_node=netpm_node;
00353 
00354 
00355       valid_assignment=0;
00356    
00357       /*
00358        * Check if the assignment if task node to netpm node is valid
00359        */ 
00360       if(gs_smart_check_if_valid_assignment(mg_task_node->mapped_task_node, 
00361               mg_task_node->mapped_server_node->server, &valid_assignment)<0){
00362 
00363         ERRPRINTF("SMART: Error checking if task to server assignment valid\n");
00364         return -1;
00365       }
00366 
00367       /*
00368        * If it is not valid return with valid_graph=0.
00369        */ 
00370     
00371       if(!valid_assignment){
00372 
00373         *valid_graph=0;
00374         return 0;
00375       }
00376  
00377      /*
00378       * Time this mapping task node
00379       */
00380       if(gs_smart_time_mg_task_node(mg_task_node, &exe_time)<0){
00381         ERRPRINTF("SMART: Error estimated exection time of task node\n");
00382         return -1;
00383       } 
00384 
00385       mg_task_node->time=exe_time;
00386 
00387 
00388      /*
00389       * If this mapping task node is a non-blocking task increase the 
00390       * task workload of the netpm server node which has been assigned 
00391       * this task.
00392       *
00393       * Also check if it is non-blocking check to see if this task
00394       * has the highest execution time of the group of non-blocking tasks.
00395       * For a group of non blocking tasks only the nonblocking task with
00396       * the highest execution time will contribute to the accumulated 
00397       * mapping graph's execution time.
00398       * 
00399       */    
00400  
00401      if( mg_task_node->mapped_task_node->blocking==0){
00402         if(timing_nbl_tasks==0){
00403           max_time_nbl_tasks=mg_task_node->time;
00404         
00405           if(!netpm_node->server) return -1;
00406 
00407         }
00408         netpm_node->server->task_workload =(netpm_node->server->task_workload+
00409                                                   ((( mg_task_node->time/100 )+10)));
00410       //  LOGPRINTF("THE TASK WORKLOAD FOR SERVER %s IS %d\n", netpm_node->server->hostname, netpm_node->server->task_workload);
00411         timing_nbl_tasks=1;
00412       }
00413       if(timing_nbl_tasks==1){
00414         if(mg_task_node->time>max_time_nbl_tasks){
00415           max_time_nbl_tasks=mg_task_node->time;
00416         }
00417       }  
00418      
00419       /*
00420        * If this is the first blocking task at the end of a
00421        * group of non-blocking tasks, then reset task workload of each
00422        * server netpm node.  This blocking task will also be executed
00423        * in parallel with the group of non-blocking tasks therefore
00424        * if its execution time is higher than the execution time of the 
00425        * group of non-blocking tasks then this will be the execution time that will contribute to
00426        * accumulated mapping graph's execution time.
00427        * 
00428        */ 
00429 
00430       if((mg_task_node->mapped_task_node->blocking==1) && (timing_nbl_tasks==1)){
00431         mg->total_time=max_time_nbl_tasks+mg->total_time;
00432         for(j=0;j<netpm->nb_nodes;j++){
00433           if(!netpm->netpm_nodes) return -1;
00434           if(!netpm->netpm_nodes[j]) return -1;
00435           if(!netpm->netpm_nodes[j]->server) return -1;
00436           netpm->netpm_nodes[j]->server->task_workload=0;
00437         }
00438         max_time_nbl_tasks=0;
00439         timing_nbl_tasks=0;
00440       }
00441 
00442       /*
00443        * Time a blocking graph which is not at the end of a non-blocking group of tasks
00444        */
00445       else if((mg_task_node->mapped_task_node->blocking==1) && (timing_nbl_tasks==0)){
00446         mg->total_time=mg_task_node->time+mg->total_time;
00447         for(j=0;j<netpm->nb_nodes;j++){
00448           if(!netpm->netpm_nodes) return -1;
00449           if(!netpm->netpm_nodes[j]) return -1;
00450           if(!netpm->netpm_nodes[j]->server) return -1;
00451           netpm->netpm_nodes[j]->server->task_workload=0;
00452         }
00453         timing_nbl_tasks=0;
00454       }
00455 
00456       /*
00457        * Set up forward task links of mapping task node and
00458        * create mapping object nodes. 
00459        *
00460        */ 
00461 
00462 
00463       mg_task_node->nb_for_links=tg_rem_task_node->nb_node_frwd_links; 
00464       mg_task_node->mg_node_for_links=(gs_smart_mg_link **)
00465                         calloc(mg_task_node->nb_for_links, sizeof(gs_smart_mg_link *));
00466 
00467       if(!mg_task_node->mg_node_for_links) return -1;
00468 
00469       for(j=0; j<mg_task_node->nb_for_links; j++){
00470         mg_task_node->mg_node_for_links[j]=(gs_smart_mg_link *)
00471                                                calloc(1, sizeof(gs_smart_mg_link ));
00472         
00473         if(!mg_task_node->mg_node_for_links[j]) return -1;
00474         mg_for_link=mg_task_node->mg_node_for_links[j];
00475         mg_for_link->for_link_pos=j; 
00476 
00477         /*
00478          * Point link back to mapping task node
00479          */   
00480         mg_for_link->mg_node_ptr=mg->mg_nodes[i];
00481 
00482         mg_for_link->mg_obj_node_ptr=(gs_smart_mg_obj_node *)calloc(1, sizeof(gs_smart_mg_obj_node));
00483  
00484         if(!mg_for_link->mg_obj_node_ptr) return -1;
00485 
00486         mg_obj_node=mg_for_link->mg_obj_node_ptr;
00487 
00488         /*
00489          * Assign task object node to mapping object node
00490          */ 
00491         
00492         if(!tg_rem_task_node->tg_node_frwrd_link) return -1;
00493         if(!tg_rem_task_node->tg_node_frwrd_link[j]) return -1;
00494         if(!tg_rem_task_node->tg_node_frwrd_link[j]->obj_node_ptr) return -1;
00495         mg_obj_node->mapped_obj_node=tg_rem_task_node->tg_node_frwrd_link[j]->obj_node_ptr;
00496       }
00497 
00498 
00499       /*
00500        * Setup back task links of mapping task node 
00501        */ 
00502       mg_task_node->nb_back_links=tg_rem_task_node->nb_node_bkwd_links; 
00503       mg_task_node->mg_node_back_links=(gs_smart_mg_link **)
00504                         calloc(mg_task_node->nb_back_links, sizeof(gs_smart_mg_link *));
00505 
00506 
00507       if(!mg_task_node->mg_node_back_links) return -1;
00508       mg_task_node->back_link_set=(int *)
00509                         calloc(mg_task_node->nb_back_links, sizeof(int));
00510 
00511       if(!mg_task_node->back_link_set) return -1;
00512       map_vec_pos++;
00513     }
00514 
00515     /*
00516      * Set up client mapping node
00517      */ 
00518     else if(tg->task_nodes[i]->node_type==GS_SMART_TG_CLIENT_NODE){
00519       mg->mg_nodes[i]->node_type=GS_SMART_MG_CLIENT_NODE;
00520       mg->mg_nodes[i]->mg_client_node=(gs_smart_mg_client_node *)
00521                                       calloc(1, sizeof(gs_smart_mg_client_node));
00522 
00523 
00524       if(!mg->mg_nodes[i]->mg_client_node) return -1;
00525       mg_client_node=mg->mg_nodes[i]->mg_client_node;
00526       
00527       tg_client_node=tg->task_nodes[i]->tg_client_node;
00528       mg_client_node->mapped_client_node=tg_client_node;      
00529             
00530      }
00531     /*
00532      * Set up end mapping node
00533      */ 
00534 
00535     else if(tg->task_nodes[i]->node_type==GS_SMART_TG_END_NODE){
00536       mg->total_time=max_time_nbl_tasks+mg->total_time;
00537       mg->mg_nodes[i]->node_type=GS_SMART_MG_END_NODE;
00538     }
00539   }
00540   return 0;
00541 }
00542 
00543 
00544 
00545 
00546 int gs_smart_mg_count_c_comm_for_obj_links(gs_smart_tg * tg, gs_smart_netpm * netpm,
00547                                     gs_smart_mg *mg, int task_id, 
00548                                  int back_link_id, int * _mg_nb_obj_for_links, int * _sent_remotely){
00549   gs_smart_tg_remote_task_node * tg_rem_task_node;
00550   gs_smart_mg_node * mg_node;
00551   gs_smart_mg_task_node * mg_task_node=NULL, * mg_dest_task_node=NULL;
00552   gs_smart_mg_link  * mg_back_link;
00553   gs_smart_tg_obj_node * tg_obj_node; 
00554   gs_smart_tg_task_node * tg_dest_task_node;
00555 
00556   int j,k;
00557   int tg_nb_obj_for_links, mg_nb_obj_for_links;
00558   int dest_task_id;
00559   int sent_remotely=0;
00560 
00561   j=back_link_id;
00562  
00563   mg_node=mg->mg_nodes[task_id];
00564    
00565   mg_task_node=mg_node->mg_task_node;
00566   tg_rem_task_node=tg->task_nodes[task_id]->tg_rem_task_node;
00567 
00568   mg_back_link=mg_task_node->mg_node_back_links[j];
00569   /*  
00570    * If tg obj node point back to the previous client,
00571    * then the mg_obj_node should only have one forward link
00572    * which points to the next task node.
00573    */ 
00574   tg_obj_node=tg_rem_task_node->tg_node_bkwrd_link[j]->obj_node_ptr;
00575   gs_smart_tg_task_node * tg_back_link_node=tg_obj_node->tg_obj_bkwrd_link->task_node_ptr;
00576   if(tg_back_link_node->id!=task_id-1){
00577     *_mg_nb_obj_for_links=1;
00578     *_sent_remotely=0;
00579     return 0;
00580   }
00581   
00582 
00583   tg_nb_obj_for_links=tg_rem_task_node->tg_node_bkwrd_link[j]->obj_node_ptr->nb_obj_frwd_links;
00584   mg_nb_obj_for_links=tg_nb_obj_for_links;
00585   k=1;
00586   for(k=1; k<tg_nb_obj_for_links;k++){
00587     tg_obj_node=tg_rem_task_node->tg_node_bkwrd_link[j]->obj_node_ptr;
00588     tg_dest_task_node=tg_obj_node->tg_obj_frwrd_link[k]->task_node_ptr;
00589     dest_task_id=tg_dest_task_node->id;
00590     if(mg->mg_nodes[dest_task_id]->node_type==GS_SMART_MG_TASK_NODE){
00591       mg_dest_task_node=mg->mg_nodes[dest_task_id]->mg_task_node;
00592       if(mg_dest_task_node->mapped_server_node->server->smart==0){
00593        *_mg_nb_obj_for_links=1;
00594        *_sent_remotely=0;
00595        return 0;
00596       }
00597     }
00598   }
00599   if(tg_nb_obj_for_links>1){
00600     sent_remotely=1;
00601   }
00602   *_mg_nb_obj_for_links=mg_nb_obj_for_links;
00603   *_sent_remotely=sent_remotely;
00604   return 0;
00605 }
00606 
00607 
00608 
00609 
00610 
00611 
00612 
00613 
00614 
00615 
00616 
00617 
00618 
00619 /*
00620  *    This function constructs the back links from the mapping task node.
00621  *    It also creates the object nodes that these back links point to and
00622  *    the forward links that eminate from these object nodes..
00623  *    Each back link is also timed and this is added to the total accumulated
00624  *    mapping graph time.
00625  *   
00626  *    
00627  *    @param tg - (in) Task Graph of a group of tasks 
00628  *    @param netpm -- (in) Network performance model
00629  *    server assignment
00630  *    @param mg -- (inout) mapping graph
00631  *    @param task_id -- (in) specifies which mapping node to construct 
00632  *    back links for.
00633  *    @param back_link_pos - (in) specifies which back link to construct
00634  *    @param mg_nb_obj_for_links - (in) specifies nb object for links 
00635  *    which eminate from the back links object node.
00636  *    
00637  *    @returns -1 if there is an error
00638  *
00639  */
00640 
00641 
00642 
00643 int gs_smart_mg_construct_c_comm_obj_back_links(gs_smart_tg * tg, gs_smart_netpm * netpm,
00644                                           gs_smart_mg *mg, int task_id, int back_link_pos, 
00645                                                                  int mg_nb_obj_for_links){
00646 
00647   gs_smart_tg_remote_task_node * tg_rem_task_node;
00648   gs_smart_tg_obj_node * tg_obj_node;
00649   gs_smart_mg_node * mg_node;
00650   gs_smart_mg_task_node * mg_task_node=NULL;
00651   gs_smart_mg_link  * mg_back_link;
00652   gs_smart_netpm_node * netpm_node;
00653   gs_smart_mg_obj_node * mg_obj_node;  
00654   double bw;
00655   int byte_size;
00656   if(!mg->mg_nodes[task_id]) return -1;
00657   mg_node=mg->mg_nodes[task_id];
00658   mg_task_node=mg_node->mg_task_node;
00659 
00660   if(!mg_task_node->mapped_server_node) return -1;
00661   netpm_node=mg_task_node->mapped_server_node;
00662   
00663   if(!tg->task_nodes) return -1;
00664   if(!tg->task_nodes[task_id]) return -1;
00665   if(!tg->task_nodes[task_id]->tg_rem_task_node) return -1;
00666   tg_rem_task_node=tg->task_nodes[task_id]->tg_rem_task_node;
00667    
00668 
00669   if(!mg_task_node->mg_node_back_links) return -1;
00670   mg_task_node->mg_node_back_links[back_link_pos]=(gs_smart_mg_link *)
00671                                              calloc(1, sizeof(gs_smart_mg_link ));
00672         
00673 
00674   
00675   /*
00676    * Creating the back link object node and pointing the back link to it
00677    */
00678   if(!mg_task_node->mg_node_back_links[back_link_pos]) return -1;
00679   mg_back_link=mg_task_node->mg_node_back_links[back_link_pos];
00680   mg_back_link->back_link_pos=back_link_pos; 
00681   if(!mg->mg_nodes[task_id]) return -1;
00682 
00683   mg_back_link->mg_node_ptr=mg->mg_nodes[task_id];
00684   mg_back_link->mg_obj_node_ptr=(gs_smart_mg_obj_node *)
00685                                        calloc(1, sizeof(gs_smart_mg_obj_node));
00686   if(!mg_back_link->mg_obj_node_ptr) return -1;
00687 
00688   /*
00689    * Setting the obj node which that back link points to.
00690    * Assigning the object node of the task graph which 
00691    * corresponds to this mapping object node 
00692    */ 
00693   mg_obj_node = mg_back_link->mg_obj_node_ptr;
00694 
00695   if(!tg_rem_task_node->tg_node_bkwrd_link) return -1;
00696   if(!tg_rem_task_node->tg_node_bkwrd_link[back_link_pos]) return -1;
00697   if(!tg_rem_task_node->tg_node_bkwrd_link[back_link_pos]->obj_node_ptr) return -1;
00698   tg_obj_node=tg_rem_task_node->tg_node_bkwrd_link[back_link_pos]->obj_node_ptr;
00699 
00700 
00701   mg_obj_node->mapped_obj_node=tg_obj_node;
00702 
00703   
00704  /*
00705   * Assigning the network link of the network performance model
00706   * that corresponds to this backtg_rem_task_node->tg_node_bkwrd_link[j]->obj_node_ptr link.
00707   */
00708   if(!netpm_node->netpm_links) return -1;
00709   if(!netpm_node->netpm_links[0]) return -1;
00710   mg_back_link->mapped_netpm_link=netpm_node->netpm_links[0];
00711 
00712 
00713   /*
00714    *
00715    * Find the bandwidth of the netpm link which corresponds
00716    * to this back links
00717    */ 
00718 
00719   if(netpm_node->netpm_links[0]->bw==0){
00720     netpm_node->netpm_links[0]->bw=netpm->avg_c_bw;
00721   }
00722   bw=netpm_node->netpm_links[0]->bw;
00723   byte_size=mg_obj_node->mapped_obj_node->byte_size;
00724 
00725 
00726  /*
00727   * Calculate time for this backlink and add it 
00728   * to the accumulated total time of mapping graph.
00729   */ 
00730   mg_back_link->time=(double)((byte_size/bw));
00731   mg->total_time=mg_back_link->time+mg->total_time;
00732 
00733   /*
00734    * Set the back link set array corresponding to this
00735    * back link to 1 so that this back link does not get
00736    * set again.
00737    */ 
00738   if(!mg_task_node->back_link_set) return -1;
00739   mg_task_node->back_link_set[back_link_pos]=1;
00740 
00741 
00742   /*  
00743    * If this does not point back to the previous client,
00744    * then the mg_obj_node should only have one forward link
00745    * point to the next task node.
00746    */ 
00747 
00748    gs_smart_tg_task_node * tg_back_link_node=tg_obj_node->tg_obj_bkwrd_link->task_node_ptr;
00749    if(tg_back_link_node->id!=task_id-1){
00750       mg_obj_node->nb_for_links=1;
00751    }
00752    else{
00753      mg_obj_node->nb_for_links=mg_nb_obj_for_links;
00754    }
00755 
00756 
00757   /*
00758    * Setting forward link from obj node to be the same as
00759    * back link of next task node.  Also assigning netpm
00760    * link to forward object link of object node.
00761    */ 
00762   mg_obj_node->mg_obj_for_links=(gs_smart_mg_link **)calloc(mg_obj_node->nb_for_links, sizeof(gs_smart_mg_link *)); 
00763   if(!mg_obj_node->mg_obj_for_links) return -1;
00764 
00765   mg_obj_node->mg_obj_for_links[0]=mg_back_link;
00766 
00767 
00768   /*
00769    * Setting back link from obj node to client node
00770    */ 
00771   mg_obj_node->mg_obj_back_link=(gs_smart_mg_link *)
00772                                        calloc(1, sizeof(gs_smart_mg_link));
00773 
00774   if(!mg_obj_node->mg_obj_back_link) return -1;
00775   if(!mg->mg_nodes[task_id-1]) return -1;
00776   mg_obj_node->mg_obj_back_link->mg_node_ptr=mg->mg_nodes[task_id-1];
00777   mg_obj_node->mg_obj_back_link->mg_obj_node_ptr=mg_obj_node;
00778 
00779   return 0;
00780 }
00781 
00782 
00783 
00784 
00785 
00786 int gs_smart_mg_set_c_comm_dep(gs_smart_tg * tg, gs_smart_netpm * netpm,
00787                            gs_smart_mg *mg, int task_id, int back_link_pos,int for_obj_link_pos,
00788                                  int mg_this_o_f_link_cnt, int * mg_next_o_f_link_cnt){
00789 
00790   int dest_task_id;
00791   int dest_back_link_pos;
00792   gs_smart_tg_obj_node * tg_obj_node;
00793   gs_smart_mg_node * mg_dest_node;
00794   gs_smart_mg_task_node * mg_dest_task_node;
00795   gs_smart_mg_link *mg_for_link, * mg_dest_back_link;
00796   gs_smart_mg_node * mg_node;
00797   gs_smart_mg_task_node * mg_task_node=NULL;
00798   gs_smart_mg_link * mg_back_link, * mg_obj_back_link;
00799   gs_smart_mg_obj_node * mg_obj_node;
00800   gs_smart_netpm_node * netpm_node;
00801   double bw;
00802   int byte_size;
00803 
00804 
00805   mg_node=mg->mg_nodes[task_id];
00806   mg_task_node=mg_node->mg_task_node;
00807 
00808   mg_back_link=mg_task_node->mg_node_back_links[back_link_pos];
00809   mg_obj_node = mg_back_link->mg_obj_node_ptr;
00810 
00811 
00812   /*
00813    * Setting client obj for links
00814    */ 
00815   mg_obj_node->mg_obj_for_links[mg_this_o_f_link_cnt]=
00816                              (gs_smart_mg_link *)calloc(1, sizeof(gs_smart_mg_link ));
00817 
00818   mg_for_link= mg_obj_node->mg_obj_for_links[mg_this_o_f_link_cnt];
00819   mg_for_link->for_link_pos=mg_this_o_f_link_cnt;
00820 
00821   /*
00822    * Finding destination node for obj for link
00823    */
00824   tg_obj_node=mg_obj_node->mapped_obj_node;
00825   dest_task_id=tg_obj_node->tg_obj_frwrd_link[for_obj_link_pos]->task_node_ptr->id;
00826   dest_back_link_pos=tg_obj_node->tg_obj_frwrd_link[for_obj_link_pos]->back_link_pos;
00827 
00828   mg_dest_node=mg->mg_nodes[dest_task_id];
00829 
00830 
00831   mg_dest_task_node=mg_dest_node->mg_task_node;
00832 
00833   netpm_node=mg_dest_task_node->mapped_server_node;
00834 
00835   
00836   if(mg_dest_task_node->mapped_server_node->server->smart==1){
00837       
00838     mg_dest_back_link=mg_dest_task_node->mg_node_back_links[dest_back_link_pos];
00839     mg_for_link->mg_obj_node_ptr=mg_obj_node;
00840     mg_for_link->mg_node_ptr=mg_dest_node;
00841    
00842     
00843     mg_for_link->mapped_netpm_link=netpm_node->netpm_links[0];
00844    
00845     /*
00846      * Check to see if argument already sent to server
00847      */
00848     int i;
00849     int already_on_server=0;
00850     gs_server_t * this_dest_server;
00851     gs_server_t * prev_dest_server;
00852     gs_smart_mg_task_node * mg_prev_task_node;
00853     this_dest_server=mg_dest_task_node->mapped_server_node->server;
00854     for(i=0;(i<mg_this_o_f_link_cnt) && (!already_on_server);i++){
00855       mg_prev_task_node=mg_obj_node->mg_obj_for_links[i]->mg_node_ptr->mg_task_node;
00856       prev_dest_server=mg_prev_task_node->mapped_server_node->server;
00857       if(strcmp(this_dest_server->componentid, prev_dest_server->componentid)==0){
00858         already_on_server=1; 
00859         mg_obj_node->mg_obj_for_links[i]->store_arg=1;
00860       }
00861     }
00862                  
00863     /*
00864      * if not already on server then send it
00865      */  
00866     if(!already_on_server){
00867       if(netpm_node->netpm_links[0]->bw==0.0){
00868         netpm_node->netpm_links[0]->bw=netpm->avg_c_bw;
00869       }
00870       bw=netpm_node->netpm_links[0]->bw;
00871       byte_size= mg_for_link->mg_obj_node_ptr->mapped_obj_node->byte_size;
00872       mg_for_link->time=(double)((byte_size/bw));
00873       mg_for_link->already_on_server=0;
00874       mg_obj_node->nb_dest_servers++; 
00875 
00876     }
00877     else{
00878       mg_for_link->time=0.0;
00879       mg_for_link->already_on_server=1;
00880 
00881     }    
00882  
00883 
00884 
00885     mg_obj_node->mg_obj_back_link=(gs_smart_mg_link *)calloc(1, sizeof(gs_smart_mg_link ));
00886 
00887     mg_obj_back_link= mg_obj_node->mg_obj_back_link;
00888     mg_obj_back_link->mg_node_ptr=mg->mg_nodes[task_id-1];
00889     mg_obj_back_link->mg_obj_node_ptr=mg_obj_node;
00890 
00891     mg_dest_task_node->mg_node_back_links[dest_back_link_pos]=mg_for_link;
00892     mg_dest_task_node->back_link_set[dest_back_link_pos]=2;
00893     mg_this_o_f_link_cnt++;
00894   }
00895   * mg_next_o_f_link_cnt= mg_this_o_f_link_cnt; 
00896 
00897        
00898 
00899   return 0;
00900 }
00901 
00902 
00903 
00904 
00905 
00906 
00907 int gs_smart_mg_count_s_comm_for_obj_links(gs_smart_tg * tg, gs_smart_netpm * netpm,
00908                                     gs_smart_mg *mg, int task_id, 
00909                                  int for_link_id, int * _mg_nb_obj_for_links, int * _sent_remotely){
00910   gs_smart_tg_remote_task_node * tg_rem_task_node;
00911   gs_smart_mg_node * mg_node;
00912   gs_smart_mg_task_node * mg_task_node=NULL, * mg_dest_task_node=NULL;
00913   gs_smart_mg_link  * mg_for_link;
00914   gs_smart_tg_obj_node * tg_obj_node; 
00915   gs_smart_tg_task_node * tg_dest_task_node;
00916 
00917   int j,k;
00918   int tg_nb_obj_for_links, mg_nb_obj_for_links;
00919   int dest_task_id;
00920   int sent_remotely=1;
00921 
00922   j=for_link_id;
00923  
00924   mg_node=mg->mg_nodes[task_id];
00925    
00926   mg_task_node=mg_node->mg_task_node;
00927   tg_rem_task_node=tg->task_nodes[task_id]->tg_rem_task_node;
00928 
00929   mg_for_link=mg_task_node->mg_node_for_links[j];
00930   tg_nb_obj_for_links=tg_rem_task_node->tg_node_frwrd_link[j]->obj_node_ptr->nb_obj_frwd_links;
00931   mg_nb_obj_for_links=tg_nb_obj_for_links;
00932   if(mg_task_node->mapped_server_node->server->smart==0){
00933     mg_nb_obj_for_links=1;
00934     sent_remotely=0;
00935   }
00936   else{
00937     for(k=0; k<tg_nb_obj_for_links;k++){
00938       tg_obj_node=tg_rem_task_node->tg_node_frwrd_link[j]->obj_node_ptr;
00939       tg_dest_task_node=tg_obj_node->tg_obj_frwrd_link[k]->task_node_ptr;
00940       dest_task_id=tg_dest_task_node->id;
00941       if(mg->mg_nodes[dest_task_id]->node_type==GS_SMART_MG_TASK_NODE){
00942         mg_dest_task_node=mg->mg_nodes[dest_task_id]->mg_task_node;
00943         if(mg_dest_task_node->mapped_server_node->server->smart==0){
00944           *_mg_nb_obj_for_links=1;
00945           *_sent_remotely=0;
00946           return 0;
00947         }
00948       }
00949     }
00950     
00951   }
00952   tg_obj_node=tg_rem_task_node->tg_node_frwrd_link[j]->obj_node_ptr;
00953   tg_dest_task_node=tg_obj_node->tg_obj_frwrd_link[0]->task_node_ptr;
00954   if((tg_dest_task_node->node_type==GS_SMART_TG_CLIENT_NODE) ||  (mg_nb_obj_for_links==0)){
00955     sent_remotely=0;
00956     mg_nb_obj_for_links=1;
00957   }
00958 
00959   *_mg_nb_obj_for_links=mg_nb_obj_for_links;
00960   *_sent_remotely=sent_remotely;
00961   return 0;
00962 }
00963 
00964 
00965 
00966 
00967 
00968 
00969 int gs_smart_mg_construct_s_comm_obj_for_links(gs_smart_tg * tg, gs_smart_netpm * netpm,
00970                          gs_smart_mg *mg, int task_id, int j, int mg_nb_obj_for_links){
00971 
00972   gs_smart_mg_node * mg_node;
00973   gs_smart_mg_task_node * mg_task_node=NULL;
00974   gs_smart_mg_obj_node * mg_obj_node;
00975   gs_smart_mg_link  * mg_for_link;
00976   int i;
00977   mg_node=mg->mg_nodes[task_id];
00978    
00979   mg_task_node=mg_node->mg_task_node;
00980        
00981   mg_for_link=mg_task_node->mg_node_for_links[j];
00982   mg_obj_node=mg_for_link-> mg_obj_node_ptr;
00983   mg_for_link->for_link_pos=j;
00984 
00985   mg_obj_node->nb_for_links=mg_nb_obj_for_links;
00986   mg_obj_node->mg_obj_for_links=(gs_smart_mg_link **)
00987                  calloc(mg_nb_obj_for_links, sizeof(gs_smart_mg_link *)); 
00988 
00989   for(i=0;i<mg_nb_obj_for_links;i++){
00990     mg_obj_node->mg_obj_for_links[i]=(gs_smart_mg_link *)
00991                    calloc(1, sizeof(gs_smart_mg_link));
00992   }
00993 
00994   mg_obj_node->mg_obj_for_links[mg_nb_obj_for_links-1]->delete_arg=1;
00995   return 0;
00996 }
00997 
00998 
00999 
01000 
01001 
01002 
01003 
01004 
01005 int gs_smart_mg_set_s_comm(gs_smart_tg * tg, gs_smart_netpm * netpm,
01006                          gs_smart_mg *mg, int task_id, int for_task_link_pos, int k,
01007                          int mg_this_o_f_link_cnt, int * mg_next_o_f_link_cnt){
01008 
01009   gs_smart_tg_remote_task_node * tg_rem_task_node;
01010   gs_smart_tg_task_node * tg_dest_task_node;
01011   gs_smart_tg_obj_node * tg_obj_node; 
01012   gs_smart_mg_task_node * mg_task_node=NULL, * mg_dest_task_node=NULL;
01013   gs_smart_mg_obj_node * mg_obj_node;
01014   gs_smart_mg_link  * mg_for_link, * mg_obj_for_link=NULL;
01015   gs_smart_mg_link  * mg_dest_back_link;
01016   gs_smart_mg_node * mg_node;
01017   gs_smart_netpm_node * netpm_node;
01018   gs_server_t * this_dest_server;
01019   gs_server_t * prev_dest_server;
01020   gs_smart_mg_task_node * mg_prev_task_node;
01021   int dest_task_id=0,  tg_nb_obj_for_links;
01022   int dest_back_link_pos=0; 
01023   int src_smart_id, dest_smart_id;
01024   int netpm_for_link_pos;
01025   int byte_size;
01026   double bw;
01027   int already_on_server=0;
01028   int j;
01029  
01030   mg_node=mg->mg_nodes[task_id];
01031   mg_task_node=mg_node->mg_task_node;
01032 
01033   tg_rem_task_node=tg->task_nodes[task_id]->tg_rem_task_node;
01034   mg_for_link=mg_task_node->mg_node_for_links[for_task_link_pos];
01035   mg_obj_node=mg_for_link->mg_obj_node_ptr;
01036   netpm_node= mg_task_node->mapped_server_node;
01037   tg_nb_obj_for_links=mg_obj_node->mapped_obj_node->nb_obj_frwd_links; 
01038  
01039    if(mg_obj_node->sent_remotely==1){
01040      tg_obj_node=tg_rem_task_node->tg_node_frwrd_link[for_task_link_pos]->obj_node_ptr;
01041      tg_dest_task_node=tg_obj_node->tg_obj_frwrd_link[k]->task_node_ptr;
01042      dest_back_link_pos=tg_obj_node->tg_obj_frwrd_link[k]->back_link_pos;
01043 
01044      dest_task_id=tg_dest_task_node->id;
01045      if(mg->mg_nodes[dest_task_id]->node_type==GS_SMART_MG_TASK_NODE){
01046        mg_dest_task_node=mg->mg_nodes[dest_task_id]->mg_task_node;
01047        if(mg_dest_task_node->mapped_server_node->server->smart==1){
01048          src_smart_id= mg_task_node->mapped_server_node->server->smart_id;
01049          dest_smart_id=mg_dest_task_node->mapped_server_node->server->smart_id;
01050          mg_obj_for_link=mg_obj_node->mg_obj_for_links[ mg_this_o_f_link_cnt];
01051          mg_obj_for_link->for_link_pos= mg_this_o_f_link_cnt;
01052          mg_obj_for_link->mg_obj_node_ptr=mg_obj_node;
01053         
01054          if(dest_smart_id==src_smart_id){
01055            netpm_for_link_pos=-1; 
01056            mg_obj_for_link->caching=1;   
01057          }
01058          else if(dest_smart_id>src_smart_id){
01059            netpm_for_link_pos=dest_smart_id;
01060            mg_obj_for_link->caching=0;
01061          }
01062          else{
01063            netpm_for_link_pos=dest_smart_id+1;
01064            mg_obj_for_link->caching=0;
01065          }
01066          mg_obj_for_link->mg_node_ptr=mg->mg_nodes[dest_task_id];
01067 
01068          mg_obj_for_link->mg_obj_node_ptr=mg_obj_node;
01069 
01070          already_on_server=0;
01071 
01072          this_dest_server=mg_dest_task_node->mapped_server_node->server;
01073 
01074          for(j=0; (j<mg_this_o_f_link_cnt) && (!already_on_server) ;j++){
01075            mg_prev_task_node=mg_obj_node->mg_obj_for_links[j]->mg_node_ptr->mg_task_node;
01076            prev_dest_server=mg_prev_task_node->mapped_server_node->server;
01077            if(strcmp(this_dest_server->componentid, prev_dest_server->componentid)==0){
01078              already_on_server=1; 
01079            }
01080          }
01081          if(already_on_server){
01082           // mg->total_time=0.0;
01083            mg_obj_for_link->already_on_server=1;
01084          }
01085          else{
01086            if(!mg_obj_for_link->caching){
01087              mg_obj_for_link->mapped_netpm_link=netpm_node->netpm_links[netpm_for_link_pos];
01088            
01089              if(netpm_node->netpm_links[netpm_for_link_pos]->bw==0.0){
01090                netpm_node->netpm_links[netpm_for_link_pos]->bw=netpm->avg_s_bw;
01091              }
01092 
01093              bw=netpm_node->netpm_links[netpm_for_link_pos]->bw;
01094              byte_size=mg_obj_for_link->mg_obj_node_ptr->mapped_obj_node->byte_size;
01095              mg_obj_for_link->time=(double)((byte_size/bw));
01096              mg->total_time=mg->total_time+mg_obj_for_link->time;
01097              mg_obj_node->nb_dest_servers++;
01098            }
01099            else if(mg_obj_for_link->caching){
01100              byte_size=mg_obj_for_link->mg_obj_node_ptr->mapped_obj_node->byte_size;
01101              mg_obj_for_link->time=(double)((double)(byte_size/(double)100000000));
01102              mg->total_time=mg->total_time+mg_obj_for_link->time;
01103            }
01104            mg_obj_for_link->already_on_server=0;
01105          }
01106          mg_dest_back_link= mg_dest_task_node->mg_node_back_links[dest_back_link_pos];
01107   
01108          mg_dest_task_node->mg_node_back_links[dest_back_link_pos]=mg_obj_for_link;
01109          mg_dest_task_node->back_link_set[dest_back_link_pos]=4;
01110 
01111          mg_obj_node->mg_obj_back_link=(gs_smart_mg_link *)
01112                                          calloc(1, sizeof(gs_smart_mg_link));
01113          mg_obj_node->mg_obj_back_link->mg_node_ptr=mg_node;
01114          mg_obj_node->mg_obj_back_link->mg_obj_node_ptr=mg_obj_node;
01115  
01116         mg_this_o_f_link_cnt++; 
01117       }
01118      }
01119    }
01120    else if(mg_obj_node->sent_remotely==0){
01121          mg_obj_for_link=mg_obj_node->mg_obj_for_links[0];
01122          mg_obj_for_link->for_link_pos=0;
01123           /*
01124           * Point the obj for link to the next client node
01125           */  
01126          mg_obj_for_link->mg_node_ptr=mg->mg_nodes[task_id+1];
01127          /*
01128           * Point the object node forward link back on itself
01129           */ 
01130          mg_obj_for_link->mg_obj_node_ptr=mg_obj_node;
01131          
01132         
01133          /*
01134           * Find the netpm link which will be assigned to
01135           * this forward link.
01136           */ 
01137          netpm_node= mg_task_node->mapped_server_node;
01138          mg_obj_for_link->mapped_netpm_link=netpm_node->netpm_links[0];
01139           
01140          double bw=netpm_node->netpm_links[0]->bw;
01141          int byte_size=mg_obj_for_link->mg_obj_node_ptr->mapped_obj_node->byte_size;
01142          mg_obj_for_link->time=(double)((byte_size/bw));
01143 
01144 
01145            mg->total_time=mg->total_time+mg_obj_for_link->time;
01146 
01147 
01148 
01149          mg_obj_node->mg_obj_back_link=(gs_smart_mg_link *)
01150                                          calloc(1, sizeof(gs_smart_mg_link));
01151          mg_obj_node->mg_obj_back_link->mg_node_ptr=mg_node;
01152          mg_obj_node->mg_obj_back_link->mg_obj_node_ptr=mg_obj_node;
01153           mg_this_o_f_link_cnt++;
01154     }
01155   
01156 
01157   * mg_next_o_f_link_cnt=mg_this_o_f_link_cnt;
01158 
01159   return 0;
01160 }
01161 
01162 
01163 
01164 
01165 
01166 
01167 
01168 
01169 
01170 int gs_smart_free_mg(gs_smart_mg * mg){
01171   int i, j;
01172   gs_smart_mg_node * mg_node;
01173   gs_smart_mg_task_node * mg_task_node=NULL;
01174 
01175   gs_smart_mg_link * mg_task_back_link, * mg_obj_back_link;
01176   gs_smart_mg_obj_node * mg_obj_node;
01177 
01178   if(!mg) return -1;
01179  
01180   for(i=0;i<mg->nb_mg_nodes;i++){
01181     if(!mg->mg_nodes[i]) return -1;
01182     mg_node=mg->mg_nodes[i];
01183     if(mg_node->node_type==GS_SMART_MG_CLIENT_NODE){
01184       if(mg_node->mg_client_node) free(mg_node->mg_task_node);
01185     }
01186     else if(mg_node->node_type==GS_SMART_MG_TASK_NODE){
01187       if(!mg_node->mg_task_node) return -1;
01188       mg_task_node=mg_node->mg_task_node;
01189       for(j=0;j<mg_task_node->nb_back_links;j++){
01190         if(!mg_task_node->mg_node_back_links[j]) return -1;
01191         mg_task_back_link=mg_task_node->mg_node_back_links[j];
01192         /*
01193          * Only free obj node and obj back link once
01194          */ 
01195         if(mg_task_back_link->for_link_pos==0){
01196           if(!mg_task_back_link->mg_obj_node_ptr) return -1;
01197           mg_obj_node=mg_task_back_link->mg_obj_node_ptr; 
01198           if(!mg_obj_node->mg_obj_back_link) return -1;
01199           mg_obj_back_link=mg_obj_node->mg_obj_back_link;
01200           if(mg_obj_back_link) free(mg_obj_back_link);
01201           if(mg_obj_node) free(mg_obj_node); 
01202         }
01203         if(mg_task_back_link) free(mg_task_back_link);
01204 
01205       }
01206 
01207       if(mg_task_node->back_link_set) free(mg_task_node->back_link_set);
01208       if(mg_task_node->mg_node_for_links) free(mg_task_node->mg_node_for_links); 
01209       if(mg_task_node) free(mg_task_node);
01210     }
01211     if(mg_node) free(mg_node); 
01212   }
01213   return 0;
01214 }
01215 
01216 
01217 
01218 
01219 
01220 
01221 
01222 int gs_smart_mg_create_argument_file_names(gs_smart_mg *mg){
01223   gs_smart_mg_task_node * mg_task_node=NULL;
01224   gs_smart_mg_node * mg_node;
01225   gs_smart_mg_node * mg_dest_node;
01226   gs_smart_mg_obj_node * mg_obj_node;
01227   gs_smart_mg_link * mg_task_for_link, * mg_task_back_link;
01228   gs_smart_mg_link * mg_obj_for_link, * mg_obj_back_link;
01229   int i, j;
01230   int dep_id=0;
01231   int smart_app_num;
01232   pid_t pid;
01233   pid=getpid();
01234   char GS_SMART_ARG_FILE_EXT[FN_LEN];
01235 
01236   if(gs_smart_read_app_no_file("/tmp/gs_app_num", &smart_app_num)<0){
01237     ERRPRINTF("SMART : Error reading and updating application number file\n");
01238     return -1;
01239   }
01240   for(i=0;i<mg->nb_mg_nodes;i++){
01241     mg_node=mg->mg_nodes[i];   
01242     if(mg_node->node_type==GS_SMART_MG_TASK_NODE){
01243       mg_task_node=mg_node->mg_task_node;
01244       for(j=0; j<mg_task_node->nb_back_links; j++){
01245         mg_task_back_link=mg_task_node-> mg_node_back_links[j];
01246         mg_obj_node= mg_task_back_link->mg_obj_node_ptr;
01247         mg_obj_back_link=mg_obj_node->mg_obj_back_link;
01248         if(mg_obj_back_link->mg_node_ptr->node_type==GS_SMART_MG_CLIENT_NODE){
01249           if((mg_obj_node->nb_for_links>1) && (!mg_obj_node->smart_arg_file)){
01250             snprintf(GS_SMART_ARG_FILE_EXT, FN_LEN, "/tmp/gs_smart_arg_files/%s.%d", GS_SMART_ARG_FILE_PREFIX, pid);
01251             
01252             if(gs_smart_get_obj_file_name(&mg_obj_node->smart_arg_file, GS_SMART_ARG_FILE_EXT,smart_app_num, dep_id)<0){
01253               ERRPRINTF("SMART : Error getting argument file name\n");
01254               return -1;
01255             }
01256             dep_id++;
01257           }
01258         }
01259  
01260       }
01261       for(j=0; j<mg_task_node->nb_for_links; j++){
01262         mg_task_for_link=mg_task_node-> mg_node_for_links[j];
01263         mg_obj_node= mg_task_for_link->mg_obj_node_ptr;
01264         mg_obj_for_link=mg_obj_node->mg_obj_for_links[0];
01265         mg_dest_node=mg_obj_for_link-> mg_node_ptr;
01266         if(mg_dest_node->node_type==GS_SMART_MG_TASK_NODE){
01267           snprintf(GS_SMART_ARG_FILE_EXT, FN_LEN, "/tmp/gs_smart_arg_files/%s.%d", GS_SMART_ARG_FILE_PREFIX, pid);
01268             
01269           if(gs_smart_get_obj_file_name(&mg_obj_node->smart_arg_file, GS_SMART_ARG_FILE_EXT,smart_app_num, dep_id)<0){
01270             ERRPRINTF("SMART : Error getting argument file name\n");
01271             return -1;
01272           }
01273           dep_id++;
01274         }
01275       }
01276     }
01277   }
01278 
01279   return 0;
01280 }
01281 
01282 
01283 
01284 
01285 
01286 int gs_smart_check_if_valid_assignment(gs_smart_tg_remote_task_node * tg_rem_task_node,
01287                                  gs_server_t * mapped_server, int * _valid_assignment){
01288 
01289    gs_server_t * task_server;
01290    int i,valid_assignment, nb_servers;
01291    i=valid_assignment=0;
01292    nb_servers=tg_rem_task_node->nb_servers;
01293    for(i=0; ((i< nb_servers)&&(!valid_assignment)) ; i++ ){
01294      task_server=tg_rem_task_node->avail_servers[i];
01295      
01296      if(strcmp(mapped_server->componentid, task_server->componentid)==0){
01297       
01298        valid_assignment=1; 
01299      }
01300    }
01301 
01302   *_valid_assignment=valid_assignment;
01303   return 0;
01304 }
01305 
01306 
01307 
01308 
01309 
01310 
01311 
01312 
01313 
01314 
01315 
01316 
01317 
01318 
01319 
01320 
01321 int gs_smart_mg_embed_mapping(grpc_function_handle_t * handle, int task_nb){
01322 
01323   gs_problem_t *problem=NULL;
01324   gs_argument_t *argptr=NULL;
01325   gs_smart_mg_node * mg_node=NULL, * mg_src_node=NULL;
01326   gs_smart_mg_task_node * mg_task_node=NULL;
01327   gs_smart_mg_task_node * mg_dest_task_node=NULL;
01328   gs_smart_mg_obj_node * mg_obj_node=NULL;
01329   gs_smart_mg_link * mg_task_for_link=NULL, * mg_task_back_link=NULL;
01330   gs_smart_mg_link * mg_obj_for_link=NULL;
01331 
01332   int input_cnt, output_cnt;
01333   int mg_node_id;
01334   int i, j;
01335   int dest_server_cnt=0;
01336   problem=handle->problem_desc;
01337   mg_node_id=(task_nb*2+2);
01338   mg_node=best_mg->mg_nodes[mg_node_id];
01339   mg_task_node=mg_node->mg_task_node;
01340 
01341 
01342   /*
01343    * Setting task to server mapping
01344    */ 
01345   gs_server_t * mapped_server;
01346   mapped_server=mg_task_node->mapped_server_node->server;
01347 
01348   /*
01349    * If the SmartGridSolve mapped server is not the same as the top
01350    * server assigned by GridSolve then change the server list so 
01351    * that the SmartGridSolve server is the top of the list
01352    */ 
01353   if(strcmp(mapped_server->componentid, handle->server_list[0]->componentid)!=0){
01354     gs_server_t * temp=handle->server_list[0];
01355 
01356     for(i=0;i<handle->num_servers;i++){
01357       if(strcmp(mapped_server->componentid, handle->server_list[i]->componentid)==0){
01358         handle->server_list[0]=handle->server_list[i];
01359         handle->server_list[i]=temp;
01360         for(j=1;j<i;j++){
01361           temp=handle->server_list[j];
01362           handle->server_list[j]=handle->server_list[i];
01363           handle->server_list[i]=temp; 
01364         }
01365         break;
01366       }
01367     }
01368   }
01369   
01370   problem->has_smart_arg_comm=0;
01371 
01372   input_cnt=output_cnt=0;
01373   for(argptr= problem->arglist;argptr!=NULL;argptr=argptr->next) {
01374     if(argptr->objecttype!=GS_SCALAR){
01375      /*
01376       * Examine input object nodes
01377       */ 
01378       if( ( argptr->inout==GS_INOUT ) || ( argptr->inout==GS_IN ) ) {
01379         mg_task_back_link=mg_task_node->mg_node_back_links[input_cnt];
01380         argptr->input_arg_deleted=mg_task_back_link->delete_arg;
01381         mg_obj_node=mg_task_back_link->mg_obj_node_ptr;
01382 
01383        if(mg_obj_node->sent_remotely){
01384           mg_src_node=mg_obj_node->mg_obj_back_link->mg_node_ptr;
01385 
01386           if((mg_node_id-1)== (mg_src_node->id)){
01387             if(mg_src_node->node_type==GS_SMART_MG_CLIENT_NODE){
01388 
01389               argptr->input_nb_dest_servers=mg_obj_node->nb_dest_servers;
01390               argptr->enc_input_dest_servers=(char **)
01391                              calloc(mg_obj_node->nb_dest_servers, sizeof(char *));
01392 
01393               dest_server_cnt=0;
01394               for(i=0;i<mg_obj_node->nb_for_links ;i++){
01395                 mg_obj_for_link=mg_obj_node->mg_obj_for_links[i];
01396                 mg_dest_task_node=mg_obj_for_link->mg_node_ptr->mg_task_node;
01397                 /*
01398                  * Check if the input is sent to server in the standard way
01399                  * or is stored on the server for future tasks
01400                  */
01401                   argptr->input_nb_dest_servers=0;
01402                   if(i==0){
01403                     if( mg_obj_for_link->store_arg){
01404                       argptr->input_arg_stored=1;
01405                       problem->has_smart_arg_comm=1;
01406                     }
01407                   }
01408                   else{
01409                     /*
01410                      *  If any of the other broadcast for links are sending 
01411                      *  to servers that aren't the server of this task
01412                      *  then embed the info of their file locations. 
01413                      */  
01414                     if(!mg_obj_for_link->already_on_server){
01415                       argptr->input_arg_sent_remotely=1;
01416                       problem->has_smart_arg_comm=1;
01417                      
01418                       gs_server_t * dest_server=mg_dest_task_node->mapped_server_node->server;
01419                       if(gs_encode_server(&argptr->enc_input_dest_servers[dest_server_cnt], 
01420                                           dest_server) < 0){
01421                         ERRPRINTF("SMART: Error encoding server string\n");
01422                         return -1; 
01423                       }
01424                       dest_server_cnt++;
01425                     }    
01426                   }
01427                 }
01428               }
01429             }
01430             else{
01431               problem->has_smart_arg_comm=1;
01432               argptr->input_arg_received_remotely=1;
01433 
01434             }
01435  
01436             argptr->input_arg_file=mg_obj_node->smart_arg_file; 
01437             
01438           }
01439           else{
01440             argptr->input_arg_received_remotely=0;
01441 
01442 
01443 
01444           }
01445          
01446           argptr->input_nb_dest_servers=dest_server_cnt;
01447           input_cnt++;
01448         }
01449 
01450         if( ( argptr->inout==GS_INOUT ) || ( argptr->inout==GS_OUT ) ) {
01451           mg_task_for_link=mg_task_node->mg_node_for_links[output_cnt];
01452           mg_obj_node=mg_task_for_link->mg_obj_node_ptr;
01453           if(mg_obj_node->sent_remotely){
01454             argptr->output_arg_sent_remotely=1;
01455             problem->has_smart_arg_comm=1;
01456  
01457      
01458 
01459 
01460             argptr->output_nb_dest_servers=mg_obj_node->nb_dest_servers;
01461                
01462             argptr->enc_output_dest_servers=(char **)
01463                              calloc(mg_obj_node->nb_dest_servers, sizeof(char *));
01464  
01465             dest_server_cnt=0;
01466             for(i=0;i<mg_obj_node->nb_for_links ;i++){
01467               mg_obj_for_link=mg_obj_node->mg_obj_for_links[i];
01468               mg_dest_task_node=mg_obj_for_link->mg_node_ptr->mg_task_node;
01469            
01470               if(mg_obj_for_link->caching==1){
01471                 argptr->output_cached=1;
01472                 problem->has_smart_arg_comm=1;
01473              }  
01474              else if(!mg_obj_for_link->already_on_server){
01475                gs_server_t * dest_server=mg_dest_task_node->mapped_server_node->server;
01476                if(gs_encode_server(&argptr->enc_output_dest_servers[dest_server_cnt], 
01477                                                                   dest_server) < 0){
01478                  ERRPRINTF("SMART: Error encoding server string\n");
01479                  return -1; 
01480                }
01481                dest_server_cnt++;
01482              }    
01483            }
01484            argptr->output_arg_file=mg_obj_node->smart_arg_file;
01485          }
01486          else{
01487            argptr->output_arg_sent_remotely=0;
01488          }
01489           argptr->output_nb_dest_servers=dest_server_cnt;
01490          output_cnt++;
01491        }
01492      }
01493    }
01494   return 0;
01495 
01496 }
01497 
01498 
01499 
01500 
01501 
01502 int gs_smart_time_mg_task_node(gs_smart_mg_task_node * mg_task_node, double * time){
01503  
01504  double old_effective_speed = -1.0;
01505  double new_effective_speed = -1.0;
01506   double computational_score = -1.0;
01507   if(!mg_task_node) return -1;
01508   gs_smart_netpm_node * mapped_server_node;
01509   gs_smart_tg_remote_task_node * mapped_task_node;
01510 
01511   mapped_server_node=mg_task_node->mapped_server_node;
01512   mapped_task_node= mg_task_node->mapped_task_node;
01513 
01514  new_effective_speed = ((double) mapped_server_node->server->kflops * (double) mapped_server_node->server->ncpu) /
01515       (((double) (mapped_server_node->server->workload+ mapped_server_node->server->task_workload) / 100.0) + 1.0);
01516  old_effective_speed = ((double) mapped_server_node->server->kflops * (double) mapped_server_node->server->ncpu) /
01517       (((double) (mapped_server_node->server->workload) / 100.0) + 1.0);
01518 
01519 
01520   computational_score = (double) mapped_task_node->nb_flops / (double) new_effective_speed;
01521 
01522   if(isnan(computational_score)){
01523     return -1; 
01524   }
01525   //printf("THE COMPUTATIONAL SCORE OF EXECUTING A TASK WITH %f FLOPS ON %s WHEN WORKLOAD IS %d AND TASK WORKLOAD IS %d IS %f\n", mapped_task_node->nb_flops, mapped_server_node->server->hostname, mapped_server_node->server->workload, mapped_server_node->server->task_workload, computational_score);
01526 
01527 
01528   *time=computational_score;
01529   return 0;
01530 
01531 }
01532 
01533 
01534 
01535 
01536 
01537 int gs_smart_time_tg_task_node(gs_smart_tg_remote_task_node * tg_rem_task_node, 
01538                                            gs_server_t * server, double * time){
01539 
01540  double new_effective_speed = -1.0;
01541   double computational_score = -1.0;
01542   if((!tg_rem_task_node ) || (!server)) return -1;
01543 
01544 
01545  new_effective_speed = ((double) server->kflops * (double) server->ncpu) /
01546       (((double) (server->workload + server->task_workload) / 100.0) + 1.0);
01547 
01548   computational_score = (double) tg_rem_task_node->nb_flops / (double) new_effective_speed;
01549 
01550   if(isnan(computational_score)){
01551     return -1;
01552   }
01553   computational_score=computational_score;
01554   //printf("\n\n\nTIMING TASK NODE ON SERVER %s KFLOPS %d NB_CPU %d WORKLOAD %d TASK WORKLOAD %d COMPUTATIONAL SCORE %f\n", server->hostname,  server->kflops, server->ncpu, server->workload, server->task_workload, computational_score);
01555 
01556   *time=computational_score;
01557   return 0;
01558 
01559 }
01560 
01561 
01562 int gs_smart_time_tg_task_node_with_c_comm(gs_smart_tg_remote_task_node * tg_rem_task_node, 
01563                                            gs_server_t * server, double * time){
01564 
01565  double new_effective_speed = -1.0;
01566   double computational_score = -1.0;
01567 //  int i;
01568   if((!tg_rem_task_node ) || (!server)) return -1;
01569 
01570 
01571  new_effective_speed = ((double) server->kflops * (double) server->ncpu) /
01572       (((double) (server->workload+ server->task_workload) / 100.0) + 1.0);
01573 
01574   computational_score = (double) tg_rem_task_node->nb_flops / (double) new_effective_speed;
01575 
01576   if(isnan(computational_score)){
01577     return -1;
01578   }
01579  // printf("\n\n\nTIMING TASK NODE ON SERVER %s WORKLOAD %d TASK WORKLOAD %d COMPUTATIONAL SCORE %f\n", server->hostname, server->workload, server->task_workload, computational_score);
01580 /*
01581   for(i=0;i<tg_rem_task_node->nb_node_bkwd_links;i++){
01582     printf("BYTE SIZE OF LINK %d IS %d\n", i, tg_rem_task_node->tg_node_bkwrd_link[i]->obj_node_ptr->byte_size);
01583   }
01584 */ 
01585   //printf("\n\n\nCOMMUNICATION LOAD=%d\n", tg_rem_task_node->problem->);
01586   computational_score=computational_score/100;
01587   *time=computational_score;
01588   return 0;
01589 
01590 }
01591 
01592 
01593 
01594 
01595 
01596 int gs_smart_mg_print_to_dotgraph(gs_smart_mg * mg, char * filename){
01597 
01598 
01599   gs_smart_mg_node * mg_node, *mg_src_node;
01600   gs_smart_mg_task_node * mg_task_node;
01601   gs_smart_mg_task_node * mg_dest_task_node;
01602   gs_smart_mg_link * mg_for_task_link, * mg_back_task_link;
01603   gs_smart_mg_obj_node * mg_obj_node=NULL;
01604   gs_smart_mg_link * mg_obj_for_link;
01605   FILE *fp;
01606   int i, j, k, node_type;
01607   int client_comm_exists;
01608 
01609   fp = fopen(filename, "w");
01610   if(fp<0){
01611     ERRPRINTF("SMART: Error opening file\n");
01612     return -1;
01613   }
01614   fprintf(fp, "digraph Mapping_Graph {\n");
01615   for(i=0; i<mg->nb_mg_nodes;i++){
01616     mg_node=mg->mg_nodes[i];
01617     node_type=mg_node->node_type;
01618     if(node_type==GS_SMART_MG_START_NODE){
01619       fprintf(fp, " start_node_0 [shape=polygon, label=\"start_node_0\",sides=5, peripheries=2, style=\"filled\", fillcolor=\"#FFA500\" fontsize=\"15.0\"];\n");
01620       
01621     }
01622     if(node_type==GS_SMART_MG_END_NODE){
01623       fprintf(fp, " end_node_%d [shape=polygon, label=\"end_node_%d\\ntotal_time=%f\",sides=5, peripheries=2, style=\"filled\", fillcolor=\"#FFA500\" fontsize=\"15.0\"];\n", i, i, mg->total_time);
01624        fprintf(fp, " start_node_0 -> client_1 [color=red];\n");
01625       fprintf(fp, " client_%d -> end_node_%d [color=red];\n", i-1, i);
01626       break;
01627     }
01628     else if(node_type==GS_SMART_MG_CLIENT_NODE){
01629       fprintf(fp, " client_%d [shape=\"diamond\", style=\"filled\", fillcolor=\"#ADD8E6\" fontsize=\"15.0\"];\n", i);
01630     }
01631     else if(node_type==GS_SMART_TG_REM_TASK_NODE){
01632       mg_task_node=mg_node->mg_task_node;
01633       if(mg_task_node->mapped_server_node->server->smart==1){
01634         fprintf(fp, " %s_%d [shape=box,label=\"%s_%d\\n%s_%d\\nblocking=%d\\ntime=%f\",  style=filled,fillcolor=\"red\",fontsize=15.0];\n", mg_task_node->mapped_task_node->problem->name, i, mg_task_node->mapped_task_node->problem->name, i,  mg_task_node->mapped_server_node->server->hostname, mg_task_node->mapped_server_node->server->port, mg_task_node->mapped_task_node->blocking, mg_task_node->time);
01635 
01636       }
01637       else
01638       {
01639         fprintf(fp, " %s_%d [shape=box,label=\"%s_%d\\n%s_%d\\nblocking=%d\\ntime=%f\",  style=filled,fillcolor=\"deepskyblue\",fontsize=15.0];\n", mg_task_node->mapped_task_node->problem->name, i, mg_task_node->mapped_task_node->problem->name, i,  mg_task_node->mapped_server_node->server->hostname, mg_task_node->mapped_server_node->server->port, mg_task_node->mapped_task_node->blocking, mg_task_node->time);
01640       
01641       }
01642     }
01643   }
01644 
01645   for(i=0; i<mg->nb_mg_nodes;i++){
01646     mg_node=mg->mg_nodes[i];
01647     node_type=mg_node->node_type;
01648     if(node_type==GS_SMART_MG_TASK_NODE){
01649       mg_task_node=mg_node->mg_task_node;
01650       client_comm_exists=0;
01651       for(j=0;j<mg_task_node->nb_for_links;j++){
01652          mg_for_task_link= mg_task_node->mg_node_for_links[j];
01653          mg_obj_node=mg_for_task_link->mg_obj_node_ptr;
01654         fprintf(fp, " out_%s_%s_%d [label=\"%s\\nbytes=%d\" ];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name,   i, mg_obj_node->mapped_obj_node->name, mg_obj_node->mapped_obj_node->byte_size);
01655 
01656         fprintf(fp, "%s_%d -> out_%s_%s_%d;\n", mg_task_node->mapped_task_node->problem->name, i, mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i);
01657          for(k=0;k<mg_obj_node->nb_for_links;k++){
01658            mg_obj_for_link= mg_obj_node->mg_obj_for_links[k];
01659            if(mg_obj_for_link->mg_node_ptr->node_type==GS_SMART_MG_TASK_NODE){
01660              mg_dest_task_node=mg_obj_for_link->mg_node_ptr->mg_task_node;
01661              if(!mg_obj_for_link->caching){
01662                if(mg_obj_for_link->already_on_server){
01663                  fprintf(fp, "out_%s_%s_%d -> %s_%d [color=red label=\"time=%f\"];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i, mg_dest_task_node->mapped_task_node->problem->name, mg_obj_for_link->mg_node_ptr->id,  mg_obj_for_link->time);
01664 
01665                }
01666                else{
01667                  fprintf(fp, "out_%s_%s_%d -> %s_%d [label=\"time=%f\"];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i, mg_dest_task_node->mapped_task_node->problem->name, mg_obj_for_link->mg_node_ptr->id,  mg_obj_for_link->time);
01668 
01669                }
01670  
01671              }
01672              else{
01673 
01674                if(mg_obj_for_link->already_on_server){
01675                  fprintf(fp, "out_%s_%s_%d -> %s_%d [color=red label=\"time=%f\" style=dotted];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i, mg_dest_task_node->mapped_task_node->problem->name, mg_obj_for_link->mg_node_ptr->id,  mg_obj_for_link->time);
01676                }
01677                else{
01678                  fprintf(fp, "out_%s_%s_%d -> %s_%d [label=\"time=%f\" style=dotted];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i, mg_dest_task_node->mapped_task_node->problem->name, mg_obj_for_link->mg_node_ptr->id,  mg_obj_for_link->time);
01679 
01680 
01681                }
01682 
01683              }
01684 
01685            }
01686            else if(mg_obj_for_link->mg_node_ptr->node_type==GS_SMART_MG_CLIENT_NODE){
01687              mg_dest_task_node=mg_obj_for_link->mg_node_ptr->mg_task_node;
01688              if(mg_obj_for_link->already_on_server){
01689                fprintf(fp, "out_%s_%s_%d -> client_%d [color=red label=\"time=%f\"];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i, mg_obj_for_link->mg_node_ptr->id, mg_obj_for_link->time);
01690              }  
01691              else{
01692                fprintf(fp, "out_%s_%s_%d -> client_%d [label=\"time=%f\"];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, i, mg_obj_for_link->mg_node_ptr->id, mg_obj_for_link->time);
01693 
01694              }
01695 
01696 
01697               client_comm_exists=1;
01698 
01699            }
01700            
01701          }
01702  
01703       }
01704       if(!client_comm_exists){
01705          fprintf(fp, "%s_%d -> client_%d [color=red];\n", mg_task_node->mapped_task_node->problem->name, i, i+1);
01706       }
01707 
01708  
01709      client_comm_exists=0;
01710      for(j=0;j<mg_task_node->nb_back_links;j++){
01711         mg_back_task_link= mg_task_node->mg_node_back_links[j];
01712         mg_obj_node=mg_back_task_link->mg_obj_node_ptr;
01713         mg_src_node=mg_obj_node->mg_obj_back_link->mg_node_ptr;
01714         if(mg_src_node->node_type==GS_SMART_MG_CLIENT_NODE){
01715           if((i-1)== (mg_src_node->id)){
01716             fprintf(fp, " in_%s_%s_%d [label=\"%s\\nbytes=%d\" ];\n", mg_obj_node->mapped_obj_node->name,   mg_task_node->mapped_task_node->problem->name, mg_src_node->id  , mg_obj_node->mapped_obj_node->name, mg_obj_node->mapped_obj_node->byte_size);
01717             fprintf(fp, "client_%d -> in_%s_%s_%d;\n", mg_src_node->id, mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, mg_src_node->id);
01718             for(k=0;k<mg_obj_node->nb_for_links;k++){
01719               mg_obj_for_link=mg_obj_node->mg_obj_for_links[k];
01720               if(mg_obj_for_link->already_on_server){
01721                 fprintf(fp, "in_%s_%s_%d -> %s_%d [color=red label=\"time=%f\"];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, mg_src_node->id , mg_obj_for_link->mg_node_ptr->mg_task_node->mapped_task_node->problem->name, mg_obj_for_link->mg_node_ptr->id,  mg_obj_for_link->time);
01722               }
01723               else{
01724                 fprintf(fp, "in_%s_%s_%d -> %s_%d [label=\"time=%f\"];\n", mg_obj_node->mapped_obj_node->name, mg_task_node->mapped_task_node->problem->name, mg_src_node->id , mg_obj_for_link->mg_node_ptr->mg_task_node->mapped_task_node->problem->name, mg_obj_for_link->mg_node_ptr->id,  mg_obj_for_link->time);
01725               }
01726               if(mg_obj_for_link->mg_node_ptr->id==i){
01727                 client_comm_exists=1;
01728               }
01729             } 
01730           } 
01731         }
01732       }
01733       
01734       if(!client_comm_exists){
01735          fprintf(fp, "client_%d -> %s_%d [color=red];\n", i-1, mg_task_node->mapped_task_node->problem->name, i);
01736       }
01737 
01738 
01739 
01740     }
01741   }
01742   fprintf(fp, "\n}\n");
01743   fclose(fp);
01744   // system("dot -Tjpg map_graph.dot -o /var/www/col/smartgridsolve/smart_graphs/map_graph.jpg");
01745   return 0;
01746 }
01747 
01748 
01749 
01750 
01751 
01752 #endif