Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a proper example of master/slaves, using mailboxes
[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   int i;
31
32   printf("Got %ld slaves and %ld tasks to process\n", slaves_count,number_of_tasks);
33 //  INFO2("Got %ld slaves and %ld tasks to process", slaves_count,number_of_tasks);
34
35   for (i = 0; i < number_of_tasks; i++) {
36     char mailbox[256];
37     char sprintf_buffer[256];
38     m_task_t task=NULL;
39
40     sprintf(mailbox,"slave-%ld",i % slaves_count);
41     sprintf(sprintf_buffer, "Task_%d", i);
42     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
43     if (number_of_tasks<10000 || i%10000 == 0) {          
44       printf("Sending \"%s\" (of %ld) to mailbox \"%s\"\n", task->name, number_of_tasks, mailbox);
45       fflush(stdout);
46     }
47      
48     MSG_task_send(task, mailbox);
49    // INFO0("Sent");
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      MSG_task_send(MSG_task_create("finalize", 0, 0, 0), mailbox); 
58    } 
59   
60 //  INFO0("Goodbye now!");
61   exit(0);
62 } /* end_of_master */
63
64 /** Receiver function  */
65 int slave(int argc, char *argv[])
66 {
67   m_task_t task = NULL;
68   int res;
69   int id = -1;
70   char mailbox[80];
71
72   xbt_assert1(sscanf(argv[1],"%d", &id),
73          "Invalid argument %s\n",argv[1]);
74
75   sprintf(mailbox,"slave-%d",id);
76
77   while(1) {
78     res = MSG_task_receive(&(task), mailbox);
79     xbt_assert0(res == MSG_OK, "MSG_task_get failed");
80
81 //  INFO1("Received \"%s\"", MSG_task_get_name(task));
82     if (!strcmp(MSG_task_get_name(task),"finalize")) {
83         MSG_task_destroy(task);
84         break;
85     }
86      
87 //    INFO1("Processing \"%s\"", MSG_task_get_name(task));
88     MSG_task_execute(task);
89 //    INFO1("\"%s\" done", MSG_task_get_name(task));
90     MSG_task_destroy(task);
91     task = NULL;
92   }
93   INFO0("I'm done. See you!");
94   return 0;
95 } /* end_of_slave */
96
97 /** Test function */
98 MSG_error_t test_all(const char *platform_file,
99                             const char *application_file)
100 {
101   MSG_error_t res = MSG_OK;
102
103   /* MSG_config("surf_workstation_model","KCCFLN05"); */
104   {                             /*  Simulation setting */
105     MSG_set_channel_number(0);
106     MSG_paje_output("msg_test.trace");
107     MSG_create_environment(platform_file);
108   }
109   {                            /*   Application deployment */
110     MSG_function_register("master", master);
111     MSG_function_register("slave", slave);
112     MSG_launch_application(application_file);
113   }
114   res = MSG_main();
115   
116   INFO1("Simulation time %g",MSG_get_clock());
117   return res;
118 } /* end_of_test_all */
119
120
121 /** Main function */
122 int main(int argc, char *argv[])
123 {
124   MSG_error_t res = MSG_OK;
125
126   MSG_global_init(&argc,argv);
127   if (argc < 3) {
128      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
129      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
130      exit(1);
131   }
132   res = test_all(argv[1],argv[2]);
133   MSG_clean();
134
135   if(res==MSG_OK)
136     return 0;
137   else
138     return 1;
139 } /* end_of_main */