Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #193 from Takishipp/signals
[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 #include <boost/algorithm/string/classification.hpp>
15 #include <boost/algorithm/string/split.hpp>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
18
19 static void IB_create_host_callback(simgrid::s4u::Host& host){
20   using simgrid::surf::NetworkIBModel;
21   using simgrid::surf::IBNode;
22
23   static int id=0;
24   // pour t->id -> rajouter une nouvelle struct dans le dict, pour stocker les comms actives
25   if(((NetworkIBModel*)surf_network_model)->active_nodes==nullptr)
26     ((NetworkIBModel*)surf_network_model)->active_nodes = xbt_dict_new_homogeneous(nullptr);
27
28   IBNode* act = new IBNode(id);
29
30   id++;
31   xbt_dict_set(((NetworkIBModel*)surf_network_model)->active_nodes, host.cname(), act, nullptr);
32 }
33
34 static void IB_action_state_changed_callback(simgrid::surf::NetworkAction* action)
35 {
36   using simgrid::surf::NetworkIBModel;
37   using simgrid::surf::IBNode;
38
39   if (action->getState() != simgrid::surf::Action::State::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 static void IB_action_init_callback(simgrid::surf::NetworkAction* action, simgrid::s4u::Host* src,
51                                     simgrid::s4u::Host* dst)
52 {
53   simgrid::surf::NetworkIBModel* ibModel = (simgrid::surf::NetworkIBModel*)surf_network_model;
54
55   simgrid::surf::IBNode* act_src = (simgrid::surf::IBNode*)xbt_dict_get_or_null(ibModel->active_nodes, src->cname());
56   xbt_assert(act_src, "could not find src node active comms !");
57
58   simgrid::surf::IBNode* act_dst = (simgrid::surf::IBNode*)xbt_dict_get_or_null(ibModel->active_nodes, dst->cname());
59   xbt_assert(act_dst, "could not find dst node active comms !");
60
61   ibModel->active_comms[action]=std::make_pair(act_src, act_dst);
62
63   ibModel->updateIBfactors(action, act_src, act_dst, 0);
64 }
65
66 /*********
67  * Model *
68  *********/
69
70 /************************************************************************/
71 /* New model based on MPI contention model for Infiniband platforms */
72 /************************************************************************/
73 /* @Inproceedings{mescal_vienne_phd, */
74 /*  author={Jérôme Vienne}, */
75 /*  title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
76 /*  address={Grenoble FRANCE}, */
77 /*  month=june, */
78 /*  year={2010} */
79 /*  } */
80 void surf_network_model_init_IB()
81 {
82   if (surf_network_model)
83     return;
84
85   surf_network_model = new simgrid::surf::NetworkIBModel();
86   all_existing_models->push_back(surf_network_model);
87   simgrid::s4u::Link::onCommunicationStateChange.connect(IB_action_state_changed_callback);
88   simgrid::s4u::Link::onCommunicate.connect(IB_action_init_callback);
89   simgrid::s4u::Host::onCreation.connect(IB_create_host_callback);
90   xbt_cfg_setdefault_double("network/weight-S", 8775);
91
92 }
93
94 namespace simgrid {
95   namespace surf {
96
97     NetworkIBModel::NetworkIBModel()
98     : NetworkSmpiModel() {
99       haveGap_=false;
100       active_nodes=nullptr;
101
102       const char* IB_factors_string=xbt_cfg_get_string("smpi/IB-penalty-factors");
103       std::vector<std::string> radical_elements;
104       boost::split(radical_elements, IB_factors_string, boost::is_any_of(";"));
105
106       surf_parse_assert(radical_elements.size() == 3, "smpi/IB-penalty-factors should be provided and contain 3 "
107                                                       "elements, semi-colon separated. Example: 0.965;0.925;1.35");
108
109       Be = xbt_str_parse_double(radical_elements.front().c_str(),
110                                 "First part of smpi/IB-penalty-factors is not numerical: %s");
111       Bs = xbt_str_parse_double((radical_elements.at(1)).c_str(),
112                                 "Second part of smpi/IB-penalty-factors is not numerical: %s");
113       ys = xbt_str_parse_double(radical_elements.back().c_str(),
114                                 "Third part of smpi/IB-penalty-factors is not numerical: %s");
115     }
116
117     NetworkIBModel::~NetworkIBModel()
118     {
119       xbt_dict_cursor_t cursor = nullptr;
120       IBNode* instance = nullptr;
121       char *name = nullptr;
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 num_comm_out = (double) root->ActiveCommsUp.size();
129       double max_penalty_out=0.0;
130       //first, compute all outbound penalties to get their max
131       for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
132         double my_penalty_out = 1.0;
133
134         if(num_comm_out!=1){
135           if((*it)->destination->nbActiveCommsDown > 2)//number of comms sent to the receiving node
136             my_penalty_out = num_comm_out * Bs * ys;
137           else
138             my_penalty_out = num_comm_out * Bs;
139         }
140
141         max_penalty_out = std::max(max_penalty_out,my_penalty_out);
142       }
143
144       for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
145
146         //compute inbound penalty
147         double my_penalty_in = 1.0;
148         int nb_comms = (*it)->destination->nbActiveCommsDown;//total number of incoming comms
149         if(nb_comms!=1)
150           my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] //number of comm sent to dest by root node
151                                                                 * Be
152                                                                 * (*it)->destination->ActiveCommsDown.size();//number of different nodes sending to dest
153
154         double penalty = std::max(my_penalty_in,max_penalty_out);
155
156         double rate_before_update = (*it)->action->getBound();
157         //save initial rate of the action
158         if((*it)->init_rate==-1)
159           (*it)->init_rate= rate_before_update;
160
161         double penalized_bw = num_comm_out ? (*it)->init_rate / penalty : (*it)->init_rate;
162
163         if (not double_equals(penalized_bw, rate_before_update, sg_surf_precision)) {
164           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 );
165           lmm_update_variable_bound(maxminSystem_, (*it)->action->getVariable(), penalized_bw);
166         }else{
167           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 );
168         }
169
170       }
171       XBT_DEBUG("Finished computing IB penalties");
172     }
173
174     void NetworkIBModel::updateIBfactors_rec(IBNode *root, bool* updatedlist) {
175       if(updatedlist[root->id]==0){
176         XBT_DEBUG("IB - Updating rec %d", root->id);
177         computeIBfactors(root);
178         updatedlist[root->id]=1;
179         for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
180           if(updatedlist[(*it)->destination->id]!=1)
181             updateIBfactors_rec((*it)->destination, updatedlist);
182         }
183         for (std::map<IBNode*, int>::iterator it= root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
184           if(updatedlist[it->first->id]!=1)
185             updateIBfactors_rec(it->first, updatedlist);
186         }
187       }
188     }
189
190
191     void NetworkIBModel::updateIBfactors(NetworkAction *action, IBNode *from, IBNode * to, int remove) {
192       if (from == to)//disregard local comms (should use loopback)
193         return;
194
195       bool* updated=(bool*)xbt_malloc0(xbt_dict_size(active_nodes)*sizeof(bool));
196       ActiveComm* comm=nullptr;
197       if(remove){
198         if(to->ActiveCommsDown[from]==1)
199           to->ActiveCommsDown.erase(from);
200         else
201           to->ActiveCommsDown[from]-=1;
202
203         to->nbActiveCommsDown--;
204         for (std::vector<ActiveComm*>::iterator it= from->ActiveCommsUp.begin();
205             it != from->ActiveCommsUp.end(); ++it) {
206           if((*it)->action==action){
207             comm=(*it);
208             from->ActiveCommsUp.erase(it);
209             break;
210           }
211         }
212         action->unref();
213
214       }else{
215         action->ref();
216         ActiveComm* comm=new ActiveComm();
217         comm->action=action;
218         comm->destination=to;
219         from->ActiveCommsUp.push_back(comm);
220
221         to->ActiveCommsDown[from]+=1;
222         to->nbActiveCommsDown++;
223       }
224       XBT_DEBUG("IB - Updating %d", from->id);
225       updateIBfactors_rec(from, updated);
226       XBT_DEBUG("IB - Finished updating %d", from->id);
227       if(comm)
228         delete comm;
229       xbt_free(updated);
230     }
231
232   }
233 }