Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2271765b5626c6c1c03f2a0469a29538c39cdf2f
[simgrid.git] / examples / msg / irc_isend / peer.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,"Messages specific for this msg example");
15
16 int sender(int argc, char *argv[]);
17 int receiver(int argc, char *argv[]);
18
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 /** Sender function  */
22 int sender(int argc, char *argv[])
23 {
24   long number_of_tasks = atol(argv[1]);
25   double task_comp_size = atof(argv[2]);
26   double task_comm_size = atof(argv[3]);
27   long receivers_count = atol(argv[4]);
28   double sleep_start_time = atof(argv[5]);
29   double sleep_test_time = atof(argv[6]);
30
31   INFO2("sleep_start_time : %f , sleep_test_time : %f",sleep_start_time,sleep_test_time);
32
33   msg_comm_t comm = NULL;
34   int i;
35   m_task_t task = NULL;
36   MSG_process_sleep (sleep_start_time);
37   for (i = 0; i < number_of_tasks; i++)
38   {
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     INFO2("Send to receiver-%ld Task_%d",i % receivers_count,i);
48
49     if(sleep_test_time == 0)
50     {
51         MSG_comm_wait(comm,-1);
52     }
53     else
54     {
55                 while(MSG_comm_test(comm) == 0){
56                         MSG_process_sleep (sleep_test_time);
57                 };
58                 MSG_comm_destroy(comm);
59     }
60
61   }
62
63   for (i = 0; i < receivers_count; i++)
64   {
65     char mailbox[80];
66     sprintf(mailbox,"receiver-%ld",i % receivers_count);
67     task = MSG_task_create ("finalize", 0, 0, 0);
68     comm = MSG_task_isend(task, mailbox);
69     INFO1("Send to receiver-%ld finalize",i % receivers_count);
70     if(sleep_test_time == 0)
71     {
72         MSG_comm_wait(comm,-1);
73     }
74     else
75     {
76                 while(MSG_comm_test(comm) == 0){
77                         MSG_process_sleep (sleep_test_time);
78                 };
79                 MSG_comm_destroy(comm);
80     }
81
82   }
83
84   INFO0("Goodbye now!");
85   return 0;
86 } /* end_of_sender */
87
88 /** Receiver function  */
89 int receiver(int argc, char *argv[])
90 {
91   m_task_t task = NULL;
92   MSG_error_t res;
93   int id = -1;
94   char mailbox[80];
95   msg_comm_t res_irecv;
96   double sleep_start_time = atof(argv[2]);
97   double sleep_test_time = atof(argv[3]);
98   INFO2("sleep_start_time : %f , sleep_test_time : %f",sleep_start_time,sleep_test_time);
99
100   xbt_assert1(sscanf(argv[1],"%d", &id),
101          "Invalid argument %s\n",argv[1]);
102
103   MSG_process_sleep (sleep_start_time);
104
105   sprintf(mailbox,"receiver-%d",id);
106   while(1) {
107         res_irecv = MSG_task_irecv(&(task), mailbox);
108         INFO0("Wait to receive a task");
109
110     if(sleep_test_time == 0)
111     {
112         res = MSG_comm_wait(res_irecv,-1);
113         xbt_assert0(res == MSG_OK, "MSG_task_get failed");
114     }
115     else
116     {
117                 while(MSG_comm_test(res_irecv) == 0){
118                         MSG_process_sleep (sleep_test_time);
119                 };
120                 MSG_comm_destroy(res_irecv);
121     }
122
123         INFO1("Received \"%s\"", MSG_task_get_name(task));
124         if (!strcmp(MSG_task_get_name(task),"finalize")) {
125           MSG_task_destroy(task);
126           break;
127     }
128
129     INFO1("Processing \"%s\"", MSG_task_get_name(task));
130     MSG_task_execute(task);
131     INFO1("\"%s\" done", MSG_task_get_name(task));
132     MSG_task_destroy(task);
133     task = NULL;
134   }
135   INFO0("I'm done. See you!");
136   return 0;
137 } /* end_of_receiver */
138
139 /** Test function */
140 MSG_error_t test_all(const char *platform_file,
141                             const char *application_file)
142 {
143   MSG_error_t res = MSG_OK;
144
145   /* MSG_config("workstation/model","KCCFLN05"); */
146   {                             /*  Simulation setting */
147     MSG_set_channel_number(0);
148     MSG_create_environment(platform_file);
149   }
150   {                            /*   Application deployment */
151     MSG_function_register("sender", sender);
152     MSG_function_register("receiver", receiver);
153     MSG_launch_application(application_file);
154   }
155   res = MSG_main();
156   
157   INFO1("Simulation time %g",MSG_get_clock());
158   return res;
159 } /* end_of_test_all */
160
161
162 /** Main function */
163 int main(int argc, char *argv[])
164 {
165   MSG_error_t res = MSG_OK;
166
167   MSG_global_init(&argc,argv);
168   if (argc < 3) {
169      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
170      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
171      exit(1);
172   }
173   res = test_all(argv[1],argv[2]);
174    SIMIX_message_sizes_output("toto.txt");
175   MSG_clean();
176
177   if(res==MSG_OK)
178     return 0;
179   else
180     return 1;
181 } /* end_of_main */