Logo AND Algorithmique Numérique Distribuée

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