Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update requested changes
[simgrid.git] / examples / msg / async-waitall / async-waitall.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_waitall, "Messages specific for this msg example");
9
10 static int sender(int argc, char *argv[])
11 {
12   xbt_assert(argc==5,"This function expects 4 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
18   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
19   for (int i = 0; i < number_of_tasks; i++) {
20     char mailbox[80];
21     char taskname[80];
22     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
23     snprintf(taskname,79, "Task_%d", i);
24     msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size, NULL);
25     comm[i] = MSG_task_isend(task, mailbox);
26     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
27   }
28   for (int i = 0; i < receivers_count; i++) {
29     char mailbox[80];
30     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
31     msg_task_t task = MSG_task_create("finalize", 0, 0, 0);
32     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
33     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
34   }
35
36   /* Here we are waiting for the completion of all communications */
37   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
38   for (int i = 0; i < number_of_tasks + receivers_count; i++)
39     MSG_comm_destroy(comm[i]);
40
41   XBT_INFO("Goodbye now!");
42   xbt_free(comm);
43   return 0;
44 }
45
46 static int receiver(int argc, char *argv[])
47 {
48   xbt_assert(argc==2,"This function expects 1 parameter from the XML deployment file");
49   int id = xbt_str_parse_int(argv[1], "Any process of this example must have a numerical name, not %s");
50
51   char mailbox[80];
52   snprintf(mailbox,79, "receiver-%d", id);
53    
54   MSG_process_sleep(10);
55   while (1) {
56     XBT_INFO("Wait to receive a task");
57     msg_task_t task = NULL;
58     msg_comm_t comm = MSG_task_irecv(&task, mailbox);
59     msg_error_t res = MSG_comm_wait(comm, -1);
60     xbt_assert(res == MSG_OK, "MSG_task_get failed");
61     MSG_comm_destroy(comm);
62     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
63     if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
64       MSG_task_destroy(task);
65       break;
66     }
67
68     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
69     MSG_task_execute(task);
70     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
71     MSG_task_destroy(task);
72   }
73   XBT_INFO("I'm done. See you!");
74   return 0;
75 }
76
77 int main(int argc, char *argv[])
78 {
79   MSG_init(&argc, argv);
80   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
81              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
82
83   MSG_create_environment(argv[1]);
84   MSG_function_register("sender", sender);
85   MSG_function_register("receiver", receiver);
86   MSG_launch_application(argv[2]);
87
88   msg_error_t res = MSG_main();
89
90   XBT_INFO("Simulation time %g", MSG_get_clock());
91
92   return res != MSG_OK;
93 }