1 /* Copyright (c) 2007-2015. 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. */
7 #include "simgrid/msg.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
11 /** @addtogroup MSG_examples
13 * - <b>gtnets</b> Simple ping-pong using GTNeTs instead of the SimGrid network models.
18 //keep a pointer to all surf running tasks.
21 double start_time, end_time, elapsed_time;
22 double gl_data_size[NTASKS];
23 msg_task_t gl_task_array[NTASKS];
24 const char *slavenames[NTASKS];
25 const char *masternames[NTASKS];
26 int gl_task_array_id = 0;
27 int count_finished = 0;
29 static int master(int argc, char *argv[])
33 xbt_assert(argc == 4, "Strange number of arguments expected 3 got %d", argc - 1);
36 double task_comm_size = xbt_str_parse_double(argv[1], "Invalid task communication size: %s");
39 char *slavename = argv[2];
40 int id = xbt_str_parse_int(argv[3], "Invalid ID as argument 3: %s"); //unique id to control statistics
41 char *id_alias = bprintf("flow_%d", id);
42 slavenames[id] = slavename;
43 TRACE_category(id_alias);
45 masternames[id] = MSG_host_get_name(MSG_host_self());
47 { /* Task creation. */
48 todo = MSG_task_create("Task_0", 0, task_comm_size, NULL);
49 MSG_task_set_category(todo, id_alias);
50 //keep track of running tasks
51 gl_task_array[id] = todo;
52 gl_data_size[id] = task_comm_size;
57 /* time measurement */
58 sprintf(id_alias, "%d", id);
59 start_time = MSG_get_clock();
60 MSG_task_send(todo, id_alias);
61 end_time = MSG_get_clock();
67 static int slave(int argc, char *argv[])
69 msg_task_t task = NULL;
73 xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
75 int id = xbt_str_parse_int(argv[1], "Invalid id: %s");
76 sprintf(id_alias, "%d", id);
79 msg_error_t a = MSG_task_receive(&(task), id_alias);
82 if(count_finished == 0){
86 xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
88 elapsed_time = MSG_get_clock() - start_time;
93 for (id = 0; id < NTASKS; id++) {
94 if (gl_task_array[id] == NULL) continue;
95 if (gl_task_array[id] == task) {
96 XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ; message from %s to %s with remaining : %f",
97 id, gl_data_size[id] / elapsed_time, masternames[id], slavenames[id], 0.0);
98 MSG_task_destroy(gl_task_array[id]);
99 gl_task_array[id]=NULL;
101 remaining = MSG_task_get_remaining_communication(gl_task_array[id]);
102 XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ; message from %s to %s with remaining : %f",
103 id, (gl_data_size[id] - remaining) / elapsed_time, masternames[id], slavenames[id], remaining);
105 MSG_task_destroy(gl_task_array[id]);
106 gl_task_array[id]=NULL;
113 snprintf(mark, 100, "flow_%d_finished", trace_id);
114 TRACE_mark("endmark", mark);
116 if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
121 int main(int argc, char *argv[])
123 msg_error_t res = MSG_OK;
126 MSG_init(&argc, argv);
127 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
128 "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
130 MSG_create_environment(argv[1]);
131 TRACE_declare_mark("endmark");
133 MSG_function_register("master", master);
134 MSG_function_register("slave", slave);
135 MSG_launch_application(argv[2]);
139 return res != MSG_OK;