Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c68e4f0cac955d8f7c9cfceafd4f88c07430dfc1
[simgrid.git] / examples / c / comm-pingpong / comm-pingpong.c
1 /* Copyright (c) 2007-2021. 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/actor.h"
7 #include "simgrid/comm.h"
8 #include "simgrid/engine.h"
9 #include "simgrid/forward.h"
10 #include "simgrid/mailbox.h"
11 #include "xbt/asserts.h"
12 #include "xbt/log.h"
13 #include "xbt/str.h"
14
15 #include <stdio.h> /* snprintf */
16
17 XBT_LOG_NEW_DEFAULT_CATEGORY(app_pingpong, "Messages specific for this example");
18
19 static void pinger(int argc, char* argv[])
20 {
21   sg_mailbox_t mailbox_in  = sg_mailbox_by_name("Mailbox 1");
22   sg_mailbox_t mailbox_out = sg_mailbox_by_name("Mailbox 2");
23
24   XBT_INFO("Ping from mailbox %s to mailbox %s", sg_mailbox_get_name(mailbox_in), sg_mailbox_get_name(mailbox_out));
25
26   /* - Do the ping with a 1-Byte task (latency bound) ... */
27   double* now = (double*)xbt_malloc(sizeof(double));
28   *now        = simgrid_get_clock();
29   sg_mailbox_put(mailbox_out, now, 1);
30
31   /* - ... then wait for the (large) pong */
32   double* sender_time = (double*)sg_mailbox_get(mailbox_in);
33
34   double communication_time = simgrid_get_clock() - *sender_time;
35   XBT_INFO("Task received : large communication (bandwidth bound)");
36   XBT_INFO("Pong time (bandwidth bound): %.3f", communication_time);
37   xbt_free(sender_time);
38 }
39
40 static void ponger(int argc, char* argv[])
41 {
42   sg_mailbox_t mailbox_in  = sg_mailbox_by_name("Mailbox 2");
43   sg_mailbox_t mailbox_out = sg_mailbox_by_name("Mailbox 1");
44
45   XBT_INFO("Pong from mailbox %s to mailbox %s", sg_mailbox_get_name(mailbox_in), sg_mailbox_get_name(mailbox_out));
46
47   /* - Receive the (small) ping first ....*/
48   double* sender_time       = (double*)sg_mailbox_get(mailbox_in);
49   double communication_time = simgrid_get_clock() - *sender_time;
50   XBT_INFO("Task received : small communication (latency bound)");
51   XBT_INFO(" Ping time (latency bound) %f", communication_time);
52   xbt_free(sender_time);
53
54   /*  - ... Then send a 1GB pong back (bandwidth bound) */
55   double* payload = (double*)xbt_malloc(sizeof(double));
56   *payload        = simgrid_get_clock();
57   XBT_INFO("task_bw->data = %.3f", *payload);
58   sg_mailbox_put(mailbox_out, payload, 1e9);
59 }
60
61 int main(int argc, char* argv[])
62 {
63   simgrid_init(&argc, argv);
64   xbt_assert(argc > 2,
65              "Usage: %s platform_file deployment_file\n"
66              "\tExample: %s ../../platforms/small_platform.xml app-pingpong_d.xml\n",
67              argv[0], argv[0]);
68
69   simgrid_load_platform(argv[1]);
70
71   simgrid_register_function("pinger", pinger);
72   simgrid_register_function("ponger", ponger);
73   simgrid_load_deployment(argv[2]);
74
75   simgrid_run();
76
77   XBT_INFO("Total simulation time: %.3f", simgrid_get_clock());
78
79   return 0;
80 }