Logo AND Algorithmique Numérique Distribuée

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