1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved. */
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. */
6 /** @addtogroup MSG_examples
8 * - <b>masterslave/masterslave_console.c</b>: demonstrate how to use
9 * lua files instead of XML for the platform and deployment
10 * declaration using @ref MSG_load_platform_script. The most
11 * interesting part is probably not the C code, but rather the
12 * <b>masterslave/masterslave_script.lua</b>, which demonstrates
13 * how to express the platform and deployment in lua.
18 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
19 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
21 /* Create a log channel to have nice outputs. */
23 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
24 "Messages specific for this msg example");
25 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
27 int master(int argc, char *argv[]);
28 int slave(int argc, char *argv[]);
29 MSG_error_t test_all(const char *);
31 /** Emitter function */
32 int master(int argc, char *argv[])
34 long number_of_tasks = atol(argv[1]);
35 double task_comp_size = atof(argv[2]);
36 double task_comm_size = atof(argv[3]);
37 long slaves_count = atol(argv[4]);
40 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
42 for (i = 0; i < number_of_tasks; i++) {
44 char sprintf_buffer[256];
47 sprintf(mailbox, "slave-%ld", i % slaves_count);
48 sprintf(sprintf_buffer, "Task_%d", i);
50 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
52 if (number_of_tasks < 10000 || i % 10000 == 0)
53 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
54 number_of_tasks, mailbox);
55 MSG_task_send(task, mailbox);
59 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
60 for (i = 0; i < slaves_count; i++) {
63 sprintf(mailbox, "slave-%ld", i % slaves_count);
64 m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
65 MSG_task_send(finalize, mailbox);
68 XBT_INFO("Goodbye now!");
72 /** Receiver function */
73 int slave(int argc, char *argv[])
76 _XBT_GNUC_UNUSED int res;
79 _XBT_GNUC_UNUSED int read;
81 read = sscanf(argv[1], "%d", &id);
82 xbt_assert(read, "Invalid argument %s\n", argv[1]);
84 sprintf(mailbox, "slave-%d", id);
87 res = MSG_task_receive(&(task), mailbox);
88 xbt_assert(res == MSG_OK, "MSG_task_get failed");
90 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
91 if (!strcmp(MSG_task_get_name(task), "finalize")) {
92 MSG_task_destroy(task);
96 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
97 MSG_task_execute(task);
98 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
99 MSG_task_destroy(task);
102 XBT_INFO("I'm done. See you!");
107 MSG_error_t test_all(const char *file) //(void)
109 MSG_error_t res = MSG_OK;
111 /*start by registering functions before loading script */
112 MSG_function_register("master", master);
113 MSG_function_register("slave", slave);
114 MSG_load_platform_script(file);
118 XBT_INFO("Simulation time %g", MSG_get_clock());
120 } /* end_of_test_all */
123 int main(int argc, char *argv[])
125 MSG_error_t res = MSG_OK;
127 MSG_global_init(&argc, argv);
129 printf("Usage: %s platform_script[.lua]\n", argv[0]);
130 printf("example: %s platform_script.lua\n", argv[0]);
133 res = test_all(argv[1]);