Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ensure that the dict subsystem is initialized when creating a dict
[simgrid.git] / doc / msg-tuto-src / masterworker0.c
1 /* Copyright (c) 2007-2010, 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
12
13 /** Master expects 3+ arguments given in the XML deployment file: */
14 static int master(int argc, char *argv[])
15 {
16   int workers_count = 0;
17   msg_host_t *workers = NULL;
18   msg_task_t *todo = NULL;
19
20   int i;
21
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 */
25
26   {                             /*  Task creation */
27     char sprintf_buffer[64];
28
29     todo = xbt_new0(msg_task_t, number_of_tasks);
30
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);
34     }
35   }
36
37   {                             /* Process organization */
38     workers_count = argc - 4;
39     workers = xbt_new0(msg_host_t, workers_count);
40
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]);
44     }
45   }
46
47   XBT_INFO("Got %d workers and %ld tasks to process", workers_count, number_of_tasks);
48
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 ! :)");
53     }
54
55     MSG_task_send(todo[i], MSG_host_get_name(workers[i % workers_count]));
56     XBT_INFO("Sent");
57   }
58
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]));
63   }
64
65   XBT_INFO("Goodbye now!");
66   free(workers);
67   free(todo);
68   return 0;
69 }                               /* end_of_master */
70
71 /** Worker expects a single argument given in the XML deployment file: */
72 static int worker(int argc, char *argv[])
73 {
74   msg_task_t task = NULL;
75   XBT_ATTRIB_UNUSED int res;
76   while (1) {
77     res = MSG_task_receive(&(task),MSG_host_get_name(MSG_host_self()));
78     xbt_assert(res == MSG_OK, "MSG_task_receive failed");
79
80     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
81     if (!strcmp(MSG_task_get_name(task), "finalize")) {
82       MSG_task_destroy(task);
83       break;
84     }
85
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);
90     task = NULL;
91   }
92   XBT_INFO("I'm done. See you!");
93   return 0;
94 }                               /* end_of_worker */
95
96 /** Main function */
97 int main(int argc, char *argv[])
98 {
99   msg_error_t res = MSG_OK;
100
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]);
106   }
107   {                             /*   Application deployment */
108     MSG_function_register("master", master);
109     MSG_function_register("worker", worker);
110     MSG_launch_application(argv[2]);
111   }
112   res = MSG_main();
113
114   XBT_INFO("Simulation time %g", MSG_get_clock());
115   return (res != MSG_OK);
116 }                               /* end_of_main */