Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cover with a test Mailbox::ready() method introduced in commit 1ed0e64dc405fbb627ff8a...
[simgrid.git] / doc / tuto-msg / masterworker.c
1 /* Copyright (c) 2007-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/msg.h"
7
8 #include <stdio.h> /* sprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11
12 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
13
14 /** Master expects 3+ arguments given in the XML deployment file: */
15 static int master(int argc, char* argv[])
16 {
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 */
20
21   /* Create the tasks in advance */
22   msg_task_t* todo = xbt_new0(msg_task_t, number_of_tasks);
23
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);
28   }
29
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);
33
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]);
37   }
38   XBT_INFO("Got %d workers and %ld tasks to process", worker_count, number_of_tasks);
39
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 ! :)");
45     }
46
47     MSG_task_send(todo[i], MSG_host_get_name(workers[i % worker_count]));
48     XBT_INFO("Sent");
49   }
50
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]));
55   }
56
57   XBT_INFO("Goodbye now!");
58   free(workers);
59   free(todo);
60   return 0;
61 }
62
63 /** Worker does not expect any argument from XML deployment file. */
64 static int worker(int argc, char* argv[])
65 {
66   while (1) {
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");
70
71     XBT_INFO("Received '%s'", MSG_task_get_name(task));
72     if (!strcmp(MSG_task_get_name(task), "finalize")) {
73       MSG_task_destroy(task);
74       break;
75     }
76
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);
81   }
82   XBT_INFO("I'm done. See you!");
83   return 0;
84 } /* end_of_worker */
85
86 /** Main function */
87 int main(int argc, char* argv[])
88 {
89
90   MSG_init(&argc, argv);
91   xbt_assert(argc > 2,
92              "Usage: %s platform_file deployment_file\n"
93              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
94              argv[0], argv[0]);
95
96   /*  Create a simulated platform */
97   MSG_create_environment(argv[1]);
98
99   /*   Application deployment */
100   MSG_function_register("master", master);
101   MSG_function_register("worker", worker);
102   MSG_launch_application(argv[2]);
103
104   /* Run the simulation */
105   msg_error_t res = MSG_main();
106
107   XBT_INFO("Simulation time %g", MSG_get_clock());
108   return (res != MSG_OK);
109 } /* end_of_main */