Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3dafdbdea38b4e1958616857a0651d586d066a9d
[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   atask = MSG_task_create("Task1", 1e9, 1e9, NULL);
18   double clock_sta = MSG_get_clock();
19   XBT_INFO("%s:%s task 1 created %g", host_name, pr_name, clock_sta);
20   MSG_task_execute(atask);
21   double clock_end = MSG_get_clock();
22
23   XBT_INFO("%s:%s task 1 executed %g", host_name, pr_name, clock_end - clock_sta);
24
25   MSG_task_destroy(atask);
26   atask = NULL;
27
28   MSG_process_sleep(1);
29
30   atask = MSG_task_create("Task2", 1e10, 1e10, NULL);
31
32   clock_sta = MSG_get_clock();
33   XBT_INFO("%s:%s task 2 created %g", host_name, pr_name, clock_sta);
34   MSG_task_execute(atask);
35   clock_end = MSG_get_clock();
36
37   XBT_INFO("%s:%s task 2 executed %g", host_name, pr_name, clock_end - clock_sta);
38
39   MSG_task_destroy(atask);
40   atask = NULL;
41
42   return 0;
43 }
44
45 static int master_main(int argc, char *argv[])
46 {
47   msg_host_t pm0 = MSG_host_by_name("Fafard");
48   msg_vm_t   vm0 = MSG_vm_create_core(pm0, "VM0");
49   MSG_vm_start(vm0);
50
51   MSG_process_create("compute", computation_fun, NULL, (msg_host_t)vm0);
52
53   while(MSG_get_clock()<100) {
54     if (atask != NULL)
55       XBT_INFO("aTask remaining duration: %g", MSG_task_get_remaining_work_ratio(atask));
56     MSG_process_sleep(1);
57   }
58
59   MSG_process_sleep(10000);
60   MSG_vm_destroy(vm0);
61   return 1;
62 }
63
64 int main(int argc, char *argv[]){
65   MSG_init(&argc, argv);
66
67   xbt_assert(argc == 2);
68   MSG_create_environment(argv[1]);
69
70   MSG_process_create("master_", master_main, NULL, MSG_host_by_name("Fafard"));
71
72   int res = MSG_main();
73   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
74
75   return !(res == MSG_OK);
76 }
77