Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
af7a2223a61e079e0079964fbb82f44fff58d166
[simgrid.git] / examples / msg / trace-masterworker / trace-masterworker.c
1 /* Copyright (c) 2010-2019. 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 = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
23
24     //setting the variable "task_creation" to value i
25     TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_creation", i);
26
27     //setting the category of task to "compute"
28     //the category of a task must be defined before it is sent or executed
29     MSG_task_set_category(task, "compute");
30     MSG_task_send(task, "master_mailbox");
31   }
32   TRACE_mark("msmark", "finish_send_tasks");
33
34   for (int i = 0; i < workers_count; i++) {
35     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
36     MSG_task_set_category(finalize, "finalize");
37     MSG_task_send(finalize, "master_mailbox");
38   }
39
40   return 0;
41 }
42
43 static int worker(int argc, char *argv[])
44 {
45   msg_task_t task = NULL;
46
47   TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "is_worker", 1);
48   TRACE_host_variable_set(MSG_host_get_name(MSG_host_self()), "task_computation", 0);
49   while (1) {
50     MSG_task_receive(&(task), "master_mailbox");
51
52     if (strcmp(MSG_task_get_name(task), "finalize") == 0) {
53       MSG_task_destroy(task);
54       break;
55     }
56     // adding the value returned by MSG_task_get_compute_duration(task) to the variable "task_computation"
57     TRACE_host_variable_add(MSG_host_get_name(MSG_host_self()), "task_computation", MSG_task_get_flops_amount(task));
58     MSG_task_execute(task);
59     MSG_task_destroy(task);
60     task = NULL;
61   }
62   return 0;
63 }
64
65 int main(int argc, char *argv[])
66 {
67   MSG_init(&argc, argv);
68   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
69              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
70
71   MSG_create_environment(argv[1]);
72
73   //declaring user variables
74   TRACE_host_variable_declare("is_worker");
75   TRACE_host_variable_declare("is_master");
76   TRACE_host_variable_declare("task_creation");
77   TRACE_host_variable_declare("task_computation");
78
79   //declaring user markers and values
80   TRACE_declare_mark("msmark");
81   TRACE_declare_mark_value ("msmark", "start_send_tasks");
82   TRACE_declare_mark_value ("msmark", "finish_send_tasks");
83
84   //declaring user categories with RGB colors (values from 0 to 1)
85   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
86   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
87   //categories without user-defined colors receive random colors generated by the tracing system
88   TRACE_category ("request");
89   TRACE_category_with_color ("report", NULL);
90
91   MSG_function_register("master", master);
92   MSG_function_register("worker", worker);
93   MSG_launch_application(argv[2]);
94
95   MSG_main();
96
97   unsigned int cursor;
98   xbt_dynar_t categories = TRACE_get_categories ();
99   if (categories){
100     XBT_INFO ("Declared tracing categories:");
101     char *category;
102     xbt_dynar_foreach (categories, cursor, category){
103       XBT_INFO ("%s", category);
104     }
105     xbt_dynar_free (&categories);
106   }
107
108   xbt_dynar_t marks = TRACE_get_marks ();
109   if (marks){
110     XBT_INFO ("Declared marks:");
111     char *mark;
112     xbt_dynar_foreach (marks, cursor, mark){
113       XBT_INFO ("%s", mark);
114     }
115     xbt_dynar_free (&marks);
116   }
117
118   return 0;
119 }