Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
end slavery in java world
[simgrid.git] / examples / msg / trace-masterslave / trace-masterslave.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   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
86              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
87
88   MSG_create_environment(argv[1]);
89
90   //declaring user variables
91   TRACE_host_variable_declare("is_slave");
92   TRACE_host_variable_declare("is_master");
93   TRACE_host_variable_declare("task_creation");
94   TRACE_host_variable_declare("task_computation");
95
96   //declaring user markers and values
97   TRACE_declare_mark("msmark");
98   TRACE_declare_mark_value ("msmark", "start_send_tasks");
99   TRACE_declare_mark_value ("msmark", "finish_send_tasks");
100
101   //declaring user categories with RGB colors (values from 0 to 1)
102   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
103   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
104   //categories without user-defined colors receive random colors generated by the tracing system
105   TRACE_category ("request");
106   TRACE_category_with_color ("report", NULL);
107
108   MSG_function_register("master", master);
109   MSG_function_register("slave", slave);
110   MSG_launch_application(argv[2]);
111
112   MSG_main();
113
114   unsigned int cursor;
115   xbt_dynar_t categories = TRACE_get_categories ();
116   if (categories){
117     XBT_INFO ("Declared tracing categories:");
118     char *category;
119     xbt_dynar_foreach (categories, cursor, category){
120       XBT_INFO ("%s", category);
121     }
122     xbt_dynar_free (&categories);
123   }
124
125   xbt_dynar_t marks = TRACE_get_marks ();
126   if (marks){
127     XBT_INFO ("Declared marks:");
128     char *mark;
129     xbt_dynar_foreach (marks, cursor, mark){
130       XBT_INFO ("%s", mark);
131     }
132     xbt_dynar_free (&marks);
133   }
134
135   return 0;
136 }