Logo AND Algorithmique Numérique Distribuée

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