Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to register functions that are void (*code)(std::vector<std::string>)
[simgrid.git] / src / simix / smx_deployment.cpp
1 /* Copyright (c) 2007-2018. 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 <xbt/ex.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 extern int surf_parse_lineno;
18
19 void SIMIX_init_application()
20 {
21   sg_platf_exit();
22   sg_platf_init();
23 }
24
25 /**
26  * @brief An application deployer.
27  *
28  * Creates the process described in @a file.
29  * @param file a filename of a xml description of the application. This file
30  * follows this DTD :
31  *
32  *     @include surfxml.dtd
33  *
34  * Here is a small example of such a platform
35  *
36  *     @include small_deployment.xml
37  *
38  */
39 void SIMIX_launch_application(std::string file)
40 {
41   XBT_ATTRIB_UNUSED int parse_status;
42   xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_launch_application.");
43
44   SIMIX_init_application();
45
46   surf_parse_open(file);
47   try {
48     parse_status = surf_parse();
49     surf_parse_close();
50     xbt_assert(not parse_status, "Parse error at %s:%d", file.c_str(), surf_parse_lineno);
51   }
52   catch (xbt_ex& e) {
53     XBT_ERROR(
54         "Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
55         file.c_str(), surf_parse_lineno);
56     throw;
57   }
58 }
59
60 void SIMIX_launch_application(const char* file) // deprecated
61 {
62   simgrid_load_deployment(file);
63 }
64
65 // Wrap a main() function into a ActorCodeFactory:
66 static simgrid::simix::ActorCodeFactory toActorCodeFactory(xbt_main_func_t code)
67 {
68   return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
69 }
70 static simgrid::simix::ActorCodeFactory toActorCodeFactory(void (*code)(std::vector<std::string>))
71 {
72   return [code](std::vector<std::string> args) { return simgrid::xbt::wrap_main(code, std::move(args)); };
73 }
74
75 /**
76  * @brief Registers a #xbt_main_func_t code in a global table.
77  *
78  * Registers a code function in a global table.
79  * This table is then used by #SIMIX_launch_application.
80  * @param name the reference name of the function.
81  * @param code the function
82  */
83 void SIMIX_function_register(std::string name, xbt_main_func_t code)
84 {
85   simix_global->registered_functions[name] = toActorCodeFactory(code);
86 }
87 void SIMIX_function_register(std::string name, void (*code)(std::vector<std::string>))
88 {
89   simix_global->registered_functions[name] = toActorCodeFactory(code);
90 }
91
92 void SIMIX_function_register(const char* name, xbt_main_func_t code) // deprecated
93 {
94   simgrid_register_function(name, code);
95 }
96
97 /**
98  * @brief Registers a #xbt_main_func_t code as default value.
99  *
100  * Registers a code function as being the default value. This function will get used by SIMIX_launch_application() when
101  * there is no registered function of the requested name in.
102  * @param code the function
103  */
104 void SIMIX_function_register_default(xbt_main_func_t code)
105 {
106   xbt_assert(simix_global, "SIMIX_global_init has to be called before SIMIX_function_register.");
107   simix_global->default_function = toActorCodeFactory(code);
108 }
109
110 /**
111  * @brief Gets a #smx_actor_t code from the global table.
112  *
113  * Gets a code function from the global table. Returns nullptr if there are no function registered with the name.
114  * This table is then used by #SIMIX_launch_application.
115  * @param name the reference name of the function.
116  * @return The #smx_actor_t or nullptr.
117  */
118 simgrid::simix::ActorCodeFactory& SIMIX_get_actor_code_factory(std::string name)
119 {
120   xbt_assert(simix_global,
121               "SIMIX_global_init has to be called before SIMIX_get_actor_code_factory.");
122
123   auto i = simix_global->registered_functions.find(name);
124   if (i == simix_global->registered_functions.end())
125     return simix_global->default_function;
126   else
127     return i->second;
128 }
129
130 /** @brief Bypass the parser, get arguments, and set function to each process */
131
132 void SIMIX_process_set_function(const char* process_host, const char* process_function, xbt_dynar_t arguments,
133                                 double process_start_time, double process_kill_time)
134 {
135   simgrid::kernel::routing::ActorCreationArgs actor;
136
137   sg_host_t host = sg_host_by_name(process_host);
138   if (not host)
139     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
140   actor.host = process_host;
141   actor.args.push_back(process_function);
142   /* add arguments */
143   unsigned int i;
144   char *arg;
145   xbt_dynar_foreach(arguments, i, arg) {
146     actor.args.push_back(arg);
147   }
148
149   // Check we know how to handle this function name:
150   simgrid::simix::ActorCodeFactory& parse_code = SIMIX_get_actor_code_factory(process_function);
151   xbt_assert(parse_code, "Function '%s' unknown", process_function);
152
153   actor.function   = process_function;
154   actor.host       = process_host;
155   actor.kill_time  = process_kill_time;
156   actor.start_time = process_start_time;
157   actor.on_failure = simgrid::kernel::routing::ActorOnFailure::DIE;
158   sg_platf_new_actor(&actor);
159 }
160
161 namespace simgrid {
162 namespace simix {
163
164 void register_function(std::string name, ActorCodeFactory factory)
165 {
166   simix_global->registered_functions[name] = std::move(factory);
167 }
168
169 }
170 }