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[])
31 char *slavename = NULL;
32 double task_comm_size = 0;
35 //unique id to control statistics
38 xbt_assert(argc == 4, "Strange number of arguments expected 3 got %d", argc - 1);
42 read = sscanf(argv[1], "%lg", &task_comm_size);
43 xbt_assert(read, "Invalid argument %s\n", argv[1]);
48 sprintf(id_alias, "flow_%d", id);
49 slavenames[id] = slavename;
50 TRACE_category(id_alias);
52 masternames[id] = MSG_host_get_name(MSG_host_self());
54 { /* Task creation. */
55 char sprintf_buffer[64] = "Task_0";
56 todo = MSG_task_create(sprintf_buffer, 0, task_comm_size, NULL);
57 MSG_task_set_category(todo, id_alias);
58 //keep track of running tasks
59 gl_task_array[id] = todo;
60 gl_data_size[id] = task_comm_size;
65 /* time measurement */
66 sprintf(id_alias, "%d", id);
67 start_time = MSG_get_clock();
68 MSG_task_send(todo, id_alias);
69 end_time = MSG_get_clock();
74 static int slave(int argc, char *argv[])
76 msg_task_t task = NULL;
80 xbt_assert(argc == 2, "Strange number of arguments expected 1 got %d", argc - 1);
82 int id = atoi(argv[1]);
83 sprintf(id_alias, "%d", id);
86 int a = MSG_task_receive(&(task), id_alias);
89 if(count_finished == 0){
93 xbt_assert(a == MSG_OK,"Hey?! What's up? Unexpected behavior");
95 elapsed_time = MSG_get_clock() - start_time;
100 for (id = 0; id < NTASKS; id++) {
101 if (gl_task_array[id] == NULL) continue;
102 if (gl_task_array[id] == task) {
103 XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ; message from %s to %s with remaining : %f",
104 id, gl_data_size[id] / elapsed_time, masternames[id], slavenames[id], 0.0);
105 MSG_task_destroy(gl_task_array[id]);
106 gl_task_array[id]=NULL;
108 remaining = MSG_task_get_remaining_communication(gl_task_array[id]);
109 XBT_INFO ("===> Estimated Bw of FLOW[%d] : %f ; message from %s to %s with remaining : %f",
110 id, (gl_data_size[id] - remaining) / elapsed_time, masternames[id], slavenames[id], remaining);
112 MSG_task_destroy(gl_task_array[id]);
113 gl_task_array[id]=NULL;
120 snprintf(mark, 100, "flow_%d_finished", trace_id);
121 TRACE_mark("endmark", mark);
123 if(bool_printed==2 && gl_task_array[trace_id]) MSG_task_destroy(gl_task_array[trace_id]);
128 int main(int argc, char *argv[])
130 msg_error_t res = MSG_OK;
133 MSG_init(&argc, argv);
134 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
135 "\tExample: %s platform.xml deployment.xml\n", argv[0], argv[0]);
137 MSG_create_environment(argv[1]);
139 TRACE_declare_mark("endmark");
141 MSG_function_register("master", master);
142 MSG_function_register("slave", slave);
143 MSG_launch_application(argv[2]);
147 return res != MSG_OK;