Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a new Infiniband network model, based on the works of Jerome Vienne.
[simgrid.git] / src / surf / network_ib.cpp
1 /* Copyright (c) 2014. 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 "network_ib.hpp"
8 #include "simgrid/sg_config.h"
9 #include "maxmin_private.hpp"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
12
13 double Bs=0.925;
14 double Be=0.965;
15 double ys=1.35;
16
17
18 static void IB_create_host_callback(sg_platf_host_cbarg_t t){
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==NULL)
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, t->id, act, NULL);
29  
30 }
31
32 static void IB_action_state_changed_callback(NetworkActionPtr action, e_surf_action_state_t statein, e_surf_action_state_t stateout){
33  if(statein!=SURF_ACTION_RUNNING|| stateout!=SURF_ACTION_DONE)
34     return;
35   std::pair<IBNode*,IBNode*> pair = ((NetworkIBModel*)surf_network_model)->active_comms[action];
36   XBT_VERB("action %p finished", action);
37  
38  ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, pair.first, pair.second, 1);
39
40   ((NetworkIBModel*)surf_network_model)->active_comms.erase(action);
41   
42 }
43
44
45 static void IB_action_init_callback(NetworkActionPtr action,RoutingEdgePtr src, RoutingEdgePtr dst, double size, double rate){
46   if(((NetworkIBModel*)surf_network_model)->active_nodes==NULL)
47     xbt_die("IB comm added, without any node connected !");
48   
49   IBNode* act_src= (IBNode*) xbt_dict_get_or_null(((NetworkIBModel*)surf_network_model)->active_nodes, src->getName());
50   if(act_src==NULL)
51     xbt_die("could not find src node active comms !");
52   //act_src->rate=rate;
53   
54   IBNode* act_dst= (IBNode*) xbt_dict_get_or_null(((NetworkIBModel*)surf_network_model)->active_nodes, dst->getName());
55   if(act_dst==NULL)
56     xbt_die("could not find dst node active comms !");  
57  // act_dst->rate=rate;
58   
59   ((NetworkIBModel*)surf_network_model)->active_comms[action]=make_pair<IBNode*,IBNode*>(act_src, act_dst);
60   //post the action in the second dist, to retrieve in the other callback
61   XBT_VERB("action %p init", action);
62
63   ((NetworkIBModel*)surf_network_model)->updateIBfactors(action, act_src, act_dst, 0);
64   
65 }
66
67
68
69 /*********
70  * Model *
71  *********/
72
73 /************************************************************************/
74 /* New model based on MPI contention model for Infiniband platforms */
75 /************************************************************************/
76 /* @Inproceedings{mescal_vienne_phd, */
77 /*  author={Jérôme Vienne}, */
78 /*  title={prédiction de performances d’applications de calcul haute performance sur réseau Infiniband}, */
79 /*  address={Grenoble FRANCE}, */
80 /*  month=june, */
81 /*  year={2010} */
82 /*  } */
83 void surf_network_model_init_IB(void)
84 {
85
86   if (surf_network_model)
87     return;
88   surf_network_model = new NetworkIBModel();
89   net_define_callbacks();
90   xbt_dynar_push(model_list, &surf_network_model);
91   surf_callback_connect(networkActionStateChangedCallbacks, IB_action_state_changed_callback);
92   surf_callback_connect(networkCommunicateCallbacks, IB_action_init_callback);
93
94   sg_platf_host_add_cb(IB_create_host_callback);
95   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
96 }
97
98 NetworkIBModel::NetworkIBModel()
99  : NetworkSmpiModel() {
100   m_haveGap=false;
101   active_nodes=NULL;
102 }
103
104 NetworkIBModel::~NetworkIBModel()
105 {
106   xbt_dict_cursor_t cursor = NULL;
107   IBNode* instance = NULL;
108   char *name = NULL;
109   xbt_dict_foreach(active_nodes, cursor, name, instance)
110     delete instance;
111   xbt_dict_free(&active_nodes);
112 }
113
114 void NetworkIBModel::computeIBfactors(IBNode *root) {
115   double penalized_bw=0.0;
116   double num_comm_out = (double) root->ActiveCommsUp.size();
117   double max_penalty_out=0.0;
118   //first, compute all outbound penalties to get their max
119   for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
120     double my_penalty_out = 1.0;
121
122     if(num_comm_out!=1){
123       if((*it)->destination->nbActiveCommsDown > 2)//number of comms sent to the receiving node
124         my_penalty_out = num_comm_out * Bs * ys;
125       else
126         my_penalty_out = num_comm_out * Bs;
127     }
128
129     max_penalty_out = max(max_penalty_out,my_penalty_out);
130   }
131
132   for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
133
134     //compute inbound penalty
135     double my_penalty_in = 1.0;
136     int nb_comms = (*it)->destination->nbActiveCommsDown;//total number of incoming comms
137     if(nb_comms!=1)
138       my_penalty_in = ((*it)->destination->ActiveCommsDown)[root] //number of comm sent to dest by root node
139                       * Be 
140                       * (*it)->destination->ActiveCommsDown.size();//number of different nodes sending to dest
141     
142     double penalty=max(my_penalty_in,max_penalty_out);
143     
144     double rate_before_update = (*it)->action->getBound();
145     //save initial rate of the action
146     if((*it)->init_rate==-1) 
147       (*it)->init_rate= rate_before_update;
148     
149     penalized_bw= ! num_comm_out ? (*it)->init_rate : (*it)->init_rate /penalty;
150     
151     if (!double_equals(penalized_bw, rate_before_update, sg_surf_precision)){
152      // XBT_VERB("%d->%d action %p penalty updated : in %f, out %f, final %f, before %f , initial rate %f", root->id,(*it)->destination->id,(*it)->action,my_penalty_in,my_penalty_out,penalized_bw, (*it)->action->getBound(), (*it)->init_rate );
153       lmm_update_variable_bound(p_maxminSystem, (*it)->action->getVariable(), penalized_bw);
154     }/*else{
155      // XBT_VERB("%d->%d action %p penalty not updated : in %f, out %f, final %f, initial rate %f", root->id,(*it)->destination->id,(*it)->action,my_penalty_in,my_penalty_out,penalized_bw, (*it)->init_rate );
156     }*/
157
158   }
159   XBT_VERB("Finished computing");
160 }
161
162 void NetworkIBModel::updateIBfactors_rec(IBNode *root, bool* updatedlist) {
163   if(updatedlist[root->id]==0){
164     XBT_VERB("Updating rec %d", root->id);
165     computeIBfactors(root);
166     updatedlist[root->id]=1;
167     for (std::vector<ActiveComm*>::iterator it= root->ActiveCommsUp.begin(); it != root->ActiveCommsUp.end(); ++it) {
168         if(updatedlist[(*it)->destination->id]!=1)
169           updateIBfactors_rec((*it)->destination, updatedlist);
170     }
171     for (std::map<IBNode*, int>::iterator it= root->ActiveCommsDown.begin(); it != root->ActiveCommsDown.end(); ++it) {
172         if(updatedlist[it->first->id]!=1)
173           updateIBfactors_rec(it->first, updatedlist);
174     }
175   }
176 }
177
178
179 void NetworkIBModel::updateIBfactors(NetworkActionPtr action, IBNode *from, IBNode * to, int remove) {
180   if (from == to)//disregard local comms (should use loopback)
181     return;
182   
183   bool* updated=(bool*)xbt_malloc0(xbt_dict_size(active_nodes)*sizeof(bool));
184   ActiveComm* comm=NULL;
185   if(remove){
186     if(to->ActiveCommsDown[from]==1)
187       to->ActiveCommsDown.erase(from);
188     else
189       to->ActiveCommsDown[from]-=1;
190
191     to->nbActiveCommsDown--;
192     for (std::vector<ActiveComm*>::iterator it= from->ActiveCommsUp.begin(); 
193          it != from->ActiveCommsUp.end(); ++it) {
194       XBT_VERB("inside vector");
195       if((*it)->action==action){
196         comm=(*it);
197         from->ActiveCommsUp.erase(it);
198         XBT_VERB("action deleted");
199         break;
200       }
201     }
202     action->unref();
203     /*from->ActiveCommsUp.erase(
204         std::remove(from->ActiveCommsUp.begin(),
205             from->ActiveCommsUp.end(), make_pair(to, action)),
206             from->ActiveCommsUp.end());*/
207
208   }else{
209     //from->ActiveCommsUp.push_back(std::make_pair(to, action));
210     action->ref();
211
212     ActiveComm* comm=new ActiveComm();
213     comm->action=action;
214     comm->destination=to;
215     from->ActiveCommsUp.push_back(comm);
216
217     to->ActiveCommsDown[from]+=1;
218     to->nbActiveCommsDown++;
219   }
220   XBT_VERB("Updating %d", from->id);
221   updateIBfactors_rec(from, updated);
222   XBT_VERB("Finished updating %d", from->id);
223   if(comm)delete comm;
224   xbt_free(updated);
225 }