Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #181 from bcamus/master
[simgrid.git] / examples / msg / app-chainsend / peer.c
1 /* Copyright (c) 2012-2014. 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 "peer.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_peer, "Messages specific for the peer");
10
11 void peer_init_chain(peer_t peer, message_t msg)
12 {
13   peer->prev = msg->prev_hostname;
14   peer->next = msg->next_hostname;
15   peer->total_pieces = msg->num_pieces;
16   peer->init = 1;
17 }
18
19 static void peer_forward_msg(peer_t peer, message_t msg)
20 {
21   msg_task_t task = task_message_data_new(NULL, msg->data_length);
22   XBT_DEBUG("Sending (isend) from %s into mailbox %s", peer->me, peer->next);
23   msg_comm_t comm = MSG_task_isend(task, peer->next);
24   queue_pending_connection(comm, peer->pending_sends);
25 }
26
27 int peer_execute_task(peer_t peer, msg_task_t task)
28 {
29   int done = 0;
30   message_t msg = MSG_task_get_data(task);
31
32   XBT_DEBUG("Peer %s got message of type %d\n", peer->me, msg->type);
33   if (msg->type == MESSAGE_BUILD_CHAIN)
34     peer_init_chain(peer, msg);
35   else if (msg->type == MESSAGE_SEND_DATA) {
36     xbt_assert(peer->init, "peer_execute_task() failed: got msg_type %d before initialization", msg->type);
37     if (peer->next != NULL)
38       peer_forward_msg(peer, msg);
39     peer->pieces++;
40     peer->bytes += msg->data_length;
41     if (peer->pieces >= peer->total_pieces) {
42       XBT_DEBUG("%d pieces receieved", peer->pieces);
43       done = 1;
44     }
45   }
46
47   MSG_task_execute(task);
48
49   return done;
50 }
51
52 msg_error_t peer_wait_for_message(peer_t peer)
53 {
54   msg_error_t status;
55   msg_comm_t comm = NULL;
56   msg_task_t task = NULL;
57   int done = 0;
58
59   while (done == 0) {
60     comm = MSG_task_irecv(&task, peer->me);
61     queue_pending_connection(comm, peer->pending_recvs);
62     int idx = MSG_comm_waitany(peer->pending_recvs);
63     if (idx != -1) {
64       comm = xbt_dynar_get_as(peer->pending_recvs, idx, msg_comm_t);
65       status = MSG_comm_get_status(comm);
66       XBT_DEBUG("peer_wait_for_message: error code = %d", status);
67       xbt_assert(status == MSG_OK, "peer_wait_for_message() failed");
68
69       task = MSG_comm_get_task(comm);
70       MSG_comm_destroy(comm);
71       xbt_dynar_cursor_rm(peer->pending_recvs, (unsigned int*)&idx);
72       done = peer_execute_task(peer, task);
73
74       task_message_delete(task);
75       task = NULL;
76     }
77     process_pending_connections(peer->pending_sends);
78   }
79
80   return status;
81 }
82
83 void peer_init(peer_t p, int argc, char *argv[])
84 {
85   p->init = 0;
86   p->prev = NULL;
87   p->next = NULL;
88   p->pieces = 0;
89   p->bytes = 0;
90   p->pending_recvs = xbt_dynar_new(sizeof(msg_comm_t), NULL);
91   p->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
92   /* Set mailbox name: use host number from argv or hostname if no argument given */
93   if (argc > 1) {
94     p->me = bprintf("host%s", argv[1]);
95   } else {
96     p->me = xbt_strdup(MSG_host_get_name(MSG_host_self()));
97   }
98 }
99
100 void peer_shutdown(peer_t p)
101 {
102   unsigned int size = xbt_dynar_length(p->pending_sends);
103   unsigned int idx;
104   msg_comm_t *comms = xbt_new(msg_comm_t, size);
105
106   for (idx = 0; idx < size; idx++) {
107     comms[idx] = xbt_dynar_get_as(p->pending_sends, idx, msg_comm_t);
108   }
109
110   XBT_DEBUG("Waiting for sends to finish before shutdown...");
111   MSG_comm_waitall(comms, size, PEER_SHUTDOWN_DEADLINE);
112
113   for (idx = 0; idx < size; idx++) {
114     MSG_comm_destroy(comms[idx]);
115   }
116
117   xbt_free(comms);
118 }
119
120 void peer_delete(peer_t p)
121 {
122   xbt_dynar_free(&p->pending_recvs);
123   xbt_dynar_free(&p->pending_sends);
124   xbt_free(p->me);
125   xbt_free(p->prev);
126   xbt_free(p->next);
127
128   xbt_free(p);
129 }
130
131 void peer_print_stats(peer_t p, float elapsed_time)
132 {
133   XBT_INFO("### %f %llu bytes (Avg %f MB/s); copy finished (simulated).", elapsed_time, p->bytes, p->bytes / 1024.0 / 1024.0 / elapsed_time);
134 }
135
136 /** Peer function  */
137 int peer(int argc, char *argv[])
138 {
139   peer_t p = xbt_new(s_peer_t, 1);
140   msg_error_t status;
141
142   XBT_DEBUG("peer");
143
144   peer_init(p, argc, argv);
145   float start_time = MSG_get_clock();
146   status = peer_wait_for_message(p);
147   peer_shutdown(p);
148   float end_time = MSG_get_clock();
149   peer_print_stats(p, end_time - start_time);
150   peer_delete(p);
151
152   return status;
153 }