Logo AND Algorithmique Numérique Distribuée

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