Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[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,"Messages specific for this msg example");
15
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 MSG_error_t test_all(const char *platform_file, const char *application_file);
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 ("is_master", 1);
30
31   int i;
32   for (i = 0; i < number_of_tasks; i++) {
33     m_task_t task=NULL;
34     task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
35
36     //setting the variable "task_creation" to value i
37     TRACE_host_variable_set ("task_creation", i);
38
39     //setting the category of task to "compute"
40     //the category of a task must be defined before it is sent or executed
41     TRACE_msg_set_task_category (task, "compute");
42     MSG_task_send(task, "master_mailbox");
43   }
44   
45    for (i = 0; i < slaves_count; i++) { 
46      m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
47      TRACE_msg_set_task_category(finalize, "finalize");
48      MSG_task_send(finalize, "master_mailbox");
49    } 
50   
51   return 0;
52 }
53
54 /** Receiver function  */
55 int slave(int argc, char *argv[])
56 {
57   m_task_t task = NULL;
58   int res;
59
60   TRACE_host_variable_set ("is_slave", 1);
61   while(1) {
62     res = MSG_task_receive(&(task), "master_mailbox");
63
64     if (!strcmp(MSG_task_get_name(task),"finalize")) {
65       MSG_task_destroy(task);
66         break;
67     }
68
69     //adding the value returned by MSG_task_get_compute_duration(task)
70     //to the variable "task_computation"
71     TRACE_host_variable_add ("task_computation",
72         MSG_task_get_compute_duration(task));
73     MSG_task_execute(task);
74     MSG_task_destroy(task);
75     task = NULL;
76   }
77   return 0;
78 }
79
80 /** Test function */
81 MSG_error_t test_all(const char *platform_file,
82                             const char *application_file)
83 {
84   MSG_error_t res = MSG_OK;
85
86   {                             /*  Simulation setting */
87     MSG_set_channel_number(0);
88     MSG_create_environment(platform_file);
89   }
90   {                            /*   Application deployment */
91     MSG_function_register("master", master);
92     MSG_function_register("slave", slave);
93     MSG_launch_application(application_file);
94   }
95   res = MSG_main();
96   
97   INFO1("Simulation time %g",MSG_get_clock());
98   return res;
99 }
100
101
102 /** Main function */
103 int main(int argc, char *argv[])
104 {
105   MSG_error_t res = MSG_OK;
106
107   //starting the simulation tracing
108   TRACE_start ("ms.trace");
109
110   //declaring user variables
111   TRACE_host_variable_declare ("is_slave");
112   TRACE_host_variable_declare ("is_master");
113   TRACE_host_variable_declare ("task_creation");
114   TRACE_host_variable_declare ("task_computation");
115
116   //declaring user categories
117   TRACE_category ("compute");
118   TRACE_category ("finalize");
119
120   MSG_global_init(&argc,argv);
121   if (argc < 3) {
122      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
123      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
124      exit(1);
125   }
126   res = test_all(argv[1],argv[2]);
127   MSG_clean();
128
129   //ending the simulation tracing
130   TRACE_end();
131
132   if(res==MSG_OK)
133     return 0;
134   else
135     return 1;
136 } /* end_of_main */