Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'dev/s4u_tuto_fixes' into 'master'
[simgrid.git] / src / surf / host_clm03.cpp
1 /* Copyright (c) 2013-2022. 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 <simgrid/kernel/routing/NetPoint.hpp>
7 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9
10 #include "simgrid/sg_config.hpp"
11 #include "src/kernel/EngineImpl.hpp"
12 #include "src/kernel/resource/NetworkModel.hpp"
13 #include "src/surf/host_clm03.hpp"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(res_host);
16
17 void surf_host_model_init_current_default()
18 {
19   simgrid::config::set_default<bool>("network/crosstraffic", true);
20   auto host_model = std::make_shared<simgrid::kernel::resource::HostCLM03Model>("Host_CLM03");
21   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
22   engine->add_model(host_model);
23   engine->get_netzone_root()->set_host_model(host_model);
24   surf_cpu_model_init_Cas01();
25   surf_network_model_init_LegrandVelho();
26 }
27
28 void surf_host_model_init_compound()
29 {
30   auto host_model = std::make_shared<simgrid::kernel::resource::HostCLM03Model>("Host_CLM03");
31   auto* engine    = simgrid::kernel::EngineImpl::get_instance();
32   engine->add_model(host_model);
33   engine->get_netzone_root()->set_host_model(host_model);
34 }
35
36 namespace simgrid {
37 namespace kernel {
38 namespace resource {
39
40 double HostCLM03Model::next_occurring_event(double /*now*/)
41 {
42   /* nothing specific to be done here
43    * EngineImpl::solve already calls all the models next_occurring_event properly */
44   return -1.0;
45 }
46
47 void HostCLM03Model::update_actions_state(double /*now*/, double /*delta*/)
48 {
49   /* I've no action to update */
50 }
51
52 /* Helper function for executeParallelTask */
53 static inline double has_cost(const double* array, size_t pos)
54 {
55   if (array)
56     return array[pos];
57   return -1.0;
58 }
59
60 Action* HostCLM03Model::execute_parallel(const std::vector<s4u::Host*>& host_list, const double* flops_amount,
61                                          const double* bytes_amount, double rate)
62 {
63   Action* action = nullptr;
64   auto net_model = host_list[0]->get_netpoint()->get_englobing_zone()->get_network_model();
65   if ((host_list.size() == 1) && (has_cost(bytes_amount, 0) <= 0) && (has_cost(flops_amount, 0) > 0)) {
66     action = host_list[0]->get_cpu()->execution_start(flops_amount[0], rate);
67   } else if ((host_list.size() == 1) && (has_cost(flops_amount, 0) <= 0)) {
68     action = net_model->communicate(host_list[0], host_list[0], bytes_amount[0], rate);
69   } else if ((host_list.size() == 2) && (has_cost(flops_amount, 0) <= 0) && (has_cost(flops_amount, 1) <= 0)) {
70     int nb       = 0;
71     double value = 0.0;
72
73     for (size_t i = 0; i < host_list.size() * host_list.size(); i++) {
74       if (has_cost(bytes_amount, i) > 0.0) {
75         nb++;
76         value = has_cost(bytes_amount, i);
77       }
78     }
79     if (nb == 1) {
80       action = net_model->communicate(host_list[0], host_list[1], value, rate);
81     } else if (nb == 0) {
82       xbt_die("Cannot have a communication with no flop to exchange in this model. You should consider using the "
83               "ptask model");
84     } else {
85       xbt_die("Cannot have a communication that is not a simple point-to-point in this model. You should consider "
86               "using the ptask model");
87     }
88   } else {
89     xbt_die(
90         "This model only accepts one of the following. You should consider using the ptask model for the other cases.\n"
91         " - execution with one host only and no communication\n"
92         " - Self-comms with one host only\n"
93         " - Communications with two hosts and no computation");
94   }
95   return action;
96 }
97
98 Action* HostCLM03Model::execute_thread(const s4u::Host* host, double flops_amount, int thread_count)
99 {
100   auto cpu = host->get_cpu();
101   /* Create a single action whose cost is thread_count * flops_amount and that requests thread_count cores. */
102   return cpu->execution_start(thread_count * flops_amount, thread_count, -1);
103 }
104
105 } // namespace resource
106 } // namespace kernel
107 } // namespace simgrid