Logo AND Algorithmique Numérique Distribuée

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