Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
49fe3653cd1d835ef0d4a30709b363412d1a4f83
[simgrid.git] / teshsuite / msg / app-pingpong / app-pingpong.c
1 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "simgrid/msg.h"
7
8 #include <stdio.h> /* snprintf */
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_app_pingpong, "Messages specific for this msg example");
11
12 static int pinger(int argc, char* argv[])
13 {
14   xbt_assert(argc == 2, "The pinger function one argument from the XML deployment file");
15   XBT_INFO("Ping -> %s", argv[1]);
16   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
17
18   /* - Do the ping with a 1-Byte task (latency bound) ... */
19   double now                = MSG_get_clock();
20   msg_task_t ping_task      = MSG_task_create("small communication (latency bound)", 0.0, 1, NULL);
21   ping_task->data           = xbt_new(double, 1);
22   *(double*)ping_task->data = now;
23   MSG_task_send(ping_task, argv[1]);
24
25   /* - ... then wait for the (large) pong */
26   msg_task_t pong_task = NULL;
27   int a                = MSG_task_receive(&pong_task, MSG_host_get_name(MSG_host_self()));
28   xbt_assert(a == MSG_OK, "Unexpected behavior");
29
30   double sender_time        = *((double*)(pong_task->data));
31   double communication_time = MSG_get_clock() - sender_time;
32   XBT_INFO("Task received : %s", pong_task->name);
33   xbt_free(pong_task->data);
34   MSG_task_destroy(pong_task);
35   XBT_INFO("Pong time (bandwidth bound): %.3f", communication_time);
36
37   return 0;
38 }
39
40 static int ponger(int argc, char* argv[])
41 {
42   xbt_assert(argc == 2, "The ponger function one argument from the XML deployment file");
43   XBT_INFO("Pong -> %s", argv[1]);
44   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
45
46   /* - Receive the (small) ping first ....*/
47   msg_task_t ping_task = NULL;
48   int a                = MSG_task_receive(&ping_task, MSG_host_get_name(MSG_host_self()));
49   xbt_assert(a == MSG_OK, "Unexpected behavior");
50
51   double sender_time        = *((double*)(ping_task->data));
52   double communication_time = MSG_get_clock() - sender_time;
53   XBT_INFO("Task received : %s", ping_task->name);
54   xbt_free(ping_task->data);
55   MSG_task_destroy(ping_task);
56   XBT_INFO(" Ping time (latency bound) %f", communication_time);
57
58   /*  - ... Then send a 1GB pong back (bandwidth bound) */
59   double now                = MSG_get_clock();
60   msg_task_t pong_task      = MSG_task_create("large communication (bandwidth bound)", 0.0, 1e9, NULL);
61   pong_task->data           = xbt_new(double, 1);
62   *(double*)pong_task->data = now;
63   XBT_INFO("task_bw->data = %.3f", *((double*)pong_task->data));
64   MSG_task_send(pong_task, argv[1]);
65
66   return 0;
67 }
68
69 int main(int argc, char* argv[])
70 {
71   MSG_init(&argc, argv);
72
73   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
74                        "\tExample: %s ../../platforms/small_platform.xml app-pingpong_d.xml\n",
75              argv[0], argv[0]);
76
77   MSG_create_environment(argv[1]); /* - Load the platform description */
78
79   MSG_function_register("pinger", pinger); /* - Register the functions to be executed by the processes */
80   MSG_function_register("ponger", ponger);
81
82   MSG_launch_application(argv[2]); /* - Deploy the application */
83
84   msg_error_t res = MSG_main(); /* - Run the simulation */
85
86   XBT_INFO("Total simulation time: %.3f", MSG_get_clock());
87   return res != MSG_OK;
88 }