Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cecdfc8d7e4bf5bde244896ea80966a6ad74c207
[simgrid.git] / src / surf / network_ib.cpp
1 /* Copyright (c) 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "network_ib.hpp"
8 #include "simgrid/sg_config.h"
9 #include "maxmin_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
12
13 static void IB_create_host_callback(sg_platf_host_cbarg_t t){
14   
15   static int id=0;
16 // pour t->id -> rajouter une nouvelle struct dans le dict, pour stocker les comms actives
17   if(((NetworkIBModel*)surf_network_model)->active_nodes==NULL)
18     ((NetworkIBModel*)surf_network_model)->active_nodes=xbt_dict_new();
19   
20   IBNode* act = new IBNode(id);
21
22   id++;
23   xbt_dict_set(((NetworkIBModel*)surf_network_model)->active_nodes, t->id, act, NULL);
24  
25 }
26
27 static void IB_action_state_changed_callback(NetworkAction *action, e_surf_action_state_t statein, e_surf_action_state_t stateout){
28  if(statein!=SURF_ACTION_RUNNING|| stateout!=SURF_ACTION_DONE)
29     return;
30   std::pair<IBNode*,IBNode*> pair = ((NetworkIBModel*)surf_network_model)->active_comms[action];
31   XBT_DEBUG("IB callback - action %p finished", action);
32  
33  ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, pair.first, pair.second, 1);
34
35   ((NetworkIBModel*)surf_network_model)->active_comms.erase(action);
36   
37 }
38
39
40 static void IB_action_init_callback(NetworkAction *action,RoutingEdge *src, RoutingEdge *dst, double size, double rate){
41   if(((NetworkIBModel*)surf_network_model)->active_nodes==NULL)
42     xbt_die("IB comm added, without any node connected !");
43   
44   IBNode* act_src= (IBNode*) xbt_dict_get_or_null(((NetworkIBModel*)surf_network_model)->active_nodes, src->getName());
45   if(act_src==NULL)
46     xbt_die("could not find src node active comms !");
47   //act_src->rate=rate;
48   
49   IBNode* act_dst= (IBNode*) xbt_dict_get_or_null(((NetworkIBModel*)surf_network_model)->active_nodes, dst->getName());
50   if(act_dst==NULL)
51     xbt_die("could not find dst node active comms !");  
52  // act_dst->rate=rate;
53   
54   ((NetworkIBModel*)surf_network_model)->active_comms[action]=make_pair(act_src, act_dst);
55   //post the action in the second dist, to retrieve in the other callback
56   XBT_DEBUG("IB callback - action %p init", action);
57
58   ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, act_src, act_dst, 0);
59   
60 }
61
62
63
64 /*********
65  * Model *
66  *********/
67
68 /************************************************************************/
69 /* New model based on MPI contention model for Infiniband platforms */
70 /************************************************************************/
71 /* @Inproceedings{mescal_vienne_phd, */
72 /*  author={Jérôme Vienne}, */
73 /*  title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
74 /*  address={Grenoble FRANCE}, */
75 /*  month=june, */
76 /*  year={2010} */
77 /*  } */
78 void surf_network_model_init_IB(void)
79 {
80
81   if (surf_network_model)
82     return;
83   surf_network_model = new NetworkIBModel();
84   net_define_callbacks();
85   xbt_dynar_push(model_list, &surf_network_model);
86   surf_callback_connect(networkActionStateChangedCallbacks, IB_action_state_changed_callback);
87   surf_callback_connect(networkCommunicateCallbacks, IB_action_init_callback);
88
89   sg_platf_host_add_cb(IB_create_host_callback);
90   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
91   
92 }
93
94 NetworkIBModel::NetworkIBModel()
95  : NetworkSmpiModel() {
96   m_haveGap=false;
97   active_nodes=NULL;
98     
99   const char* IB_factors_string=sg_cfg_get_string("smpi/IB_penalty_factors");
100   xbt_dynar_t radical_elements = xbt_str_split(IB_factors_string, ";");
101   
102   if(xbt_dynar_length(radical_elements)!=3)
103     surf_parse_error("smpi/IB_penalty_factors should be provided and contain 3 elements, semi-colon separated : for example 0.965;0.925;1.35");
104   
105   Be = atof(xbt_dynar_get_as(radical_elements, 0, char *));
106   Bs = atof(xbt_dynar_get_as(radical_elements, 1, char *));
107   ys = atof(xbt_dynar_get_as(radical_elements, 2, char *));
108
109   xbt_dynar_free(&radical_elements);
110 }
111
112 NetworkIBModel::~NetworkIBModel()
113 {
114   xbt_dict_cursor_t cursor = NULL;
115   IBNode* instance = NULL;
116   char *name = NULL;
117   xbt_dict_foreach(active_nodes, cursor, name, instance)
118     delete instance;
119   xbt_dict_free(&active_nodes);
120 }
121
122 void NetworkIBModel::computeIBfactors(IBNode *root) {
123   double penalized_bw=0.0;
124   double num_comm_out = (double) root->ActiveCommsUp.size();
125   double max_penalty_out=0.0;
126   //first, compute all outbound penalties to get their max
127   for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
128     double my_penalty_out = 1.0;
129
130     if(num_comm_out!=1){
131       if((*it)->destination->nbActiveCommsDown > 2)//number of comms sent to the receiving node
132         my_penalty_out = num_comm_out * Bs * ys;
133       else
134         my_penalty_out = num_comm_out * Bs;
135     }
136
137     max_penalty_out = max(max_penalty_out,my_penalty_out);
138   }
139
140   for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
141
142     //compute inbound penalty
143     double my_penalty_in = 1.0;
144     int nb_comms = (*it)->destination->nbActiveCommsDown;//total number of incoming comms
145     if(nb_comms!=1)
146       my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] //number of comm sent to dest by root node
147                       * Be 
148                       * (*it)->destination->ActiveCommsDown.size();//number of different nodes sending to dest
149     
150     double penalty=max(my_penalty_in,max_penalty_out);
151     
152     double rate_before_update = (*it)->action->getBound();
153     //save initial rate of the action
154     if((*it)->init_rate==-1) 
155       (*it)->init_rate= rate_before_update;
156     
157     penalized_bw= ! num_comm_out ? (*it)->init_rate : (*it)->init_rate /penalty;
158     
159     if (!double_equals(penalized_bw, rate_before_update, sg_surf_precision)){
160       XBT_DEBUG("%d->%d action %p penalty updated : bw now %f, before %f , initial rate %f", root->id,(*it)->destination->id,(*it)->action,penalized_bw, (*it)->action->getBound(), (*it)->init_rate );
161       lmm_update_variable_bound(p_maxminSystem, (*it)->action->getVariable(), penalized_bw);
162     }else{
163       XBT_DEBUG("%d->%d action %p penalty not updated : bw %f, initial rate %f", root->id,(*it)->destination->id,(*it)->action,penalized_bw, (*it)->init_rate );
164     }
165
166   }
167   XBT_DEBUG("Finished computing IB penalties");
168 }
169
170 void NetworkIBModel::updateIBfactors_rec(IBNode *root, bool* updatedlist) {
171   if(updatedlist[root->id]==0){
172     XBT_DEBUG("IB - Updating rec %d", root->id);
173     computeIBfactors(root);
174     updatedlist[root->id]=1;
175     for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
176         if(updatedlist[(*it)->destination->id]!=1)
177           updateIBfactors_rec((*it)->destination, updatedlist);
178     }
179     for (std::map<IBNode*, int>::iterator it= root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
180         if(updatedlist[it->first->id]!=1)
181           updateIBfactors_rec(it->first, updatedlist);
182     }
183   }
184 }
185
186
187 void NetworkIBModel::updateIBfactors(NetworkAction *action, IBNode *from, IBNode * to, int remove) {
188   if (from == to)//disregard local comms (should use loopback)
189     return;
190   
191   bool* updated=(bool*)xbt_malloc0(xbt_dict_size(active_nodes)*sizeof(bool));
192   ActiveComm* comm=NULL;
193   if(remove){
194     if(to->ActiveCommsDown[from]==1)
195       to->ActiveCommsDown.erase(from);
196     else
197       to->ActiveCommsDown[from]-=1;
198
199     to->nbActiveCommsDown--;
200     for (std::vector<ActiveComm*>::iterator it= from->ActiveCommsUp.begin(); 
201          it != from->ActiveCommsUp.end(); ++it) {
202       if((*it)->action==action){
203         comm=(*it);
204         from->ActiveCommsUp.erase(it);
205         break;
206       }
207     }
208     action->unref();
209
210   }else{
211     action->ref();
212     ActiveComm* comm=new ActiveComm();
213     comm->action=action;
214     comm->destination=to;
215     from->ActiveCommsUp.push_back(comm);
216
217     to->ActiveCommsDown[from]+=1;
218     to->nbActiveCommsDown++;
219   }
220   XBT_DEBUG("IB - Updating %d", from->id);
221   updateIBfactors_rec(from, updated);
222   XBT_DEBUG("IB - Finished updating %d", from->id);
223   if(comm)
224     delete comm;
225   xbt_free(updated);
226 }