Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
S4U version of actor-yield example
[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 void launch_computation_worker(msg_host_t host)
46 {
47   const char *pr_name = "compute";
48   char **argv = xbt_new(char *, 2);
49   argv[0] = xbt_strdup(pr_name);
50   argv[1] = NULL;
51
52   MSG_process_create_with_arguments(pr_name, computation_fun, NULL, host, 1, argv);
53 }
54
55 static int master_main(int argc, char *argv[])
56 {
57   xbt_dynar_t hosts_dynar = MSG_hosts_as_dynar();
58   msg_host_t pm0 = MSG_host_by_name("Fafard");
59   msg_vm_t   vm0 = MSG_vm_create_core(pm0, "VM0");
60   MSG_vm_start(vm0);
61
62   launch_computation_worker((msg_host_t)vm0);
63
64   while(MSG_get_clock()<100) {
65     if (atask != NULL)
66       XBT_INFO("aTask remaining duration: %g", MSG_task_get_flops_amount(atask));
67     MSG_process_sleep(1);
68   }
69
70   MSG_process_sleep(10000);
71   MSG_vm_destroy(vm0);
72   xbt_dynar_free(&hosts_dynar);
73   return 1;
74 }
75
76 int main(int argc, char *argv[]){
77   MSG_init(&argc, argv);
78
79   xbt_assert(argc == 2);
80   MSG_create_environment(argv[1]);
81
82   MSG_process_create("master_", master_main, NULL, MSG_host_by_name("Fafard"));
83
84   int res = MSG_main();
85   XBT_INFO("Bye (simulation time %g)", MSG_get_clock());
86
87   return !(res == MSG_OK);
88 }
89