Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Exposing a pointer to internals is not considered as good OOP practices
[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     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   iter=0;
64   xbt_dynar_sort(smpi_factor, &factor_cmp);
65   xbt_dynar_foreach(smpi_factor, iter, fact) {
66     XBT_DEBUG("ordered smpi_factor:\t%ld : %f", fact.factor, fact.value);
67
68   }
69   return smpi_factor;
70 }
71
72 /*********
73  * Model *
74  *********/
75
76 /************************************************************************/
77 /* New model based on LV08 and experimental results of MPI ping-pongs   */
78 /************************************************************************/
79 /* @Inproceedings{smpi_ipdps, */
80 /*  author={Pierre-Nicolas Clauss and Mark Stillwell and Stéphane Genaud and Frédéric Suter and Henri Casanova and Martin Quinson}, */
81 /*  title={Single Node On-Line Simulation of {MPI} Applications with SMPI}, */
82 /*  booktitle={25th IEEE International Parallel and Distributed Processing Symposium (IPDPS'11)}, */
83 /*  address={Anchorage (Alaska) USA}, */
84 /*  month=may, */
85 /*  year={2011} */
86 /*  } */
87 void surf_network_model_init_SMPI(void)
88 {
89
90   if (surf_network_model)
91     return;
92   surf_network_model = new simgrid::surf::NetworkSmpiModel();
93   net_define_callbacks();
94   xbt_dynar_push(all_existing_models, &surf_network_model);
95
96   xbt_cfg_setdefault_double(_sg_cfg_set, "network/sender_gap", 10e-6);
97   xbt_cfg_setdefault_double(_sg_cfg_set, "network/weight_S", 8775);
98 }
99
100 namespace simgrid {
101 namespace surf {
102
103 NetworkSmpiModel::NetworkSmpiModel()
104  : NetworkCm02Model() {
105   m_haveGap=true;
106 }
107
108 NetworkSmpiModel::~NetworkSmpiModel(){
109   xbt_dict_free(&gap_lookup);
110   xbt_dynar_free(&smpi_bw_factor);
111   xbt_dynar_free(&smpi_lat_factor);
112 }
113
114 void NetworkSmpiModel::gapAppend(double size, Link* link, NetworkAction *act)
115 {
116   const char *src = link->getName();
117   xbt_fifo_t fifo;
118   NetworkCm02Action *action= static_cast<NetworkCm02Action*>(act);
119
120   if (sg_sender_gap > 0.0) {
121     if (!gap_lookup) {
122       gap_lookup = xbt_dict_new_homogeneous(NULL);
123     }
124     fifo = (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup, src);
125     action->m_senderGap = 0.0;
126     if (fifo && xbt_fifo_size(fifo) > 0) {
127       /* Compute gap from last send */
128       /*last_action =
129           (surf_action_network_CM02_t)
130           xbt_fifo_get_item_content(xbt_fifo_get_last_item(fifo));*/
131      // bw = net_get_link_bandwidth(link);
132       action->m_senderGap = sg_sender_gap;
133         /*  max(sg_sender_gap,last_action->sender.size / bw);*/
134       action->m_latency += action->m_senderGap;
135     }
136     /* Append action as last send */
137     /*action->sender.link_name = link->lmm_resource.generic_resource.name;
138     fifo =
139         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
140                                           action->sender.link_name);
141     if (!fifo) {
142       fifo = xbt_fifo_new();
143       xbt_dict_set(gap_lookup, action->sender.link_name, fifo, NULL);
144     }
145     action->sender.fifo_item = xbt_fifo_push(fifo, action);*/
146     action->m_senderSize = size;
147   }
148 }
149
150 void NetworkSmpiModel::gapRemove(Action *lmm_action)
151 {
152   xbt_fifo_t fifo;
153   size_t size;
154   NetworkCm02Action *action = static_cast<NetworkCm02Action*>(lmm_action);
155
156   if (sg_sender_gap > 0.0 && action->p_senderLinkName
157       && action->p_senderFifoItem) {
158     fifo =
159         (xbt_fifo_t) xbt_dict_get_or_null(gap_lookup,
160                                           action->p_senderLinkName);
161     xbt_fifo_remove_item(fifo, action->p_senderFifoItem);
162     size = xbt_fifo_size(fifo);
163     if (size == 0) {
164       xbt_fifo_free(fifo);
165       xbt_dict_remove(gap_lookup, action->p_senderLinkName);
166       size = xbt_dict_length(gap_lookup);
167       if (size == 0) {
168         xbt_dict_free(&gap_lookup);
169       }
170     }
171   }
172 }
173
174 double NetworkSmpiModel::bandwidthFactor(double size)
175 {
176   if (!smpi_bw_factor)
177     smpi_bw_factor =
178         parse_factor(sg_cfg_get_string("smpi/bw_factor"));
179
180   unsigned int iter = 0;
181   s_smpi_factor_t fact;
182   double current=1.0;
183   xbt_dynar_foreach(smpi_bw_factor, iter, fact) {
184     if (size <= fact.factor) {
185       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
186       return current;
187     }else
188       current=fact.value;
189   }
190   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
191
192   return current;
193 }
194
195 double NetworkSmpiModel::latencyFactor(double size)
196 {
197   if (!smpi_lat_factor)
198     smpi_lat_factor =
199         parse_factor(sg_cfg_get_string("smpi/lat_factor"));
200
201   unsigned int iter = 0;
202   s_smpi_factor_t fact;
203   double current=1.0;
204   xbt_dynar_foreach(smpi_lat_factor, iter, fact) {
205     if (size <= fact.factor) {
206       XBT_DEBUG("%f <= %ld return %f", size, fact.factor, current);
207       return current;
208     }else
209       current=fact.value;
210   }
211   XBT_DEBUG("%f > %ld return %f", size, fact.factor, current);
212
213   return current;
214 }
215
216 double NetworkSmpiModel::bandwidthConstraint(double rate, double bound, double size)
217 {
218   return rate < 0 ? bound : std::min(bound, rate * bandwidthFactor(size));
219 }
220
221 /************
222  * Resource *
223  ************/
224
225
226
227 /**********
228  * Action *
229  **********/
230
231 }
232 }