Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
827d25840c057c22490e557241f3538f89609e17
[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   INFO4("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     m_task_t task = NULL;
36     task = MSG_task_create("task", 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     m_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
46     TRACE_msg_set_task_category(finalize, "finalize");
47     MSG_task_send(finalize, "master_mailbox");
48   }
49
50   return 0;
51 }
52
53 /** Receiver function  */
54 int slave(int argc, char *argv[])
55 {
56   m_task_t task = NULL;
57   int res;
58
59   while (1) {
60     res = MSG_task_receive(&(task), "master_mailbox");
61     if (res != MSG_OK) {
62       INFO0("error");
63       break;
64     }
65
66     if (!strcmp(MSG_task_get_name(task), "finalize")) {
67       MSG_task_destroy(task);
68       break;
69     }
70
71     INFO1("Executing task %f", MSG_task_get_compute_duration(task));
72     MSG_task_execute(task);
73     INFO0("End of execution");
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   MSG_global_init(&argc, argv);
108   if (argc < 3) {
109     printf("Usage: %s platform_file deployment_file\n", argv[0]);
110     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
111     exit(1);
112   }
113
114   //declaring user categories
115   TRACE_category_with_color ("compute", "1 0 0");  //compute is red
116   TRACE_category_with_color ("finalize", "0 1 0"); //finalize is green
117
118   res = test_all(argv[1], argv[2]);
119   MSG_clean();
120
121   if (res == MSG_OK)
122     return 0;
123   else
124     return 1;
125 }                               /* end_of_main */