Logo AND Algorithmique Numérique Distribuée

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