Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
have uniform log format for the same example
[simgrid.git] / examples / msg / app-masterworker / app-masterworker.c
1 /* Copyright (c) 2010-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_app_masterworker, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  *
13  *  - <b>Master/Workers: app-masterworker/app-masterworker.c</b>. This good old example is also very simple. Its
14  *    basic version is fully commented on this page: \ref MSG_ex_master_worker.
15  */
16
17 /** @brief Master expects 4 arguments given in the XML deployment file: */
18 static int master(int argc, char *argv[])
19 {
20   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");    /** - Number of tasks      */
21   double comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");  /** - Task compute cost    */
22   double comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");  /** - Task communication size */
23   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");    /** - Number of workers    */
24
25   int i;
26
27   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
28
29   for (i = 0; i < number_of_tasks; i++) {  /** For each task to be executed: */
30     char mailbox[256];
31     char task_name[256];
32
33     sprintf(mailbox, "worker-%ld", i % workers_count); /** - Select a @ref worker in a round-robin way */
34     sprintf(task_name, "Task_%d", i);
35     msg_task_t task = MSG_task_create(task_name, comp_size, comm_size, NULL);   /** - Create a task */
36     if (number_of_tasks < 10000 || i % 10000 == 0)
37       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
38
39     MSG_task_send(task, mailbox); /** - Send the task to the @ref worker */
40   }
41
42   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
43   for (i = 0; i < workers_count; i++) { /** - Eventually tell all the workers to stop by sending a "finalize" task */
44     char mailbox[80];
45
46     sprintf(mailbox, "worker-%ld", i % workers_count);
47     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
48     MSG_task_send(finalize, mailbox);
49   }
50
51   return 0;
52 }
53
54 /** @brief Worker expects a single argument given in the XML deployment file: */
55 static int worker(int argc, char *argv[])
56 {
57   msg_task_t task = NULL;
58   char mailbox[80];
59
60   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
61
62   sprintf(mailbox, "worker-%ld", id); /** - unique id of the worker */
63
64   while (1) {  /** The worker wait in an infinite loop for tasks sent by the \ref master */
65     int res = MSG_task_receive(&(task), mailbox);
66     xbt_assert(res == MSG_OK, "MSG_task_get failed");
67
68 //  XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
69     if (!strcmp(MSG_task_get_name(task), "finalize")) {
70       MSG_task_destroy(task);  /** - Exit if 'finalize' is received */
71       break;
72     }
73 //    XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
74     MSG_task_execute(task);    /**  - Otherwise, process the task */
75 //    XBT_INFO("\"%s\" done", MSG_task_get_name(task));
76     MSG_task_destroy(task);
77     task = NULL;
78   }
79   XBT_INFO("I'm done. See you!");
80   return 0;
81 }
82
83 int main(int argc, char *argv[])
84 {
85   MSG_init(&argc, argv);
86   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
87              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
88
89   MSG_create_environment(argv[1]);          /** - Load the platform description */
90
91   MSG_function_register("master", master);  /** - Register the function to be executed by the processes */
92   MSG_function_register("worker", worker);
93   MSG_launch_application(argv[2]);          /** - Deploy the application */
94
95   msg_error_t res = MSG_main();             /** - Run the simulation */
96
97   XBT_INFO("Simulation time %g", MSG_get_clock());
98
99   return res != MSG_OK;
100 }