Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
MSG almost works. It enabled me to find some bug in the SURF. I'm going to optimize...
[simgrid.git] / testsuite / msg / msg_test.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2002,2004,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 /** \file msg_test.c 
9  *  \brief Test program for msg.
10 */
11
12 #include "msg/msg.h"
13
14 /** This flag enable the debugging messages from PRINT_DEBUG_MESSAGE() */
15 #define VERBOSE
16 #include "messages.h"
17
18 int unix_emitter(int argc, char *argv[]);
19 int unix_receiver(int argc, char *argv[]);
20 void test_all(const char *platform_file, const char *application_file, double sharing);
21
22
23 /** The names of the channels we will use in this simulation. There is
24     only one channel identified by the name PORT_22. */
25 typedef enum {
26   PORT_22 = 0,
27   MAX_CHANNEL
28 } channel_t;
29
30 void print_args(int argc, char** argv);
31 void print_args(int argc, char** argv)
32 {
33   int i ; 
34
35   fprintf(stderr,"<");
36   for(i=0; i<argc; i++) 
37     fprintf(stderr,"%s ",argv[i]);
38   fprintf(stderr,">\n");
39 }
40
41 /** The number of task each slave will process */
42 #define NB_TASK 3
43 int unix_emitter(int argc, char *argv[])
44 {
45   int slaves_count = 0;
46   m_host_t *slaves = NULL;
47   int todo_count = 0;
48   m_task_t *todo = NULL;
49
50   int i;
51   PRINT_MESSAGE("Hello !");
52   print_args(argc,argv);
53
54   {                  /* Process organisation */
55     slaves_count = argc - 1;
56     slaves = calloc(slaves_count, sizeof(m_host_t));
57     
58     for (i = 1; i < argc; i++) {
59       slaves[i-1] = MSG_get_host_by_name(argv[i]);
60       if(slaves[i-1]==NULL) {
61         PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
62         abort();
63       }
64     }
65   }
66
67   {                  /*  Task creation */
68     char sprintf_buffer[64];
69     int slave  = slaves_count;
70
71     todo = calloc(NB_TASK * slave, sizeof(m_task_t));
72     todo_count = NB_TASK * slave;
73
74     for (i = 0; i < NB_TASK * slave; i++) {
75       sprintf(sprintf_buffer, "Task_%d", i);
76       todo[i] = MSG_task_create(sprintf_buffer, 5000, 10, NULL);
77     }
78   }
79
80   PRINT_MESSAGE("Got %d slave(s) :\n", slaves_count);
81   for (i = 0; i < slaves_count; i++)
82     PRINT_MESSAGE("\t %s\n", slaves[i]->name);
83
84   PRINT_MESSAGE("Got %d task to process :\n", todo_count);
85
86   for (i = 0; i < todo_count; i++)
87     PRINT_MESSAGE("\t\"%s\"\n", todo[i]->name);
88
89   for (i = 0; i < todo_count; i++) {
90     PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
91                   todo[i]->name,
92                   slaves[i % slaves_count]->name);
93     MSG_task_put(todo[i], slaves[i % slaves_count],
94                  PORT_22);
95     PRINT_MESSAGE("Send completed\n");
96   }
97   
98   free(slaves);
99   free(todo);
100   return 0;
101 }
102
103 int unix_receiver(int argc, char *argv[])
104 {
105   int todo_count = 0;
106   m_task_t *todo = (m_task_t *) calloc(NB_TASK, sizeof(m_task_t));
107   int i;
108
109   PRINT_MESSAGE("Hello !");
110   print_args(argc,argv);
111
112   for (i = 0; i < NB_TASK;) {
113     int a;
114     PRINT_MESSAGE("Awaiting Task %d \n", i);
115     a = MSG_task_get(&(todo[i]), PORT_22);
116     if (a == MSG_OK) {
117       todo_count++;
118       PRINT_MESSAGE("Received \"%s\" \n", todo[i]->name);
119       PRINT_MESSAGE("Processing \"%s\" \n", todo[i]->name);
120       MSG_task_execute(todo[i]);
121       PRINT_MESSAGE("\"%s\" done \n", todo[i]->name);
122       MSG_task_destroy(todo[i]);
123       i++;
124     } else {
125       PRINT_MESSAGE("Hey ?! What's up ? \n");
126       DIE("Unexpected behaviour");
127     }
128   }
129   free(todo);
130   PRINT_MESSAGE("I'm done. See you!\n");
131   return 0;
132 }
133
134
135 void test_all(const char *platform_file,const char *application_file, double sharing)
136 {
137   {                             /*  Simulation setting */
138     MSG_global_init();
139     MSG_set_verbosity(MSG_SILENT);
140     MSG_set_channel_number(MAX_CHANNEL);
141     if(sharing<=0) {
142       MSG_set_sharing_policy(MSG_TCP,.1);
143     } else {
144       MSG_set_sharing_policy(MSG_STORE_AND_FORWARD,sharing);
145     }
146     MSG_create_environment(platform_file);
147   }
148   {                            /*   Application deployment */
149     MSG_function_register("master", unix_emitter);
150     MSG_function_register("slave", unix_receiver);
151     MSG_launch_application(application_file);
152   }
153   MSG_main();
154   printf("Simulation time %Lg\n",MSG_getClock());
155 /*   MSG_clean(); */
156 }
157
158 int main(int argc, char *argv[])
159 {
160   test_all("msg_platform.txt","msg_deployment.txt",-.1);
161 /*   test_all("msg_platform.txt","msg_deployment.txt",.1); */
162   return (0);
163 }