Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
db2d0f718a3d3c9037fac03ba1d3e5827b989157
[simgrid.git] / examples / msg / async-waitany / async-waitany.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static int sender(int argc, char *argv[])
12 {
13   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
14   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
15   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
16   long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
17   int diff_com = xbt_str_parse_int(argv[5], "Invalid value for diff_comm: %s");
18   double coef = 0;
19   xbt_dynar_t d = xbt_dynar_new(sizeof(msg_comm_t), NULL);
20   int i;
21   msg_task_t task;
22   char mailbox[256];
23   char sprintf_buffer[256];
24   msg_comm_t comm;
25
26   for (i = 0; i < number_of_tasks; i++) {
27     if (diff_com == 0)
28       coef = 1;
29     else
30       coef = (i + 1);
31
32     sprintf(mailbox, "receiver-%ld", (i % receivers_count));
33     sprintf(sprintf_buffer, "Task_%d", i);
34     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size / coef, NULL);
35     comm = MSG_task_isend(task, mailbox);
36     xbt_dynar_push_as(d, msg_comm_t, comm);
37     XBT_INFO("Send to receiver-%ld %s comm_size %f", i % receivers_count, sprintf_buffer, task_comm_size / coef);
38   }
39   /* Here we are waiting for the completion of all communications */
40
41   while (!xbt_dynar_is_empty(d)) {
42     xbt_dynar_remove_at(d, MSG_comm_waitany(d), &comm);
43     MSG_comm_destroy(comm);
44   }
45   xbt_dynar_free(&d);
46
47   /* Here we are waiting for the completion of all tasks */
48   sprintf(mailbox, "finalize");
49
50   msg_comm_t res_irecv;
51   XBT_ATTRIB_UNUSED msg_error_t res_wait;
52   for (i = 0; i < receivers_count; i++) {
53     task = NULL;
54     res_irecv = MSG_task_irecv(&(task), mailbox);
55     res_wait = MSG_comm_wait(res_irecv, -1);
56     xbt_assert(res_wait == MSG_OK, "MSG_comm_wait failed");
57     MSG_comm_destroy(res_irecv);
58     MSG_task_destroy(task);
59   }
60
61   XBT_INFO("Goodbye now!");
62   return 0;
63 }
64
65 static int receiver(int argc, char *argv[])
66 {
67   int id = -1;
68   char mailbox[80];
69   xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
70   int tasks = xbt_str_parse_int(argv[2], "Invalid amount of tasks: %s");
71   msg_task_t *task = xbt_new(msg_task_t, tasks);
72
73   XBT_ATTRIB_UNUSED int read;
74   read = sscanf(argv[1], "%d", &id);
75   xbt_assert(read, "Invalid argument %s\n", argv[1]);
76   sprintf(mailbox, "receiver-%d", id);
77   MSG_process_sleep(10);
78   msg_comm_t res_irecv;
79   for (int i = 0; i < tasks; i++) {
80     XBT_INFO("Wait to receive task %d", i);
81     task[i] = NULL;
82     res_irecv = MSG_task_irecv(&task[i], mailbox);
83     xbt_dynar_push_as(comms, msg_comm_t, res_irecv);
84   }
85
86   /* Here we are waiting for the receiving of all communications */
87   msg_task_t task_com;
88   while (!xbt_dynar_is_empty(comms)) {
89     XBT_ATTRIB_UNUSED msg_error_t err;
90     xbt_dynar_remove_at(comms, MSG_comm_waitany(comms), &res_irecv);
91     task_com = MSG_comm_get_task(res_irecv);
92     MSG_comm_destroy(res_irecv);
93     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task_com));
94     MSG_task_execute(task_com);
95     XBT_INFO("\"%s\" done", MSG_task_get_name(task_com));
96     err = MSG_task_destroy(task_com);
97     xbt_assert(err == MSG_OK, "MSG_task_destroy failed");
98   }
99   xbt_dynar_free(&comms);
100   xbt_free(task);
101
102   /* Here we tell to sender that all tasks are done */
103   sprintf(mailbox, "finalize");
104   res_irecv = MSG_task_isend(MSG_task_create(NULL, 0, 0, NULL), mailbox);
105   MSG_comm_wait(res_irecv, -1);
106   MSG_comm_destroy(res_irecv);
107   XBT_INFO("I'm done. See you!");
108   return 0;
109 }
110
111 int main(int argc, char *argv[])
112 {
113   msg_error_t res = MSG_OK;
114
115   MSG_init(&argc, argv);
116   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
117                   "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
118
119   MSG_create_environment(argv[1]);
120
121   MSG_function_register("sender", sender);
122   MSG_function_register("receiver", receiver);
123   MSG_launch_application(argv[2]);
124
125   res = MSG_main();
126
127   XBT_INFO("Simulation time %g", MSG_get_clock());
128
129   return res != MSG_OK;
130 }