Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
673d3d4d3be35ee4c4e003d9d0ffc9e5c8b4c8b2
[simgrid.git] / examples / msg / tracing / volume.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   //defining the category of the master process
27   TRACE_msg_set_process_category(MSG_process_self(), "master");
28
29   long number_of_tasks = atol(argv[1]);
30   long slaves_count = atol(argv[4]);
31   int p = 1000000000;
32   int c = 10000000;
33
34   int i;
35   for (i = 0; i < number_of_tasks; i++) {
36     m_task_t task = NULL;
37     task = MSG_task_create("task_compute", p, c, NULL);
38     TRACE_msg_set_task_category(task, "compute");
39     MSG_task_send(task, "master_mailbox");
40     task = NULL;
41     task = MSG_task_create("task_request", p, c, NULL);
42     TRACE_msg_set_task_category(task, "request");
43     MSG_task_send(task, "master_mailbox");
44     task = NULL;
45     task = MSG_task_create("task_data", p, c, NULL);
46     TRACE_msg_set_task_category(task, "data");
47     MSG_task_send(task, "master_mailbox");
48   }
49
50   for (i = 0; i < slaves_count; i++) {
51     m_task_t finalize = MSG_task_create("finalize", 0, 1000, 0);
52     TRACE_msg_set_task_category(finalize, "finalize");
53     MSG_task_send(finalize, "master_mailbox");
54   }
55
56   return 0;
57 }
58
59 /** Receiver function  */
60 int slave(int argc, char *argv[])
61 {
62   //defining the category of this slave process
63   TRACE_msg_set_process_category(MSG_process_self(), "slave");
64
65   m_task_t task = NULL;
66   int res;
67
68   while (1) {
69     res = MSG_task_receive(&(task), "master_mailbox");
70
71     if (!strcmp(MSG_task_get_name(task), "finalize")) {
72       MSG_task_destroy(task);
73       break;
74     }
75
76     MSG_task_execute(task);
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     //--cfg=tracing/msg/volume
95     // - the communication volume among processes expects that:
96     //     - the processes involved have a category
97     //     - the sent tasks have a category
98
99     //declaring user categories (for tasks)
100     TRACE_category_with_color ("compute", "1 0 0"); //red
101     TRACE_category_with_color ("request", "0 1 0"); //green
102     TRACE_category_with_color ("data", "0 0 1");    //blue
103     TRACE_category_with_color ("finalize", "0 0 0");//black
104
105     //declaring user categories (for processes)
106     TRACE_category_with_color ("master", "1 0 0");
107     TRACE_category_with_color ("slave", "0 0 1");
108   }
109   {                             /*   Application deployment */
110     MSG_function_register("master", master);
111     MSG_function_register("slave", slave);
112     MSG_launch_application(application_file);
113   }
114   res = MSG_main();
115
116   INFO1("Simulation time %g", MSG_get_clock());
117   return res;
118 }
119
120
121 /** Main function */
122 int main(int argc, char *argv[])
123 {
124   MSG_error_t res = MSG_OK;
125
126   MSG_global_init(&argc, argv);
127   if (argc < 3) {
128     printf("Usage: %s platform_file deployment_file\n", argv[0]);
129     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
130     exit(1);
131   }
132
133   res = test_all(argv[1], argv[2]);
134   MSG_clean();
135
136   if (res == MSG_OK)
137     return 0;
138   else
139     return 1;
140 }                               /* end_of_main */