Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rework the presentation of MSG tracing examples
[simgrid.git] / examples / msg / trace-masterworker / trace-masterworker.c
1 /* Copyright (c) 2010-2016. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/msg.h"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_trace_masterworker, "Messages specific for this msg example");
9
10 static int master(int argc, char *argv[])
11 {
12   long number_of_tasks = xbt_str_parse_int(argv[1], "Invalid amount of tasks: %s");
13   double task_comp_size = xbt_str_parse_double(argv[2], "Invalid computational size: %s");
14   double task_comm_size = xbt_str_parse_double(argv[3], "Invalid communication size: %s");
15   long workers_count = xbt_str_parse_int(argv[4], "Invalid amount of workers: %s");
16
17   //setting the variable "is_master" (previously declared) to value 1
18   TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_master", 1);
19
20   TRACE_mark("msmark", "start_send_tasks");
21   for (int i = 0; i < number_of_tasks; i++) {
22     msg_task_t task = NULL;
23     task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
24
25     //setting the variable "task_creation" to value i
26     TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
27
28     //setting the category of task to "compute"
29     //the category of a task must be defined before it is sent or executed
30     MSG_task_set_category(task, "compute");
31     MSG_task_send(task, "master_mailbox");
32   }
33   TRACE_mark("msmark", "finish_send_tasks");
34
35   for (int i = 0; i < workers_count; i++) {
36     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
37     MSG_task_set_category(finalize, "finalize");
38     MSG_task_send(finalize, "master_mailbox");
39   }
40
41   return 0;
42 }
43
44 static int worker(int argc, char *argv[])
45 {
46   msg_task_t task = NULL;
47
48   TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_worker", 1);
49   TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_computation", 0);
50   while (1) {
51     MSG_task_receive(&(task), "master_mailbox");
52
53     if (!strcmp(MSG_task_get_name(task), "finalize")) {
54       MSG_task_destroy(task);
55       break;
56     }
57     //adding the value returned by MSG_task_get_compute_duration(task)
58     //to the variable "task_computation"
59     TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()), "task_computation", MSG_task_get_flops_amount(task));
60     MSG_task_execute(task);
61     MSG_task_destroy(task);
62     task = NULL;
63   }
64   return 0;
65 }
66
67 int main(int argc, char *argv[])
68 {
69   MSG_init(&argc, argv);
70   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
71              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
72
73   MSG_create_environment(argv[1]);
74
75   //declaring user variables
76   TRACE_host_variable_declare("is_worker");
77   TRACE_host_variable_declare("is_master");
78   TRACE_host_variable_declare("task_creation");
79   TRACE_host_variable_declare("task_computation");
80
81   //declaring user markers and values
82   TRACE_declare_mark("msmark");
83   TRACE_declare_mark_value ("msmark", "start_send_tasks");
84   TRACE_declare_mark_value ("msmark", "finish_send_tasks");
85
86   //declaring user categories with RGB colors (values from 0 to 1)
87   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
88   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
89   //categories without user-defined colors receive random colors generated by the tracing system
90   TRACE_category ("request");
91   TRACE_category_with_color ("report", NULL);
92
93   MSG_function_register("master", master);
94   MSG_function_register("worker", worker);
95   MSG_launch_application(argv[2]);
96
97   MSG_main();
98
99   unsigned int cursor;
100   xbt_dynar_t categories = TRACE_get_categories ();
101   if (categories){
102     XBT_INFO ("Declared tracing categories:");
103     char *category;
104     xbt_dynar_foreach (categories, cursor, category){
105       XBT_INFO ("%s", category);
106     }
107     xbt_dynar_free (&categories);
108   }
109
110   xbt_dynar_t marks = TRACE_get_marks ();
111   if (marks){
112     XBT_INFO ("Declared marks:");
113     char *mark;
114     xbt_dynar_foreach (marks, cursor, mark){
115       XBT_INFO ("%s", mark);
116     }
117     xbt_dynar_free (&marks);
118   }
119
120   return 0;
121 }