Logo AND Algorithmique Numérique Distribuée

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