Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f17ed6482723d90145536ea113d370414bd19325
[simgrid.git] / examples / msg / app-chainsend / broadcaster.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 "broadcaster.h"
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster, "Messages specific for the broadcaster");
10
11 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
12 {
13   xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), xbt_free_ref);
14   for (int i = 1; i <= hostcount; i++) {
15     char *hostname = bprintf("host%d", i);
16     XBT_DEBUG("%s", hostname);
17     xbt_dynar_push(host_list, &hostname);
18   }
19   return host_list;
20 }
21
22 int broadcaster_build_chain(broadcaster_t bc)
23 {
24   msg_task_t task = NULL;
25   char** cur               = (char**)xbt_dynar_iterator_next(bc->it);
26   const char* me           = MSG_host_get_name(MSG_host_self());
27   const char* current_host = NULL;
28   const char* prev         = NULL;
29   const char* next         = NULL;
30   const char* last         = NULL;
31
32   /* Build the chain if there's at least one peer */
33   if (cur != NULL) {
34     /* init: prev=NULL, host=current cur, next=next cur */
35     next = *cur;
36     bc->first = next;
37
38     /* This iterator iterates one step ahead: cur is current iterated element, but is actually next in the chain */
39     do {
40       /* following steps: prev=last, host=next, next=cur */
41       cur = (char**)xbt_dynar_iterator_next(bc->it);
42       prev = last;
43       current_host = next;
44       if (cur != NULL)
45         next = *cur;
46       else
47         next = NULL;
48       XBT_DEBUG("Building chain--broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
49
50       /* Send message to current peer */
51       task = task_message_chain_new(prev, next, bc->piece_count);
52       MSG_task_send(task, current_host);
53
54       last = current_host;
55     } while (cur != NULL);
56   }
57
58   return MSG_OK;
59 }
60
61 int broadcaster_send_file(broadcaster_t bc)
62 {
63   const char* me  = MSG_host_get_name(MSG_host_self());
64   msg_task_t task = NULL;
65
66   bc->current_piece = 0;
67
68   while (bc->current_piece < bc->piece_count) {
69     task = task_message_data_new(NULL, PIECE_SIZE);
70     XBT_DEBUG("Sending (send) piece %d from %s into mailbox %s", bc->current_piece, me, bc->first);
71     MSG_task_send(task, bc->first);
72     bc->current_piece++;
73   }
74
75   return MSG_OK;
76 }
77
78 broadcaster_t broadcaster_init(xbt_dynar_t host_list, unsigned int piece_count)
79 {
80   int status;
81   broadcaster_t bc = xbt_new(s_broadcaster_t, 1);
82
83   bc->piece_count = piece_count;
84   bc->current_piece = 0;
85   bc->host_list = host_list;
86   bc->it = xbt_dynar_iterator_new(bc->host_list, forward_indices_list);
87   bc->max_pending_sends = MAX_PENDING_SENDS;
88   bc->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
89
90   status = broadcaster_build_chain(bc);
91   xbt_assert(status == MSG_OK, "Chain initialization failed");
92
93   return bc;
94 }
95
96 static void broadcaster_destroy(broadcaster_t bc)
97 {
98   /* Destroy iterator and hostlist */
99   xbt_dynar_iterator_delete(bc->it);
100   xbt_dynar_free(&bc->pending_sends);
101   xbt_dynar_free(&bc->host_list);
102   xbt_free(bc);
103 }
104
105 /** Emitter function  */
106 int broadcaster(int argc, char *argv[])
107 {
108   broadcaster_t bc = NULL;
109   xbt_dynar_t host_list = NULL;
110   int status;
111   unsigned int piece_count = PIECE_COUNT;
112
113   XBT_DEBUG("broadcaster");
114
115   /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
116   host_list = build_hostlist_from_hostcount(xbt_str_parse_int(argv[1], "Invalid number of peers: %s"));
117
118   /* argv[2] is the number of pieces */
119   if (argc > 2) {
120     piece_count = xbt_str_parse_int(argv[2], "Invalid number of pieces: %s");
121     XBT_DEBUG("piece_count set to %d", piece_count);
122   } else {
123     XBT_DEBUG("No piece_count specified, defaulting to %d", piece_count);
124   }
125   bc = broadcaster_init(host_list, piece_count);
126
127   /* TODO: Error checking */
128   status = broadcaster_send_file(bc);
129
130   broadcaster_destroy(bc);
131
132   return status;
133 }