Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : add comments for the example bugged2_liveness
[simgrid.git] / examples / msg / icomms / peer2.c
1 /* Copyright (c) 2010. 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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/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 int sender(int argc, char *argv[]);
18 int receiver(int argc, char *argv[]);
19
20 MSG_error_t test_all(const char *platform_file,
21                      const char *application_file);
22
23 /** Sender function  */
24 int sender(int argc, char *argv[])
25 {
26   long number_of_tasks = atol(argv[1]);
27   double task_comp_size = atof(argv[2]);
28   double task_comm_size = atof(argv[3]);
29   long receivers_count = atol(argv[4]);
30
31   msg_comm_t *comm = xbt_new(msg_comm_t, number_of_tasks + receivers_count);
32   int i;
33   m_task_t task = NULL;
34   for (i = 0; i < number_of_tasks; i++) {
35     char mailbox[256];
36     char sprintf_buffer[256];
37     sprintf(mailbox, "receiver-%ld", i % receivers_count);
38     sprintf(sprintf_buffer, "Task_%d", i);
39     task =
40         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
41                         NULL);
42     comm[i] = MSG_task_isend(task, mailbox);
43     XBT_INFO("Send to receiver-%ld Task_%d", i % receivers_count, i);
44   }
45   for (i = 0; i < receivers_count; i++) {
46     char mailbox[80];
47     sprintf(mailbox, "receiver-%ld", i % receivers_count);
48     task = MSG_task_create("finalize", 0, 0, 0);
49     comm[i + number_of_tasks] = MSG_task_isend(task, mailbox);
50     XBT_INFO("Send to receiver-%ld finalize", i % receivers_count);
51
52   }
53   /* Here we are waiting for the completion of all communications */
54   MSG_comm_waitall(comm, (number_of_tasks + receivers_count), -1);
55
56   XBT_INFO("Goodbye now!");
57   xbt_free(comm);
58   return 0;
59 }                               /* end_of_sender */
60
61 /** Receiver function  */
62 int receiver(int argc, char *argv[])
63 {
64   m_task_t task = NULL;
65   _XBT_GNUC_UNUSED MSG_error_t res;
66   int id = -1;
67   char mailbox[80];
68   msg_comm_t res_irecv;
69   _XBT_GNUC_UNUSED int read;
70   read = sscanf(argv[1], "%d", &id);
71   xbt_assert(read, "Invalid argument %s\n", argv[1]);
72   MSG_process_sleep(10);
73   sprintf(mailbox, "receiver-%d", id);
74   while (1) {
75     res_irecv = MSG_task_irecv(&(task), mailbox);
76     XBT_INFO("Wait to receive a task");
77     res = MSG_comm_wait(res_irecv, -1);
78     xbt_assert(res == MSG_OK, "MSG_task_get failed");
79     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
80     if (!strcmp(MSG_task_get_name(task), "finalize")) {
81       MSG_task_destroy(task);
82       break;
83     }
84
85     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
86     MSG_task_execute(task);
87     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
88     MSG_task_destroy(task);
89     task = NULL;
90   }
91   XBT_INFO("I'm done. See you!");
92   return 0;
93 }                               /* end_of_receiver */
94
95 /** Test function */
96 MSG_error_t test_all(const char *platform_file,
97                      const char *application_file)
98 {
99   MSG_error_t res = MSG_OK;
100
101   /* MSG_config("workstation/model","KCCFLN05"); */
102   {                             /*  Simulation setting */
103     MSG_set_channel_number(0);
104     MSG_create_environment(platform_file);
105   }
106   {                             /*   Application deployment */
107     MSG_function_register("sender", sender);
108     MSG_function_register("receiver", receiver);
109     MSG_launch_application(application_file);
110   }
111   res = MSG_main();
112
113   XBT_INFO("Simulation time %g", MSG_get_clock());
114   return res;
115 }                               /* end_of_test_all */
116
117
118 /** Main function */
119 int main(int argc, char *argv[])
120 {
121   MSG_error_t res = MSG_OK;
122
123   MSG_global_init(&argc, argv);
124   if (argc < 3) {
125     printf("Usage: %s platform_file deployment_file\n", argv[0]);
126     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
127     exit(1);
128   }
129   res = test_all(argv[1], argv[2]);
130   MSG_clean();
131
132   if (res == MSG_OK)
133     return 0;
134   else
135     return 1;
136 }                               /* end_of_main */