Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move the set-maestro test to its own directory
[simgrid.git] / examples / msg / set-maestro / set-maestro.c
1 /* Copyright (c) 2007-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 /** @addtogroup MSG_examples
10  *
11  *  - <b>sendrecv/sendrecv.c: Ping-pong example</b>. It's hard to think of a simpler example. The tesh files laying in
12  *    the directory are instructive concerning the way to pass options to the simulators (as described in \ref options).
13  */
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
16
17 double task_comm_size_lat = 1;
18 double task_comm_size_bw = 10e8;
19
20 static int sender(int argc, char *argv[])
21 {
22   msg_host_t host = NULL;
23   double time;
24   msg_task_t task_la = NULL;
25   msg_task_t task_bw = NULL;
26   char sprintf_buffer_la[64];
27   char sprintf_buffer_bw[64];
28
29   XBT_INFO("sender");
30   XBT_INFO("host = %s", argv[1]);
31
32   host = MSG_host_by_name(argv[1]);
33
34   if (host == NULL) {
35     XBT_INFO("Unknown host %s. Stopping Now! ", argv[1]);
36     abort();
37   }
38
39   /* Latency */
40   time = MSG_get_clock();
41   sprintf(sprintf_buffer_la, "latency task");
42   task_la = MSG_task_create(sprintf_buffer_la, 0.0, task_comm_size_lat, NULL);
43   task_la->data = xbt_new(double, 1);
44   *(double *) task_la->data = time;
45   XBT_INFO("task_la->data = %e", *((double *) task_la->data));
46   MSG_task_send(task_la, argv[1]);
47
48   /* Bandwidth */
49   time = MSG_get_clock();
50   sprintf(sprintf_buffer_bw, "bandwidth task");
51   task_bw = MSG_task_create(sprintf_buffer_bw, 0.0, task_comm_size_bw, NULL);
52   task_bw->data = xbt_new(double, 1);
53   *(double *) task_bw->data = time;
54   XBT_INFO("task_bw->data = %e", *((double *) task_bw->data));
55   MSG_task_send(task_bw, argv[1]);
56
57   return 0;
58 }
59
60 static int receiver(int argc, char *argv[])
61 {
62   msg_task_t task_la = NULL;
63   msg_task_t task_bw = NULL;
64
65   XBT_INFO("receiver");
66
67   /* Get Latency */
68   int a = MSG_task_receive(&task_la,MSG_host_get_name(MSG_host_self()));
69   xbt_assert(a == MSG_OK, "Unexpected behavior");
70
71   double time1 = MSG_get_clock();
72   double sender_time = *((double *) (task_la->data));
73   double time = sender_time;
74   double communication_time = time1 - time;
75   XBT_INFO("Task received : %s", task_la->name);
76   xbt_free(task_la->data);
77   MSG_task_destroy(task_la);
78   XBT_INFO("Communic. time %e", communication_time);
79   XBT_INFO("--- la %f ----", communication_time);
80
81   /* Get Bandwidth */
82   a = MSG_task_receive(&task_bw,MSG_host_get_name(MSG_host_self()));
83   xbt_assert(a == MSG_OK, "Unexpected behavior");
84   time1 = MSG_get_clock();
85   sender_time = *((double *) (task_bw->data));
86   time = sender_time;
87   communication_time = time1 - time;
88   XBT_INFO("Task received : %s", task_bw->name);
89   xbt_free(task_bw->data);
90   MSG_task_destroy(task_bw);
91   XBT_INFO("Communic. time %e", communication_time);
92   XBT_INFO("--- bw %f ----", task_comm_size_bw / communication_time);
93
94   return 0;
95 }
96
97 struct application {
98   const char* platform_file;
99   const char* application_file;
100 };
101
102 /** Test function */
103 static msg_error_t test_all(struct application* app)
104 {
105   msg_error_t res = MSG_OK;
106
107   MSG_create_environment(app->platform_file);
108
109   /* Become one of the simulated process.
110    *
111    * This must be done after the creation of the platform because we are depending attaching to a host.*/
112   MSG_process_attach("sender", NULL, MSG_host_by_name("Tremblay"), NULL);
113
114   MSG_function_register("receiver", receiver);
115
116   MSG_launch_application(app->application_file);
117
118   // Execute the sender code:
119   const char* argv[3] = { "sender", "Jupiter", NULL };
120   sender(2, (char**) argv);
121
122   MSG_process_detach();
123   return res;
124 }                               /* end_of_test_all */
125
126 static void maestro(void* data)
127 {
128   MSG_main();
129 }
130
131 /** Main function */
132 int main(int argc, char *argv[])
133 {
134   msg_error_t res = MSG_OK;
135
136   struct application app;
137   app.platform_file = argv[1];
138   app.application_file = argv[2];
139
140   SIMIX_set_maestro(maestro, &app);
141   MSG_init(&argc, argv);
142
143   if (argc != 3) {
144     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
145     xbt_die("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
146   }
147
148   res = test_all(&app);
149
150   XBT_INFO("Total simulation time: %e", MSG_get_clock());
151   return res != MSG_OK;
152 }