Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Start to convert async-waitall to s4u
[simgrid.git] / examples / s4u / async-waitall / s4u_async-waitall.cpp
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_async_waitall, "Messages specific for this msg example");
9
10 class sender {
11 public:
12   explicit sender(std::vector<std::string> args)
13 {
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");
19
20   simgrid::s4u::CommPtr* comms = new simgrid::s4u::CommPtr[number_of_tasks + receivers_count] ;
21
22   for (int i = 0; i < number_of_tasks; i++) {
23     char mailbox[80];
24     char taskname[80];
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);
29   }
30   for (int i = 0; i < receivers_count; i++) {
31     char mailbox[80];
32     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
33
34     comms[i + number_of_tasks] = mbox->put_async((void*)mailbox, 42.0);
35     
36     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
37   }
38
39   /* Here we are waiting for the completion of all communications */
40   for (int i = 0; i < number_of_tasks + receivers_count; i++)
41     comms[i]->wai();
42
43   delete [] comms;
44 }
45 void operator()()
46 {
47   XBT_INFO("Goodbye now!");
48 }
49 };
50
51 class receiver {
52 public:
53   explicit receiver(std::vector<std::string> args)
54 {
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");
57   void *received;
58   char mailbox[80];
59   snprintf(mailbox,79, "receiver-%d", id);
60
61   simgrid::s4u::this_actor::sleep_for(10.0);
62   while (1) {
63     XBT_INFO("Wait to receive a task");
64     received = NULL;
65     comm = mbox->get_async(&received);
66     comm->wait();
67     if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
68       MSG_task_destroy(task);
69       break;
70     }
71
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);
76   }
77   XBT_INFO("I'm done. See you!");
78 }
79 void operator()()
80 {
81   simgrid::s4u::Mailbox::byName("finalize")->put(nullptr, 1);
82   XBT_INFO("I'm done. See you!");
83 }
84 };
85
86 int main(int argc, char *argv[])
87 {
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]);
91
92   MSG_create_environment(argv[1]);
93   MSG_function_register("sender", sender);
94   MSG_function_register("receiver", receiver);
95   MSG_launch_application(argv[2]);
96
97   msg_error_t res = MSG_main();
98
99   XBT_INFO("Simulation time %g", MSG_get_clock());
100
101   return res != MSG_OK;
102 }