Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the Link signals to s4u
[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 <utility>
8
9 #include "simgrid/sg_config.h"
10 #include "src/surf/HostImpl.hpp"
11 #include "src/surf/maxmin_private.hpp"
12 #include "src/surf/network_ib.hpp"
13 #include "src/surf/xml/platf.hpp"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
16
17 static void IB_create_host_callback(simgrid::s4u::Host& host){
18   using simgrid::surf::NetworkIBModel;
19   using simgrid::surf::IBNode;
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==nullptr)
24     ((NetworkIBModel*)surf_network_model)->active_nodes = xbt_dict_new_homogeneous(nullptr);
25
26   IBNode* act = new IBNode(id);
27
28   id++;
29   xbt_dict_set(((NetworkIBModel*)surf_network_model)->active_nodes, host.cname(), act, nullptr);
30 }
31
32 static void IB_action_state_changed_callback(simgrid::surf::NetworkAction* action)
33 {
34   using simgrid::surf::NetworkIBModel;
35   using simgrid::surf::IBNode;
36
37   if (action->getState() != simgrid::surf::Action::State::done)
38     return;
39   std::pair<IBNode*,IBNode*> pair = ((NetworkIBModel*)surf_network_model)->active_comms[action];
40   XBT_DEBUG("IB callback - action %p finished", action);
41
42   ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, pair.first, pair.second, 1);
43
44   ((NetworkIBModel*)surf_network_model)->active_comms.erase(action);
45
46 }
47
48 static void IB_action_init_callback(simgrid::surf::NetworkAction* action, simgrid::s4u::Host* src,
49                                     simgrid::s4u::Host* dst)
50 {
51   simgrid::surf::NetworkIBModel* ibModel = (simgrid::surf::NetworkIBModel*)surf_network_model;
52
53   simgrid::surf::IBNode* act_src = (simgrid::surf::IBNode*)xbt_dict_get_or_null(ibModel->active_nodes, src->cname());
54   xbt_assert(act_src, "could not find src node active comms !");
55
56   simgrid::surf::IBNode* act_dst = (simgrid::surf::IBNode*)xbt_dict_get_or_null(ibModel->active_nodes, dst->cname());
57   xbt_assert(act_dst, "could not find dst node active comms !");
58
59   ibModel->active_comms[action]=std::make_pair(act_src, act_dst);
60
61   ibModel->updateIBfactors(action, act_src, act_dst, 0);
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()
79 {
80   if (surf_network_model)
81     return;
82
83   surf_network_model = new simgrid::surf::NetworkIBModel();
84   all_existing_models->push_back(surf_network_model);
85   simgrid::s4u::Link::onCommunicationStateChange.connect(IB_action_state_changed_callback);
86   simgrid::s4u::Link::onCommunicate.connect(IB_action_init_callback);
87   simgrid::s4u::Host::onCreation.connect(IB_create_host_callback);
88   xbt_cfg_setdefault_double("network/weight-S", 8775);
89
90 }
91
92 namespace simgrid {
93   namespace surf {
94
95     NetworkIBModel::NetworkIBModel()
96     : NetworkSmpiModel() {
97       haveGap_=false;
98       active_nodes=nullptr;
99
100       const char* IB_factors_string=xbt_cfg_get_string("smpi/IB-penalty-factors");
101       xbt_dynar_t radical_elements = xbt_str_split(IB_factors_string, ";");
102
103       surf_parse_assert(xbt_dynar_length(radical_elements)==3,
104           "smpi/IB-penalty-factors should be provided and contain 3 elements, semi-colon separated. Example: 0.965;0.925;1.35");
105
106       Be = xbt_str_parse_double(xbt_dynar_get_as(radical_elements, 0, char *), "First part of smpi/IB-penalty-factors is not numerical: %s");
107       Bs = xbt_str_parse_double(xbt_dynar_get_as(radical_elements, 1, char *), "Second part of smpi/IB-penalty-factors is not numerical: %s");
108       ys = xbt_str_parse_double(xbt_dynar_get_as(radical_elements, 2, char *), "Third part of smpi/IB-penalty-factors is not numerical: %s");
109
110       xbt_dynar_free(&radical_elements);
111     }
112
113     NetworkIBModel::~NetworkIBModel()
114     {
115       xbt_dict_cursor_t cursor = nullptr;
116       IBNode* instance = nullptr;
117       char *name = nullptr;
118       xbt_dict_foreach(active_nodes, cursor, name, instance)
119       delete instance;
120       xbt_dict_free(&active_nodes);
121     }
122
123     void NetworkIBModel::computeIBfactors(IBNode *root) {
124       double penalized_bw=0.0;
125       double num_comm_out = (double) root->ActiveCommsUp.size();
126       double max_penalty_out=0.0;
127       //first, compute all outbound penalties to get their max
128       for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
129         double my_penalty_out = 1.0;
130
131         if(num_comm_out!=1){
132           if((*it)->destination->nbActiveCommsDown > 2)//number of comms sent to the receiving node
133             my_penalty_out = num_comm_out * Bs * ys;
134           else
135             my_penalty_out = num_comm_out * Bs;
136         }
137
138         max_penalty_out = std::max(max_penalty_out,my_penalty_out);
139       }
140
141       for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
142
143         //compute inbound penalty
144         double my_penalty_in = 1.0;
145         int nb_comms = (*it)->destination->nbActiveCommsDown;//total number of incoming comms
146         if(nb_comms!=1)
147           my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] //number of comm sent to dest by root node
148                                                                 * Be
149                                                                 * (*it)->destination->ActiveCommsDown.size();//number of different nodes sending to dest
150
151         double penalty = std::max(my_penalty_in,max_penalty_out);
152
153         double rate_before_update = (*it)->action->getBound();
154         //save initial rate of the action
155         if((*it)->init_rate==-1)
156           (*it)->init_rate= rate_before_update;
157
158         penalized_bw= ! num_comm_out ? (*it)->init_rate : (*it)->init_rate /penalty;
159
160         if (!double_equals(penalized_bw, rate_before_update, sg_surf_precision)){
161           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 );
162           lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), penalized_bw);
163         }else{
164           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 );
165         }
166
167       }
168       XBT_DEBUG("Finished computing IB penalties");
169     }
170
171     void NetworkIBModel::updateIBfactors_rec(IBNode *root, bool* updatedlist) {
172       if(updatedlist[root->id]==0){
173         XBT_DEBUG("IB - Updating rec %d", root->id);
174         computeIBfactors(root);
175         updatedlist[root->id]=1;
176         for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
177           if(updatedlist[(*it)->destination->id]!=1)
178             updateIBfactors_rec((*it)->destination, updatedlist);
179         }
180         for (std::map<IBNode*, int>::iterator it= root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
181           if(updatedlist[it->first->id]!=1)
182             updateIBfactors_rec(it->first, updatedlist);
183         }
184       }
185     }
186
187
188     void NetworkIBModel::updateIBfactors(NetworkAction *action, IBNode *from, IBNode * to, int remove) {
189       if (from == to)//disregard local comms (should use loopback)
190         return;
191
192       bool* updated=(bool*)xbt_malloc0(xbt_dict_size(active_nodes)*sizeof(bool));
193       ActiveComm* comm=nullptr;
194       if(remove){
195         if(to->ActiveCommsDown[from]==1)
196           to->ActiveCommsDown.erase(from);
197         else
198           to->ActiveCommsDown[from]-=1;
199
200         to->nbActiveCommsDown--;
201         for (std::vector<ActiveComm*>::iterator it= from->ActiveCommsUp.begin();
202             it != from->ActiveCommsUp.end(); ++it) {
203           if((*it)->action==action){
204             comm=(*it);
205             from->ActiveCommsUp.erase(it);
206             break;
207           }
208         }
209         action->unref();
210
211       }else{
212         action->ref();
213         ActiveComm* comm=new ActiveComm();
214         comm->action=action;
215         comm->destination=to;
216         from->ActiveCommsUp.push_back(comm);
217
218         to->ActiveCommsDown[from]+=1;
219         to->nbActiveCommsDown++;
220       }
221       XBT_DEBUG("IB - Updating %d", from->id);
222       updateIBfactors_rec(from, updated);
223       XBT_DEBUG("IB - Finished updating %d", from->id);
224       if(comm)
225         delete comm;
226       xbt_free(updated);
227     }
228
229   }
230 }