1 /* Copyright (c) 2012-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "broadcaster.h"
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_broadcaster,
10 "Messages specific for the broadcaster");
12 xbt_dynar_t build_hostlist_from_hostcount(int hostcount)
14 xbt_dynar_t host_list = xbt_dynar_new(sizeof(char*), xbt_free_ref);
17 for (i = 1; i <= hostcount; i++) {
18 char *hostname = bprintf("host%d", i);
19 XBT_DEBUG("%s", hostname);
20 xbt_dynar_push(host_list, &hostname);
25 int broadcaster_build_chain(broadcaster_t bc)
27 msg_task_t task = NULL;
28 char **cur = (char**)xbt_dynar_iterator_next(bc->it);
29 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
30 const char *current_host = NULL;
31 const char *prev = NULL;
32 const char *next = NULL;
33 const char *last = NULL;
35 /* Build the chain if there's at least one peer */
37 /* init: prev=NULL, host=current cur, next=next cur */
41 /* This iterator iterates one step ahead: cur is current iterated element,
42 but it's actually the next one in the chain */
44 /* following steps: prev=last, host=next, next=cur */
45 cur = (char**)xbt_dynar_iterator_next(bc->it);
52 XBT_DEBUG("Building chain -- broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", me, current_host, prev, next);
54 /* Send message to current peer */
55 task = task_message_chain_new(prev, next, bc->piece_count);
56 MSG_task_send(task, current_host);
59 } while (cur != NULL);
65 int broadcaster_send_file(broadcaster_t bc)
67 const char *me = "host0"; /* FIXME: hardcoded*/ /*MSG_host_get_name(MSG_host_self());*/
68 //msg_comm_t comm = NULL;
69 msg_task_t task = NULL;
71 bc->current_piece = 0;
73 while (bc->current_piece < bc->piece_count) {
74 task = task_message_data_new(NULL, PIECE_SIZE);
75 XBT_DEBUG("Sending (send) piece %d from %s into mailbox %s", bc->current_piece, me, bc->first);
76 MSG_task_send(task, bc->first);
83 broadcaster_t broadcaster_init(xbt_dynar_t host_list, unsigned int piece_count)
86 broadcaster_t bc = xbt_new(s_broadcaster_t, 1);
88 bc->piece_count = piece_count;
89 bc->current_piece = 0;
90 bc->host_list = host_list;
91 bc->it = xbt_dynar_iterator_new(bc->host_list, forward_indices_list);
92 bc->max_pending_sends = MAX_PENDING_SENDS;
93 bc->pending_sends = xbt_dynar_new(sizeof(msg_comm_t), NULL);
95 status = broadcaster_build_chain(bc);
96 xbt_assert(status == MSG_OK, "Chain initialization failed");
101 static void broadcaster_destroy(broadcaster_t bc)
103 /* Destroy iterator and hostlist */
104 xbt_dynar_iterator_delete(bc->it);
105 xbt_dynar_free(&bc->pending_sends);
106 xbt_dynar_free(&bc->host_list); /* FIXME: host names are not free'd */
110 /** Emitter function */
111 int broadcaster(int argc, char *argv[])
113 broadcaster_t bc = NULL;
114 xbt_dynar_t host_list = NULL;
116 unsigned int piece_count = PIECE_COUNT;
118 XBT_DEBUG("broadcaster");
120 /* Add every mailbox given by the hostcount in argv[1] to a dynamic array */
121 host_list = build_hostlist_from_hostcount(atoi(argv[1]));
123 /* argv[2] is the number of pieces */
125 piece_count = atoi(argv[2]);
126 XBT_DEBUG("piece_count set to %d", piece_count);
128 XBT_DEBUG("No piece_count specified, defaulting to %d", piece_count);
130 bc = broadcaster_init(host_list, piece_count);
132 /* TODO: Error checking */
133 status = broadcaster_send_file(bc);
135 broadcaster_destroy(bc);