1 /* Copyright (c) 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 "xbt/sysdep.h" /* calloc, printf */
11 /* Create a log channel to have nice outputs. */
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15 "Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
20 #define task_comp_size 50000000
21 #define task_comm_size 1000000
24 long number_of_slaves;
26 long my_random(long n) {
27 return n * (rand() / ((double)RAND_MAX + 1));
30 /** Emitter function */
31 int master(int argc, char *argv[])
35 for (i = 1; i <= number_of_jobs; i++) {
37 char sprintf_buffer[256];
40 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
41 sprintf(sprintf_buffer, "Task_%d", i);
43 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
45 XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
46 number_of_jobs, mailbox);
48 MSG_task_send(task, mailbox);
52 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
53 for (i = 0; i < number_of_slaves; i++) {
56 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
57 m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
58 MSG_task_send(finalize, mailbox);
61 XBT_DEBUG("Goodbye now!");
65 /** Receiver function */
66 int slave(int argc, char *argv[])
69 _XBT_GNUC_UNUSED int res;
72 XBT_DEBUG("mailbox: %s",MSG_process_get_name(MSG_process_self()));
74 res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
75 xbt_assert(res == MSG_OK, "MSG_task_get failed");
77 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
78 if (!strcmp(MSG_task_get_name(task), "finalize")) {
79 MSG_task_destroy(task);
82 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
83 MSG_task_execute(task);
84 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
85 MSG_task_destroy(task);
88 XBT_DEBUG("I'm done. See you!");
90 } /* end_of_slave */ /* end_of_test_all */
93 int main(int argc, char *argv[])
95 MSG_error_t res = MSG_OK;
98 MSG_global_init(&argc, argv);
100 printf("Usage: %s platform_file number_of_jobs number_of_slaves\n", argv[0]);
101 printf("example: %s msg_platform.xml 10 5\n", argv[0]);
105 MSG_set_channel_number(0);
106 MSG_function_register("master", master);
107 MSG_function_register("slave", slave);
109 MSG_create_environment(argv[1]);
111 number_of_jobs = atol(argv[2]);
112 number_of_slaves = atol(argv[3]);
113 long number_max = MSG_get_host_number();
114 XBT_INFO("Got %ld slaves, %ld tasks to process, and %d hosts", number_of_slaves, number_of_jobs,MSG_get_host_number());
116 m_host_t *host_table = MSG_get_host_table();
118 MSG_process_create( "master",
121 host_table[my_random(number_max)]
124 for(i = 0 ; i<number_of_slaves; i++)
126 char* name_host = bprintf("slave-%ld",i);
127 MSG_process_create( name_host,
130 host_table[my_random(number_max)]
134 xbt_free(host_table);
138 XBT_INFO("Simulation time %g", MSG_get_clock());