Logo AND Algorithmique Numérique Distribuée

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