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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 #define task_comp_size 50000000
12 #define task_comm_size 1000000
15 long number_of_slaves;
17 static long my_random(long n)
19 return n * (rand() / ((double)RAND_MAX + 1));
22 static int master(int argc, char *argv[])
26 for (i = 1; i <= number_of_jobs; i++) {
28 char sprintf_buffer[256];
29 msg_task_t task = NULL;
31 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
32 sprintf(sprintf_buffer, "Task_%d", i);
33 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
34 XBT_DEBUG("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_jobs, mailbox);
36 MSG_task_send(task, mailbox);
39 XBT_DEBUG("All tasks have been dispatched. Let's tell everybody the computation is over.");
40 for (i = 0; i < number_of_slaves; i++) {
43 sprintf(mailbox, "slave-%ld", i % number_of_slaves);
44 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
45 MSG_task_send(finalize, mailbox);
48 XBT_DEBUG("Goodbye now!");
52 static int slave(int argc, char *argv[])
54 msg_task_t task = NULL;
55 XBT_ATTRIB_UNUSED int res;
57 XBT_DEBUG("mailbox: %s",MSG_process_get_name(MSG_process_self()));
59 res = MSG_task_receive(&(task), MSG_process_get_name(MSG_process_self()));
60 xbt_assert(res == MSG_OK, "MSG_task_get failed");
62 XBT_DEBUG("Received \"%s\"", MSG_task_get_name(task));
63 if (!strcmp(MSG_task_get_name(task), "finalize")) {
64 MSG_task_destroy(task);
67 XBT_DEBUG("Processing \"%s\"", MSG_task_get_name(task));
68 MSG_task_execute(task);
69 XBT_DEBUG("\"%s\" done", MSG_task_get_name(task));
70 MSG_task_destroy(task);
76 int main(int argc, char *argv[])
78 msg_error_t res = MSG_OK;
81 MSG_init(&argc, argv);
82 xbt_assert(argc > 3, "Usage: %s platform_file number_of_jobs number_of_slaves\n"
83 "\tExample: %s msg_platform.xml 10 5\n", argv[0], argv[0]);
85 MSG_function_register("master", master);
86 MSG_function_register("slave", slave);
88 MSG_create_environment(argv[1]);
90 number_of_jobs = xbt_str_parse_int(argv[2], "Invalid amount of jobs: %s");
91 number_of_slaves = xbt_str_parse_int(argv[3], "Invalid amount of slaves: %s");
92 xbt_dynar_t host_dynar = MSG_hosts_as_dynar();
93 long number_max = xbt_dynar_length(host_dynar);
94 XBT_INFO("Got %ld slaves, %ld tasks to process, and %ld hosts", number_of_slaves, number_of_jobs,number_max);
96 msg_host_t *host_table = xbt_dynar_to_array(host_dynar);
98 MSG_process_create("master", master, NULL, host_table[my_random(number_max)]);
100 for(i = 0 ; i<number_of_slaves; i++) {
101 char* name_host = bprintf("slave-%ld",i);
102 MSG_process_create( name_host, slave, NULL, host_table[my_random(number_max)]);
105 xbt_free(host_table);
109 XBT_INFO("Simulation time %g", MSG_get_clock());
111 return res != MSG_OK;