Logo AND Algorithmique Numérique Distribuée

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