Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cc542af7fafba7dc70a3079dc9c10ae8462fff2f
[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
72   sprintf(mailbox, "jupi");
73
74   while (1) {
75     res = MSG_task_receive(&(task), mailbox);
76     xbt_assert(res == MSG_OK, "MSG_task_get failed");
77
78     if (!strcmp(MSG_task_get_name(task), "finalize")) {
79       MSG_task_destroy(task);
80       break;
81     }
82     MSG_task_execute(task);
83     XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
84
85     MSG_task_destroy(task);
86     task = NULL;
87     id--;
88   }
89   XBT_INFO("I'm done. See you!");
90   return 0;
91 }                               /* end_of_slave */
92
93 /** Main function */
94 int main(int argc, char *argv[])
95 {
96   msg_error_t res;
97   const char *platform_file;
98   const char *application_file;
99
100   MSG_init(&argc, argv);
101   if (argc != 3) {
102     printf("Usage: %s platform_file deployment_file\n", argv[0]);
103     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
104     exit(1);
105   }
106   platform_file = argv[1];
107   application_file = argv[2];
108
109   /* MSG_config("workstation/model","KCCFLN05"); */
110   {                             /*  Simulation setting */
111     MSG_create_environment(platform_file);
112   }
113   {                             /*   Application deployment */
114     MSG_function_register("master", master);
115     MSG_function_register("slave", slave);
116
117     MSG_launch_application(application_file);
118   }
119   res = MSG_main();
120
121   XBT_INFO("Simulation time %g", MSG_get_clock());
122
123   if (res == MSG_OK)
124     return 0;
125   else
126     return 1;
127 }                               /* end_of_main */