Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
df58acd05df76805bf5326472c47dc7c96f145b7
[simgrid.git] / examples / msg / tracing / ms.c
1 /* Copyright (c) 2010. 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> TODO
10  */
11
12 #include <stdio.h>
13 #include "msg/msg.h"
14 #include "xbt/sysdep.h"         /* calloc, printf */
15
16 /* Create a log channel to have nice outputs. */
17 #include "xbt/log.h"
18 #include "xbt/asserts.h"
19 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
20                              "Messages specific for this msg example");
21
22 int master(int argc, char *argv[]);
23 int slave(int argc, char *argv[]);
24
25 /** Emitter function  */
26 int master(int argc, char *argv[])
27 {
28   long number_of_tasks = atol(argv[1]);
29   double task_comp_size = atof(argv[2]);
30   double task_comm_size = atof(argv[3]);
31   long slaves_count = atol(argv[4]);
32
33   //setting the variable "is_master" (previously declared) to value 1
34   TRACE_host_variable_set(MSG_host_self()->name, "is_master", 1);
35
36   TRACE_mark("msmark", "start_send_tasks");
37   int i;
38   for (i = 0; i < number_of_tasks; i++) {
39     m_task_t task = NULL;
40     task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
41
42     //setting the variable "task_creation" to value i
43     TRACE_host_variable_set(MSG_host_self()->name, "task_creation", i);
44
45     //setting the category of task to "compute"
46     //the category of a task must be defined before it is sent or executed
47     TRACE_msg_set_task_category(task, "compute");
48     MSG_task_send(task, "master_mailbox");
49   }
50   TRACE_mark("msmark", "finish_send_tasks");
51
52   for (i = 0; i < slaves_count; i++) {
53     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
54     TRACE_msg_set_task_category(finalize, "finalize");
55     MSG_task_send(finalize, "master_mailbox");
56   }
57
58   return 0;
59 }
60
61 /** Receiver function  */
62 int slave(int argc, char *argv[])
63 {
64   m_task_t task = NULL;
65
66   TRACE_host_variable_set(MSG_host_self()->name, "is_slave", 1);
67   while (1) {
68     MSG_task_receive(&(task), "master_mailbox");
69
70     if (!strcmp(MSG_task_get_name(task), "finalize")) {
71       MSG_task_destroy(task);
72       break;
73     }
74     //adding the value returned by MSG_task_get_compute_duration(task)
75     //to the variable "task_computation"
76     TRACE_host_variable_add(MSG_host_self()->name,
77                             "task_computation",
78                             MSG_task_get_compute_duration(task));
79     MSG_task_execute(task);
80     MSG_task_destroy(task);
81     task = NULL;
82   }
83   return 0;
84 }
85
86 /** Main function */
87 int main(int argc, char *argv[])
88 {
89   MSG_global_init(&argc, argv);
90   if (argc < 3) {
91     printf("Usage: %s platform_file deployment_file\n", argv[0]);
92     exit(1);
93   }
94
95   char *platform_file = argv[1];
96   char *deployment_file = argv[2];
97   MSG_create_environment(platform_file);
98
99   //declaring user variables
100   TRACE_host_variable_declare("is_slave");
101   TRACE_host_variable_declare("is_master");
102   TRACE_host_variable_declare("task_creation");
103   TRACE_host_variable_declare("task_computation");
104
105   //declaring user markers
106   TRACE_declare_mark("msmark");
107
108   //declaring user categories with RGB colors (values from 0 to 1)
109   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
110   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
111   //categories without user-defined colors receive
112   //random colors generated by the tracing system
113   TRACE_category ("request");
114   TRACE_category_with_color ("report", NULL);
115
116   MSG_function_register("master", master);
117   MSG_function_register("slave", slave);
118   MSG_launch_application(deployment_file);
119
120   MSG_main();
121   MSG_clean();
122   return 0;
123 }                               /* end_of_main */