Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
msg properties example
[simgrid.git] / examples / msg / properties / msg_prop.c
1 /*      $Id: msg_test.c,v 1.24 2007/03/16 13:18:24 cherierm Exp $        */
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 "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h" /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
19
20 int master(int argc, char *argv[]);
21 int slave(int argc, char *argv[]);
22 int forwarder(int argc, char *argv[]);
23 MSG_error_t test_all(const char *platform_file, const char *application_file);
24
25 typedef enum {
26   PORT_22 = 0,
27   MAX_CHANNEL
28 } channel_t;
29
30 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
31
32 /** Emitter function  */
33 int master(int argc, char *argv[])
34 {
35   int slaves_count = 0;
36   m_host_t *slaves = NULL;
37   m_task_t *todo = NULL;
38   int number_of_tasks = 0;
39   double task_comp_size = 0;
40   double task_comm_size = 0;
41
42
43   int i;
44
45   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
46          "Invalid argument %s\n",argv[1]);
47   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
48          "Invalid argument %s\n",argv[2]);
49   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
50          "Invalid argument %s\n",argv[3]);
51
52   {                  /*  Task creation */
53     char sprintf_buffer[64];
54
55     todo = calloc(number_of_tasks, sizeof(m_task_t));
56
57     for (i = 0; i < number_of_tasks; i++) {
58       sprintf(sprintf_buffer, "Task_%d", i);
59       todo[i] = MSG_task_create(sprintf_buffer, task_comp_size, task_comm_size, NULL);
60     }
61   }
62
63   {                  /* Process organisation */
64     slaves_count = argc - 4;
65     slaves = calloc(slaves_count, sizeof(m_host_t));
66     xbt_dict_t props;    
67     for (i = 4; i < argc; i++) {
68       slaves[i-4] = MSG_get_host_by_name(argv[i]);
69       /* Get the property list of the host */
70       props = MSG_host_get_properties(slaves[i-4]);
71       xbt_dict_cursor_t cursor=NULL;
72       char *key,*data;
73
74       /* Print the properties of the host */
75       xbt_dict_foreach(props,cursor,key,data) {
76          INFO3("Property: %s for host: %s has value: %s",key,argv[i],data);
77       }
78
79      /* Try to get a property that does not exist */
80      char *noexist=xbt_strdup("Unknown");
81      const char *value = MSG_host_get_property_value(slaves[i-4],noexist);
82      if ( value == NULL) 
83        INFO2("Property: %s for host %s is undefined", noexist, argv[i]);
84      else
85        INFO3("Property: %s for host %s has value: %s", noexist, argv[i], value);
86
87      if(slaves[i-4]==NULL) {
88          INFO1("Unknown host %s. Stopping Now! ", argv[i]);
89          abort();
90        }
91      }
92   }
93
94   for (i = 0; i < number_of_tasks; i++) {
95     if(MSG_host_self()==slaves[i % slaves_count]) {
96     }
97
98     MSG_task_put(todo[i], slaves[i % slaves_count],
99                  PORT_22);
100   }
101   
102   for (i = 0; i < slaves_count; i++) 
103     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
104                  slaves[i], PORT_22);
105   
106   free(slaves);
107   free(todo);
108   return 0;
109 } /* end_of_master */
110
111 /** Receiver function  */
112 int slave(int argc, char *argv[])
113 {
114   while(1) {
115     m_task_t task = NULL;
116     int a;
117     a = MSG_task_get(&(task), PORT_22);
118     if (a == MSG_OK) {
119       INFO1("Received \"%s\" ", MSG_task_get_name(task));
120       if(MSG_task_get_data(task)==FINALIZE) {
121         MSG_task_destroy(task);
122         break;
123       }
124
125       /* Get the property list of current slave process */
126       xbt_dict_t props = MSG_process_get_properties(MSG_process_self());
127       xbt_dict_cursor_t cursor=NULL;
128       char *key,*data;
129
130       /* Print the properties of the process */
131       xbt_dict_foreach(props,cursor,key,data) {
132          INFO3("Property: %s for process %s has value: %s",key,MSG_process_get_name(MSG_process_self()),data);
133       }
134
135       /* Try to get a property that does not exist */
136       char *noexist=xbt_strdup("UnknownProcessProp");
137       const char *value = MSG_process_get_property_value(MSG_process_self(),noexist);
138       if ( value == NULL) 
139         INFO2("Property: %s for process %s is undefined", noexist, MSG_process_get_name(MSG_process_self()));
140       else
141         INFO3("Property: %s for process %s has value: %s", noexist, MSG_process_get_name(MSG_process_self()), value);
142
143       MSG_task_execute(task);
144       MSG_task_destroy(task);
145     } else {
146       INFO0("Hey ?! What's up ? ");
147       xbt_assert0(0,"Unexpected behavior");
148     }
149   }
150   return 0;
151 } /* end_of_slave */
152
153 /** Forwarder function */
154 int forwarder(int argc, char *argv[])
155 {
156   int i;
157   int slaves_count;
158   m_host_t *slaves;
159
160   {                  /* Process organisation */
161     slaves_count = argc - 1;
162     slaves = calloc(slaves_count, sizeof(m_host_t));
163     
164     for (i = 1; i < argc; i++) {
165       slaves[i-1] = MSG_get_host_by_name(argv[i]);
166       if(slaves[i-1]==NULL) {
167         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
168         abort();
169       }
170     }
171   }
172
173   i=0;
174   while(1) {
175     m_task_t task = NULL;
176     int a;
177     a = MSG_task_get(&(task), PORT_22);
178     if (a == MSG_OK) {
179       INFO1("Received \"%s\" ", MSG_task_get_name(task));
180       if(MSG_task_get_data(task)==FINALIZE) {
181         INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
182         for (i = 0; i < slaves_count; i++) 
183           MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
184                        slaves[i], PORT_22);
185         MSG_task_destroy(task);
186         break;
187       }
188       MSG_task_put(task, slaves[i % slaves_count],
189                    PORT_22);
190       i++;
191     } else {
192       INFO0("Hey ?! What's up ? ");
193       xbt_assert0(0,"Unexpected behavior");
194     }
195   }
196   return 0;
197 } /* end_of_forwarder */
198
199 /** Test function */
200 MSG_error_t test_all(const char *platform_file,
201                             const char *application_file)
202 {
203   MSG_error_t res = MSG_OK;
204
205   /* MSG_config("surf_workstation_model","KCCFLN05"); */
206   {                             /*  Simulation setting */
207     MSG_set_channel_number(MAX_CHANNEL);
208     MSG_paje_output("msg_test.trace");
209     MSG_create_environment(platform_file);
210   }
211   {                            /*   Application deployment */
212     MSG_function_register("master", master);
213     MSG_function_register("slave", slave);
214     MSG_function_register("forwarder", forwarder);
215     MSG_launch_application(application_file);
216   }
217   res = MSG_main();
218   
219   INFO1("Simulation time %g",MSG_get_clock());
220   return res;
221 } /* end_of_test_all */
222
223
224 /** Main function */
225 int main(int argc, char *argv[])
226 {
227   MSG_error_t res = MSG_OK;
228
229   MSG_global_init(&argc,argv);
230   if (argc < 3) {
231      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
232      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
233      exit(1);
234   }
235   res = test_all(argv[1],argv[2]);
236   MSG_clean();
237
238   if(res==MSG_OK)
239     return 0;
240   else
241     return 1;
242 } /* end_of_main */