1 /* Copyright (c) 2007-2017. 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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
12 /** Master expects 3+ arguments given in the XML deployment file: */
13 static int master(int argc, char *argv[])
15 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s"); /** - Number of tasks */
16 double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - Task compute cost */
17 double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - Task communication size */
19 /* Create the tasks in advance */
20 msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
22 for (int i = 0; i < number_of_tasks; i++) {
23 char sprintf_buffer[64];
24 sprintf(sprintf_buffer, "Task_%d", i);
25 todo[i] = MSG_task_create(sprintf_buffer, comp_size, comm_size, NULL);
28 /* Get the info about the worker processes from my parameters */
29 int workers_count = argc - 4;
30 msg_host_t* workers = xbt_new0(msg_host_t, workers_count);
32 for (int i = 4; i < argc; i++) {
33 workers[i - 4] = MSG_get_host_by_name(argv[i]);
34 xbt_assert(workers[i - 4] != NULL, "Unknown host %s. Stopping Now! ", argv[i]);
36 XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
38 /* Dispatch the tasks */
39 for (int i = 0; i < number_of_tasks; i++) {
40 XBT_INFO("Sending '%s' to '%s'", todo[i]->name, MSG_host_get_name(workers[i % workers_count]));
41 if (MSG_host_self() == workers[i % workers_count]) {
42 XBT_INFO("Hey ! It's me ! :)");
45 MSG_task_send(todo[i], MSG_host_get_name(workers[i % workers_count]));
49 XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
50 for (int i = 0; i < workers_count; i++) {
51 msg_task_t finalize = MSG_task_create("finalize", 0, 0, FINALIZE);
52 MSG_task_send(finalize, MSG_host_get_name(workers[i]));
55 XBT_INFO("Goodbye now!");
61 /** Worker expects a single argument given in the XML deployment file: */
62 static int worker(int argc, char *argv[])
65 msg_task_t task = NULL;
66 int res = MSG_task_receive(&(task), MSG_host_get_name(MSG_host_self()));
67 xbt_assert(res == MSG_OK, "MSG_task_receive failed");
69 XBT_INFO("Received '%s'", MSG_task_get_name(task));
70 if (!strcmp(MSG_task_get_name(task), "finalize")) {
71 MSG_task_destroy(task);
75 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
76 MSG_task_execute(task);
77 XBT_INFO("'%s' done", MSG_task_get_name(task));
78 MSG_task_destroy(task);
80 XBT_INFO("I'm done. See you!");
85 int main(int argc, char *argv[])
88 MSG_init(&argc, argv);
89 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
90 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
92 /* Create a simulated platform */
93 MSG_create_environment(argv[1]);
95 /* Application deployment */
96 MSG_function_register("master", master);
97 MSG_function_register("worker", worker);
98 MSG_launch_application(argv[2]);
100 /* Run the simulation */
101 msg_error_t res = MSG_main();
103 XBT_INFO("Simulation time %g", MSG_get_clock());
104 return (res != MSG_OK);