Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / trace-masterworker / trace-masterworker.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /** @addtogroup MSG_examples
8  * 
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>.
15  */
16
17 #include "simgrid/msg.h"
18
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_trace_masterworker, "Messages specific for this msg example");
20
21 static int master(int argc, char *argv[])
22 {
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");
27
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);
30
31   TRACE_mark("msmark", "start_send_tasks");
32   int i;
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);
36
37     //setting the variable "task_creation" to value i
38     TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
39
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");
44   }
45   TRACE_mark("msmark", "finish_send_tasks");
46
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");
51   }
52
53   return 0;
54 }
55
56 static int worker(int argc, char *argv[])
57 {
58   msg_task_t task = NULL;
59
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);
62   while (1) {
63     MSG_task_receive(&(task), "master_mailbox");
64
65     if (!strcmp(MSG_task_get_name(task), "finalize")) {
66       MSG_task_destroy(task);
67       break;
68     }
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);
74     task = NULL;
75   }
76   return 0;
77 }
78
79 int main(int argc, char *argv[])
80 {
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]);
84
85   MSG_create_environment(argv[1]);
86
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");
92
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");
97
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);
104
105   MSG_function_register("master", master);
106   MSG_function_register("worker", worker);
107   MSG_launch_application(argv[2]);
108
109   MSG_main();
110
111   unsigned int cursor;
112   xbt_dynar_t categories = TRACE_get_categories ();
113   if (categories){
114     XBT_INFO ("Declared tracing categories:");
115     char *category;
116     xbt_dynar_foreach (categories, cursor, category){
117       XBT_INFO ("%s", category);
118     }
119     xbt_dynar_free (&categories);
120   }
121
122   xbt_dynar_t marks = TRACE_get_marks ();
123   if (marks){
124     XBT_INFO ("Declared marks:");
125     char *mark;
126     xbt_dynar_foreach (marks, cursor, mark){
127       XBT_INFO ("%s", mark);
128     }
129     xbt_dynar_free (&marks);
130   }
131
132   return 0;
133 }