Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0e1fc40c52b51bdbe8788e83d73dff7b4a206d26
[simgrid.git] / examples / c / 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/actor.h"
7 #include "simgrid/comm.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/mailbox.h"
10
11 #include "xbt/asserts.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14
15 #include <stdio.h>
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(async_wait, "Messages specific for this example");
18
19 /* Main function of the Sender process */
20 static void sender(int argc, char* argv[])
21 {
22   xbt_assert(argc == 5, "The sender function expects 4 arguments from the XML deployment file");
23   long messages_count     = xbt_str_parse_int(argv[1], "Invalid amount of messages: %s");  /* - number of messages */
24   double message_size     = xbt_str_parse_double(argv[2], "Invalid message size: %s");     /* - communication cost */
25   double sleep_start_time = xbt_str_parse_double(argv[3], "Invalid sleep start time: %s"); /* - start time */
26   double sleep_test_time  = xbt_str_parse_double(argv[4], "Invalid test time: %s");        /* - test time */
27
28   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
29   sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
30
31   sg_actor_sleep_for(sleep_start_time);
32   for (int i = 0; i < messages_count; i++) {
33     char* payload = bprintf("Message %d", i);
34
35     /* This actor first sends a message asynchronously with @ref sg_mailbox_put_async. Then, if: */
36     sg_comm_t comm = sg_mailbox_put_async(mailbox, payload, message_size);
37     XBT_INFO("Send '%s' to 'receiver'", payload);
38
39     if (sleep_test_time > 0) {          /* - "test_time" is set to 0, wait on @ref sg_comm_wait */
40       while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
41         sg_actor_sleep_for(sleep_test_time);
42       }
43     } else {
44       sg_comm_wait(comm);
45     }
46   }
47
48   sg_comm_t comm = sg_mailbox_put_async(mailbox, xbt_strdup("finalize"), 0);
49   XBT_INFO("Send 'finalize' to 'receiver'");
50
51   if (sleep_test_time > 0) {
52     while (sg_comm_test(comm) == 0) {
53       sg_actor_sleep_for(sleep_test_time);
54     }
55   } else {
56     sg_comm_wait(comm);
57   }
58 }
59
60 /* Receiver process expects 3 arguments: */
61 static void receiver(int argc, char* argv[])
62 {
63   xbt_assert(argc == 3, "The relay_runner function does not accept any parameter from the XML deployment file");
64   double sleep_start_time = xbt_str_parse_double(argv[1], "Invalid sleep start parameter: %s"); /* - start time */
65   double sleep_test_time  = xbt_str_parse_double(argv[2], "Invalid sleep test parameter: %s");  /* - test time */
66   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
67
68   sg_actor_sleep_for(sleep_start_time); /* This actor first sleeps for "start time" seconds.  */
69
70   sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
71   void* received       = NULL;
72
73   XBT_INFO("Wait for my first message");
74   while (1) {
75     /* Then it posts asynchronous receives (@ref sg_mailbox_get_async) and*/
76     sg_comm_t comm = sg_mailbox_get_async(mailbox, &received);
77
78     if (sleep_test_time > 0) {          /* - if "test_time" is set to 0, wait on @ref sg_comm_wait */
79       while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
80         sg_actor_sleep_for(sleep_test_time);
81       }
82     } else {
83       sg_comm_wait(comm);
84     }
85     XBT_INFO("I got a '%s'.", (char*)received);
86
87     if (strcmp((char*)received, "finalize") == 0) { /* If the received task is "finalize", the actor ends */
88       free(received);
89       break;
90     }
91     free(received);
92   }
93 }
94
95 int main(int argc, char* argv[])
96 {
97   simgrid_init(&argc, argv);
98   xbt_assert(argc > 2,
99              "Usage: %s platform_file deployment_file\n"
100              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
101              argv[0], argv[0]);
102
103   simgrid_load_platform(argv[1]); /* - Load the platform description */
104
105   simgrid_register_function("sender", sender);
106   simgrid_register_function("receiver", receiver);
107   simgrid_load_deployment(argv[2]); /* - Deploy the sender and receiver actors */
108
109   simgrid_run(); /* - Run the simulation */
110
111   return 0;
112 }