Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
removing tracing functions from examples that are not in msg/tracing directory
[simgrid.git] / examples / msg / tracing / tasks.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   INFO4 ("master %ld %f %f %ld", number_of_tasks, task_comp_size, task_comm_size, slaves_count);
29
30   int i;
31   for (i = 0; i < number_of_tasks; i++) {
32     m_task_t task=NULL;
33     task = MSG_task_create("task", task_comp_size, task_comm_size, NULL);
34
35     //setting the category of task to "compute"
36     //the category of a task must be defined before it is sent or executed
37     TRACE_msg_set_task_category (task, "compute");
38     MSG_task_send(task, "master_mailbox");
39   }
40   
41    for (i = 0; i < slaves_count; i++) { 
42      m_task_t finalize = MSG_task_create ("finalize", 0, 0, 0);
43      TRACE_msg_set_task_category(finalize, "finalize");
44      MSG_task_send(finalize, "master_mailbox");
45    } 
46   
47   return 0;
48 }
49
50 /** Receiver function  */
51 int slave(int argc, char *argv[])
52 {
53   m_task_t task = NULL;
54   int res;
55
56   while(1) {
57     res = MSG_task_receive(&(task), "master_mailbox");
58     if (res != MSG_OK){
59       INFO0("error");
60       break;
61     }
62
63     if (!strcmp(MSG_task_get_name(task),"finalize")) {
64       MSG_task_destroy(task);
65         break;
66     }
67
68     INFO1("Executing task %f", MSG_task_get_compute_duration(task));
69     MSG_task_execute(task);
70     INFO0("End of execution");
71     MSG_task_destroy(task);
72     task = NULL;
73   }
74   return 0;
75 }
76
77 /** Test function */
78 MSG_error_t test_all(const char *platform_file,
79                             const char *application_file)
80 {
81   MSG_error_t res = MSG_OK;
82
83   {                             /*  Simulation setting */
84     MSG_set_channel_number(0);
85     MSG_create_environment(platform_file);
86   }
87   {                            /*   Application deployment */
88     MSG_function_register("master", master);
89     MSG_function_register("slave", slave);
90     MSG_launch_application(application_file);
91   }
92   res = MSG_main();
93   
94   INFO1("Simulation time %g",MSG_get_clock());
95   return res;
96 }
97
98
99 /** Main function */
100 int main(int argc, char *argv[])
101 {
102   MSG_error_t res = MSG_OK;
103
104   //starting the simulation tracing
105   TRACE_start_with_mask ("tasks.trace", TRACE_TASK);
106
107   //declaring user categories
108   TRACE_category ("compute");
109   TRACE_category ("finalize");
110
111   MSG_global_init(&argc,argv);
112   if (argc < 3) {
113      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
114      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
115      exit(1);
116   }
117   res = test_all(argv[1],argv[2]);
118   MSG_clean();
119
120   //ending the simulation tracing
121   TRACE_end();
122
123   if(res==MSG_OK)
124     return 0;
125   else
126     return 1;
127 } /* end_of_main */