Logo AND Algorithmique Numérique Distribuée

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