Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adapt masterslave_bypass to the brand new AS tag.
[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,number_of_tasks);
36         for (i = 0; i < number_of_tasks; i++) {
37         char mailbox[256];
38         char sprintf_buffer[256];
39         m_task_t task=NULL;
40
41     sprintf(mailbox,"slave-%ld",i % slaves_count);
42         sprintf(sprintf_buffer, "Task_%d", i);
43         task = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
44         if (number_of_tasks<10000 || i%10000 == 0)
45             INFO3("Sending \"%s\" (of %ld) to mailbox \"%s\"", task->name, number_of_tasks, mailbox);
46             MSG_task_send(task, mailbox);
47           }
48
49         INFO0("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     INFO0("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   int res;
67   int id = -1;
68   char mailbox[80];
69
70   xbt_assert1(sscanf(argv[1],"%d", &id),
71          "Invalid argument %s\n",argv[1]);
72
73   sprintf(mailbox,"slave-%d",id);
74
75   while(1) {
76     res = MSG_task_receive(&(task), mailbox);
77     xbt_assert0(res == MSG_OK, "MSG_task_get failed");
78
79     INFO1("Received \"%s\"", MSG_task_get_name(task));
80     if (!strcmp(MSG_task_get_name(task),"finalize")) {
81         MSG_task_destroy(task);
82         break;
83     }
84
85     INFO1("Processing \"%s\"", MSG_task_get_name(task));
86     MSG_task_execute(task);
87     INFO1("\"%s\" done", MSG_task_get_name(task));
88     MSG_task_destroy(task);
89     task = NULL;
90   }
91   INFO0("I'm done. See you!");
92   return 0;
93 } /* end_of_slave */
94
95 /** Test function */
96 MSG_error_t test_all(const char *file)//(void)
97 {
98   MSG_error_t res = MSG_OK;
99   /*  Simulation setting */
100   MSG_set_channel_number(MAX_CHANNEL);
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   INFO1("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 */