Logo AND Algorithmique Numérique Distribuée

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