Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5d26431b7d96816bfb7abfcb1e6b82bdd5385801
[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 *action)
108 {
109   const char *src = link->getName();
110   xbt_fifo_t fifo;
111
112   if (sg_sender_gap > 0.0) {
113     if (!gap_lookup) {
114       gap_lookup = xbt_dict_new_homogeneous(NULL);
115     }
116     fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
117     action->m_senderGap = 0.0;
118     if (fifo && xbt_fifo_size(fifo) > 0) {
119       /* Compute gap from last send */
120       /*last_action =
121           (surf_action_network_CM02_t)
122           xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));*/
123      // bw = net_get_link_bandwidth(link);
124       action->m_senderGap = sg_sender_gap;
125         /*  max(sg_sender_gap,last_action->sender.size / bw);*/
126       action->m_latency += action->m_senderGap;
127     }
128     /* Append action as last send */
129     /*action->sender.link_name = link->lmm_resource.generic_resource.name;
130     fifo =
131         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
132                                           action->sender.link_name);
133     if (!fifo) {
134       fifo = xbt_fifo_new();
135       xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
136     }
137     action->sender.fifo_item = xbt_fifo_push(fifo, action);*/
138     action->m_senderSize = size;
139   }
140 }
141
142 void NetworkSmpiModel::gapRemove(Action *lmm_action)
143 {
144   xbt_fifo_t fifo;
145   size_t size;
146   NetworkCm02Action *action = static_cast<NetworkCm02Action*>(lmm_action);
147
148   if (sg_sender_gap > 0.0 && action->p_senderLinkName
149       && action->p_senderFifoItem) {
150     fifo =
151         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
152                                           action->p_senderLinkName);
153     xbt_fifo_remove_item(fifo, action->p_senderFifoItem);
154     size = xbt_fifo_size(fifo);
155     if (size == 0) {
156       xbt_fifo_free(fifo);
157       xbt_dict_remove(gap_lookup, action->p_senderLinkName);
158       size = xbt_dict_length(gap_lookup);
159       if (size == 0) {
160         xbt_dict_free(&gap_lookup);
161       }
162     }
163   }
164 }
165
166 double NetworkSmpiModel::bandwidthFactor(double size)
167 {
168   if (!smpi_bw_factor)
169     smpi_bw_factor =
170         parse_factor(sg_cfg_get_string("smpi/bw_factor"));
171
172   unsigned int iter = 0;
173   s_smpi_factor_t fact;
174   double current=1.0;
175   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
176     if (size <= fact.factor) {
177       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
178       return current;
179     }else
180       current=fact.value;
181   }
182   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
183
184   return current;
185 }
186
187 double NetworkSmpiModel::latencyFactor(double size)
188 {
189   if (!smpi_lat_factor)
190     smpi_lat_factor =
191         parse_factor(sg_cfg_get_string("smpi/lat_factor"));
192
193   unsigned int iter = 0;
194   s_smpi_factor_t fact;
195   double current=1.0;
196   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
197     if (size <= fact.factor) {
198       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
199       return current;
200     }else
201       current=fact.value;
202   }
203   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
204
205   return current;
206 }
207
208 double NetworkSmpiModel::bandwidthConstraint(double rate, double bound, double size)
209 {
210   return rate < 0 ? bound : min(bound, rate * bandwidthFactor(size));
211 }
212
213 /************
214  * Resource *
215  ************/
216
217
218
219 /**********
220  * Action *
221  **********/