Logo AND Algorithmique Numérique Distribuée

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