Logo AND Algorithmique Numérique Distribuée

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