Logo AND Algorithmique Numérique Distribuée

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