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. */
9 #include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
10 #include "xbt/sysdep.h" /* calloc, printf */
12 /* Create a log channel to have nice outputs. */
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 int forwarder(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file, const char *application_file);
27 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
29 /** Emitter function */
30 int master(int argc, char *argv[])
33 m_host_t *slaves = NULL;
34 int number_of_tasks = 0;
35 double task_comp_size = 0;
36 double task_comm_size = 0;
41 xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
42 "Invalid argument %s\n",argv[1]);
43 xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
44 "Invalid argument %s\n",argv[2]);
45 xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
46 "Invalid argument %s\n",argv[3]);
48 { /* Process organisation */
49 slaves_count = argc - 4;
50 slaves = calloc(slaves_count, sizeof(m_host_t));
52 for (i = 4; i < argc; i++) {
53 slaves[i-4] = MSG_get_host_by_name(argv[i]);
54 if(slaves[i-4]==NULL) {
55 INFO1("Unknown host %s. Stopping Now! ", argv[i]);
61 INFO1("Got %d slave(s) :", slaves_count);
62 for (i = 0; i < slaves_count; i++)
63 INFO1("%s", slaves[i]->name);
65 INFO1("Got %d task to process :", number_of_tasks);
67 for (i = 0; i < number_of_tasks; i++) {
68 m_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size,
69 calloc(1,sizeof(double)));
71 *((double*) task->data) = MSG_get_clock();
73 a = MSG_task_put_with_timeout(task, slaves[i % slaves_count], PORT_22,10.0);
75 INFO0("Send completed");
76 } else if (a == MSG_HOST_FAILURE) {
77 INFO0("Gloups. The cpu on which I'm running just turned off!. See you!");
80 } else if (a == MSG_TRANSFER_FAILURE) {
81 INFO1("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!",
82 slaves[i % slaves_count]->name);
83 MSG_task_destroy(task);
85 INFO0("Hey ?! What's up ? ");
86 xbt_assert0(0,"Unexpected behavior");
90 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
91 for (i = 0; i < slaves_count; i++) {
92 m_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
93 int a = MSG_task_put_with_timeout(task, slaves[i], PORT_22,1.0);
94 if (a == MSG_OK) continue;
95 if (a == MSG_HOST_FAILURE) {
96 INFO0("Gloups. The cpu on which I'm running just turned off!. See you!");
98 } else if (a == MSG_TRANSFER_FAILURE) {
99 INFO1("Mmh. Can't reach '%s'! Nevermind. Let's keep going!",
101 MSG_task_destroy(task);
103 INFO0("Hey ?! What's up ? ");
104 xbt_assert2(0,"Unexpected behavior with '%s': %d",slaves[i]->name,a);
108 INFO0("Goodbye now!");
111 } /* end_of_master */
113 /** Receiver function */
114 int slave(int argc, char *argv[])
117 m_task_t task = NULL;
121 time1 = MSG_get_clock();
122 a = MSG_task_get(&(task), PORT_22);
123 time2 = MSG_get_clock();
125 INFO1("Received \"%s\"", MSG_task_get_name(task));
126 if(MSG_task_get_data(task)==FINALIZE) {
127 MSG_task_destroy(task);
130 if(time1<*((double *)task->data))
131 time1 = *((double *) task->data);
132 INFO1("Communication time : \"%f\"", time2-time1);
133 INFO1("Processing \"%s\"", MSG_task_get_name(task));
134 a = MSG_task_execute(task);
136 INFO1("\"%s\" done", MSG_task_get_name(task));
138 MSG_task_destroy(task);
139 } else if (a == MSG_HOST_FAILURE) {
140 INFO0("Gloups. The cpu on which I'm running just turned off!. See you!");
143 INFO0("Hey ?! What's up ? ");
144 xbt_assert0(0,"Unexpected behavior");
146 } else if (a == MSG_HOST_FAILURE) {
147 INFO0("Gloups. The cpu on which I'm running just turned off!. See you!");
149 } else if (a == MSG_TRANSFER_FAILURE) {
150 INFO0("Mmh. Something went wrong. Nevermind. Let's keep going!");
152 INFO0("Hey ?! What's up ? ");
153 xbt_assert0(0,"Unexpected behavior");
156 INFO0("I'm done. See you!");
161 MSG_error_t test_all(const char *platform_file,
162 const char *application_file)
164 MSG_error_t res = MSG_OK;
166 /* MSG_config("workstation_model","KCCFLN05"); */
167 { /* Simulation setting */
168 MSG_set_channel_number(MAX_CHANNEL);
169 MSG_paje_output("msg_test.trace");
170 MSG_create_environment(platform_file);
172 { /* Application deployment */
173 MSG_function_register("master", master);
174 MSG_function_register("slave", slave);
175 MSG_launch_application(application_file);
179 INFO1("Simulation time %g",MSG_get_clock());
181 } /* end_of_test_all */
185 int main(int argc, char *argv[])
187 MSG_error_t res = MSG_OK;
189 MSG_global_init(&argc,argv);
191 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
192 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
195 res = test_all(argv[1],argv[2]);