Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
aaf70208e4f49ef2850945a9ac54ad12bfead1cf
[simgrid.git] / examples / msg / masterslave / masterslave_mailbox.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "xbt/sysdep.h" /* calloc, printf */
11
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 int forwarder(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file, const char *application_file);
21
22 /** Emitter function  */
23 int master(int argc, char *argv[])
24 {
25   long number_of_tasks = atol(argv[1]);
26   double task_comp_size = atof(argv[2]);
27   double task_comm_size = atof(argv[3]);
28   long slaves_count = atol(argv[4]);
29
30   TRACE_host_variable_set ("is_master", 1);
31
32   int i;
33
34   INFO2("Got %ld slaves and %ld tasks to process", slaves_count,number_of_tasks);
35
36   for (i = 0; i < number_of_tasks; i++) {
37     char mailbox[256];
38     char sprintf_buffer[256];
39     m_task_t task=NULL;
40
41     sprintf(mailbox,"slave-%ld",i % slaves_count);
42     sprintf(sprintf_buffer, "Task_%d", i);
43     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
44     TRACE_host_variable_set ("task_creation", i);
45     TRACE_msg_set_task_category (task, "compute");
46     if (number_of_tasks<10000 || i%10000 == 0) 
47       INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
48      
49     MSG_task_send(task, mailbox);
50   }
51   
52    INFO0("All tasks have been dispatched. Let's tell everybody the computation is over."); 
53    for (i = 0; i < slaves_count; i++) { 
54      char mailbox[80]; 
55     
56      sprintf(mailbox,"slave-%ld",i % slaves_count); 
57      m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
58      TRACE_msg_set_task_category(finalize, "finalize");
59      MSG_task_send(finalize, mailbox);
60    } 
61   
62 //  INFO0("Goodbye now!");
63   return 0;
64 } /* end_of_master */
65
66 /** Receiver function  */
67 int slave(int argc, char *argv[])
68 {
69   m_task_t task = NULL;
70   int res;
71   int id = -1;
72   char mailbox[80];
73
74   TRACE_host_variable_set ("is_slave", 1);
75
76   xbt_assert1(sscanf(argv[1],"%d", &id),
77          "Invalid argument %s\n",argv[1]);
78
79   sprintf(mailbox,"slave-%d",id);
80
81   while(1) {
82     res = MSG_task_receive(&(task), mailbox);
83     xbt_assert0(res == MSG_OK, "MSG_task_get failed");
84
85 //  INFO1("Received \"%s\"", MSG_task_get_name(task));
86     if (!strcmp(MSG_task_get_name(task),"finalize")) {
87         MSG_task_destroy(task);
88         break;
89     }
90      
91 //    INFO1("Processing \"%s\"", MSG_task_get_name(task));
92     TRACE_host_variable_add ("task_computation", MSG_task_get_compute_duration(task));
93     MSG_task_execute(task);
94 //    INFO1("\"%s\" done", MSG_task_get_name(task));
95     MSG_task_destroy(task);
96     task = NULL;
97   }
98   INFO0("I'm done. See you!");
99   return 0;
100 } /* end_of_slave */
101
102 /** Test function */
103 MSG_error_t test_all(const char *platform_file,
104                             const char *application_file)
105 {
106   MSG_error_t res = MSG_OK;
107
108   /* MSG_config("workstation/model","KCCFLN05"); */
109   {                             /*  Simulation setting */
110     MSG_set_channel_number(0);
111     MSG_create_environment(platform_file);
112   }
113   {                            /*   Application deployment */
114     MSG_function_register("master", master);
115     MSG_function_register("slave", slave);
116     MSG_launch_application(application_file);
117   }
118   res = MSG_main();
119   
120   INFO1("Simulation time %g",MSG_get_clock());
121   return res;
122 } /* end_of_test_all */
123
124
125 /** Main function */
126 int main(int argc, char *argv[])
127 {
128   MSG_error_t res = MSG_OK;
129   int is_tracing = 0;
130   int i;
131
132   for (i = 0; i < argc; i++){
133     if (!strcmp (argv[i], "--trace")){
134       is_tracing = 1;
135     }
136   }
137
138   if (is_tracing){
139     //if TRACE_start is not called, all other tracing
140     //functions will be disabled
141     TRACE_start ("simulation.trace");
142   }
143   TRACE_host_variable_declare ("is_slave");
144   TRACE_host_variable_declare ("is_master");
145   TRACE_host_variable_declare ("task_creation");
146   TRACE_host_variable_declare ("task_computation");
147   TRACE_category ("compute");
148   TRACE_category ("finalize");
149
150   MSG_global_init(&argc,argv);
151   if (argc < 3) {
152      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
153      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
154      exit(1);
155   }
156   res = test_all(argv[1],argv[2]);
157    SIMIX_message_sizes_output("toto.txt");
158   MSG_clean();
159
160   if (is_tracing){
161     TRACE_end();
162   }
163
164   if(res==MSG_OK)
165     return 0;
166   else
167     return 1;
168 } /* end_of_main */