Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / cloud / two_tasks_vm.c
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "simgrid/msg.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 msg_task_t atask = NULL;
12
13 static int computation_fun(int argc, char *argv[])
14 {
15   const char *pr_name = MSG_process_get_name(MSG_process_self());
16   const char *host_name = MSG_host_get_name(MSG_host_self());
17   double clock_sta, clock_end;
18   atask = MSG_task_create("Task1", 1e9, 1e9, NULL);
19   clock_sta = MSG_get_clock();
20   XBT_INFO("%s:%s task 1 created %g", host_name, pr_name, clock_sta);
21   MSG_task_execute(atask);
22   clock_end = MSG_get_clock();
23
24   XBT_INFO("%s:%s task 1 executed %g", host_name, pr_name, clock_end - clock_sta);
25
26   MSG_task_destroy(atask);
27   atask = NULL;
28
29   MSG_process_sleep(1);
30
31   atask = MSG_task_create("Task2", 1e10, 1e10, NULL);
32
33   clock_sta = MSG_get_clock();
34   XBT_INFO("%s:%s task 2 created %g", host_name, pr_name, clock_sta);
35   MSG_task_execute(atask);
36   clock_end = MSG_get_clock();
37
38   XBT_INFO("%s:%s task 2 executed %g", host_name, pr_name, clock_end - clock_sta);
39
40   MSG_task_destroy(atask);
41   atask = NULL;
42
43   return 0;
44 }
45
46 static void launch_computation_worker(msg_host_t host)
47 {
48   const char *pr_name = "compute";
49   char **argv = xbt_new(char *, 2);
50   argv[0] = xbt_strdup(pr_name);
51   argv[1] = NULL;
52
53   MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv);
54 }
55
56 static int master_main(int argc, char *argv[])
57 {
58   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
59   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
60   msg_vm_t vm0;
61   vm0 = MSG_vm_create_core(pm0, "VM0");
62   MSG_vm_start(vm0);
63   //MSG_process_sleep(1);
64
65   launch_computation_worker(vm0);
66
67   while(MSG_get_clock()<100) {
68   if (atask != NULL)
69     XBT_INFO("aTask remaining duration: %g", MSG_task_get_flops_amount(atask));
70   MSG_process_sleep(1);
71   }
72
73   MSG_process_sleep(10000);
74   MSG_vm_destroy(vm0);
75   xbt_dynar_free(&hosts_dynar);
76   return 1;
77 }
78
79 static void launch_master(msg_host_t host)
80 {
81   const char *pr_name = "master_";
82   char **argv = xbt_new(char *, 2);
83   argv[0] = xbt_strdup(pr_name);
84   argv[1] = NULL;
85
86   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
87 }
88
89 int main(int argc, char *argv[]){
90   MSG_init(&argc, argv);
91
92   xbt_assert(argc == 2);
93   MSG_create_environment(argv[1]);
94
95   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
96   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
97   launch_master(pm0);
98
99   int res = MSG_main();
100   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
101   xbt_dynar_free(&hosts_dynar);
102
103   return !(res == MSG_OK);
104 }
105