Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
merge back the master trunk into the smpi branch
[simgrid.git] / examples / msg / tracing / tasks.c
1 /* Copyright (c) 2002,2003,2004 Arnaud Legrand. All rights reserved.        */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <stdio.h>
7 #include "msg/msg.h"
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "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,
19                      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   XBT_INFO("master %ld %f %f %ld", number_of_tasks, task_comp_size,
29         task_comm_size, slaves_count);
30
31   int i;
32   for (i = 0; i < number_of_tasks; i++) {
33     char task_name[100];
34     snprintf (task_name, 100, "task-%d", i);
35     m_task_t task = NULL;
36     task = MSG_task_create(task_name, task_comp_size, task_comm_size, NULL);
37
38     //setting the category of task to "compute"
39     //the category of a task must be defined before it is sent or executed
40     TRACE_msg_set_task_category(task, "compute");
41     MSG_task_send(task, "master_mailbox");
42   }
43
44   for (i = 0; i < slaves_count; i++) {
45     char task_name[100];
46     snprintf (task_name, 100, "task-%d", i);
47     m_task_t finalize = MSG_task_create(task_name, 0, 0, xbt_strdup("finalize"));
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   while (1) {
62     res = MSG_task_receive(&(task), "master_mailbox");
63     if (res != MSG_OK) {
64       XBT_INFO("error");
65       break;
66     }
67
68     char *data = MSG_task_get_data(task);
69     if (data && !strcmp(data, "finalize")) {
70       MSG_task_destroy(task);
71       break;
72     }
73
74     XBT_INFO("Executing task %f", MSG_task_get_compute_duration(task));
75     MSG_task_execute(task);
76     XBT_INFO("End of execution");
77     MSG_task_destroy(task);
78     task = NULL;
79   }
80   return 0;
81 }
82
83 /** Test function */
84 MSG_error_t test_all(const char *platform_file,
85                      const char *application_file)
86 {
87   MSG_error_t res = MSG_OK;
88
89   {                             /*  Simulation setting */
90     MSG_set_channel_number(0);
91     MSG_create_environment(platform_file);
92   }
93   {
94     //declaring user categories
95     TRACE_category_with_color ("compute", "1 0 0");  //compute is red
96     TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
97   }
98   {                             /*   Application deployment */
99     MSG_function_register("master", master);
100     MSG_function_register("slave", slave);
101     MSG_launch_application(application_file);
102   }
103   res = MSG_main();
104
105   XBT_INFO("Simulation time %g", MSG_get_clock());
106   return res;
107 }
108
109
110 /** Main function */
111 int main(int argc, char *argv[])
112 {
113   MSG_error_t res = MSG_OK;
114
115   MSG_global_init(&argc, argv);
116   if (argc < 3) {
117     printf("Usage: %s platform_file deployment_file\n", argv[0]);
118     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
119     exit(1);
120   }
121
122   res = test_all(argv[1], argv[2]);
123   MSG_clean();
124
125   if (res == MSG_OK)
126     return 0;
127   else
128     return 1;
129 }                               /* end_of_main */