Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / tracing / ms.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>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)
18  */
19
20 #include "simgrid/msg.h"
21
22 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
23
24 static int master(int argc, char *argv[])
25 {
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");
30
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);
33
34   TRACE_mark("msmark", "start_send_tasks");
35   int i;
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);
39
40     //setting the variable "task_creation" to value i
41     TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
42
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");
47   }
48   TRACE_mark("msmark", "finish_send_tasks");
49
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");
54   }
55
56   return 0;
57 }
58
59 static int slave(int argc, char *argv[])
60 {
61   msg_task_t task = NULL;
62
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);
65   while (1) {
66     MSG_task_receive(&(task), "master_mailbox");
67
68     if (!strcmp(MSG_task_get_name(task), "finalize")) {
69       MSG_task_destroy(task);
70       break;
71     }
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);
77     task = NULL;
78   }
79   return 0;
80 }
81
82 int main(int argc, char *argv[])
83 {
84   MSG_init(&argc, argv);
85   if (argc < 3) {
86     printf("Usage: %s platform_file deployment_file\n", argv[0]);
87     exit(1);
88   }
89
90   MSG_create_environment(argv[1]);
91
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");
97
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");
102
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);
109
110   MSG_function_register("master", master);
111   MSG_function_register("slave", slave);
112   MSG_launch_application(argv[2]);
113
114   MSG_main();
115
116   unsigned int cursor;
117   xbt_dynar_t categories = TRACE_get_categories ();
118   if (categories){
119     XBT_INFO ("Declared tracing categories:");
120     char *category;
121     xbt_dynar_foreach (categories, cursor, category){
122       XBT_INFO ("%s", category);
123     }
124     xbt_dynar_free (&categories);
125   }
126
127   xbt_dynar_t marks = TRACE_get_marks ();
128   if (marks){
129     XBT_INFO ("Declared marks:");
130     char *mark;
131     xbt_dynar_foreach (marks, cursor, mark){
132       XBT_INFO ("%s", mark);
133     }
134     xbt_dynar_free (&marks);
135   }
136
137   return 0;
138 }