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)));
70 *((double*) task->data) = MSG_get_clock();
71 MSG_task_put(task, slaves[i % slaves_count], PORT_22);
72 INFO0("Send completed");
75 INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
76 for (i = 0; i < slaves_count; i++)
77 MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
80 INFO0("Goodbye now!");
85 /** Receiver function */
86 int slave(int argc, char *argv[])
93 time1 = MSG_get_clock();
94 a = MSG_task_get(&(task), PORT_22);
95 time2 = MSG_get_clock();
97 INFO1("Received \"%s\"", MSG_task_get_name(task));
98 if(MSG_task_get_data(task)==FINALIZE) {
99 MSG_task_destroy(task);
102 if(time1<*((double *)task->data))
103 time1 = *((double *) task->data);
104 INFO1("Communication time : \"%f\"", time2-time1);
105 INFO1("Processing \"%s\"", MSG_task_get_name(task));
106 MSG_task_execute(task);
107 INFO1("\"%s\" done", MSG_task_get_name(task));
108 MSG_task_destroy(task);
110 INFO0("Hey ?! What's up ? ");
111 xbt_assert0(0,"Unexpected behavior");
114 INFO0("I'm done. See you!");
119 MSG_error_t test_all(const char *platform_file,
120 const char *application_file)
122 MSG_error_t res = MSG_OK;
124 /* MSG_config("surf_workstation_model","KCCFLN05"); */
125 { /* Simulation setting */
126 MSG_set_channel_number(MAX_CHANNEL);
127 MSG_paje_output("msg_test.trace");
128 MSG_create_environment(platform_file);
130 { /* Application deployment */
131 MSG_function_register("master", master);
132 MSG_function_register("slave", slave);
133 MSG_launch_application(application_file);
137 INFO1("Simulation time %g",MSG_get_clock());
139 } /* end_of_test_all */
143 int main(int argc, char *argv[])
145 MSG_error_t res = MSG_OK;
147 MSG_global_init(&argc,argv);
149 printf ("Usage: %s platform_file deployment_file\n",argv[0]);
150 printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
153 res = test_all(argv[1],argv[2]);