Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
solving copy_data_callback issue by adding a new smpi function that can be changed...
[simgrid.git] / src / surf / cpu_interface.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 <xbt/dynar.h>
8 #include "cpu_interface.hpp"
9 #include "src/instr/instr_private.h" // TRACE_is_enabled(). FIXME: remove by subscribing tracing to the surf signals
10
11 XBT_LOG_EXTERNAL_CATEGORY(surf_kernel);
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_cpu, surf, "Logging specific to the SURF cpu module");
13
14 simgrid::surf::CpuModel *surf_cpu_model_pm;
15 simgrid::surf::CpuModel *surf_cpu_model_vm;
16
17 namespace simgrid {
18 namespace surf {
19
20 /*********
21  * Model *
22  *********/
23
24 void CpuModel::updateActionsStateLazy(double now, double /*delta*/)
25 {
26   while ((xbt_heap_size(getActionHeap()) > 0)
27          && (double_equals(xbt_heap_maxkey(getActionHeap()), now, sg_surf_precision))) {
28
29     CpuAction *action = static_cast<CpuAction*>(xbt_heap_pop(getActionHeap()));
30     XBT_CDEBUG(surf_kernel, "Something happened to action %p", action);
31     if (TRACE_is_enabled()) {
32       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)));
33       TRACE_surf_host_set_utilization(cpu->cname(), action->getCategory(), lmm_variable_getvalue(action->getVariable()),
34                                       action->getLastUpdate(), now - action->getLastUpdate());
35     }
36
37     action->finish();
38     XBT_CDEBUG(surf_kernel, "Action %p finished", action);
39
40     /* set the remains to 0 due to precision problems when updating the remaining amount */
41     action->setRemains(0);
42     action->setState(Action::State::done);
43   }
44   if (TRACE_is_enabled()) {
45     //defining the last timestamp that we can safely dump to trace file
46     //without losing the event ascending order (considering all CPU's)
47     double smaller = -1;
48     ActionList *actionSet = getRunningActionSet();
49     for(ActionList::iterator it(actionSet->begin()), itend(actionSet->end())
50        ; it != itend ; ++it) {
51       CpuAction *action = static_cast<CpuAction*>(&*it);
52         if (smaller < 0) {
53           smaller = action->getLastUpdate();
54           continue;
55         }
56         if (action->getLastUpdate() < smaller) {
57           smaller = action->getLastUpdate();
58         }
59     }
60     if (smaller > 0) {
61       TRACE_last_timestamp_to_dump = smaller;
62     }
63   }
64 }
65
66 void CpuModel::updateActionsStateFull(double now, double delta)
67 {
68   CpuAction *action = nullptr;
69   ActionList *running_actions = getRunningActionSet();
70
71   for(ActionList::iterator it(running_actions->begin()), itNext=it, itend(running_actions->end())
72      ; it != itend ; it=itNext) {
73     ++itNext;
74     action = static_cast<CpuAction*>(&*it);
75     if (TRACE_is_enabled()) {
76       Cpu *cpu = static_cast<Cpu*> (lmm_constraint_id(lmm_get_cnst_from_var(getMaxminSystem(), action->getVariable(), 0)) );
77
78       TRACE_surf_host_set_utilization(cpu->cname(), action->getCategory(), lmm_variable_getvalue(action->getVariable()),
79                                       now - delta, delta);
80       TRACE_last_timestamp_to_dump = now - delta;
81     }
82
83     action->updateRemains(lmm_variable_getvalue(action->getVariable()) * delta);
84
85
86     if (action->getMaxDuration() != NO_MAX_DURATION)
87       action->updateMaxDuration(delta);
88
89
90     if ((action->getRemainsNoUpdate() <= 0) &&
91         (lmm_get_variable_weight(action->getVariable()) > 0)) {
92       action->finish();
93       action->setState(Action::State::done);
94     } else if ((action->getMaxDuration() != NO_MAX_DURATION) &&
95                (action->getMaxDuration() <= 0)) {
96       action->finish();
97       action->setState(Action::State::done);
98     }
99   }
100 }
101
102 /************
103  * Resource *
104  ************/
105 Cpu::Cpu(Model *model, simgrid::s4u::Host *host, std::vector<double> *speedPerPstate, int core)
106  : Cpu(model, host, nullptr/*constraint*/, speedPerPstate, core)
107 {
108 }
109
110 Cpu::Cpu(Model* model, simgrid::s4u::Host* host, lmm_constraint_t constraint, std::vector<double>* speedPerPstate,
111          int core)
112     : Resource(model, host->cname(), constraint), coresAmount_(core), host_(host)
113 {
114   xbt_assert(core > 0, "Host %s must have at least one core, not 0.", host->cname());
115
116   speed_.peak = speedPerPstate->front();
117   speed_.scale = 1;
118   host->pimpl_cpu = this;
119   xbt_assert(speed_.scale > 0, "Speed of host %s must be >0", host->cname());
120
121   // Copy the power peak array:
122   for (double value : *speedPerPstate) {
123     speedPerPstate_.push_back(value);
124   }
125
126   xbt_assert(model == surf_cpu_model_pm || core==1, "Currently, VM cannot be multicore");
127 }
128
129 Cpu::~Cpu() = default;
130
131 /** @brief The amount of flop per second that this CPU can compute at its current DVFS level */
132 double Cpu::getPstateSpeedCurrent()
133 {
134   return speed_.peak;
135 }
136
137 int Cpu::getNbPStates()
138 {
139   return speedPerPstate_.size();
140 }
141
142 void Cpu::setPState(int pstate_index)
143 {
144   xbt_assert(pstate_index <= static_cast<int>(speedPerPstate_.size()),
145              "Invalid parameters for CPU %s (pstate %d > length of pstates %d)", cname(), pstate_index,
146              static_cast<int>(speedPerPstate_.size()));
147
148   double new_peak_speed = speedPerPstate_[pstate_index];
149   pstate_ = pstate_index;
150   speed_.peak = new_peak_speed;
151
152   onSpeedChange();
153 }
154
155 int Cpu::getPState()
156 {
157   return pstate_;
158 }
159
160 double Cpu::getPstateSpeed(int pstate_index)
161 {
162   xbt_assert((pstate_index <= static_cast<int>(speedPerPstate_.size())), "Invalid parameters (pstate index out of bounds)");
163
164   return speedPerPstate_[pstate_index];
165 }
166
167 double Cpu::getSpeed(double load)
168 {
169   return load * speed_.peak;
170 }
171
172 double Cpu::getAvailableSpeed()
173 {
174 /* number between 0 and 1 */
175   return speed_.scale;
176 }
177
178 void Cpu::onSpeedChange() {
179   TRACE_surf_host_set_speed(surf_get_clock(), cname(), coresAmount_ * speed_.scale * speed_.peak);
180   s4u::Host::onSpeedChange(*host_);
181 }
182
183 int Cpu::coreCount()
184 {
185   return coresAmount_;
186 }
187
188 void Cpu::setStateTrace(tmgr_trace_t trace)
189 {
190   xbt_assert(stateEvent_ == nullptr, "Cannot set a second state trace to Host %s", host_->cname());
191
192   stateEvent_ = future_evt_set->add_trace(trace, 0.0, this);
193 }
194 void Cpu::setSpeedTrace(tmgr_trace_t trace)
195 {
196   xbt_assert(speed_.event == nullptr, "Cannot set a second speed trace to Host %s", host_->cname());
197
198   speed_.event = future_evt_set->add_trace(trace, 0.0, this);
199 }
200
201
202 /**********
203  * Action *
204  **********/
205
206 void CpuAction::updateRemainingLazy(double now)
207 {
208   xbt_assert(getStateSet() == getModel()->getRunningActionSet(), "You're updating an action that is not running.");
209   xbt_assert(getPriority() > 0, "You're updating an action that seems suspended.");
210
211   double delta = now - lastUpdate_;
212
213   if (remains_ > 0) {
214     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains was %f, last_update was: %f", this, remains_, lastUpdate_);
215     double_update(&(remains_), lastValue_ * delta, sg_maxmin_precision*sg_surf_precision);
216
217     if (TRACE_is_enabled()) {
218       Cpu *cpu = static_cast<Cpu*>(lmm_constraint_id(lmm_get_cnst_from_var(getModel()->getMaxminSystem(), getVariable(), 0)));
219       TRACE_surf_host_set_utilization(cpu->cname(), getCategory(), lastValue_, lastUpdate_, now - lastUpdate_);
220     }
221     XBT_CDEBUG(surf_kernel, "Updating action(%p): remains is now %f", this, remains_);
222   }
223
224   lastUpdate_ = now;
225   lastValue_ = lmm_variable_getvalue(getVariable());
226 }
227
228 simgrid::xbt::signal<void(simgrid::surf::CpuAction*, Action::State)> CpuAction::onStateChange;
229
230 void CpuAction::setState(Action::State state){
231   Action::State previous = getState();
232   Action::setState(state);
233   onStateChange(this, previous);
234 }
235 /** @brief returns a list of all CPUs that this action is using */
236 std::list<Cpu*> CpuAction::cpus() {
237   std::list<Cpu*> retlist;
238   lmm_system_t sys = getModel()->getMaxminSystem();
239   int llen = lmm_get_number_of_cnst_from_var(sys, getVariable());
240
241   for (int i = 0; i < llen; i++) {
242     /* Beware of composite actions: ptasks put links and cpus together */
243     // extra pb: we cannot dynamic_cast from void*...
244     Resource* resource = static_cast<Resource*>(lmm_constraint_id(lmm_get_cnst_from_var(sys, getVariable(), i)));
245     Cpu* cpu           = dynamic_cast<Cpu*>(resource);
246     if (cpu != nullptr)
247       retlist.push_back(cpu);
248   }
249
250   return retlist;
251 }
252
253 }
254 }