Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9f7ed8caf6c13e7bb462705aafed7f180a35a31f
[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   int result=0;
97   double sleep_start_time = atof(argv[2]);
98   INFO1("sleep_start_time : %f ",sleep_start_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         res = MSG_comm_wait(res_irecv,-1);
110
111     xbt_assert0(res == MSG_OK, "MSG_task_get failed");
112         INFO1("Received \"%s\"", MSG_task_get_name(task));
113         if (!strcmp(MSG_task_get_name(task),"finalize")) {
114           MSG_task_destroy(task);
115           break;
116     }
117
118     INFO1("Processing \"%s\"", MSG_task_get_name(task));
119     MSG_task_execute(task);
120     INFO1("\"%s\" done", MSG_task_get_name(task));
121     MSG_task_destroy(task);
122     task = NULL;
123   }
124   INFO0("I'm done. See you!");
125   return 0;
126 } /* end_of_receiver */
127
128 /** Test function */
129 MSG_error_t test_all(const char *platform_file,
130                             const char *application_file)
131 {
132   MSG_error_t res = MSG_OK;
133
134   /* MSG_config("workstation/model","KCCFLN05"); */
135   {                             /*  Simulation setting */
136     MSG_set_channel_number(0);
137     MSG_create_environment(platform_file);
138   }
139   {                            /*   Application deployment */
140     MSG_function_register("sender", sender);
141     MSG_function_register("receiver", receiver);
142     MSG_launch_application(application_file);
143   }
144   res = MSG_main();
145   
146   INFO1("Simulation time %g",MSG_get_clock());
147   return res;
148 } /* end_of_test_all */
149
150
151 /** Main function */
152 int main(int argc, char *argv[])
153 {
154   MSG_error_t res = MSG_OK;
155
156   MSG_global_init(&argc,argv);
157   if (argc < 3) {
158      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
159      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
160      exit(1);
161   }
162   res = test_all(argv[1],argv[2]);
163    SIMIX_message_sizes_output("toto.txt");
164   MSG_clean();
165
166   if(res==MSG_OK)
167     return 0;
168   else
169     return 1;
170 } /* end_of_main */