Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Integrate Bruno's work on SIMIX onto main stream. Tests are broken, but it looks...
[simgrid.git] / examples / msg / msg_test_communication_time.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 "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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
15
16 int master(int argc, char *argv[]);
17 int slave(int argc, char *argv[]);
18 int forwarder(int argc, char *argv[]);
19 MSG_error_t test_all(const char *platform_file, const char *application_file);
20
21 typedef enum {
22   PORT_22 = 0,
23   MAX_CHANNEL
24 } channel_t;
25
26 #define FINALIZE ((void*)221297) /* a magic number to tell people to stop working */
27
28 /** Emitter function  */
29 int master(int argc, char *argv[])
30 {
31   int slaves_count = 0;
32   m_host_t *slaves = NULL;
33   m_task_t *todo = NULL;
34   int number_of_tasks = 0;
35   double task_comp_size = 0;
36   double task_comm_size = 0;
37
38
39   int i;
40
41   xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
42          "Invalid argument %s\n",argv[1]);
43   xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
44          "Invalid argument %s\n",argv[2]);
45   xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
46          "Invalid argument %s\n",argv[3]);
47
48   {                  /* Process organisation */
49     slaves_count = argc - 4;
50     slaves = calloc(slaves_count, sizeof(m_host_t));
51     
52     for (i = 4; i < argc; i++) {
53       slaves[i-4] = MSG_get_host_by_name(argv[i]);
54       if(slaves[i-4]==NULL) {
55         INFO1("Unknown host %s. Stopping Now! ", argv[i]);
56         abort();
57       }
58     }
59   }
60
61   INFO1("Got %d slave(s) :", slaves_count);
62   for (i = 0; i < slaves_count; i++)
63     INFO1("\t %s", slaves[i]->name);
64
65   INFO1("Got %d task to process :", number_of_tasks);
66
67   for (i = 0; i < number_of_tasks; i++) {
68     m_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, 
69                                     calloc(1,sizeof(double)));
70     *((double*) task->data) = MSG_get_clock();
71     MSG_task_put(task, slaves[i % slaves_count], PORT_22);
72     INFO0("Send completed");
73   }
74   
75   INFO0("All tasks have been dispatched. Let's tell everybody the computation is over.");
76   for (i = 0; i < slaves_count; i++) 
77     MSG_task_put(MSG_task_create("finalize", 0, 0, FINALIZE),
78                  slaves[i], PORT_22);
79   
80   INFO0("Goodbye now!");
81   free(slaves);
82   return 0;
83 } /* end_of_master */
84
85 /** Receiver function  */
86 int slave(int argc, char *argv[])
87 {
88   while(1) {
89     m_task_t task = NULL;
90     int a;
91     double time1,time2;
92
93     time1 = MSG_get_clock();
94     a = MSG_task_get(&(task), PORT_22);
95     time2 = MSG_get_clock();
96     if (a == MSG_OK) {
97       INFO1("Received \"%s\" ", MSG_task_get_name(task));
98       if(MSG_task_get_data(task)==FINALIZE) {
99         MSG_task_destroy(task);
100         break;
101       }
102       if(time1<*((double *)task->data))
103         time1 = *((double *) task->data);
104       INFO1("Communication time :  \"%f\" ", time2-time1);      
105       INFO1("Processing \"%s\" ", MSG_task_get_name(task));
106       MSG_task_execute(task);
107       INFO1("\"%s\" done ", MSG_task_get_name(task));
108       MSG_task_destroy(task);
109     } else {
110       INFO0("Hey ?! What's up ? ");
111       xbt_assert0(0,"Unexpected behavior");
112     }
113   }
114   INFO0("I'm done. See you!");
115   return 0;
116 } /* end_of_slave */
117
118 /** Test function */
119 MSG_error_t test_all(const char *platform_file,
120                             const char *application_file)
121 {
122   MSG_error_t res = MSG_OK;
123
124   /* MSG_config("surf_workstation_model","KCCFLN05"); */
125   {                             /*  Simulation setting */
126     MSG_set_channel_number(MAX_CHANNEL);
127     MSG_paje_output("msg_test.trace");
128     MSG_create_environment(platform_file);
129   }
130   {                            /*   Application deployment */
131     MSG_function_register("master", master);
132     MSG_function_register("slave", slave);
133     MSG_launch_application(application_file);
134   }
135   res = MSG_main();
136   
137   INFO1("Simulation time %g",MSG_get_clock());
138   return res;
139 } /* end_of_test_all */
140
141
142 /** Main function */
143 int main(int argc, char *argv[])
144 {
145   MSG_error_t res = MSG_OK;
146
147   MSG_global_init(&argc,argv);
148   if (argc < 3) {
149      printf ("Usage: %s platform_file deployment_file\n",argv[0]);
150      printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
151      exit(1);
152   }
153   res = test_all(argv[1],argv[2]);
154   MSG_clean();
155
156   if(res==MSG_OK)
157     return 0;
158   else
159     return 1;
160 } /* end_of_main */