Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
gitignore: deal with recent file movments
[simgrid.git] / examples / msg / app-pingpong / app-pingpong.c
1 /* Copyright (c) 2007-2017. 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 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_app_pingpong, "Messages specific for this msg example");
9
10 static int pinger(int argc, char *argv[])
11 {
12   xbt_assert(argc==2, "The pinger function one argument from the XML deployment file");
13   XBT_INFO("Ping -> %s", argv[1]);
14   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
15
16   /* - Do the ping with a 1-Byte task (latency bound) ... */
17   double now = MSG_get_clock();
18   msg_task_t ping_task = MSG_task_create("small communication (latency bound)", 0.0, 1, NULL);
19   ping_task->data = xbt_new(double, 1);
20   *(double *) ping_task->data = 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 *) (pong_task->data));
29   double communication_time =  MSG_get_clock() - sender_time;
30   XBT_INFO("Task received : %s", pong_task->name);
31   xbt_free(pong_task->data);
32   MSG_task_destroy(pong_task);
33   XBT_INFO("Pong time (bandwidth bound): %.3f", communication_time);
34
35   return 0;
36 }
37
38 static int ponger(int argc, char *argv[])
39 {
40   xbt_assert(argc==2, "The ponger function one argument from the XML deployment file");
41   XBT_INFO("Pong -> %s", argv[1]);
42   xbt_assert(MSG_host_by_name(argv[1]) != NULL, "Unknown host %s. Stopping Now! ", argv[1]);
43
44   /* - Receive the (small) ping first ....*/
45   msg_task_t ping_task = NULL;
46   int a = MSG_task_receive(&ping_task, MSG_host_get_name(MSG_host_self()));
47   xbt_assert(a == MSG_OK, "Unexpected behavior");
48
49   double sender_time = *((double *) (ping_task->data));
50   double communication_time = MSG_get_clock() - sender_time;
51   XBT_INFO("Task received : %s", ping_task->name);
52   xbt_free(ping_task->data);
53   MSG_task_destroy(ping_task);
54   XBT_INFO(" Ping time (latency bound) %f", communication_time);
55
56   /*  - ... Then send a 1GB pong back (bandwidth bound) */
57   double now = MSG_get_clock();
58   msg_task_t pong_task = MSG_task_create("large communication (bandwidth bound)", 0.0, 1e9, NULL);
59   pong_task->data = xbt_new(double, 1);
60   *(double *) pong_task->data = now;
61   XBT_INFO("task_bw->data = %.3f", *((double*)pong_task->data));
62   MSG_task_send(pong_task, argv[1]);
63
64   return 0;
65 }
66
67 int main(int argc, char *argv[])
68 {
69   MSG_init(&argc, argv);
70
71   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
72              "\tExample: %s ../../platforms/small_platform.xml app-pingpong_d.xml\n", argv[0], argv[0]);
73
74   MSG_create_environment(argv[1]);          /* - Load the platform description */
75
76   MSG_function_register("pinger", pinger);  /* - Register the functions to be executed by the processes */
77   MSG_function_register("ponger", ponger);
78
79   MSG_launch_application(argv[2]);          /* - Deploy the application */
80
81   msg_error_t res = MSG_main();             /* - Run the simulation */
82
83   XBT_INFO("Total simulation time: %.3f", MSG_get_clock());
84   return res!=MSG_OK;
85 }