Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8d01fc42ea51d78223ad4b6d66d0834392e0c308
[simgrid.git] / teshsuite / msg / host_on_off.c
1 /* Copyright (c) 2010-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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   double task_comp_size = 5E7;
24   double task_comm_size = 1E6;
25
26   char mailbox[256];
27   msg_task_t task = NULL;
28
29   sprintf(mailbox, "jupi");
30
31   task = MSG_task_create("task on", task_comp_size, task_comm_size, NULL);
32   XBT_INFO("Sending \"%s\"", task->name);
33   MSG_task_send_with_timeout(task, mailbox, 1);
34
35   MSG_process_sleep(1);
36   MSG_host_off(MSG_get_host_by_name("Jupiter"));
37
38   task = MSG_task_create("task off", task_comp_size, task_comm_size, NULL);
39   XBT_INFO("Sending \"%s\"", task->name);
40   MSG_task_send_with_timeout(task, mailbox, 1);
41
42   MSG_host_on(MSG_get_host_by_name("Jupiter"));
43
44   task = MSG_task_create("task on without proc", task_comp_size, task_comm_size, NULL);
45   XBT_INFO("Sending \"%s\"", task->name);
46   MSG_task_send_with_timeout(task, mailbox, 1);
47
48   char **argvF = xbt_new(char*, 2);
49   argvF[0] = xbt_strdup("slave");
50   MSG_process_create_with_arguments("slave", slave, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
51
52   task = MSG_task_create("task on with proc", task_comp_size, task_comm_size, NULL);
53   XBT_INFO("Sending \"%s\"", task->name);
54   MSG_task_send_with_timeout(task, mailbox, 1);
55
56   task = MSG_task_create("finalize", 0, 0, 0);
57   XBT_INFO("Sending \"%s\"", task->name);
58   MSG_task_send_with_timeout(task, mailbox, 1);
59
60   XBT_INFO("Goodbye now!");
61   return 0;
62 }                               /* end_of_master */
63
64 /** Receiver function  */
65 int slave(int argc, char *argv[])
66 {
67   msg_task_t task = NULL;
68   _XBT_GNUC_UNUSED int res;
69   int id = -1;
70   char mailbox[80];
71   _XBT_GNUC_UNUSED int read;
72
73   sprintf(mailbox, "jupi");
74
75   while (1) {
76     res = MSG_task_receive(&(task), mailbox);
77     xbt_assert(res == MSG_OK, "MSG_task_get failed");
78
79     if (!strcmp(MSG_task_get_name(task), "finalize")) {
80       MSG_task_destroy(task);
81       break;
82     }
83     MSG_task_execute(task);
84     XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
85
86     MSG_task_destroy(task);
87     task = NULL;
88     id--;
89   }
90   XBT_INFO("I'm done. See you!");
91   return 0;
92 }                               /* end_of_slave */
93
94 /** Main function */
95 int main(int argc, char *argv[])
96 {
97   msg_error_t res;
98   const char *platform_file;
99   const char *application_file;
100
101   MSG_init(&argc, argv);
102   if (argc != 3) {
103     printf("Usage: %s platform_file deployment_file\n", argv[0]);
104     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
105     exit(1);
106   }
107   platform_file = argv[1];
108   application_file = argv[2];
109
110   /* MSG_config("workstation/model","KCCFLN05"); */
111   {                             /*  Simulation setting */
112     MSG_create_environment(platform_file);
113   }
114   {                             /*   Application deployment */
115     MSG_function_register("master", master);
116     MSG_function_register("slave", slave);
117
118     MSG_launch_application(application_file);
119   }
120   res = MSG_main();
121
122   XBT_INFO("Simulation time %g", MSG_get_clock());
123
124   if (res == MSG_OK)
125     return 0;
126   else
127     return 1;
128 }                               /* end_of_main */