Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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   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
17   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
18   int i;
19   msg_task_t task = NULL;
20   for (i = 0; i < number_of_tasks; i++) {
21     char mailbox[256];
22     char snprintf_buffer[256];
23     snprintf(mailbox,255, "receiver-%ld", i % receivers_count);
24     snprintf(snprintf_buffer,255, "Task_%d", i);
25     task = MSG_task_create(snprintf_buffer, task_comp_size, task_comm_size, NULL);
26     comm[i] = MSG_task_isend(task, mailbox);
27     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
28   }
29   for (i = 0; i < receivers_count; i++) {
30     char mailbox[80];
31     snprintf(mailbox,79, "receiver-%ld", i % receivers_count);
32     task = MSG_task_create("finalize", 0, 0, 0);
33     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
34     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
35   }
36
37   /* Here we are waiting for the completion of all communications */
38   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
39   for (i = 0; i < number_of_tasks + receivers_count; i++)
40     MSG_comm_destroy(comm[i]);
41
42   XBT_INFO("Goodbye now!");
43   xbt_free(comm);
44   return 0;
45 }
46
47 static int receiver(int argc, char *argv[])
48 {
49   msg_task_t task = NULL;
50   XBT_ATTRIB_UNUSED msg_error_t res;
51   int id = -1;
52   char mailbox[80];
53   msg_comm_t res_irecv;
54   XBT_ATTRIB_UNUSED int read;
55   read = sscanf(argv[1], "%d", &id);
56   xbt_assert(read, "Invalid argument %s\n", argv[1]);
57   MSG_process_sleep(10);
58   snprintf(mailbox,79, "receiver-%d", id);
59   while (1) {
60     res_irecv = MSG_task_irecv(&(task), mailbox);
61     XBT_INFO("Wait to receive a task");
62     res = MSG_comm_wait(res_irecv, -1);
63     MSG_comm_destroy(res_irecv);
64     xbt_assert(res == MSG_OK, "MSG_task_get failed");
65     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
66     if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
67       MSG_task_destroy(task);
68       break;
69     }
70
71     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
72     MSG_task_execute(task);
73     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
74     MSG_task_destroy(task);
75     task = NULL;
76   }
77   XBT_INFO("I'm done. See you!");
78   return 0;
79 }
80
81 int main(int argc, char *argv[])
82 {
83   msg_error_t res = MSG_OK;
84
85   MSG_init(&argc, argv);
86   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
87              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
88
89   MSG_create_environment(argv[1]);
90   MSG_function_register("sender", sender);
91   MSG_function_register("receiver", receiver);
92   MSG_launch_application(argv[2]);
93
94   res = MSG_main();
95
96   XBT_INFO("Simulation time %g", MSG_get_clock());
97
98   return res != MSG_OK;
99 }