1 /* Copyright (c) 2010-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 /** @addtogroup MSG_examples
9 * - <b>tracing/ms.c</b> This is a master/slave program where the master creates tasks, send them to the slaves. For
10 * each task received, the slave executes it and then destroys it. This program uses several tracing functions that
11 * enable the tracing of categorized resource utilization, the use of trace marks, and user variables associated to the
12 * hosts of the platform file. You might want to run this program with the following parameters:
13 * --cfg=tracing/categorized:yes
14 * --cfg=tracing/uncategorized:yes
15 * --cfg=viva/categorized:viva_cat.plist
16 * --cfg=viva/uncategorized:viva_uncat.plist
17 * (See \ref tracing_tracing_options for details)
20 #include "simgrid/msg.h"
22 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
24 static int master(int argc, char *argv[])
26 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
27 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
28 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
29 long slaves_count = xbt_str_parse_int(argv[4], "Invalid amount of slaves: %s");
31 //setting the variable "is_master" (previously declared) to value 1
32 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_master", 1);
34 TRACE_mark("msmark", "start_send_tasks");
36 for (i = 0; i < number_of_tasks; i++) {
37 msg_task_t task = NULL;
38 task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
40 //setting the variable "task_creation" to value i
41 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
43 //setting the category of task to "compute"
44 //the category of a task must be defined before it is sent or executed
45 MSG_task_set_category(task, "compute");
46 MSG_task_send(task, "master_mailbox");
48 TRACE_mark("msmark", "finish_send_tasks");
50 for (i = 0; i < slaves_count; i++) {
51 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
52 MSG_task_set_category(finalize, "finalize");
53 MSG_task_send(finalize, "master_mailbox");
59 static int slave(int argc, char *argv[])
61 msg_task_t task = NULL;
63 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_slave", 1);
64 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_computation", 0);
66 MSG_task_receive(&(task), "master_mailbox");
68 if (!strcmp(MSG_task_get_name(task), "finalize")) {
69 MSG_task_destroy(task);
72 //adding the value returned by MSG_task_get_compute_duration(task)
73 //to the variable "task_computation"
74 TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()), "task_computation", MSG_task_get_flops_amount(task));
75 MSG_task_execute(task);
76 MSG_task_destroy(task);
82 int main(int argc, char *argv[])
84 MSG_init(&argc, argv);
86 printf("Usage: %s platform_file deployment_file\n", argv[0]);
90 MSG_create_environment(argv[1]);
92 //declaring user variables
93 TRACE_host_variable_declare("is_slave");
94 TRACE_host_variable_declare("is_master");
95 TRACE_host_variable_declare("task_creation");
96 TRACE_host_variable_declare("task_computation");
98 //declaring user markers and values
99 TRACE_declare_mark("msmark");
100 TRACE_declare_mark_value ("msmark", "start_send_tasks");
101 TRACE_declare_mark_value ("msmark", "finish_send_tasks");
103 //declaring user categories with RGB colors (values from 0 to 1)
104 TRACE_category_with_color ("compute", "1 0 0"); //compute is red
105 TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
106 //categories without user-defined colors receive random colors generated by the tracing system
107 TRACE_category ("request");
108 TRACE_category_with_color ("report", NULL);
110 MSG_function_register("master", master);
111 MSG_function_register("slave", slave);
112 MSG_launch_application(argv[2]);
117 xbt_dynar_t categories = TRACE_get_categories ();
119 XBT_INFO ("Declared tracing categories:");
121 xbt_dynar_foreach (categories, cursor, category){
122 XBT_INFO ("%s", category);
124 xbt_dynar_free (&categories);
127 xbt_dynar_t marks = TRACE_get_marks ();
129 XBT_INFO ("Declared marks:");
131 xbt_dynar_foreach (marks, cursor, mark){
132 XBT_INFO ("%s", mark);
134 xbt_dynar_free (&marks);