Logo AND Algorithmique Numérique Distribuée

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