Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines.
[simgrid.git] / examples / c / comm-wait / comm-wait.c
1 /* Copyright (c) 2010-2021. 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(comm_wait, "Messages specific for this example");
18
19 /* Main function of the Sender actor */
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   long message_size       = xbt_str_parse_int(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   XBT_INFO("Send 'finalize' to 'receiver'");
49   sg_mailbox_put(mailbox, xbt_strdup("finalize"), 0);
50 }
51
52 /* Receiver actor expects 3 arguments: */
53 static void receiver(int argc, char* argv[])
54 {
55   xbt_assert(argc == 3, "The relay_runner function does not accept any parameter from the XML deployment file");
56   double sleep_start_time = xbt_str_parse_double(argv[1], "Invalid sleep start parameter: %s"); /* - start time */
57   double sleep_test_time  = xbt_str_parse_double(argv[2], "Invalid sleep test parameter: %s");  /* - test time */
58   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
59
60   sg_actor_sleep_for(sleep_start_time); /* This actor first sleeps for "start time" seconds.  */
61
62   sg_mailbox_t mailbox = sg_mailbox_by_name("receiver");
63   void* received       = NULL;
64
65   XBT_INFO("Wait for my first message");
66   while (1) {
67     /* Then it posts asynchronous receives (@ref sg_mailbox_get_async) and*/
68     sg_comm_t comm = sg_mailbox_get_async(mailbox, &received);
69
70     if (sleep_test_time > 0) {          /* - if "test_time" is set to 0, wait on @ref sg_comm_wait */
71       while (sg_comm_test(comm) == 0) { /* - Call @ref sg_comm_test every "sleep_test_time" otherwise */
72         sg_actor_sleep_for(sleep_test_time);
73       }
74     } else {
75       sg_comm_wait(comm);
76     }
77     XBT_INFO("I got a '%s'.", (char*)received);
78
79     if (strcmp((char*)received, "finalize") == 0) { /* If the received task is "finalize", the actor ends */
80       free(received);
81       break;
82     }
83     free(received);
84   }
85 }
86
87 int main(int argc, char* argv[])
88 {
89   simgrid_init(&argc, argv);
90   xbt_assert(argc > 2,
91              "Usage: %s platform_file deployment_file\n"
92              "\tExample: %s msg_platform.xml msg_deployment.xml\n",
93              argv[0], argv[0]);
94
95   simgrid_load_platform(argv[1]); /* - Load the platform description */
96
97   simgrid_register_function("sender", sender);
98   simgrid_register_function("receiver", receiver);
99   simgrid_load_deployment(argv[2]); /* - Deploy the sender and receiver actors */
100
101   simgrid_run(); /* - Run the simulation */
102
103   return 0;
104 }