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
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:yes
16 * --cfg=tracing/uncategorized:yes
17 * --cfg=viva/categorized:viva_cat.plist
18 * --cfg=viva/uncategorized:viva_uncat.plist
19 * (See \ref tracing_tracing_options for details)
23 #include "simgrid/msg.h"
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_get_name(MSG_host_self()), "is_master", 1);
46 TRACE_mark("msmark", "start_send_tasks");
48 for (i = 0; i < number_of_tasks; i++) {
49 msg_task_t task = NULL;
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_get_name(MSG_host_self()), "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 msg_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[])
74 msg_task_t task = NULL;
76 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_slave", 1);
77 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()),
81 MSG_task_receive(&(task), "master_mailbox");
83 if (!strcmp(MSG_task_get_name(task), "finalize")) {
84 MSG_task_destroy(task);
87 //adding the value returned by MSG_task_get_compute_duration(task)
88 //to the variable "task_computation"
89 TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()),
91 MSG_task_get_flops_amount(task));
92 MSG_task_execute(task);
93 MSG_task_destroy(task);
100 int main(int argc, char *argv[])
102 MSG_init(&argc, argv);
104 printf("Usage: %s platform_file deployment_file\n", argv[0]);
108 char *platform_file = argv[1];
109 char *deployment_file = argv[2];
110 MSG_create_environment(platform_file);
112 //declaring user variables
113 TRACE_host_variable_declare("is_slave");
114 TRACE_host_variable_declare("is_master");
115 TRACE_host_variable_declare("task_creation");
116 TRACE_host_variable_declare("task_computation");
118 //declaring user markers and values
119 TRACE_declare_mark("msmark");
120 TRACE_declare_mark_value ("msmark", "start_send_tasks");
121 TRACE_declare_mark_value ("msmark", "finish_send_tasks");
123 //declaring user categories with RGB colors (values from 0 to 1)
124 TRACE_category_with_color ("compute", "1 0 0"); //compute is red
125 TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
126 //categories without user-defined colors receive
127 //random colors generated by the tracing system
128 TRACE_category ("request");
129 TRACE_category_with_color ("report", NULL);
131 MSG_function_register("master", master);
132 MSG_function_register("slave", slave);
133 MSG_launch_application(deployment_file);
138 xbt_dynar_t categories = TRACE_get_categories ();
140 XBT_INFO ("Declared tracing categories:");
142 xbt_dynar_foreach (categories, cursor, category){
143 XBT_INFO ("%s", category);
145 xbt_dynar_free (&categories);
148 xbt_dynar_t marks = TRACE_get_marks ();
150 XBT_INFO ("Declared marks:");
152 xbt_dynar_foreach (marks, cursor, mark){
153 XBT_INFO ("%s", mark);
155 xbt_dynar_free (&marks);