Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / app-pingpong / app-pingpong.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>Ping-Pong: app-pingpong/app-pingpong.c</b>. It's hard to think of a simpler example. The tesh file
12  *    laying in the directory is instructive concerning the way to pass options to the simulators (see \ref options).
13  */
14
15 XBT_LOG_NEW_DEFAULT_CATEGORY(mag_app_pingpong,"Messages specific for this msg example");
16
17 static int pinger(int argc, char *argv[])
18 {
19   XBT_INFO("Ping -> %s", argv[1]);
20   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
21
22   /** - Do the ping with a 1-Byte task (latency bound) ... */
23   double time = MSG_get_clock();
24   msg_task_t ping_task = MSG_task_create("small communication (latency bound)", 0.0, 1, NULL);
25   ping_task->data = xbt_new(double, 1);
26   *(double *) ping_task->data = time;
27   MSG_task_send(ping_task, argv[1]);
28
29   /** - ... then wait for the (large) pong */
30   msg_task_t pong_task = NULL;
31   int a = MSG_task_receive(&pong_task,MSG_host_get_name(MSG_host_self()));
32   xbt_assert(a == MSG_OK, "Unexpected behavior");
33
34   double sender_time = *((double *) (pong_task->data));
35   double communication_time =  MSG_get_clock() - sender_time;
36   XBT_INFO("Task received : %s", pong_task->name);
37   xbt_free(pong_task->data);
38   MSG_task_destroy(pong_task);
39   XBT_INFO("Pong time (bandwidth bound): %e", communication_time);
40
41   return 0;
42 }
43
44 static int ponger(int argc, char *argv[])
45 {
46   XBT_INFO("Pong -> %s", argv[1]);
47   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
48
49   /** - Receive the (small) ping first ....*/
50   msg_task_t ping_task = NULL;
51   int a = MSG_task_receive(&ping_task, MSG_host_get_name(MSG_host_self()));
52   xbt_assert(a == MSG_OK, "Unexpected behavior");
53
54   double sender_time = *((double *) (ping_task->data));
55   double communication_time = MSG_get_clock() - sender_time;
56   XBT_INFO("Task received : %s", ping_task->name);
57   xbt_free(ping_task->data);
58   MSG_task_destroy(ping_task);
59   XBT_INFO(" Ping time (latency bound) %e", communication_time);
60
61   /**  - ... Then send a 1GB pong back (bandwidth bound) */
62   double time = MSG_get_clock();
63   msg_task_t pong_task = MSG_task_create("large communication (bandwidth bound)", 0.0, 1e9, NULL);
64   pong_task->data = xbt_new(double, 1);
65   *(double *) pong_task->data = time;
66   XBT_INFO("task_bw->data = %e", *((double *) pong_task->data));
67   MSG_task_send(pong_task, argv[1]);
68
69   return 0;
70 }
71
72 int main(int argc, char *argv[])
73 {
74   msg_error_t res = MSG_OK;
75
76   MSG_init(&argc, argv);
77
78   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
79              "\tExample: %s ../../platform/small_platform.xml app-pingpong_d.xml\n", argv[0], argv[0]);
80
81   MSG_create_environment(argv[1]);          /** - Load the platform description */
82
83   MSG_function_register("pinger", pinger);  /** - Register the function to be executed by the processes */
84   MSG_function_register("ponger", ponger);
85
86   MSG_launch_application(argv[2]);          /** - Deploy the application */
87
88   res = MSG_main();                         /** - Run the simulation */
89
90   XBT_INFO("Total simulation time: %e", MSG_get_clock());
91   return res!=MSG_OK;
92 }