1 /* Copyright (c) 2010-2015. 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 "simgrid/msg.h" /* Yeah! If you want to use msg, you need to include simgrid/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 #define task_comp_size 50000000
18 #define task_comm_size 1000000
21 long number_of_slaves;
23 static long my_random(long n)
25 return n * (rand() / ((double)RAND_MAX + 1));
28 /** Emitter function */
29 static int master(int argc, char *argv[])
33 for (i = 1; i <= number_of_jobs; i++) {
35 char sprintf_buffer[256];
36 msg_task_t task = NULL;
38 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
39 sprintf(sprintf_buffer, "Task_%d", i);
40 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
41 XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
42 number_of_jobs, mailbox);
44 MSG_task_send(task, mailbox);
47 XBT_DEBUG("All tasks have been dispatched. Let's tell everybody the computation is over.");
48 for (i = 0; i < number_of_slaves; i++) {
51 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
52 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
53 MSG_task_send(finalize, mailbox);
56 XBT_DEBUG("Goodbye now!");
60 /** Receiver function */
61 static int slave(int argc, char *argv[])
63 msg_task_t task = NULL;
64 XBT_ATTRIB_UNUSED int res;
66 XBT_DEBUG("mailbox: %s",MSG_process_get_name(MSG_process_self()));
68 res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
69 xbt_assert(res == MSG_OK, "MSG_task_get failed");
71 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
72 if (!strcmp(MSG_task_get_name(task), "finalize")) {
73 MSG_task_destroy(task);
76 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
77 MSG_task_execute(task);
78 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
79 MSG_task_destroy(task);
83 } /* end_of_slave */ /* end_of_test_all */
86 int main(int argc, char *argv[])
88 msg_error_t res = MSG_OK;
91 MSG_init(&argc, argv);
92 xbt_assert(argc > 3, "Usage: %s platform_file number_of_jobs number_of_slaves\n"
93 "\tExample: %s msg_platform.xml 10 5\n",
96 MSG_function_register("master", master);
97 MSG_function_register("slave", slave);
99 MSG_create_environment(argv[1]);
101 number_of_jobs = xbt_str_parse_int(argv[2], "Invalid amount of jobs: %s");
102 number_of_slaves = xbt_str_parse_int(argv[3], "Invalid amount of slaves: %s");
103 xbt_dynar_t host_dynar = MSG_hosts_as_dynar();
104 long number_max = xbt_dynar_length(host_dynar);
105 XBT_INFO("Got %ld slaves, %ld tasks to process, and %ld hosts", number_of_slaves, number_of_jobs,number_max);
107 msg_host_t *host_table = xbt_dynar_to_array(host_dynar);
109 MSG_process_create( "master", master,
111 host_table[my_random(number_max)]
114 for(i = 0 ; i<number_of_slaves; i++) {
115 char* name_host = bprintf("slave-%ld",i);
116 MSG_process_create( name_host, slave,
118 host_table[my_random(number_max)]
122 xbt_free(host_table);
126 XBT_INFO("Simulation time %g", MSG_get_clock());
128 return res != MSG_OK;