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 static long my_random(long n)
28 return n * (rand() / ((double)RAND_MAX + 1));
31 /** Emitter function */
32 int master(int argc, char *argv[])
36 for (i = 1; i <= number_of_jobs; i++) {
38 char sprintf_buffer[256];
41 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
42 sprintf(sprintf_buffer, "Task_%d", i);
44 MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
46 XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
47 number_of_jobs, mailbox);
49 MSG_task_send(task, mailbox);
53 ("All tasks have been dispatched. Let's tell everybody the computation is over.");
54 for (i = 0; i < number_of_slaves; i++) {
57 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
58 m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
59 MSG_task_send(finalize, mailbox);
62 XBT_DEBUG("Goodbye now!");
66 /** Receiver function */
67 int slave(int argc, char *argv[])
70 _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_function_register("master", master);
106 MSG_function_register("slave", slave);
108 MSG_create_environment(argv[1]);
110 number_of_jobs = atol(argv[2]);
111 number_of_slaves = atol(argv[3]);
112 xbt_dynar_t host_dynar = MSG_hosts_as_dynar();
113 long number_max = xbt_dynar_length(host_dynar);
114 XBT_INFO("Got %ld slaves, %ld tasks to process, and %ld hosts", number_of_slaves, number_of_jobs,number_max);
116 m_host_t *host_table = xbt_dynar_to_array(host_dynar);
117 //xbt_dynar_free(&host_dynar);
119 MSG_process_create( "master",
122 host_table[my_random(number_max)]
125 for(i = 0 ; i<number_of_slaves; i++)
127 char* name_host = bprintf("slave-%ld",i);
128 MSG_process_create( name_host,
131 host_table[my_random(number_max)]
135 xbt_free(host_table);
139 XBT_INFO("Simulation time %g", MSG_get_clock());