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 static int master(int argc, char *argv[])
13 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
14 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
15 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
16 long slaves_count = xbt_str_parse_int(argv[4], "Invalid amount of slaves: %s");
20 XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count, number_of_tasks);
22 for (i = 0; i < number_of_tasks; i++) {
24 char sprintf_buffer[256];
25 msg_task_t task = NULL;
27 sprintf(mailbox, "slave-%ld", i % slaves_count);
28 sprintf(sprintf_buffer, "Task_%d", i);
29 task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
30 if (number_of_tasks < 10000 || i % 10000 == 0)
31 XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
33 MSG_task_send(task, mailbox);
36 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
37 for (i = 0; i < slaves_count; i++) {
40 sprintf(mailbox, "slave-%ld", i % slaves_count);
41 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
42 MSG_task_send(finalize, mailbox);
48 static int slave(int argc, char *argv[])
50 msg_task_t task = NULL;
51 XBT_ATTRIB_UNUSED int res;
54 XBT_ATTRIB_UNUSED int read;
56 read = sscanf(argv[1], "%d", &id);
57 xbt_assert(read, "Invalid argument %s\n", argv[1]);
59 sprintf(mailbox, "slave-%d", id);
62 res = MSG_task_receive(&(task), mailbox);
63 xbt_assert(res == MSG_OK, "MSG_task_get failed");
65 // XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
66 if (!strcmp(MSG_task_get_name(task), "finalize")) {
67 MSG_task_destroy(task);
70 // XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
71 MSG_task_execute(task);
72 // XBT_INFO("\"%s\" done", MSG_task_get_name(task));
73 MSG_task_destroy(task);
76 XBT_INFO("I'm done. See you!");
80 int main(int argc, char *argv[])
82 MSG_init(&argc, argv);
83 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
84 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
86 MSG_create_environment(argv[1]);
88 MSG_function_register("master", master);
89 MSG_function_register("slave", slave);
90 MSG_launch_application(argv[2]);
92 msg_error_t res = MSG_main();
94 XBT_INFO("Simulation time %g", MSG_get_clock());