1 /* Copyright (c) 2010-2016. 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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitall, "Messages specific for this msg example");
12 explicit sender(std::vector<std::string> args)
14 xbt_assert(args.size()== 4, "This function expects 5 parameters from the XML deployment file");
15 long number_of_tasks = xbt_str_parse_int(args[0].c_str(), "Invalid amount of tasks: %s");
16 double task_comp_size = xbt_str_parse_double(args[1].c_str(), "Invalid computational size: %s");
17 double task_comm_size = xbt_str_parse_double(args[2].c_str(), "Invalid communication size: %s");
18 long receivers_count = xbt_str_parse_int(args[3].c_str(), "Invalid amount of receivers: %s");
20 simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[number_of_tasks + receivers_count] ;
22 for (int i = 0; i < number_of_tasks; i++) {
25 snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
26 snprintf(taskname,79, "Task_%d", i);
27 comms[i] = mbox->put_async((void*)mailbox, 42.0);
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);
34 comms[i + number_of_tasks] = mbox->put_async((void*)mailbox, 42.0);
36 XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
39 /* Here we are waiting for the completion of all communications */
40 for (int i = 0; i < number_of_tasks + receivers_count; i++)
47 XBT_INFO("Goodbye now!");
53 explicit receiver(std::vector<std::string> args)
55 xbt_assert(args.size() == 1,"This function expects 1 parameter from the XML deployment file");
56 int id = xbt_str_parse_int(args[0].c_str(), "Any process of this example must have a numerical name, not %s");
59 snprintf(mailbox,79, "receiver-%d", id);
61 simgrid::s4u::this_actor::sleep_for(10.0);
63 XBT_INFO("Wait to receive a task");
65 comm = mbox->get_async(&received);
67 if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
68 MSG_task_destroy(task);
72 XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
73 MSG_task_execute(task);
74 XBT_INFO("\"%s\" done", MSG_task_get_name(task));
75 MSG_task_destroy(task);
77 XBT_INFO("I'm done. See you!");
81 simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
82 XBT_INFO("I'm done. See you!");
86 int main(int argc, char *argv[])
88 MSG_init(&argc, argv);
89 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
90 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
92 MSG_create_environment(argv[1]);
93 MSG_function_register("sender", sender);
94 MSG_function_register("receiver", receiver);
95 MSG_launch_application(argv[2]);
97 msg_error_t res = MSG_main();
99 XBT_INFO("Simulation time %g", MSG_get_clock());
101 return res != MSG_OK;