Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
963a49edceead6882c3f6d5dee1bca3cc62dda35
[simgrid.git] / examples / msg / masterslave / masterslave_console.c
1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /** @addtogroup MSG_examples
7  * 
8  * - <b>masterslave/masterslave_console.c</b>: demonstrate how to use
9  *   lua files instead of XML for the platform and deployment
10  *   declaration using @ref MSG_load_platform_script. The most
11  *   interesting part is probably not the C code, but rather the
12  *   <b>masterslave/masterslave_script.lua</b>, which demonstrates
13  *   how to express the platform and deployment in lua. 
14  *
15  */
16
17 #include <stdio.h>
18 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
19 #include "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
20
21 /* Create a log channel to have nice outputs. */
22 #include "xbt/log.h"
23 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
24                              "Messages specific for this msg example");
25 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
26
27 int master(int argc, char *argv[]);
28 int slave(int argc, char *argv[]);
29 MSG_error_t test_all(const char *);
30
31 /** Emitter function  */
32 int master(int argc, char *argv[])
33 {
34   long number_of_tasks = atol(argv[1]);
35   double task_comp_size = atof(argv[2]);
36   double task_comm_size = atof(argv[3]);
37   long slaves_count = atol(argv[4]);
38   int i;
39
40   XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
41         number_of_tasks);
42   for (i = 0; i < number_of_tasks; i++) {
43     char mailbox[256];
44     char sprintf_buffer[256];
45     msg_task_t task = NULL;
46
47     sprintf(mailbox, "slave-%ld", i % slaves_count);
48     sprintf(sprintf_buffer, "Task_%d", i);
49     task =
50         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
51                         NULL);
52     if (number_of_tasks < 10000 || i % 10000 == 0)
53       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
54             number_of_tasks, mailbox);
55     MSG_task_send(task, mailbox);
56   }
57
58   XBT_INFO
59       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
60   for (i = 0; i < slaves_count; i++) {
61     char mailbox[80];
62
63     sprintf(mailbox, "slave-%ld", i % slaves_count);
64     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
65     MSG_task_send(finalize, mailbox);
66   }
67
68   XBT_INFO("Goodbye now!");
69   return 0;
70 }                               /* end_of_master */
71
72 /** Receiver function  */
73 int slave(int argc, char *argv[])
74 {
75   msg_task_t task = NULL;
76   _XBT_GNUC_UNUSED int res;
77   int id = -1;
78   char mailbox[80];
79   _XBT_GNUC_UNUSED int read;
80
81   read = sscanf(argv[1], "%d", &id);
82   xbt_assert(read, "Invalid argument %s\n", argv[1]);
83
84   sprintf(mailbox, "slave-%d", id);
85
86   while (1) {
87     res = MSG_task_receive(&(task), mailbox);
88     xbt_assert(res == MSG_OK, "MSG_task_get failed");
89
90     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
91     if (!strcmp(MSG_task_get_name(task), "finalize")) {
92       MSG_task_destroy(task);
93       break;
94     }
95
96     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
97     MSG_task_execute(task);
98     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
99     MSG_task_destroy(task);
100     task = NULL;
101   }
102   XBT_INFO("I'm done. See you!");
103   return 0;
104 }                               /* end_of_slave */
105
106 /** Test function */
107 MSG_error_t test_all(const char *file)  //(void)
108 {
109   MSG_error_t res = MSG_OK;
110
111   /*start by registering functions before loading script */
112   MSG_function_register("master", master);
113   MSG_function_register("slave", slave);
114   MSG_load_platform_script(file);
115
116   res = MSG_main();
117
118   XBT_INFO("Simulation time %g", MSG_get_clock());
119   return res;
120 }                               /* end_of_test_all */
121
122 /** Main function */
123 int main(int argc, char *argv[])
124 {
125   MSG_error_t res = MSG_OK;
126
127   MSG_init(&argc, argv);
128   if (argc < 2) {
129     printf("Usage: %s platform_script[.lua]\n", argv[0]);
130     printf("example: %s platform_script.lua\n", argv[0]);
131     exit(1);
132   }
133   res = test_all(argv[1]);
134   MSG_clean();
135
136   if (res == MSG_OK)
137     return 0;
138   else
139     return 1;
140 }                               /* end_of_main */