Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / teshsuite / msg / async-wait / async-wait.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_wait, "Messages specific for this msg example");
11
12 /* Main function of the Sender process */
13 static int sender(int argc, char* argv[])
14 {
15   xbt_assert(argc == 7, "The sender function expects 6 arguments from the XML deployment file");
16   long number_of_tasks    = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");       /* - number of tasks */
17   double task_comp_size   = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /* - computational cost */
18   double task_comm_size   = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /* - communication cost */
19   long receivers_count    = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");   /* - number of receivers */
20   double sleep_start_time = xbt_str_parse_double(argv[5], "Invalid sleep start time: %s");   /* - start time */
21   double sleep_test_time  = xbt_str_parse_double(argv[6], "Invalid test time: %s");          /* - test time */
22
23   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
24
25   MSG_process_sleep(sleep_start_time);
26   for (int i = 0; i < number_of_tasks; i++) {
27     char mailbox[80];
28     char taskname[80];
29
30     snprintf(mailbox, 79, "receiver-%ld", i % receivers_count);
31     snprintf(taskname, 79, "Task_%d", i);
32
33     /* This process first creates a task and send it asynchronously with @ref MSG_task_isend. Then, if: */
34     msg_task_t task = MSG_task_create(taskname, task_comp_size, task_comm_size, NULL);
35     msg_comm_t comm = MSG_task_isend(task, mailbox);
36     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
37
38     if (sleep_test_time > 0) {           /* - "test_time" is set to 0, wait on @ref MSG_comm_wait */
39       while (MSG_comm_test(comm) == 0) { /* - Call @ref MSG_comm_test every "test_time" otherwise */
40         MSG_process_sleep(sleep_test_time);
41       }
42     } else {
43       MSG_comm_wait(comm, -1);
44     }
45     MSG_comm_destroy(comm);
46   }
47
48   for (int i = 0; i < receivers_count; i++) {
49     char mailbox[80];
50     snprintf(mailbox, 79, "receiver-%d", i);
51     msg_task_t task = MSG_task_create("finalize", 0, 0, 0);
52     msg_comm_t comm = MSG_task_isend(task, mailbox);
53     XBT_INFO("Send to receiver-%d finalize", i);
54     if (sleep_test_time > 0) {
55       while (MSG_comm_test(comm) == 0) {
56         MSG_process_sleep(sleep_test_time);
57       }
58     } else {
59       MSG_comm_wait(comm, -1);
60     }
61     MSG_comm_destroy(comm);
62   }
63
64   XBT_INFO("Goodbye now!");
65   return 0;
66 }
67
68 /* Receiver process expects 3 arguments: */
69 static int receiver(int argc, char* argv[])
70 {
71   xbt_assert(argc == 4, "The relay_runner function does not accept any parameter from the XML deployment file");
72   int id                  = xbt_str_parse_int(argv[1], "Invalid id: %s");                       /* - unique id */
73   double sleep_start_time = xbt_str_parse_double(argv[2], "Invalid sleep start parameter: %s"); /* - start time */
74   double sleep_test_time  = xbt_str_parse_double(argv[3], "Invalid sleep test parameter: %s");  /* - test time */
75   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
76
77   MSG_process_sleep(sleep_start_time); /* This process first sleeps for "start time" seconds.  */
78
79   char mailbox[80];
80   snprintf(mailbox, 79, "receiver-%d", id);
81   while (1) {
82     msg_task_t task = NULL;
83     msg_comm_t comm = MSG_task_irecv(&task, mailbox); /* Then it posts asynchronous receives (@ref MSG_task_irecv) and*/
84     XBT_INFO("Wait to receive a task");
85
86     if (sleep_test_time > 0) {           /* - if "test_time" is set to 0, wait on @ref MSG_comm_wait */
87       while (MSG_comm_test(comm) == 0) { /* - Call @ref MSG_comm_test every "test_time" otherwise */
88         MSG_process_sleep(sleep_test_time);
89       }
90     } else {
91       msg_error_t res = MSG_comm_wait(comm, -1);
92       xbt_assert(res == MSG_OK, "MSG_task_get failed");
93     }
94     MSG_comm_destroy(comm);
95
96     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
97     if (strcmp(MSG_task_get_name(task), "finalize") == 0) { /* If the received task is "finalize", the process ends */
98       MSG_task_destroy(task);
99       break;
100     }
101
102     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); /* Otherwise, the task is processed */
103     MSG_task_execute(task);
104     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
105     MSG_task_destroy(task);
106   }
107   XBT_INFO("I'm done. See you!");
108   return 0;
109 }
110
111 int main(int argc, char* argv[])
112 {
113   MSG_init(&argc, argv);
114   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
115                        "\tExample: %s msg_platform.xml msg_deployment.xml\n",
116              argv[0], argv[0]);
117
118   MSG_create_environment(argv[1]); /* - Load the platform description */
119
120   MSG_function_register("sender", sender);
121   MSG_function_register("receiver", receiver);
122   MSG_launch_application(argv[2]); /* - Deploy the sender and receiver processes */
123
124   msg_error_t res = MSG_main(); /* - Run the simulation */
125
126   XBT_INFO("Simulation time %g", MSG_get_clock());
127
128   return res != MSG_OK;
129 }