Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6359c7535fa09e7d2a45b47b577bb755c4e67a80
[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 #undef 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 /** The number of task each slave will process */
31 #define NB_TASK 3
32 int unix_emitter(int argc, char *argv[])
33 {
34   int slaves_count = 0;
35   m_host_t *slaves = NULL;
36   int todo_count = 0;
37   m_task_t *todo = NULL;
38
39   int i;
40
41   {                  /* Process organisation */
42     slaves_count = argc - 1;
43     slaves = calloc(slaves_count, sizeof(m_host_t));
44     
45     for (i = 1; i < argc; i++) {
46       slaves[i-1] = MSG_get_host_by_name(argv[i]);
47       if(slaves[i-1]==NULL) {
48         PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
49         abort();
50       }
51     }
52   }
53
54   {                  /*  Task creation */
55     char sprintf_buffer[64];
56     int slave  = slaves_count;
57
58     todo = calloc(NB_TASK * slave, sizeof(m_task_t));
59     todo_count = NB_TASK * slave;
60
61     for (i = 0; i < NB_TASK * slave; i++) {
62       sprintf(sprintf_buffer, "Task_%d", i);
63       todo[i] = MSG_task_create(sprintf_buffer, 5000, 10, NULL);
64     }
65   }
66
67   PRINT_MESSAGE("Got %d slave(s) :\n", slaves_count);
68   for (i = 0; i < slaves_count; i++)
69     PRINT_MESSAGE("\t %s\n", slaves[i]->name);
70
71   PRINT_MESSAGE("Got %d task to process :\n", todo_count);
72
73   for (i = 0; i < todo_count; i++)
74     PRINT_MESSAGE("\t\"%s\"\n", todo[i]->name);
75
76   for (i = 0; i < todo_count; i++) {
77     PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
78                   todo[i]->name,
79                   slaves[i % slaves_count]->name);
80     MSG_task_put(todo[i], slaves[i % slaves_count],
81                  PORT_22);
82     PRINT_MESSAGE("Send completed\n");
83   }
84   
85   free(slaves);
86   free(todo);
87   return 0;
88 }
89
90 int unix_receiver(int argc, char *argv[])
91 {
92   int todo_count = 0;
93   m_task_t *todo = (m_task_t *) calloc(NB_TASK, sizeof(m_task_t));
94   int i;
95
96
97   for (i = 0; i < NB_TASK;) {
98     int a;
99     PRINT_MESSAGE("Awaiting Task %d \n", i);
100     a = MSG_task_get(&(todo[i]), PORT_22);
101     if (a == MSG_OK) {
102       todo_count++;
103       PRINT_MESSAGE("Received \"%s\" \n", todo[i]->name);
104       PRINT_MESSAGE("Processing \"%s\" \n", todo[i]->name);
105       MSG_task_execute(todo[i]);
106       PRINT_MESSAGE("\"%s\" done \n", todo[i]->name);
107       MSG_task_destroy(todo[i]);
108       i++;
109     } else {
110       PRINT_MESSAGE("Hey ?! What's up ? \n");
111       DIE("Unexpected behaviour");
112     }
113   }
114   free(todo);
115   return 0;
116 }
117
118
119 void test_all(const char *platform_file,const char *application_file, double sharing)
120 {
121   {                             /*  Simulation setting */
122     MSG_global_init();
123     MSG_set_verbosity(MSG_SILENT);
124     MSG_set_channel_number(MAX_CHANNEL);
125     if(sharing<=0) {
126       MSG_set_sharing_policy(MSG_TCP,.1);
127     } else {
128       MSG_set_sharing_policy(MSG_STORE_AND_FORWARD,sharing);
129     }
130     MSG_create_environment(platform_file);
131   }
132   {                            /*   Application deployment */
133     MSG_function_register("master", unix_emitter);
134     MSG_function_register("slave", unix_receiver);
135     MSG_launch_application(application_file);
136   }
137   MSG_main();
138 /*   printf("Simulation time %g\n",MSG_getClock()); */
139   MSG_clean();
140 }
141
142 int main(int argc, char *argv[])
143 {
144   test_all("msg_platform.txt","msg_deployment.txt",-.1);
145   test_all("msg_platform.txt","msg_deployment.txt",.1);
146   return (0);
147 }