Logo AND Algorithmique Numérique Distribuée

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