Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bugfix: do not hardcode the number of workers in the parmap, use the command line...
[simgrid.git] / src / simix / smx_deployment.c
1 /* Copyright (c) 2007, 2009, 2010. 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 "private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse_private.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
14                                 "Logging specific to SIMIX (deployment)");
15 static int parse_argc = -1;
16 static char **parse_argv = NULL;
17 static xbt_main_func_t parse_code = NULL;
18 static char *parse_host = NULL;
19 static double start_time = 0.0;
20 static double kill_time = -1.0;
21
22 static void parse_process_init(void)
23 {
24   parse_host = xbt_strdup(A_surfxml_process_host);
25   xbt_assert1(SIMIX_host_get_by_name(parse_host),
26               "Host '%s' unknown", parse_host);
27   parse_code = SIMIX_get_registered_function(A_surfxml_process_function);
28   xbt_assert1(parse_code, "Function '%s' unknown",
29               A_surfxml_process_function);
30   parse_argc = 0;
31   parse_argv = NULL;
32   parse_argc++;
33   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
34   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_process_function);
35   surf_parse_get_double(&start_time, A_surfxml_process_start_time);
36   surf_parse_get_double(&kill_time, A_surfxml_process_kill_time);
37 }
38
39 static void parse_argument(void)
40 {
41   parse_argc++;
42   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
43   parse_argv[(parse_argc) - 1] = xbt_strdup(A_surfxml_argument_value);
44 }
45
46 static void parse_process_finalize(void)
47 {
48   smx_process_arg_t arg = NULL;
49   smx_process_t process = NULL;
50   if (start_time > SIMIX_get_clock()) {
51     arg = xbt_new0(s_smx_process_arg_t, 1);
52     arg->name = parse_argv[0];
53     arg->code = parse_code;
54     arg->data = NULL;
55     arg->hostname = parse_host;
56     arg->argc = parse_argc;
57     arg->argv = parse_argv;
58     arg->kill_time = kill_time;
59     arg->properties = current_property_set;
60
61     XBT_DEBUG("Process %s(%s) will be started at time %f", arg->name,
62            arg->hostname, start_time);
63     SIMIX_timer_set(start_time, &SIMIX_process_create_from_wrapper, arg);
64   } else {                      // start_time <= SIMIX_get_clock()
65     XBT_DEBUG("Starting Process %s(%s) right now", parse_argv[0], parse_host);
66
67     if (simix_global->create_process_function)
68       (*simix_global->create_process_function) (&process,
69                                                 parse_argv[0],
70                                                 parse_code, NULL,
71                                                 parse_host, parse_argc,
72                                                 parse_argv,
73                                                 current_property_set);
74     else
75       SIMIX_req_process_create(&process, parse_argv[0], parse_code, NULL, parse_host, parse_argc, parse_argv,
76                                current_property_set);
77     /* verify if process has been created (won't be the case if the host is currently dead, but that's fine) */
78     if (!process) {
79       xbt_free(parse_host);
80       return;
81     }
82     if (kill_time > SIMIX_get_clock()) {
83       if (simix_global->kill_process_function) {
84         SIMIX_timer_set(start_time, simix_global->kill_process_function, process);
85       }
86     }
87     xbt_free(parse_host);
88   }
89   current_property_set = NULL;
90 }
91
92 /**
93  * \brief An application deployer.
94  *
95  * Creates the process described in \a file.
96  * \param file a filename of a xml description of the application. This file
97  * follows this DTD :
98  *
99  *     \include surfxml.dtd
100  *
101  * Here is a small example of such a platform
102  *
103  *     \include small_deployment.xml
104  *
105  */
106 void SIMIX_launch_application(const char *file)
107 {
108   int parse_status;
109   xbt_assert0(simix_global,
110               "SIMIX_global_init has to be called before SIMIX_launch_application.");
111
112   // Reset callbacks
113   surf_parse_reset_callbacks();
114
115   surfxml_add_callback(STag_surfxml_process_cb_list, parse_process_init);
116   surfxml_add_callback(ETag_surfxml_argument_cb_list, parse_argument);
117   surfxml_add_callback(STag_surfxml_prop_cb_list, parse_properties);
118   surfxml_add_callback(ETag_surfxml_process_cb_list,
119                        parse_process_finalize);
120
121   surf_parse_open(file);
122   parse_status = surf_parse();
123   surf_parse_close();
124   xbt_assert1(!parse_status, "Parse error in %s", file);
125 }
126
127 /**
128  * \brief Registers a #smx_process_code_t code in a global table.
129  *
130  * Registers a code function in a global table.
131  * This table is then used by #SIMIX_launch_application.
132  * \param name the reference name of the function.
133  * \param code the function
134  */
135 XBT_INLINE void SIMIX_function_register(const char *name,
136                                         xbt_main_func_t code)
137 {
138   xbt_assert0(simix_global,
139               "SIMIX_global_init has to be called before SIMIX_function_register.");
140
141   xbt_dict_set(simix_global->registered_functions, name, code, NULL);
142 }
143
144 static xbt_main_func_t default_function = NULL;
145 /**
146  * \brief Registers a #smx_process_code_t code as default value.
147  *
148  * Registers a code function as being the default value. This function will get used by SIMIX_launch_application() when there is no registered function of the requested name in.
149  * \param code the function
150  */
151 void SIMIX_function_register_default(xbt_main_func_t code)
152 {
153   xbt_assert0(simix_global,
154               "SIMIX_global_init has to be called before SIMIX_function_register.");
155
156   default_function = code;
157 }
158
159 /**
160  * \brief Gets a #smx_process_t code from the global table.
161  *
162  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
163  * This table is then used by #SIMIX_launch_application.
164  * \param name the reference name of the function.
165  * \return The #smx_process_t or NULL.
166  */
167 xbt_main_func_t SIMIX_get_registered_function(const char *name)
168 {
169   xbt_main_func_t res = NULL;
170   xbt_assert0(simix_global,
171               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
172
173   res = xbt_dict_get_or_null(simix_global->registered_functions, name);
174   return res ? res : default_function;
175 }
176
177
178 /**
179  * \brief Bypass the parser, get arguments, and set function to each process
180  */
181
182 void SIMIX_process_set_function(const char *process_host,
183                                 const char *process_function,
184                                 xbt_dynar_t arguments,
185                                 double process_start_time,
186                                 double process_kill_time)
187 {
188   unsigned int i;
189   char *arg;
190
191   /* init process */
192   parse_host = xbt_strdup(process_host);
193   xbt_assert1(SIMIX_host_get_by_name(parse_host),
194               "Host '%s' unknown", parse_host);
195   parse_code = SIMIX_get_registered_function(process_function);
196   xbt_assert1(parse_code, "Function '%s' unknown", process_function);
197
198   parse_argc = 0;
199   parse_argv = NULL;
200   parse_argc++;
201   parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
202   parse_argv[(parse_argc) - 1] = xbt_strdup(process_function);
203   start_time = process_start_time;
204   kill_time = process_kill_time;
205
206   /* add arguments */
207   xbt_dynar_foreach(arguments, i, arg) {
208     parse_argc++;
209     parse_argv = xbt_realloc(parse_argv, (parse_argc) * sizeof(char *));
210     parse_argv[(parse_argc) - 1] = xbt_strdup(arg);
211   }
212
213   /* finalize */
214   parse_process_finalize();
215 }