1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
11 /* Create a log channel to have nice outputs. */
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
15 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 MSG_error_t test_all(const char *);
21 /** Emitter function */
22 int master(int argc, char *argv[])
24 long number_of_tasks = atol(argv[1]);
25 double task_comp_size = atof(argv[2]);
26 double task_comm_size = atof(argv[3]);
27 long slaves_count = atol(argv[4]);
30 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
32 for (i = 0; i < number_of_tasks; i++) {
34 char sprintf_buffer[256];
37 sprintf(mailbox, "slave-%ld", i % slaves_count);
38 sprintf(sprintf_buffer, "Task_%d", i);
40 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
42 if (number_of_tasks < 10000 || i % 10000 == 0)
43 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
44 number_of_tasks, mailbox);
45 MSG_task_send(task, mailbox);
49 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
50 for (i = 0; i < slaves_count; i++) {
53 sprintf(mailbox, "slave-%ld", i % slaves_count);
54 m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
55 MSG_task_send(finalize, mailbox);
58 XBT_INFO("Goodbye now!");
62 /** Receiver function */
63 int slave(int argc, char *argv[])
66 _XBT_GNUC_UNUSED int res;
69 _XBT_GNUC_UNUSED int read;
71 read = sscanf(argv[1], "%d", &id);
72 xbt_assert(read, "Invalid argument %s\n", argv[1]);
74 sprintf(mailbox, "slave-%d", id);
77 res = MSG_task_receive(&(task), mailbox);
78 xbt_assert(res == MSG_OK, "MSG_task_get failed");
80 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
81 if (!strcmp(MSG_task_get_name(task), "finalize")) {
82 MSG_task_destroy(task);
86 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
87 MSG_task_execute(task);
88 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
89 MSG_task_destroy(task);
92 XBT_INFO("I'm done. See you!");
97 MSG_error_t test_all(const char *file) //(void)
99 MSG_error_t res = MSG_OK;
101 /*start by registering functions before loading script */
102 MSG_function_register("master", master);
103 MSG_function_register("slave", slave);
104 MSG_load_platform_script(file);
108 XBT_INFO("Simulation time %g", MSG_get_clock());
110 } /* end_of_test_all */
113 int main(int argc, char *argv[])
115 MSG_error_t res = MSG_OK;
117 MSG_global_init(&argc, argv);
119 printf("Usage: %s platform_script[.lua]\n", argv[0]);
120 printf("example: %s platform_script.lua\n", argv[0]);
123 res = test_all(argv[1]);