Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove soner bugs in async examples
[simgrid.git] / examples / msg / cloud-two-tasks / cloud-two-tasks.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   xbt_dynar_free(&hosts_dynar);
61   msg_vm_t vm0;
62   vm0 = MSG_vm_create_core(pm0, "VM0");
63   MSG_vm_start(vm0);
64   //MSG_process_sleep(1);
65
66   launch_computation_worker(vm0);
67
68   while(MSG_get_clock()<100) {
69   if (atask != NULL)
70     XBT_INFO("aTask remaining duration: %g", MSG_task_get_flops_amount(atask));
71   MSG_process_sleep(1);
72   }
73
74   MSG_process_sleep(10000);
75   MSG_vm_destroy(vm0);
76   xbt_dynar_free(&hosts_dynar);
77   return 1;
78 }
79
80 static void launch_master(msg_host_t host)
81 {
82   const char *pr_name = "master_";
83   char **argv = xbt_new(char *, 2);
84   argv[0] = xbt_strdup(pr_name);
85   argv[1] = NULL;
86
87   MSG_process_create_with_arguments(pr_name, master_main, NULL, host, 1, argv);
88 }
89
90 int main(int argc, char *argv[]){
91   MSG_init(&argc, argv);
92
93   xbt_assert(argc == 2);
94   MSG_create_environment(argv[1]);
95
96   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
97   launch_master(xbt_dynar_get_as(hosts_dynar, 0, msg_host_t));
98   xbt_dynar_free(&hosts_dynar);
99
100   int res = MSG_main();
101   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
102   xbt_dynar_free(&hosts_dynar);
103
104   return !(res == MSG_OK);
105 }
106