Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics around surf::Resouce::apply_event()
[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
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     if (xbt_dynar_length(radical_elements2) != 2)
49       surf_parse_error("Malformed radical for smpi factor!");
50
51     fact.factor = xbt_str_parse_int(xbt_dynar_get_as(radical_elements2, 0, char *),
52         bprintf("Invalid factor in chunk #%d: %%s", iter+1));
53     fact.value = xbt_str_parse_double(xbt_dynar_get_as(radical_elements2, 1, char *),
54         bprintf("Invalid factor value in chunk #%d: %%s", iter+1));
55
56     xbt_dynar_push_as(smpi_factor, s_smpi_factor_t, fact);
57     XBT_DEBUG("smpi_factor:\t%ld : %f", fact.factor, fact.value);
58     xbt_dynar_free(&radical_elements2);
59   }
60   xbt_dynar_free(&radical_elements);
61   iter=0;
62   xbt_dynar_sort(smpi_factor, &factor_cmp);
63   xbt_dynar_foreach(smpi_factor, iter, fact) {
64     XBT_DEBUG("ordered smpi_factor:\t%ld : %f", fact.factor, fact.value);
65
66   }
67   return smpi_factor;
68 }
69
70 /*********
71  * Model *
72  *********/
73
74 /************************************************************************/
75 /* New model based on LV08 and experimental results of MPI ping-pongs   */
76 /************************************************************************/
77 /* @Inproceedings{smpi_ipdps, */
78 /*  author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
79 /*  title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
80 /*  booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
81 /*  address={Anchorage (Alaska) USA}, */
82 /*  month=may, */
83 /*  year={2011} */
84 /*  } */
85 void surf_network_model_init_SMPI(void)
86 {
87
88   if (surf_network_model)
89     return;
90   surf_network_model = new simgrid::surf::NetworkSmpiModel();
91   net_define_callbacks();
92   xbt_dynar_push(all_existing_models, &surf_network_model);
93
94   xbt_cfg_setdefault_double(_sg_cfg_set, "network/sender_gap", 10e-6);
95   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
96 }
97
98 namespace simgrid {
99 namespace surf {
100
101 NetworkSmpiModel::NetworkSmpiModel()
102  : NetworkCm02Model() {
103   m_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->m_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->m_senderGap = sg_sender_gap;
131         /*  max(sg_sender_gap,last_action->sender.size / bw);*/
132       action->m_latency += action->m_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->m_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->p_senderLinkName
155       && action->p_senderFifoItem) {
156     fifo =
157         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
158                                           action->p_senderLinkName);
159     xbt_fifo_remove_item(fifo, action->p_senderFifoItem);
160     size = xbt_fifo_size(fifo);
161     if (size == 0) {
162       xbt_fifo_free(fifo);
163       xbt_dict_remove(gap_lookup, action->p_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 =
176         parse_factor(sg_cfg_get_string("smpi/bw_factor"));
177
178   unsigned int iter = 0;
179   s_smpi_factor_t fact;
180   double current=1.0;
181   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
182     if (size <= fact.factor) {
183       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
184       return current;
185     }else
186       current=fact.value;
187   }
188   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
189
190   return current;
191 }
192
193 double NetworkSmpiModel::latencyFactor(double size)
194 {
195   if (!smpi_lat_factor)
196     smpi_lat_factor =
197         parse_factor(sg_cfg_get_string("smpi/lat_factor"));
198
199   unsigned int iter = 0;
200   s_smpi_factor_t fact;
201   double current=1.0;
202   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
203     if (size <= fact.factor) {
204       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
205       return current;
206     }else
207       current=fact.value;
208   }
209   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
210
211   return current;
212 }
213
214 double NetworkSmpiModel::bandwidthConstraint(double rate, double bound, double size)
215 {
216   return rate < 0 ? bound : std::min(bound, rate * bandwidthFactor(size));
217 }
218
219 /************
220  * Resource *
221  ************/
222
223
224
225 /**********
226  * Action *
227  **********/
228
229 }
230 }