Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c446e8f7c78e39a081f252fe5c19a091adf9256
[simgrid.git] / examples / msg / icomms / peer.c
1 /* Copyright (c) 2010-2014. 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 <stdio.h>
8 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 /** @addtogroup MSG_examples
18  * 
19  * @section MSG_ex_icomms Asynchronous communications
20  * 
21  * There is several examples of asynchronous communications coming in
22  * the archive. In addition to the fully documented example \ref
23  * MSG_ex_asynchronous_communications, there is several other
24  * examples in the archive:
25  * 
26  * - <b>msg/icomms/peer.c</b>: basic example of async functions (@ref MSG_task_isend, @ref MSG_task_irecv, @ref MSG_comm_wait)
27  */
28 int sender(int argc, char *argv[]);
29 int receiver(int argc, char *argv[]);
30
31 msg_error_t test_all(const char *platform_file,
32                      const char *application_file);
33
34 /** Sender function  */
35 int sender(int argc, char *argv[])
36 {
37   long number_of_tasks = atol(argv[1]);
38   double task_comp_size = atof(argv[2]);
39   double task_comm_size = atof(argv[3]);
40   long receivers_count = atol(argv[4]);
41   double sleep_start_time = atof(argv[5]);
42   double sleep_test_time = atof(argv[6]);
43
44   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time,
45         sleep_test_time);
46
47   msg_comm_t comm = NULL;
48   int i;
49   msg_task_t task = NULL;
50   MSG_process_sleep(sleep_start_time);
51   for (i = 0; i < number_of_tasks; i++) {
52     char mailbox[256];
53     char sprintf_buffer[256];
54
55     sprintf(mailbox, "receiver-%ld", i % receivers_count);
56     sprintf(sprintf_buffer, "Task_%d", i);
57
58     task =
59         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
60                         NULL);
61     comm = MSG_task_isend(task, mailbox);
62     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
63
64     if (sleep_test_time == 0) {
65       MSG_comm_wait(comm, -1);
66     } else {
67       while (MSG_comm_test(comm) == 0) {
68         MSG_process_sleep(sleep_test_time);
69       };
70     }
71     MSG_comm_destroy(comm);
72
73   }
74
75   for (i = 0; i < receivers_count; i++) {
76     char mailbox[80];
77     sprintf(mailbox, "receiver-%ld", i % receivers_count);
78     task = MSG_task_create("finalize", 0, 0, 0);
79     comm = MSG_task_isend(task, mailbox);
80     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
81     if (sleep_test_time == 0) {
82       MSG_comm_wait(comm, -1);
83     } else {
84       while (MSG_comm_test(comm) == 0) {
85         MSG_process_sleep(sleep_test_time);
86       };
87     }
88     MSG_comm_destroy(comm);
89
90   }
91
92   XBT_INFO("Goodbye now!");
93   return 0;
94 }                               /* end_of_sender */
95
96 /** Receiver function  */
97 int receiver(int argc, char *argv[])
98 {
99   msg_task_t task = NULL;
100   _XBT_GNUC_UNUSED msg_error_t res;
101   int id = -1;
102   char mailbox[80];
103   msg_comm_t res_irecv;
104   double sleep_start_time = atof(argv[2]);
105   double sleep_test_time = atof(argv[3]);
106   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time,
107         sleep_test_time);
108
109   _XBT_GNUC_UNUSED int read;
110   read = sscanf(argv[1], "%d", &id);
111   xbt_assert(read,
112               "Invalid argument %s\n", argv[1]);
113
114   MSG_process_sleep(sleep_start_time);
115
116   sprintf(mailbox, "receiver-%d", id);
117   while (1) {
118     res_irecv = MSG_task_irecv(&(task), mailbox);
119     XBT_INFO("Wait to receive a task");
120
121     if (sleep_test_time == 0) {
122       res = MSG_comm_wait(res_irecv, -1);
123       xbt_assert(res == MSG_OK, "MSG_task_get failed");
124     } else {
125       while (MSG_comm_test(res_irecv) == 0) {
126         MSG_process_sleep(sleep_test_time);
127       };
128     }
129     MSG_comm_destroy(res_irecv);
130
131     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
132     if (!strcmp(MSG_task_get_name(task), "finalize")) {
133       MSG_task_destroy(task);
134       break;
135     }
136
137     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
138     MSG_task_execute(task);
139     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
140     MSG_task_destroy(task);
141     task = NULL;
142   }
143   XBT_INFO("I'm done. See you!");
144   return 0;
145 }                               /* end_of_receiver */
146
147 /** Test function */
148 msg_error_t test_all(const char *platform_file,
149                      const char *application_file)
150 {
151   msg_error_t res = MSG_OK;
152
153   /* MSG_config("workstation/model","KCCFLN05"); */
154   {                             /*  Simulation setting */
155     MSG_create_environment(platform_file);
156   }
157   {                             /*   Application deployment */
158     MSG_function_register("sender", sender);
159     MSG_function_register("receiver", receiver);
160     MSG_launch_application(application_file);
161   }
162   res = MSG_main();
163
164   XBT_INFO("Simulation time %g", MSG_get_clock());
165   return res;
166 }                               /* end_of_test_all */
167
168
169 /** Main function */
170 int main(int argc, char *argv[])
171 {
172   msg_error_t res = MSG_OK;
173
174   MSG_init(&argc, argv);
175   if (argc < 3) {
176     printf("Usage: %s platform_file deployment_file\n", argv[0]);
177     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
178     exit(1);
179   }
180   res = test_all(argv[1], argv[2]);
181
182   if (res == MSG_OK)
183     return 0;
184   else
185     return 1;
186 }                               /* end_of_main */