Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
deprecate MSG_set_function, that seems useless since we cannot bypass the parser...
[simgrid.git] / src / simix / smx_deployment.cpp
1 /* Copyright (c) 2007-2019. 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/s4u/Host.hpp"
7 #include "smx_private.hpp"
8 #include "src/surf/xml/platf_private.hpp" // FIXME: KILLME. There must be a better way than mimicking XML here
9 #include <simgrid/engine.h>
10 #include <simgrid/s4u/Engine.hpp>
11
12 #include <string>
13 #include <vector>
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix, "Logging specific to SIMIX (deployment)");
16
17 void SIMIX_init_application() // XBT_DEPRECATED_v329
18 {
19   sg_platf_exit();
20   sg_platf_init();
21 }
22
23 void SIMIX_launch_application(const std::string& file) // XBT_DEPRECATED_v329
24 {
25   simgrid_load_deployment(file.c_str());
26 }
27 void SIMIX_function_register(const std::string& name, xbt_main_func_t code) // XBT_DEPRECATED_v329
28 {
29   simgrid::s4u::Engine::get_instance()->register_function(name, code);
30 }
31 void SIMIX_function_register(const std::string& name, void (*code)(std::vector<std::string>)) // XBT_DEPRECATED_v329
32 {
33   simgrid::s4u::Engine::get_instance()->register_function(name, code);
34 }
35 void SIMIX_function_register_default(xbt_main_func_t code) // XBT_DEPRECATED_v329
36 {
37   simgrid::s4u::Engine::get_instance()->register_default(code);
38 }
39
40 /**
41  * @brief Gets a #smx_actor_t code from the global table.
42  *
43  * Gets a code function from the global table. Returns nullptr if there are no function registered with the name.
44  * This table is then used by #SIMIX_launch_application.
45  * @param name the reference name of the function.
46  * @return The #smx_actor_t or nullptr.
47  */
48 simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(const std::string& name)
49 {
50   xbt_assert(simix_global,
51               "SIMIX_global_init has to be called before SIMIX_get_actor_code_factory.");
52
53   auto i = simix_global->registered_functions.find(name);
54   if (i == simix_global->registered_functions.end())
55     return simix_global->default_function;
56   else
57     return i->second;
58 }
59
60 /** @brief Bypass the parser, get arguments, and set function to each process */
61
62 void SIMIX_process_set_function(const char* process_host, const char* process_function, xbt_dynar_t arguments,
63                                 double process_start_time, double process_kill_time) // XBT_ATTRIB_DEPRECATED_v329
64 {
65   simgrid::kernel::routing::ActorCreationArgs actor;
66
67   sg_host_t host = sg_host_by_name(process_host);
68   if (not host)
69     throw std::invalid_argument(simgrid::xbt::string_printf("Host '%s' unknown", process_host));
70   actor.host = process_host;
71   actor.args.push_back(process_function);
72   /* add arguments */
73   unsigned int i;
74   char *arg;
75   xbt_dynar_foreach(arguments, i, arg) {
76     actor.args.push_back(arg);
77   }
78
79   // Check we know how to handle this function name:
80   simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function);
81   xbt_assert(parse_code, "Function '%s' unknown", process_function);
82
83   actor.function   = process_function;
84   actor.host       = process_host;
85   actor.kill_time  = process_kill_time;
86   actor.start_time = process_start_time;
87   actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
88   sg_platf_new_actor(&actor);
89 }
90
91 namespace simgrid {
92 namespace simix {
93
94 void register_function(const std::string& name, const ActorCodeFactory& factory)
95 {
96   simix_global->registered_functions[name] = factory;
97 }
98
99 }
100 }