Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix memleak
[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 void NetworkSmpiModel::gapAppend(double size, const NetworkCm02LinkLmmPtr link, NetworkCm02ActionLmmPtr action)
84 {
85   const char *src = link->m_name;
86   xbt_fifo_t fifo;
87   //surf_action_network_CM02_t last_action;
88   //double bw;
89
90   if (sg_sender_gap > 0.0) {
91     if (!gap_lookup) {
92       gap_lookup = xbt_dict_new_homogeneous(NULL);
93     }
94     fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
95     action->m_senderGap = 0.0;
96     if (fifo && xbt_fifo_size(fifo) > 0) {
97       /* Compute gap from last send */
98       /*last_action =
99           (surf_action_network_CM02_t)
100           xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));*/
101      // bw = net_get_link_bandwidth(link);
102       action->m_senderGap = sg_sender_gap;
103         /*  max(sg_sender_gap,last_action->sender.size / bw);*/
104       action->m_latency += action->m_senderGap;
105     }
106     /* Append action as last send */
107     /*action->sender.link_name = link->lmm_resource.generic_resource.name;
108     fifo =
109         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
110                                           action->sender.link_name);
111     if (!fifo) {
112       fifo = xbt_fifo_new();
113       xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
114     }
115     action->sender.fifo_item = xbt_fifo_push(fifo, action);*/
116     action->m_senderSize = size;
117   }
118 }
119
120 void NetworkSmpiModel::gapRemove(ActionLmmPtr lmm_action)
121 {
122   xbt_fifo_t fifo;
123   size_t size;
124   NetworkCm02ActionLmmPtr action = (NetworkCm02ActionLmmPtr)(lmm_action);
125
126   if (sg_sender_gap > 0.0 && action->p_senderLinkName
127       && action->p_senderFifoItem) {
128     fifo =
129         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
130                                           action->p_senderLinkName);
131     xbt_fifo_remove_item(fifo, action->p_senderFifoItem);
132     size = xbt_fifo_size(fifo);
133     if (size == 0) {
134       xbt_fifo_free(fifo);
135       xbt_dict_remove(gap_lookup, action->p_senderLinkName);
136       size = xbt_dict_length(gap_lookup);
137       if (size == 0) {
138         xbt_dict_free(&gap_lookup);
139       }
140     }
141   }
142 }
143
144 double NetworkSmpiModel::bandwidthFactor(double size)
145 {
146   if (!smpi_bw_factor)
147     smpi_bw_factor =
148         parse_factor(sg_cfg_get_string("smpi/bw_factor"));
149
150   unsigned int iter = 0;
151   s_smpi_factor_t fact;
152   double current=1.0;
153   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
154     if (size <= fact.factor) {
155       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
156       return current;
157     }else
158       current=fact.value;
159   }
160   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
161
162   return current;
163 }
164 double NetworkSmpiModel::latencyFactor(double size)
165 {
166   if (!smpi_lat_factor)
167     smpi_lat_factor =
168         parse_factor(sg_cfg_get_string("smpi/lat_factor"));
169
170   unsigned int iter = 0;
171   s_smpi_factor_t fact;
172   double current=1.0;
173   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
174     if (size <= fact.factor) {
175       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
176       return current;
177     }else
178       current=fact.value;
179   }
180   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
181
182   return current;
183 }
184
185 double NetworkSmpiModel::bandwidthConstraint(double rate, double bound, double size)
186 {
187   return rate < 0 ? bound : min(bound, rate * bandwidthFactor(size));
188 }
189
190 /************
191  * Resource *
192  ************/
193
194
195
196 /**********
197  * Action *
198  **********/