1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "simgrid/msg.h"
8 #include <stdio.h> /* sprintf */
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
12 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
14 /** Master expects 3+ arguments given in the XML deployment file: */
15 static int master(int argc, char* argv[])
17 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
18 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
19 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
21 /* Create the tasks in advance */
22 msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
24 for (int i = 0; i < number_of_tasks; i++) {
25 char sprintf_buffer[64];
26 sprintf(sprintf_buffer, "Task_%d", i);
27 todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
30 /* Get the info about the worker processes from my parameters */
31 int worker_count = argc - 4;
32 msg_host_t* workers = xbt_new0(msg_host_t, worker_count);
34 for (int i = 4; i < argc; i++) {
35 workers[i - 4] = MSG_get_host_by_name(argv[i]);
36 xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
38 XBT_INFO("Got %d workers and %ld tasks to process", worker_count, number_of_tasks);
40 /* Dispatch the tasks */
41 for (int i = 0; i < number_of_tasks; i++) {
42 XBT_INFO("Sending '%s' to '%s'", todo[i]->name, MSG_host_get_name(workers[i % worker_count]));
43 if (MSG_host_self() == workers[i % worker_count]) {
44 XBT_INFO("Hey ! It's me ! :)");
47 MSG_task_send(todo[i], MSG_host_get_name(workers[i % worker_count]));
51 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
52 for (int i = 0; i < worker_count; i++) {
53 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
54 MSG_task_send(finalize, MSG_host_get_name(workers[i]));
57 XBT_INFO("Goodbye now!");
63 /** Worker does not expect any argument from XML deployment file. */
64 static int worker(int argc, char* argv[])
67 msg_task_t task = NULL;
68 int res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
69 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
71 XBT_INFO("Received '%s'", MSG_task_get_name(task));
72 if (!strcmp(MSG_task_get_name(task), "finalize")) {
73 MSG_task_destroy(task);
77 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
78 MSG_task_execute(task);
79 XBT_INFO("'%s' done", MSG_task_get_name(task));
80 MSG_task_destroy(task);
82 XBT_INFO("I'm done. See you!");
87 int main(int argc, char* argv[])
90 MSG_init(&argc, argv);
92 "Usage: %s platform_file deployment_file\n"
93 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
96 /* Create a simulated platform */
97 MSG_create_environment(argv[1]);
99 /* Application deployment */
100 MSG_function_register("master", master);
101 MSG_function_register("worker", worker);
102 MSG_launch_application(argv[2]);
104 /* Run the simulation */
105 msg_error_t res = MSG_main();
107 XBT_INFO("Simulation time %g", MSG_get_clock());
108 return (res != MSG_OK);