Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
all DHT examples are now called dht-<protocol>
[simgrid.git] / examples / msg / icomms / peer.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"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  * @section MSG_ex_icomms Asynchronous communications
14  * 
15  * There is several examples of asynchronous communications coming in
16  * the archive. In addition to the fully documented example \ref
17  * MSG_ex_asynchronous_communications, there is several other
18  * examples in the archive:
19  * 
20  * - <b>msg/icomms/peer.c</b>: basic example of async functions (@ref MSG_task_isend, @ref MSG_task_irecv, @ref MSG_comm_wait)
21  */
22
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");
26   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid compute size: %s");
27   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
28   long receivers_count = xbt_str_parse_int(argv[4], "Invalid amount of receivers: %s");
29   double sleep_start_time = xbt_str_parse_double(argv[5], "Invalid sleep start time: %s");
30   double sleep_test_time = xbt_str_parse_double(argv[6], "Invalid test time: %s");
31
32   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
33
34   msg_comm_t comm = NULL;
35   int i;
36   msg_task_t task = 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     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
46     comm = MSG_task_isend(task, mailbox);
47     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
48
49     if (sleep_test_time == 0) {
50       MSG_comm_wait(comm, -1);
51     } else {
52       while (MSG_comm_test(comm) == 0) {
53         MSG_process_sleep(sleep_test_time);
54       };
55     }
56     MSG_comm_destroy(comm);
57   }
58
59   for (i = 0; i < receivers_count; i++) {
60     char mailbox[80];
61     sprintf(mailbox, "receiver-%ld", i % receivers_count);
62     task = MSG_task_create("finalize", 0, 0, 0);
63     comm = MSG_task_isend(task, mailbox);
64     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
65     if (sleep_test_time == 0) {
66       MSG_comm_wait(comm, -1);
67     } else {
68       while (MSG_comm_test(comm) == 0) {
69         MSG_process_sleep(sleep_test_time);
70       };
71     }
72     MSG_comm_destroy(comm);
73   }
74
75   XBT_INFO("Goodbye now!");
76   return 0;
77 }
78
79 static int receiver(int argc, char *argv[])
80 {
81   msg_task_t task = NULL;
82   XBT_ATTRIB_UNUSED msg_error_t res;
83   int id = -1;
84   char mailbox[80];
85   msg_comm_t res_irecv;
86   double sleep_start_time = xbt_str_parse_double(argv[2], "Invalid sleep start parameter: %s");
87   double sleep_test_time = xbt_str_parse_double(argv[3], "Invalid sleep test parameter: %s");
88   XBT_INFO("sleep_start_time : %f , sleep_test_time : %f", sleep_start_time, sleep_test_time);
89
90   XBT_ATTRIB_UNUSED int read;
91   read = sscanf(argv[1], "%d", &id);
92   xbt_assert(read, "Invalid argument %s\n", argv[1]);
93
94   MSG_process_sleep(sleep_start_time);
95
96   sprintf(mailbox, "receiver-%d", id);
97   while (1) {
98     res_irecv = MSG_task_irecv(&(task), mailbox);
99     XBT_INFO("Wait to receive a task");
100
101     if (sleep_test_time == 0) {
102       res = MSG_comm_wait(res_irecv, -1);
103       xbt_assert(res == MSG_OK, "MSG_task_get failed");
104     } else {
105       while (MSG_comm_test(res_irecv) == 0) {
106         MSG_process_sleep(sleep_test_time);
107       };
108     }
109     MSG_comm_destroy(res_irecv);
110
111     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
112     if (!strcmp(MSG_task_get_name(task), "finalize")) {
113       MSG_task_destroy(task);
114       break;
115     }
116
117     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
118     MSG_task_execute(task);
119     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
120     MSG_task_destroy(task);
121     task = NULL;
122   }
123   XBT_INFO("I'm done. See you!");
124   return 0;
125 }
126
127 int main(int argc, char *argv[])
128 {
129   msg_error_t res = MSG_OK;
130
131   MSG_init(&argc, argv);
132   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
133              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
134
135   MSG_create_environment(argv[1]);
136
137   MSG_function_register("sender", sender);
138   MSG_function_register("receiver", receiver);
139   MSG_launch_application(argv[2]);
140
141   res = MSG_main();
142
143   XBT_INFO("Simulation time %g", MSG_get_clock());
144
145   return res != MSG_OK;
146 }