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 *);
26 /** Emitter function */
27 int master(int argc, char *argv[])
29 long number_of_tasks = atol(argv[1]);
30 double task_comp_size = atof(argv[2]);
31 double task_comm_size = atof(argv[3]);
32 long slaves_count = atol(argv[4]);
35 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
37 for (i = 0; i < number_of_tasks; i++) {
39 char sprintf_buffer[256];
42 sprintf(mailbox, "slave-%ld", i % slaves_count);
43 sprintf(sprintf_buffer, "Task_%d", i);
45 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
47 if (number_of_tasks < 10000 || i % 10000 == 0)
48 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
49 number_of_tasks, mailbox);
50 MSG_task_send(task, mailbox);
54 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
55 for (i = 0; i < slaves_count; i++) {
58 sprintf(mailbox, "slave-%ld", i % slaves_count);
59 m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
60 MSG_task_send(finalize, mailbox);
63 XBT_INFO("Goodbye now!");
67 /** Receiver function */
68 int slave(int argc, char *argv[])
76 read = sscanf(argv[1], "%d", &id);
77 xbt_assert(read, "Invalid argument %s\n", argv[1]);
79 sprintf(mailbox, "slave-%d", id);
82 res = MSG_task_receive(&(task), mailbox);
83 xbt_assert(res == MSG_OK, "MSG_task_get failed");
85 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
86 if (!strcmp(MSG_task_get_name(task), "finalize")) {
87 MSG_task_destroy(task);
91 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
92 MSG_task_execute(task);
93 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
94 MSG_task_destroy(task);
97 XBT_INFO("I'm done. See you!");
102 MSG_error_t test_all(const char *file) //(void)
104 MSG_error_t res = MSG_OK;
105 /* Simulation setting */
106 MSG_set_channel_number(MAX_CHANNEL);
107 /*start by registering functions before loading script */
108 MSG_function_register("master", master);
109 MSG_function_register("slave", slave);
110 MSG_load_platform_script(file);
114 XBT_INFO("Simulation time %g", MSG_get_clock());
116 } /* end_of_test_all */
119 int main(int argc, char *argv[])
121 MSG_error_t res = MSG_OK;
123 MSG_global_init(&argc, argv);
125 printf("Usage: %s platform_script[.lua]\n", argv[0]);
126 printf("example: %s platform_script.lua\n", argv[0]);
129 res = test_all(argv[1]);