1 /* Copyright (c) 2010. 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
10 * tasks, send them to the slaves. For each task received, the slave executes
11 * it and then destroys it. This program uses several tracing functions that
12 * enable the tracing of categorized resource utilization, the use of trace marks,
13 * and user variables associated to the hosts of the platform file.
14 * You might want to run this program with the following parameters:
15 * --cfg=tracing/categorized:1
16 * --cfg=tracing/uncategorized:1
17 * --cfg=triva/categorized:triva_cat.plist
18 * --cfg=triva/uncategorized:triva_uncat.plist
19 * (See \ref tracing_tracing_options for details)
24 #include "xbt/sysdep.h" /* calloc, printf */
26 /* Create a log channel to have nice outputs. */
28 #include "xbt/asserts.h"
29 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
30 "Messages specific for this msg example");
32 int master(int argc, char *argv[]);
33 int slave(int argc, char *argv[]);
35 /** Emitter function */
36 int master(int argc, char *argv[])
38 long number_of_tasks = atol(argv[1]);
39 double task_comp_size = atof(argv[2]);
40 double task_comm_size = atof(argv[3]);
41 long slaves_count = atol(argv[4]);
43 //setting the variable "is_master" (previously declared) to value 1
44 TRACE_host_variable_set(MSG_host_self()->name, "is_master", 1);
46 TRACE_mark("msmark", "start_send_tasks");
48 for (i = 0; i < number_of_tasks; i++) {
50 task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
52 //setting the variable "task_creation" to value i
53 TRACE_host_variable_set(MSG_host_self()->name, "task_creation", i);
55 //setting the category of task to "compute"
56 //the category of a task must be defined before it is sent or executed
57 MSG_task_set_category(task, "compute");
58 MSG_task_send(task, "master_mailbox");
60 TRACE_mark("msmark", "finish_send_tasks");
62 for (i = 0; i < slaves_count; i++) {
63 m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
64 MSG_task_set_category(finalize, "finalize");
65 MSG_task_send(finalize, "master_mailbox");
71 /** Receiver function */
72 int slave(int argc, char *argv[])
76 TRACE_host_variable_set(MSG_host_self()->name, "is_slave", 1);
78 MSG_task_receive(&(task), "master_mailbox");
80 if (!strcmp(MSG_task_get_name(task), "finalize")) {
81 MSG_task_destroy(task);
84 //adding the value returned by MSG_task_get_compute_duration(task)
85 //to the variable "task_computation"
86 TRACE_host_variable_add(MSG_host_self()->name,
88 MSG_task_get_compute_duration(task));
89 MSG_task_execute(task);
90 MSG_task_destroy(task);
97 int main(int argc, char *argv[])
99 MSG_global_init(&argc, argv);
101 printf("Usage: %s platform_file deployment_file\n", argv[0]);
105 char *platform_file = argv[1];
106 char *deployment_file = argv[2];
107 MSG_create_environment(platform_file);
109 //declaring user variables
110 TRACE_host_variable_declare("is_slave");
111 TRACE_host_variable_declare("is_master");
112 TRACE_host_variable_declare("task_creation");
113 TRACE_host_variable_declare("task_computation");
115 //declaring user markers
116 TRACE_declare_mark("msmark");
118 //declaring user categories with RGB colors (values from 0 to 1)
119 TRACE_category_with_color ("compute", "1 0 0"); //compute is red
120 TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
121 //categories without user-defined colors receive
122 //random colors generated by the tracing system
123 TRACE_category ("request");
124 TRACE_category_with_color ("report", NULL);
126 MSG_function_register("master", master);
127 MSG_function_register("slave", slave);
128 MSG_launch_application(deployment_file);
132 XBT_INFO ("Declared tracing categories:");
133 xbt_dynar_t categories = TRACE_get_categories ();
136 xbt_dynar_foreach (categories, cursor, category){
137 XBT_INFO ("%s", category);
139 xbt_dynar_free (&categories);
141 XBT_INFO ("Declared marks:");
142 xbt_dynar_t marks = TRACE_get_marks ();
144 xbt_dynar_foreach (marks, cursor, mark){
145 XBT_INFO ("%s", mark);
147 xbt_dynar_free (&marks);