Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge before commiting VM changes - Adrien
[simgrid.git] / examples / msg / cloud / two_tasks_vm.c
1 /* Copyright (c) 2014. 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 <stdio.h>
8 #include "simgrid/msg.h"
9 #include "xbt/sysdep.h"
10
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 msg_task_t atask = NULL;
17
18 static int computation_fun(int argc, char *argv[])
19 {
20   const char *pr_name = MSG_process_get_name(MSG_process_self());
21   const char *host_name = MSG_host_get_name(MSG_host_self());
22   double clock_sta, clock_end;
23   atask = MSG_task_create("Task1", 1e9, 1e9, NULL);
24   clock_sta = MSG_get_clock();
25   XBT_INFO("%s:%s task 1 created %g", host_name, pr_name, clock_sta);
26   MSG_task_execute(atask);
27   clock_end = MSG_get_clock();
28
29   XBT_INFO("%s:%s task 1 executed %g", host_name, pr_name, clock_end - clock_sta);
30
31   MSG_task_destroy(atask);
32   atask = NULL;
33
34   MSG_process_sleep(1);
35
36   atask = MSG_task_create("Task2", 1e10, 1e10, NULL);
37
38   clock_sta = MSG_get_clock();
39   XBT_INFO("%s:%s task 2 created %g", host_name, pr_name, clock_sta);
40   MSG_task_execute(atask);
41   clock_end = MSG_get_clock();
42
43   XBT_INFO("%s:%s task 2 executed %g", host_name, pr_name, clock_end - clock_sta);
44
45   MSG_task_destroy(atask);
46   atask = NULL;
47
48   return 0;
49 }
50
51 static void launch_computation_worker(msg_host_t host)
52 {
53   const char *pr_name = "compute";
54   char **argv = xbt_new(char *, 2);
55   argv[0] = xbt_strdup(pr_name);
56   argv[1] = NULL;
57
58   MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv);
59 }
60
61 static int master_main(int argc, char *argv[])
62 {
63   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
64   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
65   msg_vm_t vm0;
66   vm0 = MSG_vm_create_core(pm0, "VM0");
67   MSG_vm_start(vm0);
68   //MSG_process_sleep(1);
69
70   launch_computation_worker(vm0);
71
72   while(MSG_get_clock()<100) {
73         if (atask != NULL)
74           XBT_INFO("aTask remaining duration: %g", MSG_task_get_flops_amount(atask));
75         MSG_process_sleep(1);
76   }
77
78   MSG_process_sleep(10000);
79   MSG_vm_destroy(vm0);
80   xbt_dynar_free(&hosts_dynar);
81   return 1;
82 }
83
84 static void launch_master(msg_host_t host)
85 {
86   const char *pr_name = "master_";
87   char **argv = xbt_new(char *, 2);
88   argv[0] = xbt_strdup(pr_name);
89   argv[1] = NULL;
90
91   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
92 }
93
94 int main(int argc, char *argv[]){
95   MSG_init(&argc, argv);
96
97   xbt_assert(argc == 2);
98   MSG_create_environment(argv[1]);
99
100   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
101   msg_host_t pm0 = xbt_dynar_get_as(hosts_dynar, 0, msg_host_t);
102   launch_master(pm0);
103
104   int res = MSG_main();
105   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
106   xbt_dynar_free(&hosts_dynar);
107
108   return !(res == MSG_OK);
109 }
110
111
112
113