Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removing tracing functions from examples that are not in msg/tracing directory
[simgrid.git] / examples / msg / masterslave / masterslave_mailbox.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 master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 int forwarder(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 /** Emitter function  */
22 int master(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 slaves_count = atol(argv[4]);
28
29   int i;
30
31   INFO2("Got %ld slaves and %ld tasks to process", slaves_count,number_of_tasks);
32
33   for (i = 0; i < number_of_tasks; i++) {
34     char mailbox[256];
35     char sprintf_buffer[256];
36     m_task_t task=NULL;
37
38     sprintf(mailbox,"slave-%ld",i % slaves_count);
39     sprintf(sprintf_buffer, "Task_%d", i);
40     task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
41     if (number_of_tasks<10000 || i%10000 == 0) 
42       INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
43      
44     MSG_task_send(task, mailbox);
45   }
46   
47    INFO0("All tasks have been dispatched. Let's tell everybody the computation is over."); 
48    for (i = 0; i < slaves_count; i++) { 
49      char mailbox[80]; 
50     
51      sprintf(mailbox,"slave-%ld",i % slaves_count); 
52      m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
53      MSG_task_send(finalize, 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   int i;
121
122   MSG_global_init(&argc,argv);
123   if (argc < 3) {
124      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
125      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
126      exit(1);
127   }
128   res = test_all(argv[1],argv[2]);
129    SIMIX_message_sizes_output("toto.txt");
130   MSG_clean();
131
132   if(res==MSG_OK)
133     return 0;
134   else
135     return 1;
136 } /* end_of_main */