Logo AND Algorithmique Numérique Distribuée

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