Logo AND Algorithmique Numérique Distribuée

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