Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f9121bf54c92a2b1800cd5079cb05e266a8d6459
[simgrid.git] / examples / c / app-masterworker / app-masterworker.c
1 /* Copyright (c) 2010-2021. 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/actor.h"
7 #include "simgrid/engine.h"
8 #include "simgrid/forward.h"
9 #include "simgrid/mailbox.h"
10 #include "xbt/asserts.h"
11 #include "xbt/log.h"
12 #include "xbt/str.h"
13
14 #define FINALIZE 221297 /* a magic number to tell people to stop working */
15
16 #include <stdio.h> /* snprintf */
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(app_masterworker, "Messages specific for this example");
19
20 /* Main function of the master process */
21 static void master(int argc, char* argv[])
22 {
23   xbt_assert(argc == 5, "The master function expects 4 arguments from the XML deployment file");
24   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");       /* - Number of tasks      */
25   double comp_size     = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /* - Compute cost    */
26   long comm_size       = xbt_str_parse_int(argv[3], "Invalid communication size: %s");    /* - Communication size */
27   long workers_count   = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");     /* - Number of workers    */
28
29   XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
30
31   for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */
32     char mailbox_name[80];
33     char task_name[80];
34     double* payload = xbt_malloc(sizeof(double));
35     snprintf(mailbox_name, 79, "worker-%ld", i % workers_count); /* - Select a @ref worker in a round-robin way */
36     snprintf(task_name, 79, "Task_%d", i);
37
38     sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
39     *payload             = comp_size;
40
41     if (number_of_tasks < 10000 || i % 10000 == 0)
42       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task_name, number_of_tasks, mailbox_name);
43
44     sg_mailbox_put(mailbox, payload, comm_size); /* - Send the amount of flops to compute to the @ref worker */
45   }
46
47   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
48   for (int i = 0; i < workers_count; i++) { /* - Eventually tell all the workers to stop by sending a "finalize" task */
49     char mailbox_name[80];
50     snprintf(mailbox_name, 79, "worker-%ld", i % workers_count);
51     double* payload      = xbt_malloc(sizeof(double));
52     sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
53     *payload             = FINALIZE;
54     sg_mailbox_put(mailbox, payload, 0);
55   }
56 }
57
58 /* Main functions of the Worker processes */
59 static void worker(int argc, char* argv[])
60 {
61   xbt_assert(argc == 2,
62              "The worker expects a single argument from the XML deployment file: its worker ID (its numerical rank)");
63   char mailbox_name[80];
64
65   long id = xbt_str_parse_int(argv[1], "Invalid argument %s");
66
67   snprintf(mailbox_name, 79, "worker-%ld", id);
68   sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
69
70   while (1) { /* The worker wait in an infinite loop for tasks sent by the @ref master */
71     double* payload = (double*)sg_mailbox_get(mailbox);
72
73     if (*payload == FINALIZE) {
74       free(payload); /* - Exit if 'finalize' is received */
75       break;
76     }
77     sg_actor_execute(*payload); /*  - Otherwise, process the received number of flops*/
78     free(payload);
79   }
80   XBT_INFO("I'm done. See you!");
81 }
82
83 int main(int argc, char* argv[])
84 {
85   simgrid_init(&argc, argv);
86   xbt_assert(argc > 2,
87              "Usage: %s platform_file deployment_file\n"
88              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
89              argv[0], argv[0]);
90
91   simgrid_load_platform(argv[1]); /* - Load the platform description */
92
93   simgrid_register_function("master", master); /* - Register the function to be executed by the processes */
94   simgrid_register_function("worker", worker);
95   simgrid_load_deployment(argv[2]); /* - Deploy the application */
96
97   simgrid_run(); /* - Run the simulation */
98
99   XBT_INFO("Simulation time %g", simgrid_get_clock());
100
101   return 0;
102 }