Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The output of this test has changed after r9407
[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 typedef enum {
22   PORT_22 = 0,
23   MAX_CHANNEL
24 } channel_t;
25
26 /** Emitter function  */
27 int master(int argc, char *argv[])
28 {
29   long number_of_tasks = atol(argv[1]);
30   double task_comp_size = atof(argv[2]);
31   double task_comm_size = atof(argv[3]);
32   long slaves_count = atol(argv[4]);
33   int i;
34
35   INFO2("Got %ld slaves and %ld tasks to process", slaves_count,
36         number_of_tasks);
37   for (i = 0; i < number_of_tasks; i++) {
38     char mailbox[256];
39     char sprintf_buffer[256];
40     m_task_t task = NULL;
41
42     sprintf(mailbox, "slave-%ld", i % slaves_count);
43     sprintf(sprintf_buffer, "Task_%d", i);
44     task =
45         MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size,
46                         NULL);
47     if (number_of_tasks < 10000 || i % 10000 == 0)
48       INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name,
49             number_of_tasks, mailbox);
50     MSG_task_send(task, mailbox);
51   }
52
53   INFO0
54       ("All tasks have been dispatched. Let's tell everybody the computation is over.");
55   for (i = 0; i < slaves_count; i++) {
56     char mailbox[80];
57
58     sprintf(mailbox, "slave-%ld", i % slaves_count);
59     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
60     MSG_task_send(finalize, mailbox);
61   }
62
63   INFO0("Goodbye now!");
64   return 0;
65 }                               /* end_of_master */
66
67 /** Receiver function  */
68 int slave(int argc, char *argv[])
69 {
70   m_task_t task = NULL;
71   int res;
72   int id = -1;
73   char mailbox[80];
74
75   xbt_assert1(sscanf(argv[1], "%d", &id),
76               "Invalid argument %s\n", argv[1]);
77
78   sprintf(mailbox, "slave-%d", id);
79
80   while (1) {
81     res = MSG_task_receive(&(task), mailbox);
82     xbt_assert0(res == MSG_OK, "MSG_task_get failed");
83
84     INFO1("Received \"%s\"", MSG_task_get_name(task));
85     if (!strcmp(MSG_task_get_name(task), "finalize")) {
86       MSG_task_destroy(task);
87       break;
88     }
89
90     INFO1("Processing \"%s\"", MSG_task_get_name(task));
91     MSG_task_execute(task);
92     INFO1("\"%s\" done", MSG_task_get_name(task));
93     MSG_task_destroy(task);
94     task = NULL;
95   }
96   INFO0("I'm done. See you!");
97   return 0;
98 }                               /* end_of_slave */
99
100 /** Test function */
101 MSG_error_t test_all(const char *file)  //(void)
102 {
103   MSG_error_t res = MSG_OK;
104   /*  Simulation setting */
105   MSG_set_channel_number(MAX_CHANNEL);
106   /*start by registering functions before loading script */
107   MSG_function_register("master", master);
108   MSG_function_register("slave", slave);
109   MSG_load_platform_script(file);
110
111   res = MSG_main();
112
113   INFO1("Simulation time %g", MSG_get_clock());
114   return res;
115 }                               /* end_of_test_all */
116
117 /** Main function */
118 int main(int argc, char *argv[])
119 {
120   MSG_error_t res = MSG_OK;
121
122   MSG_global_init(&argc, argv);
123   if (argc < 2) {
124     printf("Usage: %s platform_script[.lua]\n", argv[0]);
125     printf("example: %s platform_script.lua\n", argv[0]);
126     exit(1);
127   }
128   res = test_all(argv[1]);
129   MSG_clean();
130
131   if (res == MSG_OK)
132     return 0;
133   else
134     return 1;
135 }                               /* end_of_main */