Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
public headers should include simgrid in a system-wide way, not a project-wide one
[simgrid.git] / teshsuite / msg / async-waitany / async-waitany.c
1 /* Copyright (c) 2010-2018. 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 #include <stdio.h> /* snprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_waitany, "Messages specific for this msg example");
11
12 static int sender(int argc, char* argv[])
13 {
14   xbt_assert(argc == 6, "This function expects 5 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");
19   int diff_com          = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");
20
21   xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
22   /* First pack the communications in the dynar */
23   for (int i = 0; i < number_of_tasks; i++) {
24     double coef = (diff_com == 0) ? 1 : (i + 1);
25
26     char mailbox[80];
27     char taskname[80];
28     snprintf(mailbox, 79, "receiver-%ld", (i % receivers_count));
29     snprintf(taskname, 79, "Task_%d", i);
30     msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size / coef, NULL);
31     msg_comm_t comm = MSG_task_isend(task, mailbox);
32     xbt_dynar_push_as(comms, msg_comm_t, comm);
33     XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, taskname, task_comm_size / coef);
34   }
35
36   /* Here we are waiting for the completion of all communications */
37   while (xbt_dynar_is_empty(comms) == 0) {
38     msg_comm_t comm;
39     xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
40     MSG_comm_destroy(comm);
41   }
42   xbt_dynar_free(&comms);
43
44   /* Here we are waiting for the completion of all tasks */
45   for (int i = 0; i < receivers_count; i++) {
46     msg_task_t task      = NULL;
47     msg_comm_t comm      = MSG_task_irecv(&task, "finalize");
48     msg_error_t res_wait = MSG_comm_wait(comm, -1);
49     xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
50     MSG_comm_destroy(comm);
51     MSG_task_destroy(task);
52   }
53
54   XBT_INFO("Goodbye now!");
55   return 0;
56 }
57
58 static int receiver(int argc, char* argv[])
59 {
60   xbt_assert(argc == 3, "This function expects 2 parameters from the XML deployment file");
61   int id            = xbt_str_parse_int(argv[1], "ID should be numerical, not %s");
62   int task_amount   = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
63   msg_task_t* tasks = xbt_new(msg_task_t, task_amount);
64   xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
65
66   char mailbox[80];
67   snprintf(mailbox, 79, "receiver-%d", id);
68
69   MSG_process_sleep(10);
70   for (int i = 0; i < task_amount; i++) {
71     XBT_INFO("Wait to receive task %d", i);
72     tasks[i]        = NULL;
73     msg_comm_t comm = MSG_task_irecv(&tasks[i], mailbox);
74     xbt_dynar_push_as(comms, msg_comm_t, comm);
75   }
76
77   /* Here we are waiting for the receiving of all communications */
78   while (!xbt_dynar_is_empty(comms)) {
79     msg_comm_t comm;
80     // MSG_comm_waitany returns the rank of the comm that just ended. Remove it.
81     xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &comm);
82     msg_task_t task = MSG_comm_get_task(comm);
83     MSG_comm_destroy(comm);
84     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
85     MSG_task_execute(task);
86     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
87     msg_error_t err = MSG_task_destroy(task);
88     xbt_assert(err == MSG_OK, "MSG_task_destroy failed");
89   }
90   xbt_dynar_free(&comms);
91   xbt_free(tasks);
92
93   /* Here we tell to sender that all tasks are done */
94   MSG_task_send(MSG_task_create(NULL, 0, 0, NULL), "finalize");
95   XBT_INFO("I'm done. See you!");
96   return 0;
97 }
98
99 int main(int argc, char* argv[])
100 {
101   MSG_init(&argc, argv);
102   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
103                        "\tExample: %s msg_platform.xml msg_deployment.xml\n",
104              argv[0], argv[0]);
105
106   MSG_create_environment(argv[1]);
107
108   MSG_function_register("sender", sender);
109   MSG_function_register("receiver", receiver);
110   MSG_launch_application(argv[2]);
111
112   msg_error_t res = MSG_main();
113
114   XBT_INFO("Simulation time %g", MSG_get_clock());
115
116   return res != MSG_OK;
117 }