Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / examples / msg / app-masterworker / app-masterworker.c
1 /* Copyright (c) 2010-2016. 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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_app_masterworker, "Messages specific for this msg example");
9
10
11 /* Main function of the master process */
12 static int master(int argc, char *argv[])
13 {
14   xbt_assert(argc==5, "The master function expects 4 arguments from the XML deployment file");
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 */
18   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");    /* - Number of workers    */
19
20   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
21
22   for (int i = 0; i < number_of_tasks; i++) {  /* For each task to be executed: */
23     char mailbox[80];
24     char task_name[80];
25
26     snprintf(mailbox,79, "worker-%ld", i % workers_count); /* - Select a @ref worker in a round-robin way */
27     snprintf(task_name,79, "Task_%d", i);
28     msg_task_t task = MSG_task_create(task_name, comp_size, comm_size, NULL);   /* - Create a task */
29     if (number_of_tasks < 10000 || i % 10000 == 0)
30       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
31
32     MSG_task_send(task, mailbox); /* - Send the task to the @ref worker */
33   }
34
35   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
36   for (int i = 0; i < workers_count; i++) { /* - Eventually tell all the workers to stop by sending a "finalize" task */
37     char mailbox[80];
38
39     snprintf(mailbox,79, "worker-%ld", i % workers_count);
40     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
41     MSG_task_send(finalize, mailbox);
42   }
43
44   return 0;
45 }
46
47 /* Main functions of the Worker processes */
48 static int worker(int argc, char *argv[])
49 {
50   xbt_assert(argc==2, "The worker expects a single argument from the XML deployment file: its worker ID (its numerical rank)");
51   char mailbox[80];
52
53   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
54
55   snprintf(mailbox,79, "worker-%ld", id);
56
57   while (1) {  /* The worker wait in an infinite loop for tasks sent by the \ref master */
58     msg_task_t task = NULL;
59     int res = MSG_task_receive(&task, mailbox);
60     xbt_assert(res == MSG_OK, "MSG_task_get failed");
61
62     if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
63       MSG_task_destroy(task);  /* - Exit if 'finalize' is received */
64       break;
65     }
66     MSG_task_execute(task);    /*  - Otherwise, process the task */
67     MSG_task_destroy(task);
68   }
69   XBT_INFO("I'm done. See you!");
70   return 0;
71 }
72
73 int main(int argc, char *argv[])
74 {
75   MSG_init(&argc, argv);
76   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
77              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
78
79   MSG_create_environment(argv[1]);          /* - Load the platform description */
80
81   MSG_function_register("master", master);  /* - Register the function to be executed by the processes */
82   MSG_function_register("worker", worker);
83   MSG_launch_application(argv[2]);          /* - Deploy the application */
84
85   msg_error_t res = MSG_main();             /* - Run the simulation */
86
87   XBT_INFO("Simulation time %g", MSG_get_clock());
88
89   return res != MSG_OK;
90 }