Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright headers.
[simgrid.git] / src / smpi / colls / bcast / bcast-NTSB.cpp
1 /* Copyright (c) 2013-2018. 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 "../colls_private.hpp"
8
9 int bcast_NTSB_segment_size_in_byte = 8192;
10 namespace simgrid{
11 namespace smpi{
12 int Coll_bcast_NTSB::bcast(void *buf, int count, MPI_Datatype datatype,
13                                int root, MPI_Comm comm)
14 {
15   int tag = COLL_TAG_BCAST;
16   MPI_Status status;
17   int rank, size;
18   int i;
19
20   MPI_Request *send_request_array;
21   MPI_Request *recv_request_array;
22   MPI_Status *send_status_array;
23   MPI_Status *recv_status_array;
24
25   MPI_Aint extent;
26   extent = datatype->get_extent();
27
28   rank = comm->rank();
29   size = comm->size();
30
31   /* source node and destination nodes (same through out the functions) */
32   int from = (rank - 1) / 2;
33   int to_left = rank * 2 + 1;
34   int to_right = rank * 2 + 2;
35   if (to_left >= size)
36     to_left = -1;
37   if (to_right >= size)
38     to_right = -1;
39
40   /* segment is segment size in number of elements (not bytes) */
41   int segment = bcast_NTSB_segment_size_in_byte / extent;
42   segment =  segment == 0 ? 1 :segment;
43   /* pipeline length */
44   int pipe_length = count / segment;
45
46   /* use for buffer offset for sending and receiving data = segment size in byte */
47   int increment = segment * extent;
48
49   /* if the input size is not divisible by segment size =>
50      the small remainder will be done with native implementation */
51   int remainder = count % segment;
52
53   /* if root is not zero send to rank zero first */
54   if (root != 0) {
55     if (rank == root) {
56       Request::send(buf, count, datatype, 0, tag, comm);
57     } else if (rank == 0) {
58       Request::recv(buf, count, datatype, root, tag, comm, &status);
59     }
60   }
61
62   /* when a message is smaller than a block size => no pipeline */
63   if (count <= segment) {
64
65     /* case: root */
66     if (rank == 0) {
67       /* case root has only a left child */
68       if (to_right == -1) {
69         Request::send(buf, count, datatype, to_left, tag, comm);
70       }
71       /* case root has both left and right children */
72       else {
73         Request::send(buf, count, datatype, to_left, tag, comm);
74         Request::send(buf, count, datatype, to_right, tag, comm);
75       }
76     }
77
78     /* case: leaf ==> receive only */
79     else if (to_left == -1) {
80       Request::recv(buf, count, datatype, from, tag, comm, &status);
81     }
82
83     /* case: intermidiate node with only left child ==> relay message */
84     else if (to_right == -1) {
85       Request::recv(buf, count, datatype, from, tag, comm, &status);
86       Request::send(buf, count, datatype, to_left, tag, comm);
87     }
88
89     /* case: intermidiate node with both left and right children ==> relay message */
90     else {
91       Request::recv(buf, count, datatype, from, tag, comm, &status);
92       Request::send(buf, count, datatype, to_left, tag, comm);
93       Request::send(buf, count, datatype, to_right, tag, comm);
94     }
95     return MPI_SUCCESS;
96   }
97   // pipelining
98   else {
99
100     send_request_array =
101         (MPI_Request *) xbt_malloc(2 * (size + pipe_length) * sizeof(MPI_Request));
102     recv_request_array =
103         (MPI_Request *) xbt_malloc((size + pipe_length) * sizeof(MPI_Request));
104     send_status_array =
105         (MPI_Status *) xbt_malloc(2 * (size + pipe_length) * sizeof(MPI_Status));
106     recv_status_array =
107         (MPI_Status *) xbt_malloc((size + pipe_length) * sizeof(MPI_Status));
108
109
110
111     /* case: root */
112     if (rank == 0) {
113       /* case root has only a left child */
114       if (to_right == -1) {
115         for (i = 0; i < pipe_length; i++) {
116           send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
117                     tag + i, comm);
118         }
119         Request::waitall((pipe_length), send_request_array, send_status_array);
120       }
121       /* case root has both left and right children */
122       else {
123         for (i = 0; i < pipe_length; i++) {
124           send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
125                     tag + i, comm);
126           send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
127                     tag + i, comm);
128         }
129         Request::waitall((2 * pipe_length), send_request_array, send_status_array);
130       }
131     }
132
133     /* case: leaf ==> receive only */
134     else if (to_left == -1) {
135       for (i = 0; i < pipe_length; i++) {
136         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
137                   tag + i, comm);
138       }
139       Request::waitall((pipe_length), recv_request_array, recv_status_array);
140     }
141
142     /* case: intermidiate node with only left child ==> relay message */
143     else if (to_right == -1) {
144       for (i = 0; i < pipe_length; i++) {
145         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
146                   tag + i, comm);
147       }
148       for (i = 0; i < pipe_length; i++) {
149         Request::wait(&recv_request_array[i], &status);
150         send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
151                   tag + i, comm);
152       }
153       Request::waitall(pipe_length, send_request_array, send_status_array);
154
155     }
156     /* case: intermidiate node with both left and right children ==> relay message */
157     else {
158       for (i = 0; i < pipe_length; i++) {
159         recv_request_array[i] = Request::irecv((char *) buf + (i * increment), segment, datatype, from,
160                   tag + i, comm);
161       }
162       for (i = 0; i < pipe_length; i++) {
163         Request::wait(&recv_request_array[i], &status);
164         send_request_array[i] = Request::isend((char *) buf + (i * increment), segment, datatype, to_left,
165                   tag + i, comm);
166         send_request_array[i + pipe_length] = Request::isend((char *) buf + (i * increment), segment, datatype, to_right,
167                   tag + i, comm);
168       }
169       Request::waitall((2 * pipe_length), send_request_array, send_status_array);
170     }
171
172     free(send_request_array);
173     free(recv_request_array);
174     free(send_status_array);
175     free(recv_status_array);
176   }                             /* end pipeline */
177
178   /* when count is not divisible by block size, use default BCAST for the remainder */
179   if ((remainder != 0) && (count > segment)) {
180     XBT_WARN("MPI_bcast_NTSB use default MPI_bcast.");
181     Colls::bcast((char *) buf + (pipe_length * increment), remainder, datatype,
182               root, comm);
183   }
184
185   return MPI_SUCCESS;
186 }
187
188 }
189 }