1 /* Copyright (c) 2007-2010, 2013-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 FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
13 /** Master expects 3+ arguments given in the XML deployment file: */
14 static int master(int argc, char *argv[])
16 int workers_count = 0;
17 msg_host_t *workers = NULL;
18 msg_task_t *todo = NULL;
22 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
23 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
24 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
27 char sprintf_buffer[64];
29 todo = xbt_new0(msg_task_t, number_of_tasks);
31 for (i = 0; i < number_of_tasks; i++) {
32 sprintf(sprintf_buffer, "Task_%d", i);
33 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
37 { /* Process organization */
38 workers_count = argc - 4;
39 workers = xbt_new0(msg_host_t, workers_count);
41 for (i = 4; i < argc; i++) {
42 workers[i - 4] = MSG_get_host_by_name(argv[i]);
43 xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
47 XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
49 for (i = 0; i < number_of_tasks; i++) {
50 XBT_INFO("Sending \"%s\" to \"%s\"", todo[i]->name, MSG_host_get_name(workers[i % workers_count]));
51 if (MSG_host_self() == workers[i % workers_count]) {
52 XBT_INFO("Hey ! It's me ! :)");
55 MSG_task_send(todo[i], MSG_host_get_name(workers[i % workers_count]));
59 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
60 for (i = 0; i < workers_count; i++) {
61 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
62 MSG_task_send(finalize, MSG_host_get_name(workers[i]));
65 XBT_INFO("Goodbye now!");
71 /** Worker expects a single argument given in the XML deployment file: */
72 static int worker(int argc, char *argv[])
74 msg_task_t task = NULL;
75 XBT_ATTRIB_UNUSED int res;
77 res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
78 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
80 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
81 if (!strcmp(MSG_task_get_name(task), "finalize")) {
82 MSG_task_destroy(task);
86 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
87 MSG_task_execute(task);
88 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
89 MSG_task_destroy(task);
92 XBT_INFO("I'm done. See you!");
97 int main(int argc, char *argv[])
99 msg_error_t res = MSG_OK;
101 MSG_init(&argc, argv);
102 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
103 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
104 { /* Simulation setting */
105 MSG_create_environment(argv[1]);
107 { /* Application deployment */
108 MSG_function_register("master", master);
109 MSG_function_register("worker", worker);
110 MSG_launch_application(argv[2]);
114 XBT_INFO("Simulation time %g", MSG_get_clock());
115 return (res != MSG_OK);