1 /* Copyright (c) 2010-2018. 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/msg.h"
8 #include <stdio.h> /* snprintf */
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
12 static int sender(int argc, char* argv[])
14 xbt_assert(argc == 5, "This function expects 4 parameters from the XML deployment file");
15 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
16 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
17 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
18 long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
20 msg_comm_t* comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
21 for (int i = 0; i < number_of_tasks; i++) {
24 snprintf(mailbox, 79, "receiver-%ld", i % receivers_count);
25 snprintf(taskname, 79, "Task_%d", i);
26 msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size, NULL);
27 comm[i] = MSG_task_isend(task, mailbox);
28 XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
30 for (int i = 0; i < receivers_count; i++) {
32 snprintf(mailbox, 79, "receiver-%ld", i % receivers_count);
33 msg_task_t task = MSG_task_create("finalize", 0, 0, 0);
34 comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
35 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
38 /* Here we are waiting for the completion of all communications */
39 MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
40 for (int i = 0; i < number_of_tasks + receivers_count; i++)
41 MSG_comm_destroy(comm[i]);
43 XBT_INFO("Goodbye now!");
48 static int receiver(int argc, char* argv[])
50 xbt_assert(argc == 2, "This function expects 1 parameter from the XML deployment file");
51 int id = xbt_str_parse_int(argv[1], "Any process of this example must have a numerical name, not %s");
54 snprintf(mailbox, 79, "receiver-%d", id);
56 MSG_process_sleep(10);
58 XBT_INFO("Wait to receive a task");
59 msg_task_t task = NULL;
60 msg_comm_t comm = MSG_task_irecv(&task, mailbox);
61 msg_error_t res = MSG_comm_wait(comm, -1);
62 xbt_assert(res == MSG_OK, "MSG_task_get failed");
63 MSG_comm_destroy(comm);
64 XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
65 if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
66 MSG_task_destroy(task);
70 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
71 MSG_task_execute(task);
72 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
73 MSG_task_destroy(task);
75 XBT_INFO("I'm done. See you!");
79 int main(int argc, char* argv[])
81 MSG_init(&argc, argv);
82 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
83 "\tExample: %s msg_platform.xml msg_deployment.xml\n",
86 MSG_create_environment(argv[1]);
87 MSG_function_register("sender", sender);
88 MSG_function_register("receiver", receiver);
89 MSG_launch_application(argv[2]);
91 msg_error_t res = MSG_main();
93 XBT_INFO("Simulation time %g", MSG_get_clock());