Logo AND Algorithmique Numérique Distribuée

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