Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / examples / c / app-chainsend / broadcaster.c
1 /* Copyright (c) 2012-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "chainsend.h"
7
8 XBT_LOG_NEW_DEFAULT_CATEGORY(broadcaster, "Messages specific for the broadcaster");
9
10 static chain_message_t chain_message_new(sg_mailbox_t prev, sg_mailbox_t next, const unsigned int num_pieces)
11 {
12   chain_message_t msg = xbt_malloc(sizeof(s_chain_message_t));
13   msg->prev_          = prev;
14   msg->next_          = next;
15   msg->num_pieces     = num_pieces;
16
17   return msg;
18 }
19
20 static void broadcaster_build_chain(broadcaster_t bc)
21 {
22   /* Build the chain if there's at least one peer */
23   if (bc->host_count > 0)
24     bc->first = bc->mailboxes[0];
25
26   for (unsigned i = 0; i < bc->host_count; i++) {
27     sg_mailbox_t prev = i > 0 ? bc->mailboxes[i - 1] : NULL;
28     sg_mailbox_t next = i < bc->host_count - 1 ? bc->mailboxes[i + 1] : NULL;
29     XBT_DEBUG("Building chain--broadcaster:\"%s\" dest:\"%s\" prev:\"%s\" next:\"%s\"", sg_host_self_get_name(),
30               sg_mailbox_get_name(bc->mailboxes[i]), prev ? sg_mailbox_get_name(prev) : NULL,
31               next ? sg_mailbox_get_name(next) : NULL);
32     /* Send message to current peer */
33     sg_mailbox_put(bc->mailboxes[i], chain_message_new(prev, next, bc->piece_count), MESSAGE_BUILD_CHAIN_SIZE);
34   }
35 }
36
37 static void broadcaster_send_file(const_broadcaster_t bc)
38 {
39   int nb_pending_sends = 0;
40
41   for (unsigned int current_piece = 0; current_piece < bc->piece_count; current_piece++) {
42     XBT_DEBUG("Sending (send) piece %u from %s into mailbox %s", current_piece, sg_host_self_get_name(),
43               sg_mailbox_get_name(bc->first));
44     char* file_piece = bprintf("piece-%u", current_piece);
45     sg_comm_t comm   = sg_mailbox_put_async(bc->first, file_piece, MESSAGE_SEND_DATA_HEADER_SIZE + PIECE_SIZE);
46     bc->pending_sends[nb_pending_sends++] = comm;
47   }
48   sg_comm_wait_all(bc->pending_sends, nb_pending_sends);
49 }
50
51 static broadcaster_t broadcaster_init(sg_mailbox_t* mailboxes, unsigned int host_count, unsigned int piece_count)
52 {
53   broadcaster_t bc = xbt_malloc(sizeof(s_broadcaster_t));
54
55   bc->first         = NULL;
56   bc->host_count    = host_count;
57   bc->piece_count   = piece_count;
58   bc->mailboxes     = mailboxes;
59   bc->pending_sends = xbt_malloc(sizeof(sg_comm_t) * MAX_PENDING_COMMS);
60
61   broadcaster_build_chain(bc);
62
63   return bc;
64 }
65
66 static void broadcaster_destroy(broadcaster_t bc)
67 {
68   free(bc->pending_sends);
69   free(bc->mailboxes);
70   free(bc);
71 }
72
73 /** Emitter function  */
74 void broadcaster(int argc, char* argv[])
75 {
76   XBT_DEBUG("broadcaster");
77   xbt_assert(argc > 2);
78   unsigned int host_count = (unsigned int)xbt_str_parse_int(argv[1], "Invalid number of peers");
79
80   sg_mailbox_t* mailboxes = xbt_malloc(sizeof(sg_mailbox_t) * host_count);
81
82   for (unsigned int i = 1; i <= host_count; i++) {
83     char* name = bprintf("node-%u.simgrid.org", i);
84     XBT_DEBUG("%s", name);
85     mailboxes[i - 1] = sg_mailbox_by_name(name);
86     free(name);
87   }
88
89   unsigned int piece_count = (unsigned int)xbt_str_parse_int(argv[2], "Invalid number of pieces");
90
91   broadcaster_t bc = broadcaster_init(mailboxes, host_count, piece_count);
92
93   broadcaster_send_file(bc);
94
95   broadcaster_destroy(bc);
96 }