Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface cleanup
[simgrid.git] / examples / msg / tracing / ms.c
1 /* Copyright (c) 2010-2014. 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
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)
20  */
21
22 #include <stdio.h>
23 #include "simgrid/msg.h"
24 #include "xbt/sysdep.h"         /* calloc, printf */
25
26 /* Create a log channel to have nice outputs. */
27 #include "xbt/log.h"
28 #include "xbt/asserts.h"
29 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
30                              "Messages specific for this msg example");
31
32 int master(int argc, char *argv[]);
33 int slave(int argc, char *argv[]);
34
35 /** Emitter function  */
36 int master(int argc, char *argv[])
37 {
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]);
42
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);
45
46   TRACE_mark("msmark", "start_send_tasks");
47   int i;
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);
51
52     //setting the variable "task_creation" to value i
53     TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
54
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");
59   }
60   TRACE_mark("msmark", "finish_send_tasks");
61
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");
66   }
67
68   return 0;
69 }
70
71 /** Receiver function  */
72 int slave(int argc, char *argv[])
73 {
74   msg_task_t task = NULL;
75
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()),
78                           "task_computation",
79                           0);
80   while (1) {
81     MSG_task_receive(&(task), "master_mailbox");
82
83     if (!strcmp(MSG_task_get_name(task), "finalize")) {
84       MSG_task_destroy(task);
85       break;
86     }
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()),
90                             "task_computation",
91                             MSG_task_get_flops_amount(task));
92     MSG_task_execute(task);
93     MSG_task_destroy(task);
94     task = NULL;
95   }
96   return 0;
97 }
98
99 /** Main function */
100 int main(int argc, char *argv[])
101 {
102   MSG_init(&argc, argv);
103   if (argc < 3) {
104     printf("Usage: %s platform_file deployment_file\n", argv[0]);
105     exit(1);
106   }
107
108   char *platform_file = argv[1];
109   char *deployment_file = argv[2];
110   MSG_create_environment(platform_file);
111
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");
117
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");
122
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);
130
131   MSG_function_register("master", master);
132   MSG_function_register("slave", slave);
133   MSG_launch_application(deployment_file);
134
135   MSG_main();
136
137   unsigned int cursor;
138   xbt_dynar_t categories = TRACE_get_categories ();
139   if (categories){
140     XBT_INFO ("Declared tracing categories:");
141     char *category;
142     xbt_dynar_foreach (categories, cursor, category){
143       XBT_INFO ("%s", category);
144     }
145     xbt_dynar_free (&categories);
146   }
147
148   xbt_dynar_t marks = TRACE_get_marks ();
149   if (marks){
150     XBT_INFO ("Declared marks:");
151     char *mark;
152     xbt_dynar_foreach (marks, cursor, mark){
153       XBT_INFO ("%s", mark);
154     }
155     xbt_dynar_free (&marks);
156   }
157
158   return 0;
159 }                               /* end_of_main */