Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix issue with surf.h included in simix.h
[simgrid.git] / src / surf / network_smpi.cpp
1 #include "network_smpi.hpp"
2 #include "simgrid/sg_config.h"
3
4 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
5
6 xbt_dynar_t smpi_bw_factor = NULL;
7 xbt_dynar_t smpi_lat_factor = NULL;
8
9 typedef struct s_smpi_factor *smpi_factor_t;
10 typedef struct s_smpi_factor {
11   long factor;
12   double value;
13 } s_smpi_factor_t;
14
15 xbt_dict_t gap_lookup = NULL;
16
17 static int factor_cmp(const void *pa, const void *pb)
18 {
19  return (((s_smpi_factor_t*)pa)->factor > ((s_smpi_factor_t*)pb)->factor) ? 1 :
20          (((s_smpi_factor_t*)pa)->factor < ((s_smpi_factor_t*)pb)->factor) ? -1 : 0;
21 }
22
23
24 static xbt_dynar_t parse_factor(const char *smpi_coef_string)
25 {
26   char *value = NULL;
27   unsigned int iter = 0;
28   s_smpi_factor_t fact;
29   xbt_dynar_t smpi_factor, radical_elements, radical_elements2 = NULL;
30
31   smpi_factor = xbt_dynar_new(sizeof(s_smpi_factor_t), NULL);
32   radical_elements = xbt_str_split(smpi_coef_string, ";");
33   xbt_dynar_foreach(radical_elements, iter, value) {
34
35     radical_elements2 = xbt_str_split(value, ":");
36     if (xbt_dynar_length(radical_elements2) != 2)
37       xbt_die("Malformed radical for smpi factor!");
38     fact.factor = atol(xbt_dynar_get_as(radical_elements2, 0, char *));
39     fact.value = atof(xbt_dynar_get_as(radical_elements2, 1, char *));
40     xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
41     XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
42     xbt_dynar_free(&radical_elements2);
43   }
44   xbt_dynar_free(&radical_elements);
45   iter=0;
46   xbt_dynar_sort(smpi_factor, &factor_cmp);
47   xbt_dynar_foreach(smpi_factor, iter, fact) {
48     XBT_DEBUG("ordered smpi_factor:\t%ld : %f", fact.factor, fact.value);
49
50   }
51   return smpi_factor;
52 }
53
54 /*********
55  * Model *
56  *********/
57
58 /************************************************************************/
59 /* New model based on LV08 and experimental results of MPI ping-pongs   */
60 /************************************************************************/
61 /* @Inproceedings{smpi_ipdps, */
62 /*  author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
63 /*  title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
64 /*  booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
65 /*  address={Anchorage (Alaska) USA}, */
66 /*  month=may, */
67 /*  year={2011} */
68 /*  } */
69 void surf_network_model_init_SMPI(void)
70 {
71
72   if (surf_network_model)
73     return;
74   surf_network_model = new NetworkSmpiModel();
75   net_define_callbacks();
76   xbt_dynar_push(model_list, &surf_network_model);
77   //network_solve = lmm_solve;
78
79   xbt_cfg_setdefault_double(_sg_cfg_set, "network/sender_gap", 10e-6);
80   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
81 }
82
83 NetworkSmpiModel::NetworkSmpiModel()
84  : NetworkCm02Model() {
85         m_haveGap=true;
86 }
87
88 NetworkSmpiModel::~NetworkSmpiModel(){
89   if (gap_lookup) {
90     xbt_dict_free(&gap_lookup);
91   }
92 }
93
94 void NetworkSmpiModel::gapAppend(double size, const NetworkCm02LinkLmmPtr link, NetworkCm02ActionLmmPtr action)
95 {
96   const char *src = link->m_name;
97   xbt_fifo_t fifo;
98   //surf_action_network_CM02_t last_action;
99   //double bw;
100
101   if (sg_sender_gap > 0.0) {
102     if (!gap_lookup) {
103       gap_lookup = xbt_dict_new_homogeneous(NULL);
104     }
105     fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
106     action->m_senderGap = 0.0;
107     if (fifo && xbt_fifo_size(fifo) > 0) {
108       /* Compute gap from last send */
109       /*last_action =
110           (surf_action_network_CM02_t)
111           xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));*/
112      // bw = net_get_link_bandwidth(link);
113       action->m_senderGap = sg_sender_gap;
114         /*  max(sg_sender_gap,last_action->sender.size / bw);*/
115       action->m_latency += action->m_senderGap;
116     }
117     /* Append action as last send */
118     /*action->sender.link_name = link->lmm_resource.generic_resource.name;
119     fifo =
120         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
121                                           action->sender.link_name);
122     if (!fifo) {
123       fifo = xbt_fifo_new();
124       xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
125     }
126     action->sender.fifo_item = xbt_fifo_push(fifo, action);*/
127     action->m_senderSize = size;
128   }
129 }
130
131 void NetworkSmpiModel::gapRemove(ActionLmmPtr lmm_action)
132 {
133   xbt_fifo_t fifo;
134   size_t size;
135   NetworkCm02ActionLmmPtr action = (NetworkCm02ActionLmmPtr)(lmm_action);
136
137   if (sg_sender_gap > 0.0 && action->p_senderLinkName
138       && action->p_senderFifoItem) {
139     fifo =
140         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
141                                           action->p_senderLinkName);
142     xbt_fifo_remove_item(fifo, action->p_senderFifoItem);
143     size = xbt_fifo_size(fifo);
144     if (size == 0) {
145       xbt_fifo_free(fifo);
146       xbt_dict_remove(gap_lookup, action->p_senderLinkName);
147       size = xbt_dict_length(gap_lookup);
148       if (size == 0) {
149         xbt_dict_free(&gap_lookup);
150       }
151     }
152   }
153 }
154
155 double NetworkSmpiModel::bandwidthFactor(double size)
156 {
157   if (!smpi_bw_factor)
158     smpi_bw_factor =
159         parse_factor(sg_cfg_get_string("smpi/bw_factor"));
160
161   unsigned int iter = 0;
162   s_smpi_factor_t fact;
163   double current=1.0;
164   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
165     if (size <= fact.factor) {
166       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
167       return current;
168     }else
169       current=fact.value;
170   }
171   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
172
173   return current;
174 }
175 double NetworkSmpiModel::latencyFactor(double size)
176 {
177   if (!smpi_lat_factor)
178     smpi_lat_factor =
179         parse_factor(sg_cfg_get_string("smpi/lat_factor"));
180
181   unsigned int iter = 0;
182   s_smpi_factor_t fact;
183   double current=1.0;
184   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
185     if (size <= fact.factor) {
186       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
187       xbt_dynar_free(&smpi_lat_factor);
188       return current;
189     }else
190       current=fact.value;
191   }
192   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
193   xbt_dynar_free(&smpi_lat_factor);
194   return current;
195 }
196
197 double NetworkSmpiModel::bandwidthConstraint(double rate, double bound, double size)
198 {
199   return rate < 0 ? bound : min(bound, rate * bandwidthFactor(size));
200 }
201
202 /************
203  * Resource *
204  ************/
205
206
207
208 /**********
209  * Action *
210  **********/