Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix prototype declaration for gtnets compilation
[simgrid.git] / examples / msg / masterslave / masterslave_console.c
1 /* Copyright (c) 2007, 2008, 2009, 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 "surf/surfxml_parse.h" /* to override surf_parse and bypass the parser */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15 #define FINALIZE ((void*)221297)        /* a magic number to tell people to stop working */
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 MSG_error_t test_all(const char *);
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   int i;
29
30   XBT_INFO("Got %ld slaves and %ld tasks to process", slaves_count,
31         number_of_tasks);
32   for (i = 0; i < number_of_tasks; i++) {
33     char mailbox[256];
34     char sprintf_buffer[256];
35     m_task_t task = NULL;
36
37     sprintf(mailbox, "slave-%ld", i % slaves_count);
38     sprintf(sprintf_buffer, "Task_%d", i);
39     task =
40         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
41                         NULL);
42     if (number_of_tasks < 10000 || i % 10000 == 0)
43       XBT_INFO("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
44             number_of_tasks, mailbox);
45     MSG_task_send(task, mailbox);
46   }
47
48   XBT_INFO
49       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
50   for (i = 0; i < slaves_count; i++) {
51     char mailbox[80];
52
53     sprintf(mailbox, "slave-%ld", i % slaves_count);
54     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
55     MSG_task_send(finalize, mailbox);
56   }
57
58   XBT_INFO("Goodbye now!");
59   return 0;
60 }                               /* end_of_master */
61
62 /** Receiver function  */
63 int slave(int argc, char *argv[])
64 {
65   m_task_t task = NULL;
66   _XBT_GNUC_UNUSED int res;
67   int id = -1;
68   char mailbox[80];
69   _XBT_GNUC_UNUSED int read;
70
71   read = sscanf(argv[1], "%d", &id);
72   xbt_assert(read, "Invalid argument %s\n", argv[1]);
73
74   sprintf(mailbox, "slave-%d", id);
75
76   while (1) {
77     res = MSG_task_receive(&(task), mailbox);
78     xbt_assert(res == MSG_OK, "MSG_task_get failed");
79
80     XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
81     if (!strcmp(MSG_task_get_name(task), "finalize")) {
82       MSG_task_destroy(task);
83       break;
84     }
85
86     XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
87     MSG_task_execute(task);
88     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
89     MSG_task_destroy(task);
90     task = NULL;
91   }
92   XBT_INFO("I'm done. See you!");
93   return 0;
94 }                               /* end_of_slave */
95
96 /** Test function */
97 MSG_error_t test_all(const char *file)  //(void)
98 {
99   MSG_error_t res = MSG_OK;
100
101   /*start by registering functions before loading script */
102   MSG_function_register("master", master);
103   MSG_function_register("slave", slave);
104   MSG_load_platform_script(file);
105
106   res = MSG_main();
107
108   XBT_INFO("Simulation time %g", MSG_get_clock());
109   return res;
110 }                               /* end_of_test_all */
111
112 /** Main function */
113 int main(int argc, char *argv[])
114 {
115   MSG_error_t res = MSG_OK;
116
117   MSG_global_init(&argc, argv);
118   if (argc < 2) {
119     printf("Usage: %s platform_script[.lua]\n", argv[0]);
120     printf("example: %s platform_script.lua\n", argv[0]);
121     exit(1);
122   }
123   res = test_all(argv[1]);
124   MSG_clean();
125
126   if (res == MSG_OK)
127     return 0;
128   else
129     return 1;
130 }                               /* end_of_main */