Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a test trying to access the properties of a remote host -- it works
[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,
16                              "Messages specific for this msg example");
17
18 int master(int argc, char *argv[]);
19 int slave(int argc, char *argv[]);
20 MSG_error_t test_all(const char *platform_file,
21                      const char *application_file);
22
23 /** Emitter function  */
24 int master(int argc, char *argv[])
25 {
26   long number_of_tasks = atol(argv[1]);
27   double task_comp_size = atof(argv[2]);
28   double task_comm_size = atof(argv[3]);
29   long slaves_count = atol(argv[4]);
30   XBT_INFO("master %ld %f %f %ld", number_of_tasks, task_comp_size,
31         task_comm_size, slaves_count);
32
33   int i;
34   for (i = 0; i < number_of_tasks; i++) {
35     char task_name[100];
36     snprintf (task_name, 100, "task-%d", i);
37     m_task_t task = NULL;
38     task = MSG_task_create(task_name, task_comp_size, task_comm_size, NULL);
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     char task_name[100];
48     snprintf (task_name, 100, "task-%d", i);
49     m_task_t finalize = MSG_task_create(task_name, 0, 0, xbt_strdup("finalize"));
50     TRACE_msg_set_task_category(finalize, "finalize");
51     MSG_task_send(finalize, "master_mailbox");
52   }
53
54   return 0;
55 }
56
57 /** Receiver function  */
58 int slave(int argc, char *argv[])
59 {
60   m_task_t task = NULL;
61   int res;
62
63   while (1) {
64     res = MSG_task_receive(&(task), "master_mailbox");
65     if (res != MSG_OK) {
66       XBT_INFO("error");
67       break;
68     }
69
70     char *data = MSG_task_get_data(task);
71     if (data && !strcmp(data, "finalize")) {
72       MSG_task_destroy(task);
73       break;
74     }
75
76     XBT_INFO("Executing task %f", MSG_task_get_compute_duration(task));
77     MSG_task_execute(task);
78     XBT_INFO("End of execution");
79     MSG_task_destroy(task);
80     task = NULL;
81   }
82   return 0;
83 }
84
85 /** Test function */
86 MSG_error_t test_all(const char *platform_file,
87                      const char *application_file)
88 {
89   MSG_error_t res = MSG_OK;
90
91   {                             /*  Simulation setting */
92     MSG_set_channel_number(0);
93     MSG_create_environment(platform_file);
94   }
95   {
96     //declaring user categories
97     TRACE_category_with_color ("compute", "1 0 0");  //compute is red
98     TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
99   }
100   {                             /*   Application deployment */
101     MSG_function_register("master", master);
102     MSG_function_register("slave", slave);
103     MSG_launch_application(application_file);
104   }
105   res = MSG_main();
106
107   XBT_INFO("Simulation time %g", MSG_get_clock());
108   return res;
109 }
110
111
112 /** Main function */
113 int main(int argc, char *argv[])
114 {
115   MSG_error_t res = MSG_OK;
116
117   MSG_global_init(&argc, argv);
118   if (argc < 3) {
119     printf("Usage: %s platform_file deployment_file\n", argv[0]);
120     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
121     exit(1);
122   }
123
124   res = test_all(argv[1], argv[2]);
125   MSG_clean();
126
127   if (res == MSG_OK)
128     return 0;
129   else
130     return 1;
131 }                               /* end_of_main */