Logo AND Algorithmique Numérique Distribuée

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