Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[smpi] add a gestion of non-contignous data
[simgrid.git] / examples / msg / cloud / masterslave_virtual_machines.c
1 /* Copyright (c) 2007-2012. The SimGrid Team. All rights reserved.                             */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <stdio.h>
7 #include "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
8 #include "xbt/sysdep.h"         /* calloc, printf */
9
10 /* Create a log channel to have nice outputs. */
11 #include "xbt/log.h"
12 #include "xbt/asserts.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 /** @addtogroup MSG_examples
17  * 
18  *  - <b>cloud/masterslave_virtual_machines.c: Master/slaves
19  *    example, à la cloud</b>. The classical example revisited to demonstrate the use of virtual machines.
20  */
21
22 double task_comp_size = 10000000;
23 double task_comm_size = 10000000;
24
25
26 int master(int argc, char *argv[]);
27 int slave_fun(int argc, char *argv[]);
28
29 static void work_batch(int slaves_count) {
30   int i;
31   for (i = 0; i < slaves_count; i++) {
32     char taskname_buffer[64];
33     char mailbox_buffer[64];
34
35     sprintf(taskname_buffer, "Task_%d", i);
36     sprintf(mailbox_buffer,"Slave_%d",i);
37
38     XBT_INFO("Sending \"%s\" to \"%s\"",taskname_buffer,mailbox_buffer);
39     MSG_task_send(MSG_task_create(taskname_buffer, task_comp_size, task_comm_size,NULL),
40         mailbox_buffer);
41   }
42 }
43
44 int master(int argc, char *argv[]) {
45   int slaves_count = 10;
46   msg_host_t *slaves = xbt_new(msg_host_t,10);
47
48   msg_vm_t vm;
49   unsigned int i;
50
51   /* Retrive the hostnames constituting our playground today */
52   for (i = 1; i < argc; i++) {
53     slaves[i - 1] = MSG_get_host_by_name(argv[i]);
54     xbt_assert(slaves[i - 1] != NULL, "Cannot use inexistent host %s", argv[i]);
55   }
56
57   /* Launch the sub processes: one VM per host, with one process inside each */
58
59   for (i=0;i<slaves_count;i++) {
60     char slavename[64];
61     sprintf(slavename,"Slave %d",i);
62     char**argv=xbt_new(char*,3);
63     argv[0] = xbt_strdup(slavename);
64     argv[1] = bprintf("%d",i);
65     argv[2] = NULL;
66     msg_vm_t vm = MSG_vm_start(slaves[i],2);
67     MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
68   }
69
70
71   xbt_dynar_t vms = MSG_vms_as_dynar();
72   XBT_INFO("Launched %ld VMs", xbt_dynar_length(vms));
73
74   /* Send a bunch of work to every one */
75   XBT_INFO("Send a first batch of work to every one");
76   work_batch(slaves_count);
77
78   XBT_INFO("Now suspend all VMs, just for fun");
79
80   xbt_dynar_foreach(vms,i,vm) {
81     MSG_vm_suspend(vm);
82   }
83
84   XBT_INFO("Wait a while");
85   MSG_process_sleep(2);
86
87   XBT_INFO("Enough. Let's resume everybody.");
88   xbt_dynar_foreach(vms,i,vm) {
89     MSG_vm_resume(vm);
90   }
91   XBT_INFO("Sleep long enough for everyone to be done with previous batch of work");
92   MSG_process_sleep(1000-MSG_get_clock());
93
94   XBT_INFO("Add one more process per VM");
95   xbt_dynar_foreach(vms,i,vm) {
96     msg_vm_t vm = xbt_dynar_get_as(vms,i,msg_vm_t);
97     char slavename[64];
98     sprintf(slavename,"Slave %ld",i+xbt_dynar_length(vms));
99     char**argv=xbt_new(char*,3);
100     argv[0] = xbt_strdup(slavename);
101     argv[1] = bprintf("%ld",i+xbt_dynar_length(vms));
102     argv[2] = NULL;
103     MSG_vm_bind(vm, MSG_process_create_with_arguments(slavename,slave_fun,NULL,slaves[i],2,argv));
104   }
105
106   XBT_INFO("Reboot all the VMs");
107   xbt_dynar_foreach(vms,i,vm) {
108     MSG_vm_reboot(vm);
109   }
110
111   work_batch(slaves_count*2);
112
113   XBT_INFO("Migrate everyone to the second host.");
114   xbt_dynar_foreach(vms,i,vm) {
115     MSG_vm_migrate(vm,slaves[1]);
116   }
117   XBT_INFO("Suspend everyone, move them to the third host, and resume them.");
118   xbt_dynar_foreach(vms,i,vm) {
119     MSG_vm_suspend(vm);
120     MSG_vm_migrate(vm,slaves[2]);
121     MSG_vm_resume(vm);
122   }
123
124
125   XBT_INFO("Let's shut down the simulation. 10 first processes will be shut down cleanly while the second half will forcefully get killed");
126   for (i = 0; i < slaves_count; i++) {
127     char mailbox_buffer[64];
128     sprintf(mailbox_buffer,"Slave_%d",i);
129     msg_task_t finalize = MSG_task_create("finalize", 0, 0, 0);
130     MSG_task_send(finalize, mailbox_buffer);
131   }
132
133   xbt_dynar_foreach(vms,i,vm) {
134     MSG_vm_shutdown(vm);
135     MSG_vm_destroy(vm);
136   }
137
138   XBT_INFO("Goodbye now!");
139   free(slaves);
140   xbt_dynar_free(&vms);
141   return 0;
142 }                               /* end_of_master */
143
144 /** Receiver function  */
145 int slave_fun(int argc, char *argv[])
146 {
147   char *mailbox_name;
148   msg_task_t task = NULL;
149   _XBT_GNUC_UNUSED int res;
150   /* since the slaves will move around, use slave_%d as mailbox names instead of hostnames */
151   xbt_assert(argc>=2, "slave processes need to be given their rank as parameter");
152   mailbox_name=bprintf("Slave_%s",argv[1]);
153   XBT_INFO("Slave listenning on %s",argv[1]);
154   while (1) {
155     res = MSG_task_receive(&(task),mailbox_name);
156     xbt_assert(res == MSG_OK, "MSG_task_get failed");
157
158     XBT_INFO("Received \"%s\" from mailbox %s", MSG_task_get_name(task),mailbox_name);
159     if (!strcmp(MSG_task_get_name(task), "finalize")) {
160       MSG_task_destroy(task);
161       break;
162     }
163
164     MSG_task_execute(task);
165     XBT_INFO("\"%s\" done", MSG_task_get_name(task));
166     MSG_task_destroy(task);
167     task = NULL;
168   }
169
170   free(mailbox_name);
171   return 0;
172 }                               /* end_of_slave */
173
174 /** Main function */
175 int main(int argc, char *argv[])
176 {
177   msg_error_t res = MSG_OK;
178   xbt_dynar_t hosts_dynar;
179   msg_host_t*hosts= xbt_new(msg_host_t,10);
180   char**hostnames= xbt_new(char*,10);
181   char**masterargv=xbt_new(char*,12);
182   int i;
183
184   /* Get the arguments */
185   MSG_init(&argc, argv);
186   if (argc < 2) {
187     printf("Usage: %s platform_file\n", argv[0]);
188     printf("example: %s msg_platform.xml\n", argv[0]);
189     exit(1);
190   } if (argc>2) {
191     printf("Usage: %s platform_file\n", argv[0]);
192     printf("Other parameters (such as the deployment file) are ignored.");
193   }
194
195   /* load the platform file */
196   MSG_create_environment(argv[1]);
197   /* Retrieve the 10 first hosts of the platform file */
198   hosts_dynar = MSG_hosts_as_dynar();
199   xbt_assert(xbt_dynar_length(hosts_dynar)>10,
200       "I need at least 10 hosts in the platform file, but %s contains only %ld hosts_dynar.",
201       argv[1],xbt_dynar_length(hosts_dynar));
202   for (i=0;i<10;i++) {
203     hosts[i] = xbt_dynar_get_as(hosts_dynar,i,msg_host_t);
204     hostnames[i] = xbt_strdup(MSG_host_get_name(hosts[i]));
205   }
206   masterargv[0]=xbt_strdup("master");
207   for (i=1;i<11;i++) {
208     masterargv[i] = xbt_strdup(MSG_host_get_name(hosts[i-1]));
209   }
210   masterargv[11]=NULL;
211   MSG_process_create_with_arguments("master",master,NULL,hosts[0],11,masterargv);
212   res = MSG_main();
213   XBT_INFO("Simulation time %g", MSG_get_clock());
214
215   MSG_clean();
216   free(hosts);
217   free(hostnames);
218   xbt_dynar_free(&hosts_dynar);
219
220   if (res == MSG_OK)
221     return 0;
222   else
223     return 1;
224 }                               /* end_of_main */