Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Set tracing location for binaries with cmake command out of project directory.
[simgrid.git] / examples / msg / tracing / ms.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include <stdio.h>
9 #include "msg/msg.h"
10 #include "xbt/sysdep.h" /* calloc, printf */
11
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 /** Emitter function  */
22 int master(int argc, char *argv[])
23 {
24   long number_of_tasks = atol(argv[1]);
25   double task_comp_size = atof(argv[2]);
26   double task_comm_size = atof(argv[3]);
27   long slaves_count = atol(argv[4]);
28
29   //setting the variable "is_master" (previously declared) to value 1
30   TRACE_host_variable_set ("is_master", 1);
31
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 ("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   
46    for (i = 0; i < slaves_count; i++) { 
47      m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
48      TRACE_msg_set_task_category(finalize, "finalize");
49      MSG_task_send(finalize, "master_mailbox");
50    } 
51   
52   return 0;
53 }
54
55 /** Receiver function  */
56 int slave(int argc, char *argv[])
57 {
58   m_task_t task = NULL;
59   int res;
60
61   TRACE_host_variable_set ("is_slave", 1);
62   while(1) {
63     res = 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
70     //adding the value returned by MSG_task_get_compute_duration(task)
71     //to the variable "task_computation"
72     TRACE_host_variable_add ("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 /** Test function */
82 MSG_error_t test_all(const char *platform_file,
83                             const char *application_file)
84 {
85   MSG_error_t res = MSG_OK;
86
87   {                             /*  Simulation setting */
88     MSG_set_channel_number(0);
89     MSG_create_environment(platform_file);
90   }
91   {                            /*   Application deployment */
92     MSG_function_register("master", master);
93     MSG_function_register("slave", slave);
94     MSG_launch_application(application_file);
95   }
96   res = MSG_main();
97   
98   INFO1("Simulation time %g",MSG_get_clock());
99   return res;
100 }
101
102
103 /** Main function */
104 int main(int argc, char *argv[])
105 {
106   MSG_error_t res = MSG_OK;
107   int i;
108
109   //starting the simulation tracing
110   TRACE_start ("simulation.trace");
111
112   //declaring user variables
113   TRACE_host_variable_declare ("is_slave");
114   TRACE_host_variable_declare ("is_master");
115   TRACE_host_variable_declare ("task_creation");
116   TRACE_host_variable_declare ("task_computation");
117
118   //declaring user categories
119   TRACE_category ("compute");
120   TRACE_category ("finalize");
121
122   MSG_global_init(&argc,argv);
123   if (argc < 3) {
124      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
125      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
126      exit(1);
127   }
128   res = test_all(argv[1],argv[2]);
129   MSG_clean();
130
131   //ending the simulation tracing
132   TRACE_end();
133
134   if(res==MSG_OK)
135     return 0;
136   else
137     return 1;
138 } /* end_of_main */