1 /* Copyright (c) 2007-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14 "Messages specific for this msg example");
17 /** @addtogroup MSG_examples
19 * @section MSG_ex_models Models-related examples
21 * @subsection MSG_ex_PLS Packet level simulators
23 * These examples demonstrate how to use the bindings to classical
24 * Packet-Level Simulators (PLS), as explained in \ref pls. The most
25 * interesting is probably not the C files since they are unchanged
26 * from the other simulations, but the associated files, such as the
27 * platform files to see how to declare a platform to be used with
28 * the PLS bindings of SimGrid and the tesh files to see how to
29 * actually start a simulation in these settings.
31 * - <b>ns3</b>: Simple ping-pong using ns3 instead of the SimGrid network models.
35 int master(int argc, char *argv[]);
36 int slave(int argc, char *argv[]);
37 int timer(int argc, char *argv[]);
38 msg_error_t test_all(const char *platform_file,
39 const char *application_file);
41 int timer_start; //set as 1 in the master process
43 //keep a pointer to all surf running tasks.
46 double start_time, end_time, elapsed_time;
47 double gl_data_size[NTASKS];
48 msg_task_t gl_task_array[NTASKS];
49 const char *slavenames[NTASKS];
50 const char *masternames[NTASKS];
51 int gl_task_array_id = 0;
52 int count_finished = 0;
54 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
57 int master(int argc, char *argv[])
59 char *slavename = NULL;
60 double task_comm_size = 0;
63 //unique id to control statistics
66 xbt_assert(argc==4,"Strange number of arguments expected 3 got %d", argc - 1);
68 XBT_DEBUG ("Master started");
72 read = sscanf(argv[1], "%lg", &task_comm_size);
73 xbt_assert(read, "Invalid argument %s\n", argv[1]);
78 sprintf(id_alias, "flow_%d", id);
79 slavenames[id] = slavename;
80 TRACE_category(id_alias);
82 masternames[id] = MSG_host_get_name(MSG_host_self());
84 { /* Task creation. */
85 char sprintf_buffer[64] = "Task_0";
86 todo = MSG_task_create(sprintf_buffer, 100*task_comm_size, task_comm_size, NULL);
87 MSG_task_set_category(todo, id_alias);
88 //keep track of running tasks
89 gl_task_array[id] = todo;
90 gl_data_size[id] = task_comm_size;
93 { /* Process organisation */
94 MSG_get_host_by_name(slavename);
100 /* time measurement */
101 sprintf(id_alias, "%d", id);
102 start_time = MSG_get_clock();
103 //MSG_task_execute(todo);
104 MSG_task_send(todo, id_alias);
105 end_time = MSG_get_clock();
107 XBT_DEBUG ("Finished");
109 } /* end_of_master */
112 /** Timer function */
113 int timer(int argc, char *argv[])
118 xbt_assert(argc==3,"Strange number of arguments expected 2 got %d", argc - 1);
120 sscanf(argv[1], "%lf", &first_sleep);
121 sscanf(argv[2], "%lf", &sleep_time);
123 XBT_DEBUG ("Timer started");
126 MSG_process_sleep(first_sleep);
130 XBT_DEBUG ("Get sleep");
131 MSG_process_sleep(sleep_time);
132 } while(timer_start);
134 XBT_DEBUG ("Finished");
138 /** Receiver function */
139 int slave(int argc, char *argv[])
142 msg_task_t task = NULL;
147 xbt_assert(argc==2,"Strange number of arguments expected 1 got %d", argc - 1);
149 XBT_DEBUG ("Slave started");
152 sprintf(id_alias, "%d", id);
154 a = MSG_task_receive(&(task), id_alias);
157 if(count_finished == 0){
164 XBT_INFO("Hey?! What's up?");
165 xbt_die("Unexpected behavior.");
168 elapsed_time = MSG_get_clock() - start_time;
170 XBT_INFO("FLOW[%d] : Receive %.0f bytes from %s to %s",
172 MSG_task_get_data_size(task),
175 // MSG_task_execute(task);
176 MSG_task_destroy(task);
178 XBT_DEBUG ("Finished");
183 msg_error_t test_all(const char *platform_file,
184 const char *application_file)
186 msg_error_t res = MSG_OK;
188 /* MSG_config("workstation/model", "GTNETS"); */
189 /* MSG_config("workstation/model","KCCFLN05"); */
190 { /* Simulation setting */
191 MSG_create_environment(platform_file);
194 TRACE_declare_mark("endmark");
196 { /* Application deployment */
197 MSG_function_register("master", master);
198 MSG_function_register("slave", slave);
199 MSG_function_register("timer", timer);
201 MSG_launch_application(application_file);
205 } /* end_of_test_all */
208 int main(int argc, char *argv[])
210 msg_error_t res = MSG_OK;
213 MSG_init(&argc, argv);
215 printf("Usage: %s platform_file deployment_file\n", argv[0]);
219 res = test_all(argv[1], argv[2]);