Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #241 from barais/master
[simgrid.git] / src / surf / HostImpl.cpp
1 /* Copyright (c) 2013-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/plugins/vm/VirtualMachineImpl.hpp"
7 #include <string>
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_host, surf, "Logging specific to the SURF host module");
10
11 simgrid::surf::HostModel *surf_host_model = nullptr;
12
13 /*************
14  * Callbacks *
15  *************/
16
17 namespace simgrid {
18 namespace surf {
19
20 /*********
21  * Model *
22  *********/
23
24 /* Each VM has a dummy CPU action on the PM layer. This CPU action works as the
25  * constraint (capacity) of the VM in the PM layer. If the VM does not have any
26  * active task, the dummy CPU action must be deactivated, so that the VM does
27  * not get any CPU share in the PM layer. */
28 void HostModel::ignoreEmptyVmInPmLMM()
29 {
30   /* iterate for all virtual machines */
31   for (s4u::VirtualMachine* const& ws_vm : vm::VirtualMachineImpl::allVms_) {
32     Cpu* cpu = ws_vm->pimpl_cpu;
33     int active_tasks = lmm_constraint_get_variable_amount(cpu->constraint());
34
35     /* The impact of the VM over its PM is the min between its vCPU amount and the amount of tasks it contains */
36     int impact = std::min(active_tasks, ws_vm->pimpl_vm_->coreAmount());
37
38     XBT_DEBUG("set the weight of the dummy CPU action of VM%p on PM to %d (#tasks: %d)", ws_vm, impact, active_tasks);
39     if (impact > 0)
40       ws_vm->pimpl_vm_->action_->setSharingWeight(1. / impact);
41     else
42       ws_vm->pimpl_vm_->action_->setSharingWeight(0.);
43   }
44 }
45
46 /* Helper function for executeParallelTask */
47 static inline double has_cost(double* array, int pos)
48 {
49   if (array)
50     return array[pos];
51   else
52     return -1.0;
53 }
54
55 Action* HostModel::executeParallelTask(int host_nb, simgrid::s4u::Host** host_list, double* flops_amount,
56     double* bytes_amount, double rate)
57 {
58   Action* action = nullptr;
59   if ((host_nb == 1) && (has_cost(bytes_amount, 0) <= 0)) {
60     action = host_list[0]->pimpl_cpu->execution_start(flops_amount[0]);
61   } else if ((host_nb == 1) && (has_cost(flops_amount, 0) <= 0)) {
62     action = surf_network_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
63   } else if ((host_nb == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
64     int nb = 0;
65     double value = 0.0;
66
67     for (int i = 0; i < host_nb * host_nb; i++) {
68       if (has_cost(bytes_amount, i) > 0.0) {
69         nb++;
70         value = has_cost(bytes_amount, i);
71       }
72     }
73     if (nb == 1) {
74       action = surf_network_model->communicate(host_list[0], host_list[1], value, rate);
75     } else if (nb == 0) {
76       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
77           "ptask model");
78     } else {
79       xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
80           "using the ptask model");
81     }
82   } else {
83     xbt_die(
84         "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
85         " - execution with one host only and no communication\n"
86         " - Self-comms with one host only\n"
87         " - Communications with two hosts and no computation");
88   }
89   delete[] host_list;
90   delete[] flops_amount;
91   delete[] bytes_amount;
92   return action;
93 }
94
95 /************
96  * Resource *
97  ************/
98 HostImpl::HostImpl(s4u::Host* host) : piface_(host)
99 {
100   /* The VM wants to reinstall a new HostImpl, but we don't want to leak the previously existing one */
101   delete piface_->pimpl_;
102   piface_->pimpl_ = this;
103 }
104
105 void HostImpl::getAttachedStorageList(std::vector<const char*>* storages)
106 {
107   for (auto const& s : storage_)
108     if (s.second->getHost() == piface_->getCname())
109       storages->push_back(s.second->piface_.getCname());
110 }
111
112 }
113 }