Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/simgrid
[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, &now);
21   MSG_task_send(ping_task, argv[1]);
22
23   /* - ... then wait for the (large) pong */
24   msg_task_t pong_task = NULL;
25   int a                = MSG_task_receive(&pong_task, MSG_host_get_name(MSG_host_self()));
26   xbt_assert(a == MSG_OK, "Unexpected behavior");
27
28   double sender_time        = *((double*)(MSG_task_get_data(pong_task)));
29   double communication_time = MSG_get_clock() - sender_time;
30   XBT_INFO("Task received : %s", MSG_task_get_name(pong_task));
31   MSG_task_destroy(pong_task);
32   XBT_INFO("Pong time (bandwidth bound): %.3f", communication_time);
33
34   return 0;
35 }
36
37 static int ponger(int argc, char* argv[])
38 {
39   xbt_assert(argc == 2, "The ponger function one argument from the XML deployment file");
40   XBT_INFO("Pong -> %s", argv[1]);
41   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
42
43   /* - Receive the (small) ping first ....*/
44   msg_task_t ping_task = NULL;
45   int a                = MSG_task_receive(&ping_task, MSG_host_get_name(MSG_host_self()));
46   xbt_assert(a == MSG_OK, "Unexpected behavior");
47
48   double sender_time        = *((double*)(MSG_task_get_data(ping_task)));
49   double communication_time = MSG_get_clock() - sender_time;
50   XBT_INFO("Task received : %s", MSG_task_get_name(ping_task));
51   MSG_task_destroy(ping_task);
52   XBT_INFO(" Ping time (latency bound) %f", communication_time);
53
54   /*  - ... Then send a 1GB pong back (bandwidth bound) */
55   double now                = MSG_get_clock();
56   msg_task_t pong_task      = MSG_task_create("large communication (bandwidth bound)", 0.0, 1e9, &now);
57   XBT_INFO("task_bw->data = %.3f", *((double*)MSG_task_get_data(pong_task)));
58   MSG_task_send(pong_task, argv[1]);
59
60   return 0;
61 }
62
63 int main(int argc, char* argv[])
64 {
65   MSG_init(&argc, argv);
66
67   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
68                        "\tExample: %s ../../platforms/small_platform.xml app-pingpong_d.xml\n",
69              argv[0], argv[0]);
70
71   MSG_create_environment(argv[1]); /* - Load the platform description */
72
73   MSG_function_register("pinger", pinger); /* - Register the functions to be executed by the processes */
74   MSG_function_register("ponger", ponger);
75
76   MSG_launch_application(argv[2]); /* - Deploy the application */
77
78   msg_error_t res = MSG_main(); /* - Run the simulation */
79
80   XBT_INFO("Total simulation time: %.3f", MSG_get_clock());
81   return res != MSG_OK;
82 }