Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allows the bytes_amount argument of MSG_parallel_task_create to be null.
[simgrid.git] / src / simix / smx_deployment.cpp
1 /* Copyright (c) 2007, 2009-2015. 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 "smx_private.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/dict.h"
11 #include "surf/surfxml_parse.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_deployment, simix,
14                                 "Logging specific to SIMIX (deployment)");
15
16 extern int surf_parse_lineno;
17
18 void SIMIX_init_application(void)
19 {
20   surf_parse_reset_callbacks();
21 }
22
23 /**
24  * \brief An application deployer.
25  *
26  * Creates the process described in \a file.
27  * \param file a filename of a xml description of the application. This file
28  * follows this DTD :
29  *
30  *     \include surfxml.dtd
31  *
32  * Here is a small example of such a platform
33  *
34  *     \include small_deployment.xml
35  *
36  */
37 void SIMIX_launch_application(const char *file)
38 {
39   XBT_ATTRIB_UNUSED int parse_status;
40   xbt_assert(simix_global,
41               "SIMIX_global_init has to be called before SIMIX_launch_application.");
42
43   SIMIX_init_application();
44
45   surf_parse_open(file);
46   TRY {
47     parse_status = surf_parse();
48     surf_parse_close();
49     xbt_assert(!parse_status, "Parse error at %s:%d", file,surf_parse_lineno);
50   }
51   CATCH_ANONYMOUS {
52     XBT_ERROR("Unrecoverable error at %s:%d. The full exception stack follows, in case it helps you to diagnose the problem.",
53         file, surf_parse_lineno);
54     RETHROW;
55   }
56 }
57
58 /**
59  * \brief Registers a #smx_process_code_t code in a global table.
60  *
61  * Registers a code function in a global table.
62  * This table is then used by #SIMIX_launch_application.
63  * \param name the reference name of the function.
64  * \param code the function
65  */
66 void SIMIX_function_register(const char *name,
67                                         xbt_main_func_t code)
68 {
69   xbt_assert(simix_global,
70               "SIMIX_global_init has to be called before SIMIX_function_register."); 
71   xbt_dict_set(simix_global->registered_functions, name, (void*) code, NULL);
72 }
73
74 static xbt_main_func_t default_function = NULL;
75 /**
76  * \brief Registers a #smx_process_code_t code as default value.
77  *
78  * 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.
79  * \param code the function
80  */
81 void SIMIX_function_register_default(xbt_main_func_t code)
82 {
83   xbt_assert(simix_global,
84               "SIMIX_global_init has to be called before SIMIX_function_register.");
85
86   default_function = code;
87 }
88
89 /**
90  * \brief Gets a #smx_process_t code from the global table.
91  *
92  * Gets a code function from the global table. Returns NULL if there are no function registered with the name.
93  * This table is then used by #SIMIX_launch_application.
94  * \param name the reference name of the function.
95  * \return The #smx_process_t or NULL.
96  */
97 xbt_main_func_t SIMIX_get_registered_function(const char *name)
98 {
99   xbt_main_func_t res = NULL;
100   xbt_assert(simix_global,
101               "SIMIX_global_init has to be called before SIMIX_get_registered_function.");
102
103   res = (xbt_main_func_t)xbt_dict_get_or_null(simix_global->registered_functions, name);
104   return res ? res : default_function;
105 }
106
107
108 /**
109  * \brief Bypass the parser, get arguments, and set function to each process
110  */
111
112 void SIMIX_process_set_function(const char *process_host,
113                                 const char *process_function,
114                                 xbt_dynar_t arguments,
115                                 double process_start_time,
116                                 double process_kill_time)
117 {
118   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
119
120   sg_host_t host = sg_host_by_name(process_host);
121   if (!host)
122     THROWF(arg_error, 0, "Host '%s' unknown", process_host);
123   process.host = sg_host_get_name(host);
124
125   process.argc = 1 + xbt_dynar_length(arguments);
126   process.argv = (const char**)xbt_new(char *, process.argc + 1);
127   process.argv[0] = xbt_strdup(process_function);
128   /* add arguments */
129   unsigned int i;
130   char *arg;
131   xbt_dynar_foreach(arguments, i, arg) {
132     process.argv[i + 1] = xbt_strdup(arg);
133   }
134   process.argv[process.argc] = NULL;
135
136   xbt_main_func_t parse_code = SIMIX_get_registered_function(process_function);
137   xbt_assert(parse_code, "Function '%s' unknown", process_function);
138   process.function = process_function;
139
140   process.host = process_host;
141   process.kill_time = process_kill_time;
142   process.start_time = process_start_time;
143
144   sg_platf_new_process(&process);
145 }