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>Master/Worker: trace-masterworker/trace-masterworker.c</b> This is an augmented version of our basic
10 * master/worker example. It uses several tracing functions that enable the tracing of categorized resource
11 * utilization, the use of trace marks, and user variables associated to the hosts of the platform file. You might
12 * want to run this program with the following configuration options:
13 * <i>--cfg=tracing/categorized:yes</i>, <i>--cfg=tracing/uncategorized:yes</i>,
14 * <i>--cfg=viva/categorized:viva_cat.plist</i>, and <i>--cfg=viva/uncategorized:viva_uncat.plist</i>.
17 #include "simgrid/msg.h"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_trace_masterworker, "Messages specific for this msg example");
21 static int master(int argc, char *argv[])
23 long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
24 double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
25 double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
26 long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
28 //setting the variable "is_master" (previously declared) to value 1
29 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_master", 1);
31 TRACE_mark("msmark", "start_send_tasks");
33 for (i = 0; i < number_of_tasks; i++) {
34 msg_task_t task = NULL;
35 task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
37 //setting the variable "task_creation" to value i
38 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
40 //setting the category of task to "compute"
41 //the category of a task must be defined before it is sent or executed
42 MSG_task_set_category(task, "compute");
43 MSG_task_send(task, "master_mailbox");
45 TRACE_mark("msmark", "finish_send_tasks");
47 for (i = 0; i < workers_count; i++) {
48 msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
49 MSG_task_set_category(finalize, "finalize");
50 MSG_task_send(finalize, "master_mailbox");
56 static int worker(int argc, char *argv[])
58 msg_task_t task = NULL;
60 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_worker", 1);
61 TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_computation", 0);
63 MSG_task_receive(&(task), "master_mailbox");
65 if (!strcmp(MSG_task_get_name(task), "finalize")) {
66 MSG_task_destroy(task);
69 //adding the value returned by MSG_task_get_compute_duration(task)
70 //to the variable "task_computation"
71 TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()), "task_computation", MSG_task_get_flops_amount(task));
72 MSG_task_execute(task);
73 MSG_task_destroy(task);
79 int main(int argc, char *argv[])
81 MSG_init(&argc, argv);
82 xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
83 "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
85 MSG_create_environment(argv[1]);
87 //declaring user variables
88 TRACE_host_variable_declare("is_worker");
89 TRACE_host_variable_declare("is_master");
90 TRACE_host_variable_declare("task_creation");
91 TRACE_host_variable_declare("task_computation");
93 //declaring user markers and values
94 TRACE_declare_mark("msmark");
95 TRACE_declare_mark_value ("msmark", "start_send_tasks");
96 TRACE_declare_mark_value ("msmark", "finish_send_tasks");
98 //declaring user categories with RGB colors (values from 0 to 1)
99 TRACE_category_with_color ("compute", "1 0 0"); //compute is red
100 TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
101 //categories without user-defined colors receive random colors generated by the tracing system
102 TRACE_category ("request");
103 TRACE_category_with_color ("report", NULL);
105 MSG_function_register("master", master);
106 MSG_function_register("worker", worker);
107 MSG_launch_application(argv[2]);
112 xbt_dynar_t categories = TRACE_get_categories ();
114 XBT_INFO ("Declared tracing categories:");
116 xbt_dynar_foreach (categories, cursor, category){
117 XBT_INFO ("%s", category);
119 xbt_dynar_free (&categories);
122 xbt_dynar_t marks = TRACE_get_marks ();
124 XBT_INFO ("Declared marks:");
126 xbt_dynar_foreach (marks, cursor, mark){
127 XBT_INFO ("%s", mark);
129 xbt_dynar_free (&marks);