1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved. */
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. */
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"
14 #define FINALIZE 221297 /* a magic number to tell people to stop working */
16 #include <stdio.h> /* snprintf */
18 XBT_LOG_NEW_DEFAULT_CATEGORY(app_masterworker, "Messages specific for this example");
20 /* Main function of the master process */
21 static void master(int argc, char* argv[])
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 */
29 XBT_INFO("Got %ld workers and %ld tasks to process", workers_count, number_of_tasks);
31 for (int i = 0; i < number_of_tasks; i++) { /* For each task to be executed: */
32 char mailbox_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);
38 sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
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);
44 sg_mailbox_put(mailbox, payload, comm_size); /* - Send the amount of flops to compute to the @ref worker */
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);
54 sg_mailbox_put(mailbox, payload, 0);
58 /* Main functions of the Worker processes */
59 static void worker(int argc, char* argv[])
62 "The worker expects a single argument from the XML deployment file: its worker ID (its numerical rank)");
63 char mailbox_name[80];
65 long id = xbt_str_parse_int(argv[1], "Invalid argument %s");
67 snprintf(mailbox_name, 79, "worker-%ld", id);
68 sg_mailbox_t mailbox = sg_mailbox_by_name(mailbox_name);
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);
73 if (*payload == FINALIZE) {
74 free(payload); /* - Exit if 'finalize' is received */
77 sg_actor_execute(*payload); /* - Otherwise, process the received number of flops*/
80 XBT_INFO("I'm done. See you!");
83 int main(int argc, char* argv[])
85 simgrid_init(&argc, argv);
87 "Usage: %s platform_file deployment_file\n"
88 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
91 simgrid_load_platform(argv[1]); /* - Load the platform description */
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 */
97 simgrid_run(); /* - Run the simulation */
99 XBT_INFO("Simulation time %g", simgrid_get_clock());