Logo AND Algorithmique Numérique Distribuée

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