3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 /* Create a log channel to have nice outputs. */
12 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
14 int master(int argc, char *argv[]);
15 int slave(int argc, char *argv[]);
16 int forwarder(int argc, char *argv[]);
17 void test_all(const char *platform_file, const char *application_file);
24 /* This function is just used so that users can check that each process
25 * has received the arguments it was supposed to receive.
27 static void print_args(int argc, char** argv)
33 fprintf(stderr,"%s ",argv[i]);
34 fprintf(stderr,">\n");
37 /** Emitter function */
38 int master(int argc, char *argv[])
41 m_host_t *slaves = NULL;
42 m_task_t *todo = NULL;
43 int number_of_tasks = 0;
44 double task_comp_size = 0;
45 double task_comm_size = 0;
50 print_args(argc,argv);
52 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
53 "Invalid argument %s\n",argv[1]);
54 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
55 "Invalid argument %s\n",argv[2]);
56 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
57 "Invalid argument %s\n",argv[3]);
60 char sprintf_buffer[64];
62 todo = calloc(number_of_tasks, sizeof(m_task_t));
64 for (i = 0; i < number_of_tasks; i++) {
65 sprintf(sprintf_buffer, "Task_%d", i);
66 todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
70 { /* Process organisation */
71 slaves_count = argc - 4;
72 slaves = calloc(slaves_count, sizeof(m_host_t));
74 for (i = 4; i < argc; i++) {
75 slaves[i-4] = MSG_get_host_by_name(argv[i]);
76 if(slaves[i-4]==NULL) {
77 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
83 INFO1("Got %d slave(s) :", slaves_count);
84 for (i = 0; i < slaves_count; i++)
85 INFO1("\t %s", slaves[i]->name);
87 INFO1("Got %d task to process :", number_of_tasks);
89 for (i = 0; i < number_of_tasks; i++)
90 INFO1("\t\"%s\"", todo[i]->name);
92 for (i = 0; i < number_of_tasks; i++) {
93 INFO2("Sending \"%s\" to \"%s\"",
95 slaves[i % slaves_count]->name);
96 if(MSG_host_self()==slaves[i % slaves_count]) {
97 INFO0("Hey ! It's me ! :)");
99 MSG_task_put(todo[i], slaves[i % slaves_count],
101 INFO0("Send completed");
104 INFO0("All tasks have been dispatched. Bye!");
108 } /* end_of_master */
110 /** Receiver function */
111 int slave(int argc, char *argv[])
113 print_args(argc,argv);
116 m_task_t task = NULL;
118 a = MSG_task_get(&(task), PORT_22);
120 INFO1("Received \"%s\" ", task->name);
121 INFO1("Processing \"%s\" ", task->name);
122 MSG_task_execute(task);
123 INFO1("\"%s\" done ", task->name);
124 MSG_task_destroy(task);
126 INFO0("Hey ?! What's up ? ");
127 xbt_assert0(0,"Unexpected behaviour");
130 INFO0("I'm done. See you!");
134 /** Receiver function */
135 int forwarder(int argc, char *argv[])
138 int slaves_count = argc - 1;
139 m_host_t *slaves = calloc(slaves_count, sizeof(m_host_t));
141 print_args(argc,argv);
143 { /* Process organisation */
144 slaves_count = argc - 1;
145 slaves = calloc(slaves_count, sizeof(m_host_t));
147 for (i = 1; i < argc; i++) {
148 slaves[i-1] = MSG_get_host_by_name(argv[i]);
149 if(slaves[i-1]==NULL) {
150 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
158 m_task_t task = NULL;
160 a = MSG_task_get(&(task), PORT_22);
162 INFO1("Received \"%s\" ", task->name);
163 INFO2("Sending \"%s\" to \"%s\"",
165 slaves[i % slaves_count]->name);
166 MSG_task_put(task, slaves[i % slaves_count],
169 INFO0("Hey ?! What's up ? ");
170 xbt_assert0(0,"Unexpected behaviour");
174 INFO0("I'm done. See you!");
176 } /* end_of_forwarder */
180 void test_all(const char *platform_file,const char *application_file)
183 /* MSG_config("surf_workstation_model","KCCFLN05"); */
184 { /* Simulation setting */
185 MSG_set_channel_number(MAX_CHANNEL);
186 MSG_paje_output("msg_test.trace");
187 MSG_create_environment(platform_file);
189 { /* Application deployment */
190 MSG_function_register("master", master);
191 MSG_function_register("slave", slave);
192 MSG_function_register("forwarder", forwarder);
193 MSG_launch_application(application_file);
197 INFO1("Simulation time %g",MSG_getClock());
198 } /* end_of_test_all */
202 int main(int argc, char *argv[])
204 MSG_global_init_args(&argc,argv);
206 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
207 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
210 test_all(argv[1],argv[2]);