Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
spellchecking instead of sleeping
[simgrid.git] / examples / msg / async-wait / async-wait.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_async_wait, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  * @section msg_ex_async Asynchronous communications
14  * 
15  * In addition to the fully documented example of \ref MSG_ex_asynchronous_communications, there are several other
16  * examples shipped in the archive:
17  * 
18  * - <b>Basic example: async-wait/async-wait.c</b>. Illustrates the use of asynchronous functions (@ref MSG_task_isend,
19  *    @ref MSG_task_irecv, and @ref MSG_comm_wait).
20  */
21
22 /** Sender process expects 6 arguments: */
23 static int sender(int argc, char *argv[])
24 {
25   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");        /** - number of tasks */
26   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s"); /** - computational cost */
27   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s"); /** - communication cost */
28   long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");    /** - number of receivers */
29   double sleep_start_time = xbt_str_parse_double(argv[5], "Invalid sleep start time: %s"); /** - start time */
30   double sleep_test_time = xbt_str_parse_double(argv[6], "Invalid test time: %s");         /** - test time */
31
32   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
33
34   int i;
35   msg_task_t task = NULL;
36   msg_comm_t comm = NULL;
37   MSG_process_sleep(sleep_start_time);
38   for (i = 0; i < number_of_tasks; i++) {
39     char mailbox[256];
40     char sprintf_buffer[256];
41
42     sprintf(mailbox, "receiver-%ld", i % receivers_count);
43     sprintf(sprintf_buffer, "Task_%d", i);
44
45     /** This process first creates a task and send it asynchronously with @ref MSG_task_isend. Then, if: */
46     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
47     comm = MSG_task_isend(task, mailbox);
48     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
49
50     if (sleep_test_time == 0) { /** - "test_time" is set to 0, wait on @ref MSG_comm_wait */
51       MSG_comm_wait(comm, -1);
52     } else {
53       while (MSG_comm_test(comm) == 0) { /** - Call @ref MSG_comm_test every "test_time" otherwise */
54         MSG_process_sleep(sleep_test_time);
55       };
56     }
57     MSG_comm_destroy(comm);
58   }
59
60   for (i = 0; i < receivers_count; i++) {
61     char mailbox[80];
62     sprintf(mailbox, "receiver-%ld", i % receivers_count);
63     task = MSG_task_create("finalize", 0, 0, 0);
64     comm = MSG_task_isend(task, mailbox);
65     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
66     if (sleep_test_time == 0) {
67       MSG_comm_wait(comm, -1);
68     } else {
69       while (MSG_comm_test(comm) == 0) {
70         MSG_process_sleep(sleep_test_time);
71       };
72     }
73     MSG_comm_destroy(comm);
74   }
75
76   XBT_INFO("Goodbye now!");
77   return 0;
78 }
79
80 /** Receiver process expects 3 arguments: */
81 static int receiver(int argc, char *argv[])
82 {
83   msg_task_t task = NULL;
84   XBT_ATTRIB_UNUSED msg_error_t res;
85   char mailbox[80];
86   msg_comm_t res_irecv;
87   int id = xbt_str_parse_int(argv[1], "Invalid id: %s");                                        /** - unique id */
88   double sleep_start_time = xbt_str_parse_double(argv[2], "Invalid sleep start parameter: %s"); /** - start time */
89   double sleep_test_time = xbt_str_parse_double(argv[3], "Invalid sleep test parameter: %s");   /** - test time */
90   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
91
92   MSG_process_sleep(sleep_start_time); /** This process first sleeps for "start time" seconds.  */
93
94   sprintf(mailbox, "receiver-%d", id);
95   while (1) {
96     res_irecv = MSG_task_irecv(&(task), mailbox); /** Then it posts asynchronous receives (@ref MSG_task_irecv) and*/
97     XBT_INFO("Wait to receive a task");
98
99     if (sleep_test_time == 0) {               /** - if "test_time" is set to 0, wait on @ref MSG_comm_wait */
100       res = MSG_comm_wait(res_irecv, -1);
101       xbt_assert(res == MSG_OK, "MSG_task_get failed");
102     } else {
103       while (MSG_comm_test(res_irecv) == 0) { /** - Call @ref MSG_comm_test every "test_time" otherwise */
104         MSG_process_sleep(sleep_test_time);
105       };
106     }
107     MSG_comm_destroy(res_irecv);
108
109     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
110     if (!strcmp(MSG_task_get_name(task), "finalize")) { /** If the received task is "finalize", the process ends */
111       MSG_task_destroy(task);
112       break;
113     }
114
115     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task)); /** Otherwise, the task is processed */
116     MSG_task_execute(task);
117     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
118     MSG_task_destroy(task);
119     task = NULL;
120   }
121   XBT_INFO("I'm done. See you!");
122   return 0;
123 }
124
125 int main(int argc, char *argv[])
126 {
127   msg_error_t res = MSG_OK;
128
129   MSG_init(&argc, argv);
130   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
131              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
132
133   MSG_create_environment(argv[1]);/** - Load the platform description */
134
135   MSG_function_register("sender", sender);
136   MSG_function_register("receiver", receiver);
137   MSG_launch_application(argv[2]);/** - Deploy the @ref sender and @ref receiver processes */
138
139   res = MSG_main();  /** - Run the simulation */
140
141   XBT_INFO("Simulation time %g", MSG_get_clock());
142
143   return res != MSG_OK;
144 }