Logo AND Algorithmique Numérique Distribuée

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